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

ArTcpConnection.cpp

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 "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArTcpConnection.h"
00029 #include "ArLog.h"
00030 #include "ariaUtil.h"
00031 
00032 AREXPORT ArTcpConnection::ArTcpConnection()
00033 {
00034   myStatus = STATUS_NEVER_OPENED;
00035   buildStrMap();
00036   myOwnSocket = true;
00037   mySocket = new ArSocket();
00038 }
00039 
00040 AREXPORT ArTcpConnection::~ArTcpConnection()
00041 {
00042   if (myOwnSocket)
00043     delete mySocket;
00044 }
00045 
00052 AREXPORT void ArTcpConnection::setSocket(ArSocket *socket)
00053 {
00054   if (myOwnSocket)
00055   {
00056     delete mySocket;
00057     myOwnSocket = false;
00058   }
00059   mySocket = socket;
00060 }
00061 
00062 AREXPORT ArSocket *ArTcpConnection::getSocket(void)
00063 {
00064   return mySocket;
00065 }
00066 
00067 AREXPORT void ArTcpConnection::setStatus(int status)
00068 {
00069   myStatus = status;
00070 }
00071 
00072 AREXPORT void ArTcpConnection::setPort(const char *host, int port)
00073 {
00074   myPortNum = port;
00075 
00076   if (host == NULL)
00077     myHostName = "localhost";
00078   else
00079     myHostName = host;
00080 }
00081 
00082 AREXPORT bool ArTcpConnection::openSimple(void)
00083 {
00084   if (internalOpen() == 0)
00085     return true;
00086   else
00087     return false;
00088 }
00089 
00096 AREXPORT int ArTcpConnection::open(const char *host, int port)
00097 {
00098   setPort(host, port);
00099   return internalOpen();
00100 }
00101 
00102 AREXPORT int ArTcpConnection::internalOpen(void)
00103 {
00104   mySocket->init();
00105   if (mySocket->connect(const_cast<char *>(myHostName.c_str()), myPortNum,
00106                ArSocket::TCP)) 
00107   {
00108     myStatus = STATUS_OPEN;
00109     mySocket->setNonBlock();
00110     mySocket->setNoDelay(true);
00111     return 0;
00112   }
00113   
00114   myStatus = STATUS_OPEN_FAILED;
00115   switch(mySocket->getError())
00116   {
00117   case ArSocket::NetFail:
00118     return OPEN_NET_FAIL;
00119   case ArSocket::ConBadHost:
00120     return OPEN_BAD_HOST;
00121   case ArSocket::ConNoRoute:
00122     return OPEN_NO_ROUTE;
00123   case ArSocket::ConRefused:
00124     return OPEN_CON_REFUSED;
00125   case ArSocket::NoErr:
00126     ArLog::log(ArLog::Terse, "ArTcpConnection::open: No error!\n");
00127   default:
00128     return -1;
00129   }
00130 
00131 }
00132 
00133 void ArTcpConnection::buildStrMap(void)
00134 {
00135   myStrMap[OPEN_NET_FAIL] = "Network failed.";
00136   myStrMap[OPEN_BAD_HOST] = "Could not find host.";
00137   myStrMap[OPEN_NO_ROUTE] = "No route to host.";
00138   myStrMap[OPEN_CON_REFUSED] = "Connection refused.";
00139 }
00140 
00141 AREXPORT const char *ArTcpConnection::getOpenMessage(int messageNumber)
00142 {
00143   return myStrMap[messageNumber].c_str();
00144 }
00145 
00146 AREXPORT bool ArTcpConnection::close(void)
00147 {
00148   myStatus = STATUS_CLOSED_NORMALLY;
00149   return mySocket->close();
00150 }
00151 
00152 AREXPORT int ArTcpConnection::read(const char *data, unsigned int size, 
00153                    unsigned int msWait)
00154 {
00155   ArTime timeDone;
00156   unsigned int bytesRead = 0;
00157   int n;
00158 
00159   if (getStatus() != STATUS_OPEN) 
00160   {
00161     ArLog::log(ArLog::Terse, 
00162            "ArTcpConnection::read: Attempt to use port that is not open.");
00163     return -1;
00164   }
00165 
00166   int timeToWait;  
00167   timeDone.setToNow();
00168   timeDone.addMSec(msWait);
00169 
00170   do 
00171   {
00172     timeToWait = timeDone.mSecTo();
00173     if (timeToWait < 0)
00174       timeToWait = 0;
00175     n = mySocket->read(const_cast<char *>(data) + bytesRead, size - bytesRead,
00176                timeToWait);
00177     /*if (n == -1) 
00178     {
00179       ArLog::log("ArTcpConnection::read: read failed.");
00180       return -1;
00181       } */
00182     //printf("%ld %d %d\n", timeDone.mSecTo(), n, size);
00183     if (n != -1)
00184       bytesRead += n;
00185     if (bytesRead >= size)
00186       return bytesRead;
00187   } while (timeDone.mSecTo() >= 0);
00188 
00189   return bytesRead;
00190 }
00191 
00192 AREXPORT int ArTcpConnection::write(const char *data, unsigned int size)
00193 {
00194   int ret;
00195 
00196   if (getStatus() != STATUS_OPEN) 
00197   {
00198     ArLog::log(ArLog::Terse, 
00199            "ArTcpConnection::write: Attempt to use port that is not open.");
00200     return -1;
00201   }
00202   if ((ret = mySocket->write(data, size)) != -1)
00203     return ret;
00204 
00205   ArLog::log(ArLog::Terse, "ArTcpConnection::write: Write failed, closing connection.");
00206   close();
00207   return -1;
00208 }
00209 
00210 
00215 AREXPORT std::string ArTcpConnection::getHost(void)
00216 {
00217   return myHostName;
00218 }
00219 
00224 AREXPORT int ArTcpConnection::getPort(void)
00225 {
00226   return myPortNum;
00227 }
00228 
00229 AREXPORT int ArTcpConnection::getStatus(void)
00230 {
00231   return myStatus;
00232 }
00233 
00234 AREXPORT bool ArTcpConnection::isTimeStamping(void)
00235 {
00236   return false;
00237 }
00238 
00239 AREXPORT ArTime ArTcpConnection::getTimeRead(int index)
00240 {
00241   ArTime now;
00242   now.setToNow();
00243   return now;
00244 }

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0