MulticastInputModule.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00042
00043
00044 #ifdef WIN32
00045 #pragma warning(disable:4786)
00046 #endif
00047 #include <stdlib.h>
00048 #include <string>
00049 #include <ace/INET_Addr.h>
00050 #include <ace/SOCK_Dgram.h>
00051 #include <ace/Time_Value.h>
00052 #include <ace/Log_Msg.h>
00053
00054 #include "MulticastInputModule.h"
00055 #include "MulticastInputSource.h"
00056
00057 #include <math.h>
00058 #include <stdio.h>
00059 #include <iostream>
00060
00061
00062 #ifndef OT_NO_NETWORK_SUPPORT
00063
00064
00065 namespace ot {
00066
00067
00068 static const float DEG_TO_RAD = (float)(3.14159/180.0);
00069
00070
00071
00072
00073 MulticastInputModule::MulticastInputModule() : ThreadModule(), NodeFactory(), stop(0)
00074 {
00075
00076 }
00077
00078
00079
00080
00081 MulticastInputModule::~MulticastInputModule()
00082 {
00083 sources.clear();
00084 }
00085
00086
00087
00088
00089 Node * MulticastInputModule::createNode( const std::string& name, StringTable& attributes)
00090 {
00091 if( name.compare("MulticastInputSource") == 0 )
00092 {
00093 stop = 0;
00094 int number;
00095 int num = sscanf(attributes.get("number").c_str(), " %i", &number );
00096 if( num == 0 )
00097 {
00098 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Error in converting MulticastInputSource number !\n")));
00099 return NULL;
00100 }
00101 NodeVector::iterator it;
00102 for( it = sources.begin(); it != sources.end(); it++ ){
00103 MulticastInputSource * source = (MulticastInputSource*)(*it);
00104 if( source->number == number )
00105 {
00106 break;
00107 }
00108 }
00109 if( it != sources.end())
00110 {
00111 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Source with number %d exists allready\n"), number));
00112 return NULL;
00113 }
00114 MulticastInputSource * source = new MulticastInputSource( number);
00115 sources.push_back( source );
00116 ACE_DEBUG((LM_INFO, ACE_TEXT("ot:Built MulticastInputSource node. Number: %d\n"), number));
00117 return source;
00118 }
00119 return NULL;
00120 }
00121
00122
00123
00124 void MulticastInputModule::start()
00125 {
00126 if( isInitialized() && !sources.empty())
00127 ThreadModule::start();
00128 }
00129
00130
00131
00132
00133 void MulticastInputModule::close()
00134 {
00135 lock();
00136 stop = 1;
00137 unlock();
00138 }
00139
00140
00141
00142 void MulticastInputModule::run()
00143 {
00144 ACE_Time_Value timeOut( 1, 0 );
00145 int retval;
00146 ACE_INET_Addr addr( port );
00147
00148 socket = new ACE_SOCK_Dgram( addr );
00149
00150 receiveBufferSize = 3 * sizeof(short int) + 8 * sizeof(StationRecord);
00151 receiveBuffer = new char[receiveBufferSize];
00152
00153
00154
00155 std::string receiveString;
00156
00157 StationRecord *recordTemp = new StationRecord[8];
00158
00159 while(1)
00160 {
00161 do
00162 {
00163 if( (retval = socket->recv( receiveBuffer, receiveBufferSize , addr, 0, &timeOut )) == -1 )
00164 {
00165 if(errno != ETIME && errno != 0)
00166 {
00167 ACE_DEBUG((LM_ERROR, ACE_TEXT("ot:Error %d receiving data !\n"), errno));
00168 exit( -1 );
00169 }
00170 }
00171 } while( retval < 0 && stop == 0);
00172
00173 if( stop != 0 )
00174 {
00175 break;
00176 }
00177
00178
00179
00180
00181
00182 parseString(std::string(receiveBuffer, retval), recordTemp);
00183 NodeVector::iterator it;
00184
00185 lock();
00186 for( it = sources.begin(); it != sources.end(); it++)
00187 {
00188
00189 MulticastInputSource * source = (MulticastInputSource*)(*it);
00190 int bodyID = source->number;
00191 if( recordTemp[bodyID].valid == 1 )
00192 {
00193
00194 source->event.getPosition()[0] = recordTemp[bodyID].position[0];
00195 source->event.getPosition()[1] = recordTemp[bodyID].position[1];
00196 source->event.getPosition()[2] = recordTemp[bodyID].position[2];
00197
00198
00199
00200 MathUtils::eulerToQuaternion( recordTemp[bodyID].orientation[0], recordTemp[bodyID].orientation[1], recordTemp[bodyID].orientation[2], source->event.getOrientation() );
00201
00202 source->event.timeStamp();
00203
00204
00205 source->event.getConfidence() = recordTemp[bodyID].confidence;
00206
00207 source->changed = 1;
00208 }
00209 else
00210 {
00211
00212
00213 if (source->event.getConfidence() > 0.000001f)
00214 {
00215 source->changed = 1;
00216 source->event.getConfidence() = 0.0f;
00217 }
00218 }
00219
00220 }
00221 unlock();
00222 }
00223 socket->close();
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 void MulticastInputModule::parseString(std::string input, StationRecord *recordTemp)
00239 {
00240
00241
00242 const char *stream = input.c_str();
00243
00244 version = ntohs(*((short int*) stream));
00245 stream +=sizeof(u_short);
00246 u_short numberOfStations = ntohs(*((short int*) stream));
00247 stream +=sizeof(u_short);
00248 u_short flags = ntohs(*((short int*) stream));
00249 stream +=sizeof(u_short);
00250
00251 if (input.size()< 6+numberOfStations*(3*sizeof(u_short)+7*sizeof(float)))
00252 {
00253 printf("version %d numberOfStations %d flags %d\n",version, numberOfStations, flags);
00254 printf("network packet size is %d instead of %d\nEXIT.", (int)input.size(), (int)(6+numberOfStations*sizeof(StationRecord)));
00255 exit(1);
00256 }
00257
00258 if (version == 1) {
00259 for (int i=0; i<numberOfStations; i++) {
00260
00261
00262
00263
00264
00265
00266 recordTemp[i].valid = ntohs(*((u_short*) stream));
00267 stream +=sizeof(u_short);
00268 recordTemp[i].id = ntohs(*((u_short*) stream));
00269 stream +=sizeof(u_short);
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 union longToFloat {
00291 long l;
00292 float f;
00293 } con;
00294
00295 con.l = ntohl(*((long int*) stream));
00296 recordTemp[i].position[0] = con.f;
00297 stream +=sizeof(u_long);
00298
00299
00300 con.l = ntohl(*((long int*) stream));
00301 recordTemp[i].position[1] = con.f;
00302 stream +=sizeof(u_long);
00303
00304 con.l = ntohl(*((long int*) stream));
00305 recordTemp[i].position[2] = con.f;
00306 stream +=sizeof(u_long);
00307
00308 con.l = ntohl(*((long int*) stream));
00309 recordTemp[i].orientation[0] = con.f;
00310 stream +=sizeof(u_long);
00311
00312 con.l = ntohl(*((long int*) stream));
00313 recordTemp[i].orientation[1] = con.f;
00314 stream +=sizeof(u_long);
00315
00316 con.l = ntohl(*((long int*) stream));
00317 recordTemp[i].orientation[2] = con.f;
00318 stream +=sizeof(u_long);
00319
00320 recordTemp[i].buttons = ntohs(*((u_short*) stream));
00321 stream +=sizeof(u_short);
00322
00323 con.l = ntohl(*((long int*) stream));
00324 recordTemp[i].confidence = con.f;
00325 stream +=sizeof(u_long);
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 }
00341 } else {
00342 printf("received wrong version %d instead of 1. EXIT.\n",version);
00343 exit(1);
00344 }
00345 }
00346
00347
00348 void MulticastInputModule::pushEvent()
00349 {
00350 if( isInitialized() )
00351 {
00352 for( NodeVector::iterator it = sources.begin(); it != sources.end(); it++ )
00353 {
00354 MulticastInputSource *source = (MulticastInputSource *) *it;
00355 lock();
00356 if( source->changed == 1 )
00357 {
00358 source->updateObservers( source->event );
00359 source->changed = 0;
00360 }
00361 unlock();
00362 }
00363 }
00364 }
00365
00366
00367
00368
00369
00370 void MulticastInputModule::init(StringTable& attributes, ConfigNode * localTree)
00371 {
00372 ThreadModule::init( attributes, localTree );
00373 int num;
00374
00375
00376 num = sscanf(attributes.get("port").c_str(), " %i", &port );
00377 if( num == 0 )
00378 {
00379 port = 12348;
00380 }
00381
00382 }
00383
00384
00385 }
00386
00387
00388 #else
00389 #pragma message(">>> OT_NO_NETWORK_SUPPORT")
00390 #endif // OT_NO_NETWORK_SUPPORT
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406