Subversion Repositories HomeAutomation

Rev

Rev 1596 | Rev 1599 | 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/asio.hpp>
  28. #include <boost/signals2.hpp>
  29. #include <boost/shared_ptr.hpp>
  30. #include <boost/thread.hpp>
  31. #include <boost/thread/mutex.hpp>
  32.  
  33. #include "types.h"
  34. #include "Client.h"
  35. #include "TcpClient.h"
  36. #include "UdpClient.h"
  37. #include "SerialClient.h"
  38.  
  39. namespace atom {
  40. namespace net {
  41.  
  42. class Manager
  43. {
  44. public:
  45.     typedef boost::shared_ptr<Manager> Pointer;
  46.  
  47.     typedef boost::signals2::signal<void(ClientId, ServerId, ClientState)> SignalOnNewState;
  48.     typedef boost::signals2::signal<void(ClientId, ServerId, type::Byteset)> SignalOnNewData;
  49.  
  50.     virtual ~Manager();
  51.    
  52.     static Pointer Instance();
  53.     static void Delete();
  54.    
  55.     void ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data);
  56.    
  57.     ServerId StartServer(Protocol protocol, unsigned int port);
  58.     ClientId Connect(Protocol protocol, std::string address, unsigned int port_or_baud);
  59.    
  60.     void StopServer(ServerId server_id);
  61.     void Disconnect(ClientId client_id);
  62.    
  63.     void SendToAll(ServerId server_id, type::Byteset data);
  64.     void SendTo(ClientId client_id, type::Byteset data);
  65.  
  66. private:
  67.     typedef std::map<ClientId, Client::Pointer> ClientList;
  68.  
  69.     static Pointer instance_;
  70.    
  71.     boost::thread thread_;
  72.     boost::asio::io_service io_service_;
  73.     boost::asio::io_service::work io_service_work_;
  74.    
  75.     ClientList clients_;
  76.    
  77.     SignalOnNewState signal_on_new_state_;
  78.     SignalOnNewData signal_on_new_data_;
  79.    
  80.     Manager();
  81.    
  82.     void SlotOnNewState(ClientId client_id, ServerId server_id, ClientState client_state);
  83.     void SlotOnNewData(ClientId client_id, ServerId server_id, type::Byteset data);
  84.    
  85.     void StopServerHandler(ServerId server_id);
  86.     void DisconnectHandler(ClientId client_id);
  87.    
  88.     void SendToAllHandler(ServerId server_id, type::Byteset data);
  89.     void SendToHandler(ClientId client_id, type::Byteset data);
  90.    
  91.     ClientId GetFreeClientId();
  92.     ServerId GetFreeServerId();
  93. };
  94.  
  95. }; // namespace net
  96. }; // namespace atom
  97.  
  98. #endif // NET_MANAGER_H
  99.