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"
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;

View File

@ -1,5 +1,6 @@
#ifndef __BUFFER_H_
#define __BUFFER_H_
#include <string>
#include <glibmm.h>
#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_;

View File

@ -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();

View File

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

View File

@ -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<char, BUFLEN> 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 );
}

View File

@ -4,6 +4,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <boost/array.hpp>
#include <boost/asio.hpp>
@ -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<Glib::Thread*> threads;
vector<Buffer*> buffers;
vector<udp::endpoint> endpoints;
int numbufs;
int port;
};

View File

@ -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

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
Glib::RefPtr<Glib::MainLoop> Main = Glib::MainLoop::create();
Server server(NUMBUFS,4321);
Server server(4321);
server.launch_threads();
Main->run();