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

VehicleNetConfig.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////
00002 /*! \file VehicleNetConfig.cpp
00003  *  \brief Defines the methods for the VehicleNetConfig class.
00004  *  \author     $Author: rsharo $
00005  *  \version $Revision: 1.1 $
00006  *  \date    $Date: 2003/12/17 22:52:17 $
00007 *////////////////////////////////////////////////////////////////////////////
00008 
00009 #include "VehicleNetConfig.h"
00010 
00011 VehicleNetConfig::VehicleNetConfig()
00012         : m_numMappings(0),
00013           m_pMyMapping(NULL), 
00014           m_unknownMapping(SystemDefs::UNKNOWN_ID, "Unknown"),
00015           m_mcastAddr(),
00016           m_pMcastInterface(NULL),
00017           m_loopbackEnabled(false),
00018           m_timeToLive(1)
00019 {
00020 }
00021 
00022 VehicleNetConfig::VehicleNetConfig(const char *mySectionName)
00023         : m_numMappings(0),
00024           m_pMyMapping(NULL), 
00025           m_unknownMapping(SystemDefs::UNKNOWN_ID, "Unknown"),
00026           m_mcastAddr(),
00027           m_pMcastInterface(NULL),
00028           m_loopbackEnabled(false),
00029           m_timeToLive(1)
00030 {
00031         this->Init(mySectionName);
00032 }
00033 
00034 VehicleNetConfig::~VehicleNetConfig()
00035 {
00036         delete [] m_pMyMapping;
00037         delete m_pMcastInterface;
00038 }
00039 
00040 
00041 int VehicleNetConfig::Init(const char *mySectionName)
00042 {
00043         SystemProperties *props = SystemProperties::Instance();
00044 
00045         ACE_Configuration_Section_Key   mySectionKey,
00046                                                                         idSectionKey;
00047 
00048         if (props->GetSectionKey(mySectionName, mySectionKey))
00049                 ACE_ERROR_RETURN((LM_ERROR, "(%N:%l) Initialization of VehicleNetConfig failed: Unable to find system config section %s.\n", mySectionName), -1);
00050 
00051         if (props->GetSectionKey(mySectionKey, "IDs", idSectionKey))
00052                 ACE_ERROR_RETURN((LM_ERROR, "(%N:%l) Initialization of VehicleNetConfig failed: Unable to find 'IDs' subsection of config.\n"), -1);
00053 
00054         std::string mcastAddrName,
00055                                 mcastIfName;
00056 
00057         if (props->GetStringEntry(mySectionKey, "Multicast Address", mcastAddrName))
00058                 ACE_ERROR_RETURN((LM_ERROR, "(%N:%l) Initialization of VehicleNetConfig failed: 'Multicast Address' property is missing.\n"), -1);
00059 
00060         m_mcastAddr.set(mcastAddrName.c_str());
00061 
00062         if (props->GetStringEntry(mySectionKey, "Multicast Interface", mcastIfName) == 0)
00063         {
00064                 size_t len = mcastIfName.length();
00065                 m_pMcastInterface = new char[len+1];
00066                 mcastIfName.copy(m_pMcastInterface, len);
00067                 m_pMcastInterface[len]='\0';
00068         }
00069 
00070         unsigned int val;
00071         if (props->GetIntegerEntry(mySectionKey, "Multicast Loopback", val) == 0)
00072         {
00073                 m_loopbackEnabled = (val != 0);
00074         }
00075 
00076         if (props->GetIntegerEntry(mySectionKey, "Multicast TTL", val) == 0)
00077         {
00078                 m_timeToLive = val;
00079         }
00080 
00081         std::string fieldName;
00082 
00083         int status = props->EnumerateSections(idSectionKey, 0, fieldName);
00084         if (status== -1)
00085                 ACE_ERROR_RETURN((LM_ERROR, "(%N:%l) Initialization of VehicleNetConfig failed: The 'IDs' subsection of config has no vehicle sections.\n"), -1);
00086 
00087         m_numMappings = 1;
00088         if (status == 0)
00089         {
00090                 for (; props->EnumerateSections(idSectionKey, m_numMappings, fieldName) == 0; ++m_numMappings);
00091         }
00092 
00093         ACE_Configuration_Section_Key vehicleKey;
00094         m_pMyMapping = new VehicleMapping[m_numMappings];
00095 
00096         for (int i = 0; i < m_numMappings; ++i)
00097         {
00098                 props->EnumerateSections(idSectionKey, i, m_pMyMapping[i].m_vehicleName);
00099 
00100 
00101                 props->GetSectionKey(idSectionKey, m_pMyMapping[i].m_vehicleName.c_str(), vehicleKey);
00102 
00103                 unsigned int val;
00104                 if (props->GetIntegerEntry(vehicleKey, "ID", val))
00105                 {
00106                         ACE_ERROR((LM_ERROR, "(%N:%l) Vehicle '%s' is missing an ID config entry.\n", m_pMyMapping[i].m_vehicleName.c_str()));
00107                 }
00108                 else
00109                 {
00110                         m_pMyMapping[i].m_vehicleID = static_cast<int>(val);
00111                 }
00112 
00113                 std::string addrString;
00114                 if (props->GetStringEntry(vehicleKey, "Address", addrString))
00115                 {
00116                         ACE_ERROR((LM_ERROR, "(%N:%l) Vehicle '%s' is missing an Address config entry.\n", m_pMyMapping[i].m_vehicleName.c_str()));
00117                 }
00118                 else
00119                 {
00120                         m_pMyMapping[i].m_netAddress.set(addrString.c_str());
00121                 }
00122         }
00123 
00124         return 0;
00125 }
00126 
00127 
00128 const ACE_INET_Addr &VehicleNetConfig::GetVehicleAddress(SystemDefs::VehicleID_t vehicleID) const
00129 {
00130         for (int i = 0; i < m_numMappings; ++i)
00131         {
00132                 if (vehicleID == m_pMyMapping[i].m_vehicleID)
00133                 {
00134                         return m_pMyMapping[i].m_netAddress;
00135                 }
00136         }
00137 
00138         return m_unknownMapping.m_netAddress;
00139 }
00140 
00141 
00142 SystemDefs::VehicleID_t VehicleNetConfig::VehicleAddressToID(const ACE_INET_Addr &addr) const
00143 {
00144         for (int i = 0; i < m_numMappings; ++i)
00145         {
00146                 if (addr == m_pMyMapping[i].m_netAddress)
00147                 {
00148                         return m_pMyMapping[i].m_vehicleID;
00149                 }
00150         }
00151 
00152         return m_unknownMapping.m_vehicleID;
00153 }
00154 

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