Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages | Examples

simpleServerExample.cpp

Go to the documentation of this file.
00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 #include "ArNetworking.h"
00028 
00029 
00037 int main(int argc, char **argv)
00038 {
00039   Aria::init();
00040   ArRobot robot;
00041   ArArgumentParser parser(&argc, argv);
00042   ArSimpleConnector simpleConnector(&parser);
00043 
00044   // The base server object, manages all connections to clients.
00045   ArServerBase server;
00046 
00047   // This object simplifies configuration and opening of the ArServerBase
00048   // object.
00049   ArServerSimpleOpener simpleOpener(&parser);
00050 
00051   // parse the command line. fail and print the help if the parsing fails
00052   // or if the help was requested with -help
00053   parser.loadDefaultArguments();
00054   if (!simpleConnector.parseArgs() || !simpleOpener.parseArgs() || 
00055       !parser.checkHelpAndWarnUnparsed())
00056   {    
00057     simpleConnector.logOptions();
00058     simpleOpener.logOptions();
00059     exit(1);
00060   }
00061 
00062   // Use the ArSimpleOpener to open the server port
00063   if (!simpleOpener.open(&server))
00064   {
00065     ArLog::log(ArLog::Terse, "Error: Could not open server on port %d", simpleOpener.getPort());
00066     exit(1);
00067   }
00068 
00069 
00070   //
00071   // Create services attached to the base server:
00072   // 
00073   
00074   // Robot position etc.:
00075   ArServerInfoRobot serverInfoRobot(&server, &robot);
00076 
00077   // Robot control modes (only one mode can be active at once):
00078   ArServerModeStop modeStop(&server, &robot);    
00079   // old ArServerModeDrive modeDrive(&server, &robot); 
00080   ArServerModeRatioDrive modeRatioDrive(&server, &robot);
00081   ArServerModeWander modeWander(&server, &robot);
00082   modeStop.addAsDefaultMode();
00083   modeStop.activate();
00084 
00085   // This provides a simple way to add debugging commands, etc.
00086   ArServerHandlerCommands commands(&server);
00087   ArServerSimpleComUC uCCommands(&commands, &robot);  // Get information about the robot
00088   ArServerSimpleComMovementLogging loggingCommands(&commands, &robot);  // Control logging
00089   modeRatioDrive.addControlCommands(&commands);  // Drive mode diagnostics
00090 
00091   // This provides the client (e.g. MobileEyes) with a simple table of string values
00092   // (called an InfoGroup). An InfoGroup is kept globally by Aria.
00093   // The values in the table sent to clients are retrieved periodically by calling a 
00094   // functor.
00095   ArServerInfoStrings stringInfo(&server);
00096   Aria::getInfoGroup()->addAddStringCallback(stringInfo.getAddStringFunctor());
00097 
00098   // Here are some example entries in the InfoGroup:
00099   Aria::getInfoGroup()->addStringInt(
00100           "Motor Packet Count", 10, 
00101           new ArConstRetFunctorC<int, ArRobot>(&robot, 
00102                                                &ArRobot::getMotorPacCount));
00103 
00104   //
00105   // Connect to the robot:
00106   // 
00107   
00108   if (!simpleConnector.connectRobot(&robot))
00109   {
00110     printf("Error: Could not connect to robot... exiting\n");
00111     Aria::shutdown();
00112     return 1;
00113   }
00114 
00115 
00116   robot.enableMotors();
00117   robot.runAsync(true);
00118 
00119   // The simple opener might have information to display right before starting 
00120   // the server thread:
00121   simpleOpener.checkAndLog();
00122 
00123   // now let the server base run in a new thread, accepting client connections.
00124   server.runAsync();
00125 
00126   ArLog::log(ArLog::Normal, "Server is now running... Press Ctrl-C to exit.");
00127 
00128   robot.waitForRunExit();
00129   Aria::shutdown();
00130   exit(0);  
00131 }
00132 
00133 

Generated on Tue Feb 20 10:51:50 2007 for ArNetworking by  doxygen 1.4.0