MOOS 0.2375
|
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 // MOOSCommunity.cpp: implementation of the CMOOSCommunity class. 00031 // 00033 #include <MOOSLIB/MOOSLib.h> 00034 #include "MOOSCommunity.h" 00035 00036 #define DEFAULT_BRIDGE_COMMS_TICK 20 00037 00038 // Construction/Destruction 00040 00041 using namespace std; 00042 //switch yard for connect ot all communities.. 00043 bool gOnCommunityConnect(void * pParam) 00044 { 00045 CMOOSCommunity * pCommunity = (CMOOSCommunity *)(pParam); 00046 00047 return pCommunity->DoRegistration(); 00048 00049 } 00050 00051 00052 #define DEFAULT_SHARED_FREQ 10 00053 00054 CMOOSCommunity::CMOOSCommunity() 00055 { 00056 m_nSharedFreq = DEFAULT_SHARED_FREQ; 00057 m_nUDPPort = -1; 00058 m_sUDPHost = ""; 00059 m_bMOOSClientRunning = false; 00060 } 00061 00062 CMOOSCommunity::~CMOOSCommunity() 00063 { 00064 00065 } 00066 00067 bool CMOOSCommunity::WantsToSink(const SP &sIndex) 00068 { 00069 if(m_Sinks.empty()) 00070 return false; 00071 00072 return m_Sinks.find(sIndex)!=m_Sinks.end(); 00073 } 00074 00075 bool CMOOSCommunity::AddSource(const string &sStr) 00076 { 00077 //add it to permanent list (will be registered on call back) 00078 m_Sources.insert(sStr); 00079 00080 00081 //and also subscribe now 00082 if(m_CommClient.IsConnected()) 00083 { 00084 double dfPeriod = m_nSharedFreq==0?0.0:1.0/m_nSharedFreq; 00085 return m_CommClient.Register(sStr,dfPeriod); 00086 } 00087 00088 return true; 00089 } 00090 00091 bool CMOOSCommunity::AddSink(const SP &sIndex,const string &sAlias) 00092 { 00093 //m_Sinks.insert(sVar); 00094 00095 if(sAlias.empty()) 00096 { 00097 m_Sinks[sIndex] = sIndex.first; 00098 } 00099 else 00100 { 00101 //Comm@Host:port:Var 00102 m_Sinks[sIndex] = sAlias; 00103 } 00104 00105 return true; 00106 } 00107 00108 std::string CMOOSCommunity::GetAlias(const SP & sIndex) 00109 { 00110 std::map<SP,std::string>::iterator p = m_Sinks.find(sIndex); 00111 00112 if(p ==m_Sinks.end()) 00113 { 00114 MOOSTrace("trouble! not entry for %s %s %s\n",sIndex.first.c_str(),sIndex.second.c_str(),MOOSHERE); 00115 MOOSTrace("returning query name as alias\n"); 00116 return sIndex.first; 00117 } 00118 00119 return p->second; 00120 } 00121 00122 00123 bool CMOOSCommunity::InitialiseMOOSClient( const string &sHostName, 00124 long nPort, 00125 const string & sMOOSName, 00126 int nFreq) 00127 { 00128 00129 //this is used to determine the subscription period. 00130 m_nSharedFreq = nFreq; 00131 00132 m_CommClient.SetOnConnectCallBack(gOnCommunityConnect,this); 00133 00134 //we wanna be transparent... 00135 m_CommClient.FakeSource(true); 00136 00137 // 00138 00139 00140 m_bMOOSClientRunning = m_CommClient.Run(sHostName.c_str(), 00141 nPort, 00142 sMOOSName.c_str(), 00143 DEFAULT_BRIDGE_COMMS_TICK); 00144 00145 return m_bMOOSClientRunning; 00146 } 00147 00148 00149 bool CMOOSCommunity::HasMOOSSRegistration(const std::string & sVariable) 00150 { 00151 return m_CommClient.IsRegisteredFor(sVariable); 00152 } 00153 00154 00155 bool CMOOSCommunity::IsMOOSClientRunning() 00156 { 00157 return m_bMOOSClientRunning; 00158 } 00159 00160 std::string CMOOSCommunity::GetCommunityName() 00161 { 00162 return m_sCommunityName; 00163 } 00164 00165 bool CMOOSCommunity::Initialise(const string &sCommunityName) 00166 { 00167 00168 m_sCommunityName = sCommunityName; 00169 return true; 00170 } 00171 00172 bool CMOOSCommunity::SetUDPInfo(const std::string & sHost, int nPort) 00173 { 00174 //this is the address of the place UDP packets should be sent to 00175 m_sUDPHost = sHost; 00176 m_nUDPPort = nPort; 00177 return true; 00178 } 00179 00180 bool CMOOSCommunity::HasUDPConfigured() const 00181 { 00182 return m_nUDPPort!=-1; 00183 } 00184 00185 int CMOOSCommunity::GetUDPPort() 00186 { 00187 return m_nUDPPort; 00188 } 00189 00190 std::string CMOOSCommunity::GetUDPHost() 00191 { 00192 return m_sUDPHost; 00193 } 00194 00195 bool CMOOSCommunity::Fetch(MOOSMSG_LIST & Mail) 00196 { 00197 return m_CommClient.Fetch(Mail); 00198 } 00199 00200 bool CMOOSCommunity::Post(CMOOSMsg & M) 00201 { 00202 return m_CommClient.Post(M); 00203 } 00204 00205 00206 bool CMOOSCommunity::DoRegistration() 00207 { 00208 std::set<std::string>::iterator q; 00209 00210 for(q=m_Sources.begin();q!=m_Sources.end();q++) 00211 { 00212 double dfPeriod = m_nSharedFreq==0?0.0:1.0/m_nSharedFreq; 00213 00214 if(!m_CommClient.Register(*q,dfPeriod)) 00215 return false; 00216 } 00217 return true; 00218 } 00219 00220 string CMOOSCommunity::GetFormattedName() 00221 { 00222 return MOOSFormat("%s@%s",m_sCommunityName.c_str(),m_CommClient.GetDescription().c_str()); 00223 } 00224 00225 string CMOOSCommunity::GetCommsName() 00226 { 00227 return m_CommClient.GetDescription(); 00228 } 00229