Subversion Repositories HomeAutomation

Rev

Rev 1986 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1592 runge 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>
1593 runge 28
#include <boost/shared_ptr.hpp>
1592 runge 29
 
1601 runge 30
#include "common/IoService.h"
31
 
1595 runge 32
#include "types.h"
1592 runge 33
#include "Client.h"
34
#include "TcpClient.h"
35
#include "UdpClient.h"
36
#include "SerialClient.h"
1962 runge 37
#include "TcpServer.h"
1592 runge 38
 
1962 runge 39
 
1592 runge 40
namespace atom {
41
namespace net {
42
 
1939 runge 43
class Manager : public common::IoService
1592 runge 44
{
45
public:
1986 runge 46
  typedef boost::shared_ptr<Manager>                                Pointer;
47
  typedef boost::signals2::signal<void(SocketId, ClientState)>      SignalOnNewState;
48
  typedef boost::signals2::signal<void(SocketId, SocketId)>         SignalOnNewClient;
49
  typedef boost::signals2::signal<void(SocketId, common::Byteset)>  SignalOnNewData;
1592 runge 50
 
1963 runge 51
  virtual ~Manager();
1592 runge 52
 
1963 runge 53
  static Pointer Instance();
54
  static void Create();
55
  static void Delete();
56
 
1986 runge 57
  void ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewClient::slot_type& slot_on_new_client, const SignalOnNewData::slot_type& slot_on_new_data);
1963 runge 58
 
1989 runge 59
  SocketId StartServer(TransportProtocol protocol, unsigned int port);
60
  SocketId Connect(TransportProtocol protocol, std::string address, unsigned int port_or_baud);
1963 runge 61
 
62
  void StopServer(SocketId server_id);
63
  void Disconnect(SocketId client_id);
64
 
65
  void SendToAll(SocketId server_id, common::Byteset data);
66
  void SendTo(SocketId client_id, common::Byteset data);
1599 runge 67
 
1592 runge 68
private:
1963 runge 69
  typedef std::map<SocketId, Client::Pointer>     ClientList;
70
  typedef std::map<SocketId, TcpServer::Pointer>  ServerList;
1592 runge 71
 
1963 runge 72
  static Pointer instance_;
73
 
74
  ClientList        clients_;
75
  ServerList        servers_;
76
  SignalOnNewState  signal_on_new_state_;
1986 runge 77
  SignalOnNewClient signal_on_new_client_;
1963 runge 78
  SignalOnNewData   signal_on_new_data_;
79
 
80
  Manager();
81
 
82
  void SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state);
83
  void SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data);
84
  void SlotOnNewClient(SocketId server_id, TcpSocketPointer socket);
85
 
86
  void StopServerHandler(SocketId server_id);
87
  void DisconnectHandler(SocketId client_id);
88
 
89
  void SendToAllHandler(SocketId server_id, common::Byteset data);
90
  void SendToHandler(SocketId client_id, common::Byteset data);
91
 
92
  SocketId GetFreeSocketId();
1592 runge 93
};
94
 
95
}; // namespace net
96
}; // namespace atom
97
 
98
#endif // NET_MANAGER_H