MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/MOOSCommServer.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 // MOOSCommServer.h: interface for the CMOOSCommServer class.
00031 //
00033 
00034 #if !defined(AFX_MOOSCOMMSERVER_H__2FDF870F_F998_4D3C_AD18_FCC2C5C12DDA__INCLUDED_)
00035 #define AFX_MOOSCOMMSERVER_H__2FDF870F_F998_4D3C_AD18_FCC2C5C12DDA__INCLUDED_
00036 
00037 #if _MSC_VER > 1000
00038 #pragma once
00039 #endif // _MSC_VER > 1000
00040 
00041 
00042 #ifdef _WIN32
00043     #include <winsock2.h>
00044     #include "windows.h"
00045     #include "winbase.h"
00046     #include "winnt.h"
00047 #else
00048     #include <sys/time.h>
00049     #include <pthread.h>
00050 #endif
00051 
00052 #include "MOOSCommObject.h"
00053 #include "MOOSCommPkt.h"
00054 #include <MOOSGenLib/MOOSLock.h>
00055 
00056 #include <MOOSGenLib/MOOSThread.h>
00057 
00058 class XPCTcpSocket;
00059 #include <list>
00060 #include <map>
00061 //using namespace std;
00062 
00068 class CMOOSCommServer  : public CMOOSCommObject
00069 {
00070 public:
00071     typedef std::list<XPCTcpSocket*> SOCKETLIST;
00072     typedef std::map<int,std::string > SOCKETFD_2_CLIENT_NAME_MAP;
00073     typedef std::list<std::string > STRING_LIST;
00074 
00075     bool GetClientNames(STRING_LIST & sList);
00076 
00085     void SetOnRxCallBack(bool (*pfn)(const std::string  & sClient,MOOSMSG_LIST & MsgListRx,MOOSMSG_LIST & MsgListTx,void * pParam),void * pParam);
00086 
00092     void SetOnDisconnectCallBack(bool (*pfn)(std::string  & sClient,void * pParam),void * pParam);
00093 
00096     bool ListenLoop();
00097 
00100     bool ServerLoop();
00101 
00105     bool TimerLoop();
00106 
00107 
00111     bool Run(long lPort,const std::string  & sCommunityName, bool bDisableNameLookUp = false);
00112     
00113     
00115     void SetQuiet(bool bQ){m_bQuiet = bQ;};
00116 
00117 
00119     CMOOSCommServer();
00120 
00122     virtual ~CMOOSCommServer();
00123 
00124 protected:
00127     CMOOSLock m_SocketListLock;
00128 
00130     int GetMaxSocketFD();
00131 
00133     virtual void DoBanner();
00134 
00136     std::string  GetClientName(XPCTcpSocket* pSocket);
00137 
00140     void PoisonClient(XPCTcpSocket* pSocket,const std::string & sReason);
00141 
00143     bool HandShake(XPCTcpSocket* pNewSocket);
00144 
00148     bool IsUniqueName(std::string  & sClientName);
00149 
00151     int m_nTotalActions;
00152 
00154     virtual bool OnClientDisconnect();
00155 
00157     virtual bool OnAbsentClient(XPCTcpSocket* pClient);
00158 
00160     CMOOSThread m_ListenThread;
00161 
00163     CMOOSThread m_ServerThread;
00164 
00166     CMOOSThread m_TimerThread;
00167 
00170     bool (*m_pfnRxCallBack)(const std::string  & sClient,MOOSMSG_LIST & MsgListRx,MOOSMSG_LIST & MsgListTx,void * pCaller);
00171 
00174     void * m_pRxCallBackParam;
00175 
00176 
00179     bool (*m_pfnDisconnectCallBack)(std::string  & sClient,void * pParam);
00180 
00183     void * m_pDisconnectCallBackParam;
00184 
00186     XPCTcpSocket * m_pListenSocket;
00187 
00189     XPCTcpSocket * m_pFocusSocket;
00190 
00192     SOCKETLIST    m_ClientSocketList;
00193 
00194 
00196     SOCKETFD_2_CLIENT_NAME_MAP m_Socket2ClientMap;
00197 
00201     virtual bool    OnNewClient(XPCTcpSocket * pNewClient,char * sName);
00202 
00205     virtual bool    ProcessClient();
00206 
00208     long    m_lListenPort;
00209 
00211     bool    m_bQuit;
00212 
00214     int        m_nMaxSocketFD;
00215 
00217     bool    StartThreads();
00218 
00220         bool m_bDisableNameLookUp;
00221 
00223     std::string  m_sCommunityName;
00224     
00225     
00227     
00228     bool m_bQuiet;
00229 };
00230 
00231 #endif // !defined(AFX_MOOSCOMMSERVER_H__2FDF870F_F998_4D3C_AD18_FCC2C5C12DDA__INCLUDED_)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines