diff --git a/frameserver/Buffer.cpp b/frameserver/Buffer.cpp index eacd457..ae9dc49 100644 --- a/frameserver/Buffer.cpp +++ b/frameserver/Buffer.cpp @@ -1,6 +1,6 @@ #include "Buffer.h" -Buffer::Buffer( int _id ) +Buffer::Buffer( std::string _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() { Glib::Mutex::Lock lock(mutex_); return frame; } -int Buffer::get_id() +std::string Buffer::get_id() { Glib::Mutex::Lock lock(mutex_); return id; diff --git a/frameserver/Buffer.h b/frameserver/Buffer.h index 776fd77..e2ba2ce 100644 --- a/frameserver/Buffer.h +++ b/frameserver/Buffer.h @@ -1,5 +1,6 @@ #ifndef __BUFFER_H_ #define __BUFFER_H_ +#include #include #include "defines.h" @@ -9,15 +10,17 @@ class Buffer : public sigc::trackable { public: - Buffer(int _id); + Buffer(std::string _id); ~Buffer(); void set(frame_t); frame_t get(); - int get_id(); + + std::string get_id(); + void set_id(std::string id); private: - int id; + std::string id; frame_t frame; Glib::Mutex mutex_; diff --git a/frameserver/Buffers.cpp b/frameserver/Buffers.cpp index e66081b..6333b64 100644 --- a/frameserver/Buffers.cpp +++ b/frameserver/Buffers.cpp @@ -2,12 +2,12 @@ Buffers::Buffers() { - id = 0; + id = ""; } Buffers::Buffers(int _bufnum) { - id = 0; + id = ""; for( int i = 0; i < _bufnum; i++) add(); } @@ -27,11 +27,11 @@ Buffer* Buffers::get(int index) void Buffers::add() { Glib::Mutex::Lock lock(mutex_); - id += 1; - buffers.push_back( new Buffer(id) ); + id += "1"; + buffers.push_back( new Buffer( id ) ); } -void Buffers::remove(int _id) +void Buffers::remove(std::string _id) { Glib::Mutex::Lock lock(mutex_); int size = buffers.size(); diff --git a/frameserver/Buffers.h b/frameserver/Buffers.h index 18f0d9a..2c021f0 100644 --- a/frameserver/Buffers.h +++ b/frameserver/Buffers.h @@ -10,6 +10,7 @@ #include #include +#include #include @@ -27,13 +28,13 @@ class Buffers : public sigc::trackable ~Buffers(); void add(); - void remove(int); + void remove(std::string); Buffer* get(int); private: vector buffers; - int id; + std::string id; Glib::Mutex mutex_; }; #endif diff --git a/frameserver/Server.cpp b/frameserver/Server.cpp index 7b86c27..08652d4 100644 --- a/frameserver/Server.cpp +++ b/frameserver/Server.cpp @@ -1,11 +1,8 @@ #include "Server.h" -Server::Server(int _numbufs, int _port ) +Server::Server(int _port ) { - for(int i = 0; i < _numbufs; i++) - buffers.push_back( new Buffer(i) ); - - port = _port; + port = _port; } Server::~Server() @@ -48,6 +45,8 @@ void Server::listen() while (1) { frame_t frame; + + // creating the buffer each time is faster than zeroing it out boost::array recv_buf; udp::endpoint remote_endpoint; boost::system::error_code error; @@ -56,8 +55,40 @@ void Server::listen() remote_endpoint, 0, error); packetcounter++; - if( packetcounter % 1000 == 0 ) + if( packetcounter % 1000 == 0 ) 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++) { @@ -79,9 +110,9 @@ void Server::listen() { Glib::Mutex::Lock lock(mutex_); // 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(); } - for(int x = 0; x < 6; x++) + for(int x = 0; x < size; x++) { { Glib::Mutex::Lock lock(mutex_); @@ -162,8 +193,8 @@ void Server::mix() { cout << frame.segments[i].r; } - cout << endl << endl; //*/ - } + cout << endl << endl; + } //*/ usleep( 25000 ); } diff --git a/frameserver/Server.h b/frameserver/Server.h index 186fe1c..292d063 100644 --- a/frameserver/Server.h +++ b/frameserver/Server.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -18,7 +19,7 @@ using namespace std; class Server : public sigc::trackable { public: - Server(int _numbufs, int _port); + Server(int _port); ~Server(); void listen(); @@ -27,8 +28,12 @@ public: private: Glib::Mutex mutex_; + vector threads; vector buffers; + vector endpoints; + + int numbufs; int port; }; diff --git a/frameserver/servertest.sh b/frameserver/servertest.sh index c95a23d..1015d21 100644 --- a/frameserver/servertest.sh +++ b/frameserver/servertest.sh @@ -2,5 +2,5 @@ while [ 1 ] 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 diff --git a/frameserver/servertest.sh~ b/frameserver/servertest.sh~ new file mode 100644 index 0000000..c95a23d --- /dev/null +++ b/frameserver/servertest.sh~ @@ -0,0 +1,6 @@ +#!/bin/bash + +while [ 1 ] +do + echo "$1" | cat - display | netcat -q 0.1 -u 127.0.0.1 4321 +done diff --git a/frameserver/test_server b/frameserver/test_server index 3811076..4c4b2ee 100755 Binary files a/frameserver/test_server and b/frameserver/test_server differ diff --git a/frameserver/test_server.cpp b/frameserver/test_server.cpp index 17ab338..9317365 100644 --- a/frameserver/test_server.cpp +++ b/frameserver/test_server.cpp @@ -22,7 +22,7 @@ int main(void) // our main loop with support for signals and all that jazz Glib::RefPtr Main = Glib::MainLoop::create(); - Server server(NUMBUFS,4321); + Server server(4321); server.launch_threads(); Main->run();