Ben Katz / ExperimentServer

Fork of ExperimentServer by Patrick Wensing

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ExperimentServer.cpp Source File

ExperimentServer.cpp

00001 #include "ExperimentServer.h"
00002 #include "EthernetInterface.h"
00003 
00004 ExperimentServer::ExperimentServer() {
00005     _terminal = NULL;  
00006     _data_cnt = 0;
00007 }
00008 
00009 void ExperimentServer::attachTerminal( RawSerial & terminal) {
00010     _terminal = &terminal;
00011 } 
00012 
00013 void ExperimentServer::init() {
00014     // default configuration  
00015     char ip_address[] = "192.168.1.100";
00016     char subnet_mask[]= "255.255.255.0";
00017     char gateway[]    = "192.168.1.1";
00018     int server_port   = 11223;
00019     init(ip_address,subnet_mask,gateway, server_port);
00020 }   
00021     
00022 void ExperimentServer::init(const char * ip_addr, const char * subnet_mask, const char * gateway, unsigned int port) {
00023     if(_terminal!=NULL) {
00024         _terminal->printf("\r\n==============================\r\nStarting Server\r\n");
00025         _terminal->printf("...Intializing Ethernet\r\n");
00026     }
00027     _eth.init(ip_addr,subnet_mask,gateway);
00028     
00029     if(_terminal!=NULL)
00030         _terminal->printf("...Connecting\r\n");    
00031     _eth.connect();
00032     
00033     if(_terminal!=NULL)
00034         _terminal->printf("...Ethernet IP Address is %s\r\n", _eth.getIPAddress());
00035     
00036     _server.bind(port);
00037     if(_terminal!=NULL)
00038         _terminal->printf("...Listening on Port %d\r\n", port);
00039 }
00040 
00041 int ExperimentServer::getParams(float params[], int num_params) {
00042     if(_terminal!=NULL) {
00043         _terminal->printf("\r\n========================\r\nNew Experiment\r\n");
00044         _terminal->printf("...Waiting for parameters...\r\n");
00045     }
00046     int n = _server.receiveFrom(_client,(char *) params, num_params*sizeof(float));    
00047     if ( (n% 4) > 0 ) {
00048         if(_terminal!=NULL) {
00049             _terminal->printf("ERROR: input data bad size\r\n");
00050             _terminal->printf("ERROR: Expected %d got %d\r\n",4*num_params,n);
00051         }
00052         return false;    
00053     }
00054     if ( n / 4 != num_params) {
00055         if(_terminal!=NULL) {
00056             _terminal->printf("ERROR: input data bad size\r\n");
00057             _terminal->printf("ERROR: Expected %d got %d\r\n",4*num_params,n);
00058         }
00059         return false;    
00060     }
00061     
00062     if(_terminal!=NULL) {
00063         _terminal->printf("...Received input from: %s\r\n", _client.get_address());
00064         _terminal->printf("...Parameters: \r\n");
00065         for ( int j = 0 ; j < n/sizeof(float) ; j++) {
00066             _terminal->printf("      %d) %f\r\n",j+1,params[j]);
00067         }
00068     }
00069     return true;
00070 }
00071 void ExperimentServer::flushBuffer() {
00072     if(_data_cnt > 0) {
00073          _server.sendTo(_client,(char*) _buffer, 4*_data_cnt );    
00074         _data_cnt = 0;
00075     }
00076 }
00077 
00078 void ExperimentServer::sendData(float data_output[], int data_size) {
00079     if( data_size + _data_cnt > _MAX_BUFFER_SIZE) {
00080         flushBuffer();
00081     }
00082     for(int j = 0; j < data_size; j++) {
00083         _buffer[ _data_cnt++ ] = data_output[j];    
00084     }
00085 }
00086 void ExperimentServer::setExperimentComplete() {
00087     flushBuffer();
00088     char buff[] = {'0'};
00089     _server.sendTo(_client,buff,1);
00090 }