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

ArServerHandlerCommands.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 "ArExport.h"
00028 #include "ArServerHandlerCommands.h"
00029 
00030 AREXPORT ArServerHandlerCommands::ArServerHandlerCommands(
00031         ArServerBase *server) :
00032   myNetListCommandsCB(this, &ArServerHandlerCommands::netListCommands),
00033   myNetListStringCommandsCB(this, 
00034                             &ArServerHandlerCommands::netListStringCommands)
00035 {
00036   myServer = server;
00037   if (myServer != NULL)
00038   {
00039     myServer->addData("listCommands", 
00040                       "Gets a list of simple commands that can be sent to the server",
00041                          &myNetListCommandsCB, "none", "byte2: numberOfCommands, <repeats numberOfCommands> string: name, string: description",
00042                       "CustomCommands", "RETURN_SINGLE");
00043 
00044     myServer->addData("listStringCommands", 
00045                          "Gets a list of commands that can be sent to the server with string arguments",
00046                          &myNetListStringCommandsCB, "none", "byte2: numberOfCommands, <repeats numberOfCommands> string: name, string: description", "CustomCommands", "RETURN_SINGLE");
00047   }
00048 }
00049 
00050 AREXPORT ArServerHandlerCommands::~ArServerHandlerCommands()
00051 {
00052 
00053 }
00054 
00062 AREXPORT bool ArServerHandlerCommands::addCommand(
00063         const char *name, const char *description, ArFunctor *functor,
00064         const char *commandGroup)
00065 {
00066   std::string realName;
00067 
00068   if (myPrefix.size() != 0)
00069     realName = myPrefix;
00070 
00071   realName += name;
00072 
00073   ArFunctor3<ArServerClient *, ArNetPacket *, ArFunctor *> *fun = 
00074   new ArFunctor3C<ArServerHandlerCommands, ArServerClient *, 
00075           ArNetPacket *, ArFunctor *>(this,
00076                                &ArServerHandlerCommands::netParseCommand,
00077                                       NULL, NULL, functor);
00078         
00079   std::string group;
00080   if (myServer == NULL)
00081   {
00082     ArLog::log(ArLog::Normal, "Commands::addCommand: server is NULL");
00083     return false;
00084   }
00085   if (commandGroup != NULL)
00086     group = commandGroup;
00087   else
00088     group = "CustomCommands";
00089   if (myServer->addData(realName.c_str(), description, fun, "none", "none", 
00090                         group.c_str(), "RETURN_NONE"))
00091   {
00092     myCommands.push_back(realName.c_str());
00093     myCommandDescriptions.push_back(description);
00094     ArLog::log(ArLog::Verbose, "Added simple command %s", realName.c_str());
00095     return true;
00096   }
00097   else
00098   {
00099     delete fun;
00100     ArLog::log(ArLog::Normal, 
00101                "Could not add simple command %s", realName.c_str());
00102     return false;
00103   }
00104 }
00105 
00113 AREXPORT bool ArServerHandlerCommands::addStringCommand(
00114         const char *name, const char *description, 
00115         ArFunctor1<ArArgumentBuilder *> *functor, const char *commandGroup)
00116 {
00117   std::string realName;
00118 
00119   if (myPrefix.size() != 0)
00120     realName = myPrefix;
00121 
00122   realName += name;
00123 
00124   ArFunctor3<ArServerClient *, ArNetPacket *, 
00125                       ArFunctor1<ArArgumentBuilder *> *> *fun = new 
00126                                      ArFunctor3C<ArServerHandlerCommands,
00127                                             ArServerClient *, ArNetPacket *, 
00128                                        ArFunctor1<ArArgumentBuilder *> *>(this,
00129                       &ArServerHandlerCommands::netParseStringCommand,
00130                                                          NULL, NULL, functor);
00131         
00132   if (myServer == NULL)
00133   {
00134     ArLog::log(ArLog::Normal, "Commands::addStringCommand: server is NULL");
00135     return false;
00136   }              
00137   std::string group;
00138   if (commandGroup != NULL)
00139     group = commandGroup;
00140   else
00141     group = "CustomCommands";
00142   if (myServer->addData(realName.c_str(), description, fun, 
00143                         "string: argumentToCommand", "none", group.c_str(),
00144                         "RETURN_NONE"))
00145   {
00146     myStringCommands.push_back(realName.c_str());
00147     myStringCommandDescriptions.push_back(description);
00148     ArLog::log(ArLog::Verbose, "Added simple command with string %s", 
00149                realName.c_str());
00150     return true;
00151   }
00152   else
00153   {
00154     delete fun;
00155     ArLog::log(ArLog::Normal, 
00156                "Could not add simple command with string %s", 
00157                realName.c_str());
00158     return false;
00159   }
00160 }
00161 
00162 void ArServerHandlerCommands::netParseCommand(ArServerClient *client, 
00163                                                     ArNetPacket *packet, 
00164                                                     ArFunctor *functor)
00165 {
00166   if (functor == NULL)
00167   {
00168     ArLog::log(ArLog::Terse, "Command has NULL functor");
00169     return;
00170   }
00171   functor->invoke();
00172 }
00173 
00174 void ArServerHandlerCommands::netParseStringCommand(
00175         ArServerClient *client, ArNetPacket *packet, 
00176         ArFunctor1<ArArgumentBuilder *> *functor)
00177 {
00178   char buf[1024];
00179   if (packet == NULL)
00180   {
00181     ArLog::log(ArLog::Terse, "String command has NULL packet");
00182     return;
00183   }
00184   if (functor == NULL)
00185   {
00186     ArLog::log(ArLog::Terse, "String command has NULL functor");
00187     return;
00188   }
00189   packet->bufToStr(buf, sizeof(buf));
00190   ArArgumentBuilder arg;
00191   arg.add(buf);
00192   arg.setFullString(buf);
00193   functor->invoke(&arg);
00194 }
00195 
00196 AREXPORT void ArServerHandlerCommands::netListCommands(ArServerClient *client,
00197                                                        ArNetPacket *packet)
00198 {
00199   ArNetPacket sendPacket;
00200   std::list<std::string>::iterator commIt;
00201   std::list<std::string>::iterator descIt;
00202   sendPacket.byte2ToBuf(myCommands.size());
00203   for (commIt = myCommands.begin(), descIt = myCommandDescriptions.begin();
00204        commIt != myCommands.end() && descIt != myCommandDescriptions.end();
00205        commIt++, descIt++)
00206   {
00207     sendPacket.strToBuf((*commIt).c_str());
00208     sendPacket.strToBuf((*descIt).c_str());
00209   }
00210   client->sendPacketTcp(&sendPacket);
00211 }
00212 
00213 AREXPORT void ArServerHandlerCommands::netListStringCommands(
00214         ArServerClient *client, ArNetPacket *packet)
00215 {
00216   ArNetPacket sendPacket;
00217   std::list<std::string>::iterator commIt;
00218   std::list<std::string>::iterator descIt;
00219   sendPacket.byte2ToBuf(myStringCommands.size());
00220   for (commIt = myStringCommands.begin(), 
00221         descIt = myStringCommandDescriptions.begin();
00222        commIt != myStringCommands.end() && 
00223         descIt != myStringCommandDescriptions.end();
00224        commIt++, descIt++)
00225   {
00226     sendPacket.strToBuf((*commIt).c_str());
00227     sendPacket.strToBuf((*descIt).c_str());
00228   }
00229   client->sendPacketTcp(&sendPacket);
00230 }
00231 
00232 AREXPORT void ArServerHandlerCommands::setPrefix(const char *prefix)
00233 {
00234   myPrefix = prefix;
00235 }
00236 
00237 AREXPORT const char *ArServerHandlerCommands::getPrefix(void)
00238 {
00239   return myPrefix.c_str();
00240 }

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