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

VehicleComm.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////
00002 /*! \file VehicleComm.cpp
00003  *  \brief Defines the methods for the VehicleComm class.
00004  *  \author     $Author: rsharo $
00005  *  \version $Revision: 1.1 $
00006  *  \date    $Date: 2003/12/17 22:52:17 $
00007 *////////////////////////////////////////////////////////////////////////////
00008 
00009 #include "VehicleComm.h"
00010 
00011 #include "ace/Synch.h"
00012 #include "ace/SOCK_Dgram_Mcast.h"
00013 #include "ace/SOCK_Dgram.h"
00014 
00015 using SystemDefs::VehicleID_t;
00016 
00017 VehicleComm::VehicleComm()
00018         : m_pMySocket(NULL),
00019           m_pMyConfig(NULL)
00020 {
00021 }
00022 
00023 VehicleComm::VehicleComm(VehicleNetConfig *myConfig)
00024         : m_pMySocket(NULL),
00025           m_pMyConfig(NULL)
00026 {
00027         this->Connect(myConfig);
00028 }
00029 
00030 VehicleComm::~VehicleComm()
00031 {
00032         delete m_pMySocket;
00033         delete m_pMyConfig;
00034 }
00035 
00036 int VehicleComm::Connect(VehicleNetConfig *myConfig)
00037 {
00038         m_pMyConfig = myConfig;
00039 
00040         if (m_pMySocket!= NULL)
00041         {
00042                 ACE_DEBUG((LM_INFO, "Reconnecting VehicleComm -- deleting old socket.\n"));
00043                 delete m_pMySocket;
00044         }
00045 
00046         m_pMySocket=new ACE_SOCK_Dgram_Mcast;
00047 
00048         if (m_pMySocket->subscribe(m_pMyConfig->GetMulticastAddress(), 1, m_pMyConfig->GetMulticastInterface()))
00049         {
00050                 ACE_ERROR((LM_ERROR, "(%N:%l): %p", "subscribe"));
00051         }
00052 
00053         {
00054                 u_char arg = m_pMyConfig->MulticastLoopbackEnabled();
00055                 ACE_SOCK * pSocket = static_cast<ACE_SOCK *>(m_pMySocket);
00056                 int status;
00057                 
00058                 status = pSocket->set_option(IPPROTO_IP, IP_MULTICAST_LOOP,
00059                         &arg, sizeof(arg));
00060                 if (status < 0)
00061                         ACE_ERROR_RETURN((LM_NOTICE, "(%N:%l): set_option: %p"), -2);
00062                 
00063                 arg = m_pMyConfig->MulticastTimeToLive();
00064                 status = pSocket->set_option(IPPROTO_IP, IP_MULTICAST_TTL,
00065                         &arg, sizeof(arg));
00066                 if (status < 0)
00067                         ACE_ERROR_RETURN((LM_INFO, "(%N:%l): set_option: %p"), -2);
00068         }
00069 
00070         return 0;
00071 }
00072 
00073 int VehicleComm::SendMessage(VehicleID_t destID, void *pMessage, unsigned int msgLength)
00074 {
00075         if (destID == SystemDefs::BROADCAST_ID)
00076                 return this->BroadcastMessage(pMessage, msgLength);
00077 
00078         const ACE_INET_Addr &destAddr = m_pMyConfig->GetVehicleAddress(destID);
00079 
00080         ACE_SOCK_Dgram * pSocket = static_cast<ACE_SOCK_Dgram *>(m_pMySocket);
00081 
00082         int status = pSocket->send(pMessage, msgLength, destAddr);
00083         if (status < 0)
00084         {
00085         ACE_ERROR((LM_ERROR, "(%N:%l): send: %p"));
00086         }
00087 
00088         return status;
00089 }
00090 
00091 int VehicleComm::BroadcastMessage(void *pMessage, unsigned int msgLength)
00092 {
00093         int status = m_pMySocket->send(pMessage, msgLength);
00094         if (status < 0)
00095         {
00096         ACE_ERROR((LM_ERROR, "(%N:%l): send: %p"));
00097         }
00098 
00099         return status;
00100 }
00101 
00102 
00103 int VehicleComm::GetMessage(void *pMessage, unsigned int maxMsgLength)
00104 {
00105         ACE_INET_Addr whoFromAddr;
00106         int status = this->GetMessage(pMessage, maxMsgLength, whoFromAddr);
00107 
00108         return status;
00109 }
00110 
00111 
00112 int VehicleComm::GetMessage(void *pMessage, unsigned int maxMsgLength, VehicleID_t &srcID)
00113 {
00114         ACE_INET_Addr whoFromAddr;
00115         int status = this->GetMessage(pMessage, maxMsgLength, whoFromAddr);
00116 
00117         if (status >= 0)
00118         {
00119                 srcID = m_pMyConfig->VehicleAddressToID(whoFromAddr);
00120         }
00121 
00122         return status;
00123 }
00124 
00125 int VehicleComm::GetMessage(void *pMessage, unsigned int maxMsgLength, ACE_INET_Addr &whoFrom)
00126 {
00127         int status = m_pMySocket->recv(pMessage, maxMsgLength, whoFrom);
00128         if (status < 0)
00129         {
00130         ACE_ERROR((LM_ERROR, "(%N:%l): recv: %p"));
00131         }
00132 
00133         return status;
00134 }
00135 

Generated on Wed Sep 5 12:54:27 2007 for DSACSS Operational Code by  doxygen 1.3.9.1