Patrick Wensing / ExperimentServer

Dependents:   Bezier_Trajectory_Follower Dolphin 2_131TEST Jerby ... more

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( Serial & 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     
00028     int code = _eth.set_network(ip_addr,subnet_mask,gateway);
00029     if(_terminal!=NULL) {
00030         _terminal->printf("...Connecting\r\n");
00031         if(code!=0) 
00032             _terminal->printf("Error Code = %d\r\n",code);
00033     }
00034        
00035     code = _eth.connect();
00036     if(_terminal!=NULL) {
00037         _terminal->printf("...Ethernet IP Address is %s\r\n",_eth.get_ip_address());
00038         if(code!=0) 
00039             _terminal->printf("Error Code = %d\r\n",code);    
00040     }
00041     
00042     SocketAddress sock;
00043     sock.set_port(port);
00044     sock.set_ip_address(ip_addr);
00045     
00046     code = _server.open(&_eth);
00047     if(_terminal!=NULL) {
00048         _terminal->printf("...Opened\n");
00049         if(code!=0) 
00050             _terminal->printf("Error Code = %d\r\n",code);    
00051     }
00052     
00053     code = _server.bind(sock);
00054     if(_terminal!=NULL) {
00055         _terminal->printf("...Listening on Port %d\r\n",port);
00056         if(code!=0) 
00057             _terminal->printf("Error Code = %d\r\n",code);
00058     }
00059 }
00060 
00061 int ExperimentServer::getParams(float params[], int num_params) {
00062     if(_terminal!=NULL) {
00063         _terminal->printf("\r\n========================\r\nNew Experiment\r\n");
00064         _terminal->printf("...Waiting for parameters...\r\n");
00065     }
00066         
00067     int n = _server.recvfrom(&_client,(char *) params, num_params*sizeof(float));    
00068     if ( (n% 4) > 0 ) {
00069         if(_terminal!=NULL) {
00070             _terminal->printf("ERROR: input data bad size\r\n");
00071             _terminal->printf("ERROR: Expected %d got %d\r\n",4*num_params,n);
00072         }
00073         return false;    
00074     }
00075     if ( n / 4 != num_params) {
00076         if(_terminal!=NULL) {
00077             _terminal->printf("ERROR: input data bad size\r\n");
00078             _terminal->printf("ERROR: Expected %d got %d\r\n",4*num_params,n);
00079         }
00080         return false;    
00081     }
00082     
00083     if(_terminal!=NULL) {
00084         _terminal->printf("...Received input from: %s\r\n", _client.get_ip_address());
00085         _terminal->printf("...Parameters: \r\n");
00086         for ( int j = 0 ; j < n/sizeof(float) ; j++) {
00087             _terminal->printf("      %d) %f\r\n",j+1,params[j]);
00088         }
00089     }
00090     return true;
00091 }
00092 void ExperimentServer::flushBuffer() {
00093     if(_data_cnt > 0) {
00094          _server.sendto(_client,(char*) _buffer, 4*_data_cnt );    
00095         _data_cnt = 0;
00096     }
00097 }
00098 
00099 void ExperimentServer::sendData(float data_output[], int data_size) {
00100     if( data_size + _data_cnt > _MAX_BUFFER_SIZE) {
00101         flushBuffer();
00102     }
00103     for(int j = 0; j < data_size; j++) {
00104         _buffer[ _data_cnt++ ] = data_output[j];    
00105     }
00106 }
00107 void ExperimentServer::setExperimentComplete() {
00108     flushBuffer();
00109     char buff[] = {'0'};
00110     _server.sendto(_client,buff,1);
00111 }