MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/MOOSCommClient.h
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 // MOOSCommClient.h: interface for the CMOOSCommClient class.
00031 //
00033 
00034 #if !defined(MOOSCommClientH)
00035 #define MOOSCommClientH
00036 
00037 #include <MOOSGenLib/MOOSLock.h>
00038 #include <MOOSGenLib/MOOSThread.h>
00039 #include <iostream>
00040 #include <iomanip>
00041 #include "MOOSCommObject.h"
00042 #include <set>
00043 #include <string>
00044 #include <memory>
00045 
00046 
00047 #define OUTBOX_PENDING_LIMIT 1000
00048 #define INBOX_PENDING_LIMIT 1000
00049 #define CLIENT_DEFAULT_FUNDAMENTAL_FREQ 5
00050 #define CLIENT_MAX_FUNDAMENTAL_FREQ 200
00051 
00052 #ifndef UNUSED_PARAMETER
00053     #ifdef _WIN32
00054         #define UNUSED_PARAMETER(a) a
00055     #else
00056         #define UNUSED_PARAMETER(a) 
00057     #endif
00058 #endif
00059 
00060 class XPCTcpSocket;
00061 
00062 namespace MOOS
00063 {
00064   class CMOOSSkewFilter;
00065 }
00066 
00067 //extern std::auto_ptr<std::ofstream> SkewLog;
00068 
00071 class CMOOSCommClient  :public CMOOSCommObject
00072 {
00073 public:
00074 
00076     CMOOSCommClient();
00077     
00079     virtual ~CMOOSCommClient();
00080     
00081 
00083     bool Notify(const std::string &sVar, const std::string & sVal, double dfTime=-1);
00084     bool Notify(const std::string &sVar, const std::string & sVal, const std::string & sSrcAux, double dfTime=-1);
00085 
00087     bool Notify(const std::string & sVar,double dfVal, double dfTime=-1);
00088     bool Notify(const std::string & sVar,double dfVal, const std::string & sSrcAux,double dfTime=-1);
00089 
00091     bool Notify(const std::string & sVar,void *  pData, unsigned int nDataSize, double dfTime=-1);
00092     bool Notify(const std::string & sVar,void *  pData, unsigned int nDataSize, const std::string & sSrcAux,double dfTime=-1);
00093         
00094         
00098     bool Register(const std::string & sVar,double dfInterval);
00099 
00102     bool UnRegister(const std::string & sVar);
00103 
00105     bool IsConnected();
00106 
00110     bool Fetch(MOOSMSG_LIST  & MsgList);
00111 
00118     bool Post(CMOOSMsg  & Msg);
00119 
00122     bool ClientLoop();
00123 
00125     virtual bool DoClientWork();
00126 
00135     bool Run(const char * sServer,long lPort, const char * sMyName, unsigned int nFundamentalFreq=5);
00136 
00142     void SetOnConnectCallBack(bool (*pfn)(void * pParamCaller), void * pCallerParam);
00143 
00144 
00149     void SetOnDisconnectCallBack(bool (*pfn)(void * pParamCaller), void * pCallerParam);
00150 
00154     void SetOnMailCallBack(bool (*pfn)(void * pParamCaller), void * pCallerParam);
00155     
00157     bool HasMailCallBack();
00158     
00165     bool ServerRequest(const std::string &sWhat, MOOSMSG_LIST & MsgList, double dfTimeOut = 2.0, bool bContinuouslyClearBox = true);
00166 
00167 
00168 
00171     static bool PeekMail(MOOSMSG_LIST &Mail, const std::string &sKey, CMOOSMsg &Msg,bool bErase = false, bool bFindYoungest = false);
00172     
00173     
00176     static bool PeekAndCheckMail(MOOSMSG_LIST &Mail, const std::string &sKey, CMOOSMsg &Msg,bool bErase = false, bool bFindYoungest = false);
00177       
00180     bool Peek(MOOSMSG_LIST &List, int nIDRequired, bool bClearBox = true);
00181 
00182 
00184     static std::string GetLocalIPAddress();
00185     
00187     std::string GetDescription();
00188     
00190     bool FakeSource(bool bFake);
00191 
00193     bool Close(bool bNice =true);
00194 
00196     std::set<std::string> GetPublished(){return m_Published;};
00197     
00199     bool IsRegisteredFor(const std::string & sVariable);
00200 
00202     std::set<std::string> GetRegistered(){return m_Registered;};
00203     
00205     void SetQuiet(bool bQ){m_bQuiet = bQ;};
00206 
00209     void DoLocalTimeCorrection(bool b){m_bDoLocalTimeCorrection = b;};
00210     
00212     void SetVerboseDebug(bool bT){m_bVerboseDebug = bT;};
00213     
00214     bool SetCommsTick(int nCommsTick);
00215 
00216 protected:
00217     bool ClearResources();
00218     
00219     int m_nNextMsgID;
00220     
00222     void DoBanner();
00223     
00225     bool OnCloseConnection();
00226     
00228     bool m_bConnected;
00229     
00231     bool m_bFakeSource;
00232     
00235     bool HandShake();
00236     
00238     unsigned int m_nOutPendingLimit;
00239     
00241     unsigned int m_nInPendingLimit;
00242     
00245     std::string m_sMyName;
00246     
00250     CMOOSLock m_OutLock;
00254     CMOOSLock m_InLock;
00255 
00258     bool ConnectToServer();
00259     
00262     bool StartThreads();
00263     
00264 
00266     XPCTcpSocket* m_pSocket;
00267     
00270     std::string m_sDBHost;
00271     
00274     long m_lPort;
00275     
00277     bool  m_bQuit;
00278     
00280     bool  m_bMailPresent;
00281 
00282     bool UpdateMOOSSkew(double dfRQTime, double dfTXTime,double dfRXTime);
00283     
00284     /*thread to handle communications with a server object*/
00285     CMOOSThread m_ClientThread;
00286     
00289     MOOSMSG_LIST m_OutBox;
00290     
00293     MOOSMSG_LIST m_InBox;
00294     
00296     void * m_pConnectCallBackParam;
00297     
00299     bool (*m_pfnConnectCallBack)( void * pConnectParam);
00300     
00301     
00303     void * m_pDisconnectCallBackParam;
00304     
00306     bool (*m_pfnDisconnectCallBack)( void * pParam);
00307     
00309     void * m_pMailCallBackParam;
00310     
00312     bool (*m_pfnMailCallBack)(void* pParam);
00313     
00314     
00318     unsigned int m_nFundamentalFreq;
00319 
00321     std::set<std::string> m_Registered;
00322 
00324     std::set<std::string> m_Published;
00325 
00327     bool m_bQuiet;
00328     
00330     bool m_bVerboseDebug;
00331 
00333     bool m_bDoLocalTimeCorrection;
00334     
00336     std::auto_ptr< MOOS::CMOOSSkewFilter > m_pSkewFilter;
00337     
00339     bool m_bClientLoopIsRunning;
00340 };
00341 
00342 #endif // !defined(MOOSCommClientH)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines