Subversion Repositories HomeAutomation

Rev

Rev 1962 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #ifndef NET_MANAGER_H
  22. #define NET_MANAGER_H
  23.  
  24. #include <string>
  25. #include <map>
  26.  
  27. #include <boost/signals2.hpp>
  28. #include <boost/shared_ptr.hpp>
  29.  
  30. #include "common/IoService.h"
  31.  
  32. #include "types.h"
  33. #include "Client.h"
  34. #include "TcpClient.h"
  35. #include "UdpClient.h"
  36. #include "SerialClient.h"
  37. #include "TcpServer.h"
  38.  
  39.  
  40. namespace atom {
  41. namespace net {
  42.  
  43. class Manager : public common::IoService
  44. {
  45. public:
  46.   typedef boost::shared_ptr<Manager>                                          Pointer;
  47.   typedef boost::signals2::signal<void(SocketId, SocketId, ClientState)>      SignalOnNewState;
  48.   typedef boost::signals2::signal<void(SocketId, SocketId, common::Byteset)>  SignalOnNewData;
  49.  
  50.   virtual ~Manager();
  51.  
  52.   static Pointer Instance();
  53.   static void Create();
  54.   static void Delete();
  55.  
  56.   void ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data);
  57.  
  58.   SocketId StartServer(Protocol protocol, unsigned int port);
  59.   SocketId Connect(Protocol protocol, std::string address, unsigned int port_or_baud);
  60.  
  61.   void StopServer(SocketId server_id);
  62.   void Disconnect(SocketId client_id);
  63.  
  64.   void SendToAll(SocketId server_id, common::Byteset data);
  65.   void SendTo(SocketId client_id, common::Byteset data);
  66.  
  67. private:
  68.   typedef std::map<SocketId, Client::Pointer>     ClientList;
  69.   typedef std::map<SocketId, TcpServer::Pointer>  ServerList;
  70.  
  71.   static Pointer instance_;
  72.  
  73.   ClientList        clients_;
  74.   ServerList        servers_;
  75.   SignalOnNewState  signal_on_new_state_;
  76.   SignalOnNewData   signal_on_new_data_;
  77.  
  78.   Manager();
  79.  
  80.   void SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state);
  81.   void SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data);
  82.   void SlotOnNewClient(SocketId server_id, TcpSocketPointer socket);
  83.  
  84.   void StopServerHandler(SocketId server_id);
  85.   void DisconnectHandler(SocketId client_id);
  86.  
  87.   void SendToAllHandler(SocketId server_id, common::Byteset data);
  88.   void SendToHandler(SocketId client_id, common::Byteset data);
  89.  
  90.   SocketId GetFreeSocketId();
  91. };
  92.  
  93. }; // namespace net
  94. }; // namespace atom
  95.  
  96. #endif // NET_MANAGER_H
  97.