Rev 1939 | Rev 1962 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1939 | Rev 1959 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
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., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Manager.h" |
21 | #include "Manager.h" |
| 22 | 22 | ||
| 23 | #include <iostream> |
23 | #include <iostream> |
| 24 | 24 | ||
| 25 | #include <boost/lexical_cast.hpp> |
25 | #include <boost/lexical_cast.hpp> |
| 26 | #include <boost/bind.hpp> |
26 | #include <boost/bind.hpp> |
| 27 | #include <boost/cast.hpp> |
27 | #include <boost/cast.hpp> |
| 28 | 28 | ||
| 29 | namespace atom { |
29 | namespace atom { |
| 30 | namespace net { |
30 | namespace net { |
| 31 | 31 | ||
| 32 | Manager::Pointer Manager::instance_; |
32 | Manager::Pointer Manager::instance_; |
| 33 | 33 | ||
| 34 | logging::Logger Manager::LOG("net::Manager"); |
34 | logging::Logger Manager::LOG("net::Manager"); |
| 35 | 35 | ||
| 36 | Manager::Manager() |
36 | Manager::Manager() |
| 37 | { |
37 | { |
| 38 | 38 | ||
| 39 | } |
39 | } |
| 40 | 40 | ||
| 41 | Manager::~Manager() |
41 | Manager::~Manager() |
| 42 | { |
42 | { |
| 43 | this->Stop(); |
43 | this->Stop(); |
| 44 | 44 | ||
| 45 | this->clients_.clear(); |
45 | this->clients_.clear(); |
| 46 | } |
46 | } |
| 47 | 47 | ||
| 48 | Manager::Pointer Manager::Instance() |
48 | Manager::Pointer Manager::Instance() |
| 49 | { |
49 | { |
| 50 | return Manager::instance_; |
50 | return Manager::instance_; |
| 51 | } |
51 | } |
| 52 | 52 | ||
| 53 | void Manager::Create() |
53 | void Manager::Create() |
| 54 | { |
54 | { |
| 55 | Manager::instance_ = Manager::Pointer(new Manager()); |
55 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 56 | } |
56 | } |
| 57 | 57 | ||
| 58 | void Manager::Delete() |
58 | void Manager::Delete() |
| 59 | { |
59 | { |
| 60 | Manager::instance_.reset(); |
60 | Manager::instance_.reset(); |
| 61 | } |
61 | } |
| 62 | 62 | ||
| 63 | void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
63 | void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
| 64 | { |
64 | { |
| 65 | this->signal_on_new_state_.connect(slot_on_new_state); |
65 | this->signal_on_new_state_.connect(slot_on_new_state); |
| 66 | this->signal_on_new_data_.connect(slot_on_new_data); |
66 | this->signal_on_new_data_.connect(slot_on_new_data); |
| 67 | } |
67 | } |
| 68 | 68 | ||
| 69 | void Manager::SlotOnNewState( |
69 | void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state) |
| 70 | { |
70 | { |
| 71 | if (client_state == CLIENT_STATE_DISCONNECTED) |
71 | if (client_state == CLIENT_STATE_DISCONNECTED) |
| 72 | { |
72 | { |
| 73 | this->clients_.erase(client_id); |
73 | this->clients_.erase(client_id); |
| 74 | } |
74 | } |
| 75 | else if (client_state == CLIENT_STATE_ACCEPTED) |
75 | else if (client_state == CLIENT_STATE_ACCEPTED) |
| 76 | { |
76 | { |
| 77 | ClientList::iterator it = this->clients_.find(client_id); |
77 | ClientList::iterator it = this->clients_.find(client_id); |
| 78 | 78 | ||
| 79 | if (it != this->clients_.end()) |
79 | if (it != this->clients_.end()) |
| 80 | { |
80 | { |
| 81 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this-> |
81 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), it->second->GetServerId())); |
| 82 | 82 | ||
| 83 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
83 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 84 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
84 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 85 | 85 | ||
| 86 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
86 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
| 87 | 87 | ||
| 88 | this->clients_[client->GetId()] = client; |
88 | this->clients_[client->GetId()] = client; |
| 89 | 89 | ||
| 90 | this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED); |
90 | this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED); |
| 91 | return; |
91 | return; |
| 92 | } |
92 | } |
| 93 | else |
93 | else |
| 94 | { |
94 | { |
| 95 | throw std::runtime_error("Accepted unknown client!"); |
95 | throw std::runtime_error("Accepted unknown client!"); |
| 96 | } |
96 | } |
| 97 | } |
97 | } |
| 98 | 98 | ||
| 99 | this->signal_on_new_state_(client_id, server_id, client_state); |
99 | this->signal_on_new_state_(client_id, server_id, client_state); |
| 100 | } |
100 | } |
| 101 | 101 | ||
| 102 | void Manager::SlotOnNewData( |
102 | void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data) |
| 103 | { |
103 | { |
| 104 | this->signal_on_new_data_(client_id, server_id, data); |
104 | this->signal_on_new_data_(client_id, server_id, data); |
| 105 | } |
105 | } |
| 106 | 106 | ||
| 107 |
|
107 | SocketId Manager::GetFreeSocketId() |
| 108 | { |
108 | { |
| 109 |
|
109 | SocketId id = 1; |
| 110 | 110 | ||
| 111 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
111 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 112 | { |
112 | { |
| 113 | if (it->second->GetServerId() != |
113 | if (it->second->GetServerId() != id && this->clients_.find(id) == this->clients_.end()) |
| 114 | { |
114 | { |
| 115 | return |
115 | return id; |
| 116 | } |
116 | } |
| 117 | 117 | ||
| 118 |
|
118 | id++; |
| 119 | } |
119 | } |
| 120 | 120 | ||
| 121 | return |
121 | return id; |
| 122 | } |
122 | } |
| 123 | 123 | ||
| 124 | ClientId Manager::GetFreeClientId() |
- | |
| 125 | { |
- | |
| 126 | ClientId client_id = 1; |
- | |
| 127 | - | ||
| 128 | while (this->clients_.find(client_id) != this->clients_.end()) |
- | |
| 129 | { |
- | |
| 130 | client_id++; |
- | |
| 131 | } |
- | |
| 132 | - | ||
| 133 | return client_id; |
- | |
| 134 | } |
- | |
| 135 | - | ||
| 136 |
|
124 | SocketId Manager::StartServer(Protocol protocol, unsigned int port) |
| 137 | { |
125 | { |
| 138 | if (protocol != PROTOCOL_TCP) |
126 | if (protocol != PROTOCOL_TCP) |
| 139 | { |
127 | { |
| 140 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
128 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
| 141 | return 0; |
129 | return 0; |
| 142 | } |
130 | } |
| 143 | 131 | ||
| 144 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this-> |
132 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), this->GetFreeSocketId())); |
| 145 | 133 | ||
| 146 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
134 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 147 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
135 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 148 | 136 | ||
| 149 | TcpClient::AcceptorPointer acceptor = TcpClient::AcceptorPointer(new boost::asio::ip::tcp::acceptor(this->io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))); |
137 | TcpClient::AcceptorPointer acceptor = TcpClient::AcceptorPointer(new boost::asio::ip::tcp::acceptor(this->io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))); |
| 150 | 138 | ||
| 151 | client->Accept(acceptor); |
139 | client->Accept(acceptor); |
| 152 | 140 | ||
| 153 | this->clients_[client->GetId()] = client; |
141 | this->clients_[client->GetId()] = client; |
| 154 | 142 | ||
| 155 | return client->GetServerId(); |
143 | return client->GetServerId(); |
| 156 | } |
144 | } |
| 157 | 145 | ||
| 158 |
|
146 | SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
| 159 | { |
147 | { |
| 160 | Client::Pointer client; |
148 | Client::Pointer client; |
| 161 | 149 | ||
| 162 | switch (protocol) |
150 | switch (protocol) |
| 163 | { |
151 | { |
| 164 | case PROTOCOL_TCP: |
152 | case PROTOCOL_TCP: |
| 165 | { |
153 | { |
| 166 | client = Client::Pointer(new TcpClient(this->io_service_, this-> |
154 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0)); |
| 167 | break; |
155 | break; |
| 168 | } |
156 | } |
| 169 | case PROTOCOL_UDP: |
157 | case PROTOCOL_UDP: |
| 170 | { |
158 | { |
| 171 | client = Client::Pointer(new UdpClient(this->io_service_, this-> |
159 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0)); |
| 172 | break; |
160 | break; |
| 173 | } |
161 | } |
| 174 | case PROTOCOL_SERIAL: |
162 | case PROTOCOL_SERIAL: |
| 175 | { |
163 | { |
| 176 | client = Client::Pointer(new SerialClient(this->io_service_, this-> |
164 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0)); |
| 177 | break; |
165 | break; |
| 178 | } |
166 | } |
| 179 | default: |
167 | default: |
| 180 | { |
168 | { |
| 181 | throw std::runtime_error("Invalid protocol specified!"); |
169 | throw std::runtime_error("Invalid protocol specified!"); |
| 182 | return 0; |
170 | return 0; |
| 183 | } |
171 | } |
| 184 | } |
172 | } |
| 185 | 173 | ||
| 186 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
174 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 187 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
175 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 188 | 176 | ||
| 189 | client->Connect(address, port_or_baud); |
177 | client->Connect(address, port_or_baud); |
| 190 | 178 | ||
| 191 | this->clients_[client->GetId()] = client; |
179 | this->clients_[client->GetId()] = client; |
| 192 | 180 | ||
| 193 | return client->GetId(); |
181 | return client->GetId(); |
| 194 | } |
182 | } |
| 195 | 183 | ||
| 196 | void Manager::SendToAll( |
184 | void Manager::SendToAll(SocketId server_id, common::Byteset data) |
| 197 | { |
185 | { |
| 198 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
186 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
| 199 | } |
187 | } |
| 200 | 188 | ||
| 201 | void Manager::SendTo( |
189 | void Manager::SendTo(SocketId client_id, common::Byteset data) |
| 202 | { |
190 | { |
| 203 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
191 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
| 204 | } |
192 | } |
| 205 | 193 | ||
| 206 | void Manager::StopServer( |
194 | void Manager::StopServer(SocketId server_id) |
| 207 | { |
195 | { |
| 208 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
196 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
| 209 | } |
197 | } |
| 210 | 198 | ||
| 211 | void Manager::Disconnect( |
199 | void Manager::Disconnect(SocketId client_id) |
| 212 | { |
200 | { |
| 213 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
201 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
| 214 | } |
202 | } |
| 215 | 203 | ||
| 216 | void Manager::SendToAllHandler( |
204 | void Manager::SendToAllHandler(SocketId server_id, common::Byteset data) |
| 217 | { |
205 | { |
| 218 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
206 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 219 | { |
207 | { |
| 220 | if (it->second->GetServerId() == server_id) |
208 | if (it->second->GetServerId() == server_id) |
| 221 | { |
209 | { |
| 222 | it->second->Send(data); |
210 | it->second->Send(data); |
| 223 | } |
211 | } |
| 224 | } |
212 | } |
| 225 | } |
213 | } |
| 226 | 214 | ||
| 227 | void Manager::SendToHandler( |
215 | void Manager::SendToHandler(SocketId client_id, common::Byteset data) |
| 228 | { |
216 | { |
| 229 | ClientList::iterator it = this->clients_.find(client_id); |
217 | ClientList::iterator it = this->clients_.find(client_id); |
| 230 | 218 | ||
| 231 | if (it != this->clients_.end()) |
219 | if (it != this->clients_.end()) |
| 232 | { |
220 | { |
| 233 | it->second->Send(data); |
221 | it->second->Send(data); |
| 234 | } |
222 | } |
| 235 | } |
223 | } |
| 236 | 224 | ||
| 237 | void Manager::StopServerHandler( |
225 | void Manager::StopServerHandler(SocketId server_id) |
| 238 | { |
226 | { |
| 239 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
227 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 240 | { |
228 | { |
| 241 | if (it->second->GetServerId() == server_id) |
229 | if (it->second->GetServerId() == server_id) |
| 242 | { |
230 | { |
| 243 | it->second->Stop(); |
231 | it->second->Stop(); |
| 244 | } |
232 | } |
| 245 | } |
233 | } |
| 246 | } |
234 | } |
| 247 | 235 | ||
| 248 | void Manager::DisconnectHandler( |
236 | void Manager::DisconnectHandler(SocketId client_id) |
| 249 | { |
237 | { |
| 250 | ClientList::iterator it = this->clients_.find(client_id); |
238 | ClientList::iterator it = this->clients_.find(client_id); |
| 251 | 239 | ||
| 252 | if (it != this->clients_.end()) |
240 | if (it != this->clients_.end()) |
| 253 | { |
241 | { |
| 254 | it->second->Disconnect(); |
242 | it->second->Disconnect(); |
| 255 | } |
243 | } |
| 256 | } |
244 | } |
| 257 | 245 | ||
| 258 | }; // namespace net |
246 | }; // namespace net |
| 259 | }; // namespace atom |
247 | }; // namespace atom |
| 260 | 248 | ||