improvements to the server

master
Bartek Kostrzewa 2010-11-12 21:37:12 +01:00
parent e1b1e41e00
commit 7deaabdd20
10 changed files with 78 additions and 26 deletions

View File

@ -1,6 +1,6 @@
#include "Buffer.h" #include "Buffer.h"
Buffer::Buffer( int _id ) Buffer::Buffer( std::string _id )
{ {
id = _id; id = _id;
} }
@ -17,13 +17,19 @@ void Buffer::set(frame_t data)
} }
} }
void Buffer::set_id(std::string _id)
{
Glib::Mutex::Lock lock(mutex_);
id = _id;
}
frame_t Buffer::get() frame_t Buffer::get()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Mutex::Lock lock(mutex_);
return frame; return frame;
} }
int Buffer::get_id() std::string Buffer::get_id()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Mutex::Lock lock(mutex_);
return id; return id;

View File

@ -1,5 +1,6 @@
#ifndef __BUFFER_H_ #ifndef __BUFFER_H_
#define __BUFFER_H_ #define __BUFFER_H_
#include <string>
#include <glibmm.h> #include <glibmm.h>
#include "defines.h" #include "defines.h"
@ -9,15 +10,17 @@
class Buffer : public sigc::trackable class Buffer : public sigc::trackable
{ {
public: public:
Buffer(int _id); Buffer(std::string _id);
~Buffer(); ~Buffer();
void set(frame_t); void set(frame_t);
frame_t get(); frame_t get();
int get_id();
std::string get_id();
void set_id(std::string id);
private: private:
int id; std::string id;
frame_t frame; frame_t frame;
Glib::Mutex mutex_; Glib::Mutex mutex_;

View File

@ -2,12 +2,12 @@
Buffers::Buffers() Buffers::Buffers()
{ {
id = 0; id = "";
} }
Buffers::Buffers(int _bufnum) Buffers::Buffers(int _bufnum)
{ {
id = 0; id = "";
for( int i = 0; i < _bufnum; i++) for( int i = 0; i < _bufnum; i++)
add(); add();
} }
@ -27,11 +27,11 @@ Buffer* Buffers::get(int index)
void Buffers::add() void Buffers::add()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Mutex::Lock lock(mutex_);
id += 1; id += "1";
buffers.push_back( new Buffer(id) ); buffers.push_back( new Buffer( id ) );
} }
void Buffers::remove(int _id) void Buffers::remove(std::string _id)
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Mutex::Lock lock(mutex_);
int size = buffers.size(); int size = buffers.size();

View File

@ -10,6 +10,7 @@
#include <glibmm.h> #include <glibmm.h>
#include <vector> #include <vector>
#include <string>
#include <time.h> #include <time.h>
@ -27,13 +28,13 @@ class Buffers : public sigc::trackable
~Buffers(); ~Buffers();
void add(); void add();
void remove(int); void remove(std::string);
Buffer* get(int); Buffer* get(int);
private: private:
vector<Buffer*> buffers; vector<Buffer*> buffers;
int id; std::string id;
Glib::Mutex mutex_; Glib::Mutex mutex_;
}; };
#endif #endif

View File

@ -1,11 +1,8 @@
#include "Server.h" #include "Server.h"
Server::Server(int _numbufs, int _port ) Server::Server(int _port )
{ {
for(int i = 0; i < _numbufs; i++) port = _port;
buffers.push_back( new Buffer(i) );
port = _port;
} }
Server::~Server() Server::~Server()
@ -48,6 +45,8 @@ void Server::listen()
while (1) while (1)
{ {
frame_t frame; frame_t frame;
// creating the buffer each time is faster than zeroing it out
boost::array<char, BUFLEN> recv_buf; boost::array<char, BUFLEN> recv_buf;
udp::endpoint remote_endpoint; udp::endpoint remote_endpoint;
boost::system::error_code error; boost::system::error_code error;
@ -59,6 +58,38 @@ void Server::listen()
if( packetcounter % 1000 == 0 ) if( packetcounter % 1000 == 0 )
cout << endl << packetcounter << endl; cout << endl << packetcounter << endl;
// have we encountered this source before?
// DEBUG
// cout << remote_endpoint << endl;
int bufnum = 0;
{
Glib::Mutex::Lock lock(mutex_);
int size = endpoints.size();
bool known = false;
for(bufnum = 0; bufnum < size; bufnum++)
{
if(endpoints[bufnum] == remote_endpoint)
{
known = true;
break;
}
}
if( !known && size+1 < NUMBUFS )
{
// create a new buffer make a note of the endpoint
std::stringstream endpointstring;
endpointstring << remote_endpoint;
cout << "adding new buffer for " << remote_endpoint << endl;
buffers.push_back( new Buffer( endpointstring.str() ) );
endpoints.push_back( remote_endpoint );
}
// discard packet, we're not accepting any more sources!
else if( size+1 >= NUMBUFS )
break;
}
for(int i = 0; i < HEIGHT; i++) for(int i = 0; i < HEIGHT; i++)
{ {
for(int j = 0; j < WIDTH; j++) for(int j = 0; j < WIDTH; j++)
@ -79,9 +110,9 @@ void Server::listen()
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Mutex::Lock lock(mutex_);
// convert ascii to integer value // convert ascii to integer value
if( recv_buf[0]-49 < buffers.size() ) if( bufnum < buffers.size() )
{ {
buffers[ recv_buf[0]-49 ]->set(frame); buffers[ bufnum ]->set(frame);
} }
} }
@ -122,7 +153,7 @@ void Server::mix()
size = buffers.size(); size = buffers.size();
} }
for(int x = 0; x < 6; x++) for(int x = 0; x < size; x++)
{ {
{ {
Glib::Mutex::Lock lock(mutex_); Glib::Mutex::Lock lock(mutex_);
@ -162,8 +193,8 @@ void Server::mix()
{ {
cout << frame.segments[i].r; cout << frame.segments[i].r;
} }
cout << endl << endl; //*/ cout << endl << endl;
} } //*/
usleep( 25000 ); usleep( 25000 );
} }

View File

@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <list>
#include <boost/array.hpp> #include <boost/array.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
@ -18,7 +19,7 @@ using namespace std;
class Server : public sigc::trackable class Server : public sigc::trackable
{ {
public: public:
Server(int _numbufs, int _port); Server(int _port);
~Server(); ~Server();
void listen(); void listen();
@ -27,8 +28,12 @@ public:
private: private:
Glib::Mutex mutex_; Glib::Mutex mutex_;
vector<Glib::Thread*> threads; vector<Glib::Thread*> threads;
vector<Buffer*> buffers; vector<Buffer*> buffers;
vector<udp::endpoint> endpoints;
int numbufs;
int port; int port;
}; };

View File

@ -2,5 +2,5 @@
while [ 1 ] while [ 1 ]
do do
echo "$1" | cat - display | netcat -q 0.1 -u 127.0.0.1 4321 echo "$1" | cat - display | netcat -p5000$1 -q 0.1 -u 127.0.0.1 4321
done done

View File

@ -0,0 +1,6 @@
#!/bin/bash
while [ 1 ]
do
echo "$1" | cat - display | netcat -q 0.1 -u 127.0.0.1 4321
done

Binary file not shown.

View File

@ -22,7 +22,7 @@ int main(void)
// our main loop with support for signals and all that jazz // our main loop with support for signals and all that jazz
Glib::RefPtr<Glib::MainLoop> Main = Glib::MainLoop::create(); Glib::RefPtr<Glib::MainLoop> Main = Glib::MainLoop::create();
Server server(NUMBUFS,4321); Server server(4321);
server.launch_threads(); server.launch_threads();
Main->run(); Main->run();