For a more complete server, see serverDemo.cpp.
00001 #include "Aria.h" 00002 #include "ArNetworking.h" 00003 00004 00014 /* This function is called ArServerHandlerCommands when our custom command is 00015 * recieved. */ 00016 void customCommandHandler(ArArgumentBuilder *args) 00017 { 00018 if(args && args->getArg(0)) 00019 ArLog::log(ArLog::Normal, "Recieved custom command with argument \"%s\".", args->getArg(0)); 00020 else 00021 ArLog::log(ArLog::Normal, "Recieved custom command with no arguments."); 00022 } 00023 00024 00025 int main(int argc, char **argv) 00026 { 00027 Aria::init(); 00028 ArRobot robot; 00029 ArArgumentParser parser(&argc, argv); 00030 ArSimpleConnector simpleConnector(&parser); 00031 00032 // The base server object, manages all connections to clients. 00033 ArServerBase server; 00034 00035 // This object simplifies configuration and opening of the ArServerBase 00036 // object. 00037 ArServerSimpleOpener simpleOpener(&parser); 00038 00039 // parse the command line. fail and print the help if the parsing fails 00040 // or if the help was requested with -help 00041 parser.loadDefaultArguments(); 00042 if (!simpleConnector.parseArgs() || !simpleOpener.parseArgs() || 00043 !parser.checkHelpAndWarnUnparsed()) 00044 { 00045 simpleConnector.logOptions(); 00046 simpleOpener.logOptions(); 00047 exit(1); 00048 } 00049 00050 // Use the ArSimpleOpener to open the server port 00051 if (!simpleOpener.open(&server)) 00052 { 00053 ArLog::log(ArLog::Terse, "Error: Could not open server on port %d", simpleOpener.getPort()); 00054 exit(1); 00055 } 00056 00057 00058 // 00059 // Create services attached to the base server: 00060 // 00061 00062 // Robot position etc.: 00063 ArServerInfoRobot serverInfoRobot(&server, &robot); 00064 00065 // Robot control modes (only one mode can be active at once): 00066 ArServerModeStop modeStop(&server, &robot); 00067 // old ArServerModeDrive modeDrive(&server, &robot); 00068 ArServerModeRatioDrive modeRatioDrive(&server, &robot); 00069 ArServerModeWander modeWander(&server, &robot); 00070 modeStop.addAsDefaultMode(); 00071 modeStop.activate(); 00072 00073 // This provides a simple way to add new commands. 00074 ArServerHandlerCommands commands(&server); 00075 00076 // Add our custom command. ArServerHandlerCommands also has other methods 00077 // for adding commands taht take different kinds of arguments, or no 00078 // arguments. 00079 ArGlobalFunctor1<ArArgumentBuilder*> customCommandFunctor(&customCommandHandler); 00080 commands.addStringCommand("ExampleCustomCommand", "Example of a custom command. simpleServerExample will print out the text sent with the command.", &customCommandFunctor); 00081 00082 // These objects provide various debugging and diagnostic custom commands: 00083 ArServerSimpleComUC uCCommands(&commands, &robot); // Get information about the robot 00084 ArServerSimpleComMovementLogging loggingCommands(&commands, &robot); // Control logging 00085 modeRatioDrive.addControlCommands(&commands); // Drive mode diagnostics 00086 00087 // This provides the client (e.g. MobileEyes) with a simple table of string values 00088 // (called an InfoGroup). An InfoGroup is kept globally by Aria. 00089 // The values in the table sent to clients are retrieved periodically by calling a 00090 // functor. 00091 ArServerInfoStrings stringInfo(&server); 00092 Aria::getInfoGroup()->addAddStringCallback(stringInfo.getAddStringFunctor()); 00093 00094 // Here are some example entries in the InfoGroup: 00095 Aria::getInfoGroup()->addStringInt( 00096 "Motor Packet Count", 10, 00097 new ArConstRetFunctorC<int, ArRobot>(&robot, 00098 &ArRobot::getMotorPacCount)); 00099 00100 // 00101 // Connect to the robot: 00102 // 00103 00104 if (!simpleConnector.connectRobot(&robot)) 00105 { 00106 printf("Error: Could not connect to robot... exiting\n"); 00107 Aria::shutdown(); 00108 return 1; 00109 } 00110 00111 00112 robot.enableMotors(); 00113 robot.runAsync(true); 00114 00115 // The simple opener might have information to display right before starting 00116 // the server thread: 00117 simpleOpener.checkAndLog(); 00118 00119 // now let the server base run in a new thread, accepting client connections. 00120 server.runAsync(); 00121 00122 ArLog::log(ArLog::Normal, "Server is now running... Press Ctrl-C to exit."); 00123 00124 robot.waitForRunExit(); 00125 Aria::shutdown(); 00126 exit(0); 00127 } 00128 00129
1.4.7