MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/XPCUdpSocket.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS Core Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00030 
00031 /*
00032  *  XPCUdpSocket.cpp
00033  *  MOOS
00034  *
00035  *  Created by pnewman on 13/05/2009.
00036  *  Copyright 2009 Oxford University. All rights reserved.
00037  *
00038  */
00039 
00040 #include "XPCUdpSocket.h"
00041 #include <map>
00042 #include <string>
00043 
00044 
00045 
00046 XPCUdpSocket::XPCUdpSocket(long int iPort): XPCSocket("udp", iPort)
00047 {
00048 }    
00049 
00050 
00051 bool XPCUdpSocket::GetAddress(long int nPort,const std::string & sHost,sockaddr_in & Address)
00052 {
00053 
00054     std::map< std::pair< long int , std::string  >, sockaddr_in >::iterator q;
00055     
00056     std::pair< long int , std::string  > PP(nPort,sHost);
00057     
00058     q = m_KnownAdresses.find(PP);
00059     
00060     if(q!=m_KnownAdresses.end())
00061     {
00062         Address = q->second;
00063         return true;
00064     }
00065     else
00066     {
00067         
00068                 Address.sin_family = AF_INET;
00069         Address.sin_port = htons(nPort);
00070         
00071         hostType HostType;
00072         if(sHost.find_first_not_of("0123456789. ")!=std::string::npos)
00073         {
00074             HostType = NAME;
00075             XPCGetHostInfo getHostInfo(sHost.c_str(), HostType);
00076             
00077             // Store the IP address and socket port number
00078             Address.sin_addr.s_addr =inet_addr(getHostInfo.sGetHostAddress());
00079             
00080         }
00081         else
00082         {
00083             HostType = ADDRESS;
00084             // Store the IP address and socket port number
00085             Address.sin_addr.s_addr =inet_addr(sHost.c_str());
00086         }        
00087         
00088         //save it
00089         m_KnownAdresses[PP] = Address;
00090         
00091         return true;
00092     }
00093     
00094 }
00095 
00096 int XPCUdpSocket::iBroadCastMessage(void *_vMessage, int _iMessageSize,long int nPort)
00097 {
00098         return iSendMessageTo(_vMessage, _iMessageSize,nPort,"255.255.255.255");
00099 }
00100 
00101 // Sends a message to a connected host. The number of bytes sent is returned
00102 int XPCUdpSocket::iSendMessageTo(void *_vMessage, int _iMessageSize,long int nPort,const std::string & sHost)
00103 {
00104     
00105     sockaddr_in  Address;
00106     
00107     if(!GetAddress(nPort,sHost,Address))
00108     {
00109         throw XPCException("::iSendMessageTo failed to get destination address\n");
00110     }
00111     
00112     int iNumBytes=sendto(iSocket, (const char*)_vMessage, _iMessageSize,0,
00113              (struct sockaddr *) &Address, sizeof(Address) );
00114     
00115     
00116     // Sends the message to the connected host
00117     if (iNumBytes == -1)
00118     {
00119         char sMsg[512];        
00120         sprintf(sMsg, "Error sending socket message: %s", sGetError());
00121         throw XPCException(sMsg);
00122         return 0;
00123     }
00124     return iNumBytes;
00125     
00126 }
00127 
00128 // Receives a UDP message 
00129 int XPCUdpSocket::iRecieveMessage(void *_vMessage, int _iMessageSize, int _iOption)
00130 {
00131     
00132     int iNumBytes;  // The number of bytes recieved
00133     
00134     
00135     // Recieves a UDP socket message.  The number of bytes received isreturned
00136     struct sockaddr SenderInfo;
00137 
00138     socklen_t SenderInfoLen = sizeof (SenderInfo);
00139     
00140     iNumBytes = recvfrom(iSocket, (char *)_vMessage,
00141                          _iMessageSize,
00142                          _iOption,(struct sockaddr *) &SenderInfo, &SenderInfoLen);
00143     if (iNumBytes <=0)
00144     {
00145         char sMsg[512];
00146         
00147         sprintf(sMsg, "Error receiving on socket: %s", sGetError());
00148         printf("%s\n",sMsg);
00149         throw XPCException(sMsg);
00150         
00151     }
00152     
00153     return iNumBytes;
00154     
00155 }
00156 
00157 // Binds the socket to an address and port number
00158 void XPCUdpSocket::vBindSocket()
00159 {
00160     
00161     
00162     // Bind the socket to the given address and port number
00163     if (bind(iSocket, (struct sockaddr *)&clientAddress,sizeof(clientAddress)) == -1)
00164     {
00165         char sMsg[512];
00166         sprintf(sMsg, "Error binding to socket: %s", sGetError());
00167         XPCException socketExcept(sMsg);
00168         throw socketExcept;
00169         return;
00170     }
00171         
00172 }
00173 
00174 /*
00175 // allows a read with a timeout to prevent from blocking indefinitely
00176 int XPCUdpSocket::iReadMessageWithTimeOut(void *_vMessage, int _iMessageSize, double dfTimeOut,int _iOption)
00177 {
00178     return -1;
00179 }
00180 */
00181 
00182 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines