MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/MOOSMsg.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 // MOOSMsg.h: interface for the CMOOSMsg class.
00031 //
00033 
00034 #if !defined(AFX_MOOSMSG_H__B6540645_B7DA_420D_B212_96E9845BB39F__INCLUDED_)
00035 #define AFX_MOOSMSG_H__B6540645_B7DA_420D_B212_96E9845BB39F__INCLUDED_
00036 
00037 #if _MSC_VER > 1000
00038 #pragma once
00039 #endif // _MSC_VER > 1000
00040 
00041 #include <string>
00042 
00043 //MESSAGE TYPES
00044 #define MOOS_NOTIFY 'N'
00045 #define MOOS_REGISTER 'R'
00046 #define MOOS_UNREGISTER 'U'
00047 #define MOOS_NOT_SET '~'
00048 #define MOOS_COMMAND 'C'
00049 #define MOOS_ANONYMOUS 'A'
00050 #define MOOS_NULL_MSG '.'
00051 #define MOOS_DATA 'i'
00052 #define MOOS_POISON  'K'
00053 #define MOOS_WELCOME 'W'
00054 #define MOOS_SERVER_REQUEST 'Q'
00055 
00056 //MESSAGE DATA TYPES
00057 #define MOOS_DOUBLE 'D'
00058 #define MOOS_STRING    'S'
00059 #define MOOS_BINARY_STRING 'B'
00060 
00061 //5 seconds time difference between client clock and MOOSDB clock will be allowed
00062 #define SKEW_TOLERANCE 5
00063 
00066 class CMOOSMsg  
00067 {
00068 public:
00069 
00070 
00072     CMOOSMsg();
00073     virtual ~CMOOSMsg();
00074 
00076     CMOOSMsg(char cMsgType,const std::string &sKey,double dfVal,double dfTime=-1);
00077 
00079     CMOOSMsg(char cMsgType,const std::string &sKey,const std::string & sVal,double dfTime=-1);
00080         
00082         void MarkAsBinary();
00083 
00084 
00086     bool IsDataType (char cDataType)const;
00087 
00089     bool IsDouble()const{return IsDataType(MOOS_DOUBLE);}
00090 
00092     bool IsString()const{return IsDataType(MOOS_STRING) || IsDataType(MOOS_BINARY_STRING);}    
00093 
00096     bool IsSkewed(double dfTimeNow, double * pdfSkew = NULL);
00097 
00099     bool IsYoungerThan(double dfAge)const;
00100 
00102     bool IsType (char  cType)const;
00103 
00105     double GetTime()const {return m_dfTime;};
00106 
00108     double GetDouble()const {return m_dfVal;};
00109 
00111     std::string GetString()const {return m_sVal;};
00112 
00114     std::string GetKey()const {return m_sKey;};
00115     std::string GetName()const{return GetKey();};
00116 
00119     std::string GetSource()const {return m_sSrc;};
00120     std::string GetSourceAux()const {return m_sSrcAux;};
00121         void SetSourceAux(const std::string & sSrcAux){m_sSrcAux = sSrcAux;}
00122 
00124     std::string GetCommunity()const {return m_sOriginatingCommunity;};
00125 
00127     std::string GetAsString(int nFieldWidth=12, int nNumDP=5);
00128 
00130     void Trace();
00131 
00133     void SetDouble(double dfD){m_dfVal = dfD;}
00134 
00136     char m_cMsgType;
00137     
00139     char m_cDataType;
00140     
00142     std::string m_sKey;
00143     
00145     int m_nID;
00146 
00148     double m_dfTime;
00149     
00150     //DATA VARIABLES
00151     
00152     //a) numeric
00153     double m_dfVal;
00154     double m_dfVal2;
00155     
00156     //b) string
00157     std::string m_sVal;
00158 
00159     //who sent this message?
00160     std::string m_sSrc;
00161         
00162         //extra info on source (optional payload)
00163         std::string m_sSrcAux;
00164 
00165     //what community did it originate in?
00166     std::string m_sOriginatingCommunity;
00167 
00168     //serialise this message into/outof a character buffer
00169     int Serialize(unsigned char *  pBuffer,int  nLen,bool bToStream=true);
00170 
00171     //comparsion operator for sorting and storing
00172     bool operator <(const CMOOSMsg & Msg) const{ return m_dfTime<Msg.m_dfTime;};
00173 
00174     //return size of Msg in bytes when serialised
00175     unsigned int GetSizeInBytesWhenSerialised() const;
00176 
00177 
00178 
00179 private:
00180     //private things which you have no business knowing about
00181     unsigned char * m_pSerializeBufferStart;
00182     unsigned char * m_pSerializeBuffer;
00183     int  m_nSerializeBufferLen;
00184     int  m_nLength;
00185     int GetLength();
00186     void  operator << (char & cVal);
00187     void  operator << (double & dfVal);
00188     void  operator << (std::string & sVal);
00189     void  operator << (int & nVal);
00190     void  operator >> (char & cVal);
00191     void  operator >> (double & dfVal);
00192     void  operator >> (std::string & sVal);
00193     void  operator >> (int & nVal);
00194     
00195 
00196     bool CanSerialiseN(int N);
00197 
00198 };
00199 
00200 #endif // !defined(AFX_MOOSMSG_H__B6540645_B7DA_420D_B212_96E9845BB39F__INCLUDED_)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines