From c286b459baa235768b24e157b0216eb8afcd0bb7 Mon Sep 17 00:00:00 2001 From: Bartek Kostrzewa Date: Thu, 12 May 2011 12:39:13 +0200 Subject: [PATCH] added functionality to send a udp packet to a destination for display purposes --- clients/plasma.py | 11 ++++--- frameserver/Server.cpp | 65 ++++++++++++++++++++++++++++++++++--- frameserver/Server.h | 5 +-- frameserver/defines.h | 9 +++-- frameserver/test_server.cpp | 4 +-- 5 files changed, 79 insertions(+), 15 deletions(-) diff --git a/clients/plasma.py b/clients/plasma.py index 2e59249..8bd9b25 100644 --- a/clients/plasma.py +++ b/clients/plasma.py @@ -24,9 +24,11 @@ UDPSock.bind((outgoing_if, local_port)) # goes down or refuses connection #UDPSock.connect((remote_host, remote_port)) -segments = open('segments','r') +segmentsfile = open('segments','r') -alpha = chr(125) +hash = "abcdefghij" + +alpha = chr(255) z_buffer = chr(1) + "\n" @@ -45,7 +47,8 @@ frequency = 2*pi/200 while (1): #zero out the data buffer - data = z_buffer + data = hash + data += z_buffer for i in range(0,width): for j in range(0,height): pixel = fabs(sin(2*pi*(float(i)/width)+t*frequency)*sin(2*pi*(float(j)/height)+t*frequency)) @@ -54,7 +57,7 @@ while (1): for i in range(0,segwidth): for j in range(0,segments): for a in range(0,segchannels): - data += chr(255) + data += chr( 127 + int(128*sin(2*pi*(1+i)*(1+j)*(1+a)*t*frequency/200))) data += "\n" t+=1 if not data: diff --git a/frameserver/Server.cpp b/frameserver/Server.cpp index 7005cc1..1b098dc 100644 --- a/frameserver/Server.cpp +++ b/frameserver/Server.cpp @@ -13,7 +13,7 @@ Server::Server(int _port ) consoleinit = false; mode = FRAME; selected_buffer=0; - //test(); + launch_threads(); } Server::~Server() @@ -65,6 +65,14 @@ void Server::listen() socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint, 0, error); + // check whether this is a valid packet and discard it if it isn't + // note, this is a very hack way of comparing strings... + if( std::string( (char*)recv_buf.data(), (size_t)10 ) != std::string(HASH) ) + continue; + + // DEBUG send reply to sender + // socket.send_to( boost::asio::buffer("ACK",3) ,remote_endpoint); + // bufnum is used further down /* the buffer is locked for a long long time, however, we need to make sure that none of the buffers expires while we're about @@ -106,7 +114,7 @@ void Server::listen() packetcounter++; // copy frame information into the buffer - frame.z = recv_buf[0]; + frame.z = recv_buf[10]; for(int i = 0; i < HEIGHT; i++) { for(int j = 0; j < WIDTH; j++) @@ -157,10 +165,53 @@ void Server::listen() } } +/* this sends the frame content to a host expecting udp packets */ +void Server::send(frame_t _frame, udp::endpoint destination) +{ + // write the frame into a char buffer, make it static so we don't waste time creating + // it at every function call + const int length = WIDTH*HEIGHT*CHANNELS + SEGWIDTH*SEGNUM*SEGCHANNELS; + static char data[length]; + for(int i = 0; i < HEIGHT; i++) + { + for(int j = 0; j < WIDTH; j++) + { + for(int k = 0; k < CHANNELS; k++) + { + data[i*WIDTH*CHANNELS + CHANNELS*j + k] = _frame.windows[i][j][k]; + } + } + } + + for(int i = 0; i < SEGWIDTH; i++) + { + for(int j = 0; j < SEGNUM; j++) + { + for(int k = 0; k < SEGCHANNELS; k++) + { + data[WIDTH*HEIGHT*CHANNELS + + i*SEGNUM*SEGCHANNELS + SEGCHANNELS*j + k] = _frame.segments[i][j][k]; + } + } + } + + try + { + boost::asio::io_service io_service; + + udp::socket socket(io_service, udp::endpoint(udp::v4(),0)); + + // std::cerr << destination << std::endl; + socket.send_to(boost::asio::buffer(data,length), destination); + } + catch (std::exception& e) + { + std::cerr << e.what() << std::endl; + } +} /* the framemixer, this periodically (40 times a second) reads all input buffers and then produces output, ready to be displayed. - In the final version, this is where interesting things will happen. */ void Server::mix() { @@ -254,7 +305,9 @@ void Server::mix() // we do this in case output() takes a long time to return. If it read frame directly, we'd end up // waiting for a long time temp_frame = frame; - } // release lock and send off to hardware + } /* release lock and send off to hardware + * (note, temp_frame is passed by value and will live only in the + * argument of the "output" function and does not need to be locked) */ output(temp_frame); } } @@ -262,6 +315,10 @@ void Server::mix() // output to hardware using OLA void Server::output(frame_t _frame) { + boost::asio::ip::udp::endpoint remote_endpoint( boost::asio::ip::address_v4::from_string(REMOTE_IP) , REMOTE_PORT ); + // send frame to a network host + send(_frame,remote_endpoint); + // pretend we're doing something usleep( 25000 ); } diff --git a/frameserver/Server.h b/frameserver/Server.h index ef906a6..5e2aaec 100644 --- a/frameserver/Server.h +++ b/frameserver/Server.h @@ -33,11 +33,11 @@ public: Server(int _port); ~Server(); - void launch_threads(); - private: Glib::Mutex mutex_; + void launch_threads(); + void console(); void input(); void test(); @@ -51,6 +51,7 @@ private: void console_printclients(); void listen(); + void send(frame_t,udp::endpoint); void mix(); void output(frame_t); int get_size(); diff --git a/frameserver/defines.h b/frameserver/defines.h index 6f6a483..71eb9a9 100644 --- a/frameserver/defines.h +++ b/frameserver/defines.h @@ -1,14 +1,19 @@ #ifndef __DEFINES_H_ #define __DEFINES_H_ +#define HASH "abcdefghij" + // four minutes should be enough (seconds) // timeout set to two seconds for testing purposes #define BUFTIMEOUT 2 #define NUMBUFS 1000 -// one number + newline -#define HEADEROFFSET 2 +#define REMOTE_IP "127.0.0.1" +#define REMOTE_PORT 1234 + +// one byte number + 10 character hash +#define HEADEROFFSET 11 // 12 windows per floor, 7 floors, Value:Alpha #define WIDTH 12 diff --git a/frameserver/test_server.cpp b/frameserver/test_server.cpp index 9317365..f8b059e 100644 --- a/frameserver/test_server.cpp +++ b/frameserver/test_server.cpp @@ -22,9 +22,7 @@ int main(void) // our main loop with support for signals and all that jazz Glib::RefPtr Main = Glib::MainLoop::create(); - Server server(4321); - server.launch_threads(); - + Server server(4321); Main->run(); return 0;