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

ArClientSimpleConnector.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 "ArClientSimpleConnector.h"
00029 
00030 AREXPORT ArClientSimpleConnector::ArClientSimpleConnector(int *argc, char **argv) :
00031   myParseArgsCB(this, &ArClientSimpleConnector::parseArgs),
00032   myLogOptionsCB(this, &ArClientSimpleConnector::logOptions)
00033 {
00034   reset();
00035   myParser = new ArArgumentParser(argc, argv);
00036   myOwnParser = true;
00037   myParseArgsCB.setName("ArClientSimpleConnector");
00038   Aria::addParseArgsCB(&myParseArgsCB, 75);
00039   myLogOptionsCB.setName("ArClientSimpleConnector");
00040   Aria::addLogOptionsCB(&myLogOptionsCB, 75);
00041 }
00042 
00043 AREXPORT ArClientSimpleConnector::ArClientSimpleConnector(ArArgumentBuilder *builder)  :
00044   myParseArgsCB(this, &ArClientSimpleConnector::parseArgs),
00045   myLogOptionsCB(this, &ArClientSimpleConnector::logOptions)
00046 {
00047   reset();
00048   myParser = new ArArgumentParser(builder);
00049   myOwnParser = true;
00050   myParseArgsCB.setName("ArClientSimpleConnector");
00051   Aria::addParseArgsCB(&myParseArgsCB, 75);
00052   myLogOptionsCB.setName("ArClientSimpleConnector");
00053   Aria::addLogOptionsCB(&myLogOptionsCB, 75);
00054 }
00055 
00056 AREXPORT ArClientSimpleConnector::ArClientSimpleConnector(ArArgumentParser *parser) :
00057   myParseArgsCB(this, &ArClientSimpleConnector::parseArgs),
00058   myLogOptionsCB(this, &ArClientSimpleConnector::logOptions)
00059 {
00060   reset();
00061   myParser = parser;
00062   myOwnParser = false;
00063   myParseArgsCB.setName("ArClientSimpleConnector");
00064   Aria::addParseArgsCB(&myParseArgsCB, 75);
00065   myLogOptionsCB.setName("ArClientSimpleConnector");
00066   Aria::addLogOptionsCB(&myLogOptionsCB, 75);
00067 }
00068 
00069 AREXPORT ArClientSimpleConnector::~ArClientSimpleConnector(void)
00070 {
00071 
00072 }
00073 
00074 void ArClientSimpleConnector::reset(void)
00075 {
00076   myHost = NULL;
00077   myUser = NULL;
00078   myPassword = NULL;
00079   myServerKey = NULL;
00080   myPort = 7272;
00081   myNoPassword = false;
00082   myLogDataList = false;
00083 }
00084 
00085 AREXPORT bool ArClientSimpleConnector::parseArgs(void)
00086 {
00087   return parseArgs(myParser);
00088 }
00089 
00090 AREXPORT bool ArClientSimpleConnector::parseArgs(ArArgumentParser *parser)
00091 {
00092   if (parser->checkArgument("-nopassword") || 
00093       parser->checkArgument("-np"))
00094     myNoPassword = true;
00095 
00096   if (parser->checkArgument("-logDataList") || 
00097       parser->checkArgument("-ldl"))
00098     myLogDataList = true;
00099 
00100   if (!parser->checkParameterArgumentString("-host", 
00101                                              &myHost) ||
00102       !parser->checkParameterArgumentInteger("-port",
00103                                              &myPort) ||
00104       !parser->checkParameterArgumentInteger("-p",
00105                                              &myPort) ||
00106       !parser->checkParameterArgumentString("-user", 
00107                                             &myUser) ||
00108       !parser->checkParameterArgumentString("-u", 
00109                                             &myUser) ||
00110       !parser->checkParameterArgumentString("-password", 
00111                                             &myPassword) ||
00112       !parser->checkParameterArgumentString("-pwd", 
00113                                             &myPassword) ||
00114       !parser->checkParameterArgumentString("-setServerKey", 
00115                                             &myServerKey) ||
00116       !parser->checkParameterArgumentString("-ssk", 
00117                                             &myServerKey))
00118 
00119   {
00120     return false;
00121   }
00122   return true;
00123 }
00124 
00125 AREXPORT void ArClientSimpleConnector::logOptions(void) const
00126 {
00127   ArLog::log(ArLog::Terse, "Options for ArClientSimpleConnector (see docs for more details):");
00128   ArLog::log(ArLog::Terse, "-host <hostName>");
00129   ArLog::log(ArLog::Terse, "-port <portNumber>");
00130   ArLog::log(ArLog::Terse, "-p <portNumber>");
00131   ArLog::log(ArLog::Terse, "-user <user>");
00132   ArLog::log(ArLog::Terse, "-u <user>");
00133   ArLog::log(ArLog::Terse, "-password <password>");
00134   ArLog::log(ArLog::Terse, "-pwd <password>");
00135   ArLog::log(ArLog::Terse, "-nopassword");
00136   ArLog::log(ArLog::Terse, "-np");
00137   ArLog::log(ArLog::Terse, "-logDataList");
00138   ArLog::log(ArLog::Terse, "-ldl");
00139 }
00140 
00141 AREXPORT bool ArClientSimpleConnector::connectClient(ArClientBase *client)
00142 {
00143   std::string host;
00144   std::string user;
00145   char password[512];
00146   password[0] = '\0';
00147 
00148   if (myServerKey != NULL && myServerKey[0] != '\0')
00149     client->setServerKey(myServerKey);
00150   
00151   if (myUser == NULL)
00152   {
00153     user = "";
00154   }
00155   else
00156   {
00157     user = myUser;
00158     if (myPassword != NULL)
00159     {
00160       strncpy(password, myPassword, sizeof(password) - 1);
00161       password[sizeof(password) - 1] = '\0';
00162     }
00163     else if (!myNoPassword)
00164     {
00165       printf("Enter password: ");
00166       fgets(password, sizeof(password) - 1, stdin);
00167       unsigned int i;
00168       unsigned int len;
00169       len = strlen(password);
00170       for (i = 0; i < len; i++)
00171       {
00172         if (password[i] == '\r' || password[i] == '\n')
00173         {
00174           password[i] = '\0';
00175           break;
00176         }
00177       }
00178     }
00179   }
00180   if (myHost != NULL)
00181     host = myHost;
00182   else
00183     host = "localhost";
00184 
00185   bool ret;
00186   ret = client->blockingConnect(host.c_str(), myPort, true, user.c_str(), 
00187                                 password);
00188   
00189   if (ret && myLogDataList)
00190     client->logDataList();
00191 
00192   return ret;
00193 }

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