server/Server.cpp

Go to the documentation of this file.
00001 //## begin module%3796143E0382.cm preserve=no
00002 //        %X% %Q% %Z% %W%
00003 //## end module%3796143E0382.cm
00004 
00005 //## begin module%3796143E0382.cp preserve=no
00006 //## end module%3796143E0382.cp
00007 
00008 //## Module: Server%3796143E0382; Pseudo Package body
00009 //## Source file: E:\mpk\code\MPKserver\Server.cpp
00010 
00011 //## begin module%3796143E0382.additionalIncludes preserve=no
00012 //## end module%3796143E0382.additionalIncludes
00013 
00014 //## begin module%3796143E0382.includes preserve=yes
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 //IMPROVE: alphabetize these
00024 //## end module%3796143E0382.includes
00025 
00026 // Server
00027 #include "server\Server.h"
00028 //## begin module%3796143E0382.additionalDeclarations preserve=yes
00029 const int stringSize = 500 ;
00030 using std::cout ;
00031 using std::endl ;
00032 using std::istrstream ;
00033 
00034 //## end module%3796143E0382.additionalDeclarations
00035 
00036 // Class Server 
00037 
00038 
00039 //## begin Server::numberOfServers%37A1F74F009C.attr preserve=no  protected: static int {UA} 0
00040 int Server::numberOfServers = 0;
00041 //## end Server::numberOfServers%37A1F74F009C.attr
00042 
00043 Server::Server (SOCKET socket )
00044   //## begin Server::Server%933360616.hasinit preserve=no
00045       : socketOK(true)
00046   //## end Server::Server%933360616.hasinit
00047   //## begin Server::Server%933360616.initialization preserve=yes
00048   ,socket( socket )
00049   //## end Server::Server%933360616.initialization
00050 {
00051   //## begin Server::Server%933360616.body preserve=yes
00052         m_CurrentConfig = m_StartConfig ;
00053         numberOfServers++ ;
00054   //## end Server::Server%933360616.body
00055 }
00056 
00057 
00058 Server::~Server()
00059 {
00060   //## begin Server::~Server%.body preserve=yes
00061         numberOfServers-- ;
00062   //## end Server::~Server%.body
00063 }
00064 
00065 
00066 
00067 //## Other Operations (implementation)
00068 bool Server::ExecuteCommand (const char* command)
00069 {
00070   //## begin Server::ExecuteCommand%932581524.body preserve=yes
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         //determine the command
00086         //extract the first word of the command         //IMPROVE: this should be a function
00087         char firstWord[ stringSize ] = "" ;
00088         sscanf( command, "%s", firstWord ) ;
00089         int numChars = strlen( firstWord ) ;
00090         char* offset = strstr( command, firstWord ) ;
00091 
00092         //check if it's a configure command - moves the position of the robot
00093         if( stricmp( firstWord, configure ) == 0 )
00094         {
00095                 return ConfigureRobot( offset + numChars ) ;
00096         }
00097 
00098         //check if it's a "specify_robot" command
00099         if( stricmp( firstWord, specifyRobotCommand ) == 0 )
00100         {
00101                 return SpecifyRobot( offset + numChars ) ;
00102         }
00103 
00104         //check if it's a "specify_environment" command
00105         if( stricmp( firstWord, specifyEnvironment ) == 0 )
00106         {
00107                 return SpecifyEnvironment( offset + numChars ) ;
00108         }
00109 
00110         //check if it's a "specify_collision_detector" command
00111         if( stricmp( firstWord, specifyCollisionDetector ) == 0 )
00112         {
00113                 return SpecifyCollisionDetector( command + numChars ) ;
00114         }
00115 
00116         //check if it's a "specify_planner" command
00117         if( stricmp( firstWord, specifyPlanner ) == 0 )
00118         {
00119                 return SpecifyPlanner( command + numChars ) ;
00120         }
00121 
00122         //check if it's a "plan" command
00123         if( stricmp( firstWord, planCommand ) == 0 )
00124         {
00125                 return Plan() ;
00126         }
00127 
00128         //check if it's a "sendPath" command
00129         if( stricmp( firstWord, sendPath) == 0 )
00130         {
00131                 SendPath() ;    //IMPROVE: should this return a bool?
00132                 return true ;
00133         }
00134 
00135         //check if it's a "startConfig" command
00136         if( stricmp( firstWord, startConfig ) == 0 )
00137         {
00138                 return StartConfig( command + numChars ) ;
00139         }
00140 
00141         //check if it's a "goalConfig" command
00142         if( stricmp( firstWord, goalConfig ) == 0 )
00143         {
00144                 return GoalConfig( command + numChars ) ;
00145         }
00146 
00147         //check if it's a "redraw" command
00148         //IMPROVE: the most frequently used commands should come first in the if checking
00149         if( stricmp( firstWord, redrawCommand ) == 0 )
00150         {
00151                 return Redraw( command + numChars ) ;
00152         }
00153 
00154         cout << "Unknown command: " << firstWord << endl ;
00155         return false ;  //unknown command
00156   //## end Server::ExecuteCommand%932581524.body
00157 }
00158 
00159 bool Server::SpecifyRobot (const char* command)
00160 {
00161   //## begin Server::SpecifyRobot%932600044.body preserve=yes
00162         const char threeCylinderRobot[] = "ThreeCylinderRobot" ;
00163 
00164         //extract the first word of the command 
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   //## end Server::SpecifyRobot%932600044.body
00178 }
00179 
00180 bool Server::SpecifyCollisionDetector (const char* command)
00181 {
00182   //## begin Server::SpecifyCollisionDetector%932600046.body preserve=yes
00183         const char vcollide[] = "vcollide" ;
00184 
00185         //extract the first word of the command 
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 ) ;//IMPROVE: this can potentially yeild an error code
00195                 return true ;
00196         }
00197         else
00198         {
00199                 cout << "Specify CollisionDetector, Unknown Command: " << firstWord << endl ;
00200                 return false ;
00201         }
00202         planner->SetCollisionDetector( collisionDetector ) ;
00203   //## end Server::SpecifyCollisionDetector%932600046.body
00204 }
00205 
00206 bool Server::SpecifyPlanner (const char* command)
00207 {
00208   //## begin Server::SpecifyPlanner%932600048.body preserve=yes
00209         const char aca[] = "aca" ;
00210         const char sequential[] = "sequential" ;
00211 
00212         //extract the first word of the command 
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                 //P_Aca() ;             //IMPROVE: this can potentially yeild an error code
00222         }
00223         else if( stricmp( firstWord, sequential ) == 0 )
00224         {
00225                 success &= this->SetPlanner( PLANNER_SEQUENTIAL ) ;
00226                 //P_Sequential() ;
00227         }
00228         else
00229         {
00230                 //this is the case where the command is unknown
00231                 cout << "Specify Planner, Unknown Command: " << firstWord << endl ;
00232                 return false ;
00233         }
00234         //planner->SetCollisionDetector( collisionDetector ) ;
00235         return success ;
00236   //## end Server::SpecifyPlanner%932600048.body
00237 }
00238 
00239 bool Server::Plan ()
00240 {
00241   //## begin Server::Plan%932600050.body preserve=yes
00242         this->SetCollisionDetector( collisionDetectorType ) ;
00243         assert( collisionDetector != NULL ) ;   //there must be a collision detector at this point
00244 
00245         CD_BasicStyle* cd = dynamic_cast< CD_BasicStyle* >( collisionDetector ) ;
00246         planner->SetCollisionDetector( cd ) ;   //IMPROVE: is this in multiple places?
00247         //IMPROVE: don't want to have CD_Vcollide explicitly cast here 
00248 
00249         planner->SetStartConfig( m_StartConfig ) ;      //IMPROVE: why does this call plannerbase, not PL_Juan
00250         planner->SetGoalConfig( m_GoalConfig ) ;
00251         planPassed = planner->Plan() ;
00252         return true ;
00253   //## end Server::Plan%932600050.body
00254 }
00255 
00256 void Server::SendString (const char* str) const
00257 {
00258   //## begin Server::SendString%933360615.body preserve=yes
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   //## end Server::SendString%933360615.body
00272 }
00273 
00274 bool Server::SocketOK () const
00275 {
00276   //## begin Server::SocketOK%933360617.body preserve=yes
00277         return socketOK ;
00278   //## end Server::SocketOK%933360617.body
00279 }
00280 
00281 void Server::SendPath () const
00282 {
00283   //## begin Server::SendPath%933360618.body preserve=yes
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 ] = "";          //IMPROVE: is 1000 enough?
00293                 path->Serialize( str, 1000 ) ;
00294                 this->SendString( str ) ;
00295         }
00296   //## end Server::SendPath%933360618.body
00297 }
00298 
00299 bool Server::StartConfig (const char* command)
00300 {
00301   //## begin Server::StartConfig%933617731.body preserve=yes
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         //IMPROVE: need some error checking about sizes and ranges?
00314 
00315         return true ;
00316   //## end Server::StartConfig%933617731.body
00317 }
00318 
00319 bool Server::GoalConfig (const char* command)
00320 {
00321   //## begin Server::GoalConfig%933617732.body preserve=yes
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         //IMPROVE: need some error checking about sizes and ranges?
00333 
00334         return true ;
00335   //## end Server::GoalConfig%933617732.body
00336 }
00337 
00338 void Server::FixNewlines (char* str)
00339 {
00340   //## begin Server::FixNewlines%933617733.body preserve=yes
00341         
00342         char* buffer = new char[ strlen( str ) * 2 ] ;
00343         strcpy( buffer, str ) ;
00344 
00345         int j = 0 ;
00346 
00347         //look for all the '\n's in the string, and replace them with '/r/n'
00348         for( int i = 0; i < strlen( str ); i++ )
00349         {
00350                 //if you find an '\n'
00351                 if( ( str[ i ] == '\n' ) && ( str[ i - 1 ] != '\r' ) )
00352                 {
00353                         //move the string down one and insert the '\r'
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   //## end Server::FixNewlines%933617733.body
00365 }
00366 
00367 bool Server::SpecifyEnvironment (const char* command)
00368 {
00369   //## begin Server::SpecifyEnvironment%936229639.body preserve=yes
00370         return ParseObstacleFile( command ) ;
00371         //return ParseObstacleFile( "\nshapes/environment.wrl" ) ;
00372   //## end Server::SpecifyEnvironment%936229639.body
00373 }
00374 
00375 // Additional Declarations
00376   //## begin Server%3796143E0382.declarations preserve=yes
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         //IMPROVE: need some error checking about sizes and ranges?
00394         return true ;
00395 }
00396 
00397 bool Server::Redraw( const char* command ) const
00398 {
00399         //IMPROVE: why does this function require a string as a parameter?
00400         //IMPROVE: accomplish refreshing the views some other way.  I expect this to be really slow
00401         ::InvalidateRect( NULL, NULL, FALSE ) ;
00402         return true ;
00403 }
00404   //## end Server%3796143E0382.declarations
00405 
00406 //## begin module%3796143E0382.epilog preserve=yes
00407 //## end module%3796143E0382.epilog
00408 
00409 

Generated on Sat Apr 1 21:30:47 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO