00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #pragma warning( disable : 4786 )
00016 #include <assert.h>
00017 #include <iostream>
00018 #include <planners\plannertype.h>
00019 #include <stdio.h>
00020 #include <strstream>
00021 #include <string.h>
00022 #include "windows.h"
00023
00024
00025
00026
00027 #include "server\Server.h"
00028
00029 const int stringSize = 500 ;
00030 using std::cout ;
00031 using std::endl ;
00032 using std::istrstream ;
00033
00034
00035
00036
00037
00038
00039
00040 int Server::numberOfServers = 0;
00041
00042
00043 Server::Server (SOCKET socket )
00044
00045 : socketOK(true)
00046
00047
00048 ,socket( socket )
00049
00050 {
00051
00052 m_CurrentConfig = m_StartConfig ;
00053 numberOfServers++ ;
00054
00055 }
00056
00057
00058 Server::~Server()
00059 {
00060
00061 numberOfServers-- ;
00062
00063 }
00064
00065
00066
00067
00068 bool Server::ExecuteCommand (const char* command)
00069 {
00070
00071
00072 static const char specifyRobotCommand[] = "#specify_robot" ;
00073 static const char specifyCollisionDetector[] = "#specify_collision_detector" ;
00074 static const char specifyPlanner[] = "#specify_planner" ;
00075 static const char planCommand[] = "#plan" ;
00076 static const char sendPath[] = "#send_path" ;
00077 static const char startConfig[] = "#start_config" ;
00078 static const char goalConfig[] = "#goal_config" ;
00079 static const char specifyEnvironment[] = "#specify_workspace" ;
00080 static const char configure[] = "#configure" ;
00081 static const char redrawCommand[] = "#redraw" ;
00082
00083 cout << "Executing command: " << command << endl ;
00084
00085
00086
00087 char firstWord[ stringSize ] = "" ;
00088 sscanf( command, "%s", firstWord ) ;
00089 int numChars = strlen( firstWord ) ;
00090 char* offset = strstr( command, firstWord ) ;
00091
00092
00093 if( stricmp( firstWord, configure ) == 0 )
00094 {
00095 return ConfigureRobot( offset + numChars ) ;
00096 }
00097
00098
00099 if( stricmp( firstWord, specifyRobotCommand ) == 0 )
00100 {
00101 return SpecifyRobot( offset + numChars ) ;
00102 }
00103
00104
00105 if( stricmp( firstWord, specifyEnvironment ) == 0 )
00106 {
00107 return SpecifyEnvironment( offset + numChars ) ;
00108 }
00109
00110
00111 if( stricmp( firstWord, specifyCollisionDetector ) == 0 )
00112 {
00113 return SpecifyCollisionDetector( command + numChars ) ;
00114 }
00115
00116
00117 if( stricmp( firstWord, specifyPlanner ) == 0 )
00118 {
00119 return SpecifyPlanner( command + numChars ) ;
00120 }
00121
00122
00123 if( stricmp( firstWord, planCommand ) == 0 )
00124 {
00125 return Plan() ;
00126 }
00127
00128
00129 if( stricmp( firstWord, sendPath) == 0 )
00130 {
00131 SendPath() ;
00132 return true ;
00133 }
00134
00135
00136 if( stricmp( firstWord, startConfig ) == 0 )
00137 {
00138 return StartConfig( command + numChars ) ;
00139 }
00140
00141
00142 if( stricmp( firstWord, goalConfig ) == 0 )
00143 {
00144 return GoalConfig( command + numChars ) ;
00145 }
00146
00147
00148
00149 if( stricmp( firstWord, redrawCommand ) == 0 )
00150 {
00151 return Redraw( command + numChars ) ;
00152 }
00153
00154 cout << "Unknown command: " << firstWord << endl ;
00155 return false ;
00156
00157 }
00158
00159 bool Server::SpecifyRobot (const char* command)
00160 {
00161
00162 const char threeCylinderRobot[] = "ThreeCylinderRobot" ;
00163
00164
00165 char firstWord[ stringSize ] = "" ;
00166 sscanf( command, "%s", firstWord ) ;
00167 int numChars = strlen( firstWord ) ;
00168
00169 if( stricmp( firstWord, threeCylinderRobot ) == 0 )
00170 {
00171 R_ThreeCylinderRobot() ;
00172 return true ;
00173 }
00174
00175 cout << "Specify Robot, Unknown Command: " << firstWord << endl ;
00176 return false ;
00177
00178 }
00179
00180 bool Server::SpecifyCollisionDetector (const char* command)
00181 {
00182
00183 const char vcollide[] = "vcollide" ;
00184
00185
00186 char firstWord[ stringSize ] = "" ;
00187 sscanf( command, "%s", firstWord ) ;
00188 int numChars = strlen( firstWord ) ;
00189
00190 delete collisionDetector ;
00191
00192 if( stricmp( firstWord, vcollide ) == 0 )
00193 {
00194 this->SetCollisionDetector( CD_VCOLLIDE ) ;
00195 return true ;
00196 }
00197 else
00198 {
00199 cout << "Specify CollisionDetector, Unknown Command: " << firstWord << endl ;
00200 return false ;
00201 }
00202 planner->SetCollisionDetector( collisionDetector ) ;
00203
00204 }
00205
00206 bool Server::SpecifyPlanner (const char* command)
00207 {
00208
00209 const char aca[] = "aca" ;
00210 const char sequential[] = "sequential" ;
00211
00212
00213 char firstWord[ stringSize ] = "" ;
00214 sscanf( command, "%s", firstWord ) ;
00215 int numChars = strlen( firstWord ) ;
00216
00217 bool success = true ;
00218 if( stricmp( firstWord, aca ) == 0 )
00219 {
00220 success &= this->SetPlanner( PLANNER_ACA ) ;
00221
00222 }
00223 else if( stricmp( firstWord, sequential ) == 0 )
00224 {
00225 success &= this->SetPlanner( PLANNER_SEQUENTIAL ) ;
00226
00227 }
00228 else
00229 {
00230
00231 cout << "Specify Planner, Unknown Command: " << firstWord << endl ;
00232 return false ;
00233 }
00234
00235 return success ;
00236
00237 }
00238
00239 bool Server::Plan ()
00240 {
00241
00242 this->SetCollisionDetector( collisionDetectorType ) ;
00243 assert( collisionDetector != NULL ) ;
00244
00245 CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector ) ;
00246 planner->SetCollisionDetector( cd ) ;
00247
00248
00249 planner->SetStartConfig( m_StartConfig ) ;
00250 planner->SetGoalConfig( m_GoalConfig ) ;
00251 planPassed = planner->Plan() ;
00252 return true ;
00253
00254 }
00255
00256 void Server::SendString (const char* str) const
00257 {
00258
00259 char* copy = new char[ strlen( str ) * 2 ] ;
00260 strcpy( copy, str ) ;
00261 FixNewlines( copy ) ;
00262
00263 if( send( socket, copy, strlen( copy ), 0 ) == SOCKET_ERROR )
00264 {
00265 fprintf( stderr, "mpk send error: %d\n", GetLastError() ) ;
00266 closesocket( socket ) ;
00267 socketOK = false ;
00268 }
00269
00270 delete[] copy ;
00271
00272 }
00273
00274 bool Server::SocketOK () const
00275 {
00276
00277 return socketOK ;
00278
00279 }
00280
00281 void Server::SendPath () const
00282 {
00283
00284 const PathBase* path = planner->GetPath() ;
00285 if( planPassed == false )
00286 {
00287 char failedString[] = "#PATH_FAILED\n" ;
00288 this->SendString( failedString ) ;
00289 }
00290 else
00291 {
00292 char str[ 1000 ] = "";
00293 path->Serialize( str, 1000 ) ;
00294 this->SendString( str ) ;
00295 }
00296
00297 }
00298
00299 bool Server::StartConfig (const char* command)
00300 {
00301
00302 int length = strlen( command ) ;
00303 char* copy = new char [ length + 1 ] ;
00304 strcpy( copy, command ) ;
00305 istrstream instr( copy, length ) ;
00306
00307 Configuration config ;
00308 instr >> config ;
00309 m_StartConfig = config ;
00310 assert( m_StartConfig.Length() != 0 ) ;
00311
00312 delete[] copy ;
00313
00314
00315 return true ;
00316
00317 }
00318
00319 bool Server::GoalConfig (const char* command)
00320 {
00321
00322 int length = strlen( command ) ;
00323 char* copy = new char [ length + 1 ] ;
00324 strcpy( copy, command ) ;
00325 istrstream instr( copy, strlen( copy ) ) ;
00326 Configuration config ;
00327 instr >> config ;
00328 m_GoalConfig = config ;
00329 assert( m_GoalConfig.Length() != 0 ) ;
00330
00331 delete[] copy ;
00332
00333
00334 return true ;
00335
00336 }
00337
00338 void Server::FixNewlines (char* str)
00339 {
00340
00341
00342 char* buffer = new char[ strlen( str ) * 2 ] ;
00343 strcpy( buffer, str ) ;
00344
00345 int j = 0 ;
00346
00347
00348 for( int i = 0; i < strlen( str ); i++ )
00349 {
00350
00351 if( ( str[ i ] == '\n' ) && ( str[ i - 1 ] != '\r' ) )
00352 {
00353
00354 strcpy( buffer + j + 2, str + i +1 ) ;
00355 buffer[ j ] = char( '\r' );
00356 buffer[ j + 1 ] = char( '\n' );
00357 j++ ;
00358 }
00359 j++ ;
00360 }
00361
00362 strcpy( str, buffer ) ;
00363 delete[] buffer ;
00364
00365 }
00366
00367 bool Server::SpecifyEnvironment (const char* command)
00368 {
00369
00370 return ParseObstacleFile( command ) ;
00371
00372
00373 }
00374
00375
00376
00377 bool Server::ConfigureRobot( const char* command )
00378 {
00379 int length = strlen( command ) ;
00380 char* copy = new char [ length + 1 ] ;
00381 strcpy( copy, command ) ;
00382 istrstream instr( copy, strlen( copy ) ) ;
00383 Configuration config ;
00384 instr >> config ;
00385 m_CurrentConfig = config ;
00386 double c0 = config[ 0 ] ;
00387 double c1 = config[ 1 ] ;
00388 double c2 = config[ 2 ] ;
00389 assert( m_CurrentConfig.Length() != 0 ) ;
00390
00391 Redraw( NULL ) ;
00392 delete[] copy ;
00393
00394 return true ;
00395 }
00396
00397 bool Server::Redraw( const char* command ) const
00398 {
00399
00400
00401 ::InvalidateRect( NULL, NULL, FALSE ) ;
00402 return true ;
00403 }
00404
00405
00406
00407
00408
00409