Rev 1601 | Rev 1627 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1601 | Rev 1606 | ||
|---|---|---|---|
| 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 | Manager::Manager() |
34 | Manager::Manager() |
| 35 | { |
35 | { |
| 36 | } |
36 | } |
| 37 | 37 | ||
| 38 | Manager::~Manager() |
38 | Manager::~Manager() |
| 39 | { |
39 | { |
| 40 | this->clients_.clear(); |
40 | this->clients_.clear(); |
| 41 | } |
41 | } |
| 42 | 42 | ||
| 43 | Manager::Pointer Manager::Instance() |
43 | Manager::Pointer Manager::Instance() |
| 44 | { |
44 | { |
| 45 | return Manager::instance_; |
45 | return Manager::instance_; |
| 46 | } |
46 | } |
| 47 | 47 | ||
| 48 | void Manager::Create() |
48 | void Manager::Create() |
| 49 | { |
49 | { |
| 50 | Manager::instance_ = Manager::Pointer(new Manager()); |
50 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 51 | } |
51 | } |
| 52 | 52 | ||
| 53 | void Manager::Delete() |
53 | void Manager::Delete() |
| 54 | { |
54 | { |
| 55 | Manager::instance_.reset(); |
55 | Manager::instance_.reset(); |
| 56 | } |
56 | } |
| 57 | 57 | ||
| 58 | void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
58 | void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
| 59 | { |
59 | { |
| 60 | this->signal_on_new_state_.connect(slot_on_new_state); |
60 | this->signal_on_new_state_.connect(slot_on_new_state); |
| 61 | this->signal_on_new_data_.connect(slot_on_new_data); |
61 | this->signal_on_new_data_.connect(slot_on_new_data); |
| 62 | } |
62 | } |
| 63 | 63 | ||
| 64 | void Manager::SlotOnNewState(ClientId client_id, ServerId server_id, ClientState client_state) |
64 | void Manager::SlotOnNewState(ClientId client_id, ServerId server_id, ClientState client_state) |
| 65 | { |
65 | { |
| 66 | if (client_state == CLIENT_STATE_DISCONNECTED) |
66 | if (client_state == CLIENT_STATE_DISCONNECTED) |
| 67 | { |
67 | { |
| 68 | this->clients_.erase(client_id); |
68 | this->clients_.erase(client_id); |
| 69 | } |
69 | } |
| 70 | else if (client_state == CLIENT_STATE_ACCEPTED) |
70 | else if (client_state == CLIENT_STATE_ACCEPTED) |
| 71 | { |
71 | { |
| 72 | ClientList::iterator it = this->clients_.find(client_id); |
72 | ClientList::iterator it = this->clients_.find(client_id); |
| 73 | 73 | ||
| 74 | if (it != this->clients_.end()) |
74 | if (it != this->clients_.end()) |
| 75 | { |
75 | { |
| 76 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), it->second->GetServerId())); |
76 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), it->second->GetServerId())); |
| 77 | 77 | ||
| 78 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
78 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 79 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
79 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 80 | 80 | ||
| 81 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
81 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
| 82 | 82 | ||
| 83 | this->clients_[client->GetId()] = client; |
83 | this->clients_[client->GetId()] = client; |
| 84 | 84 | ||
| 85 | this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED); |
85 | this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED); |
| 86 | return; |
86 | return; |
| 87 | } |
87 | } |
| 88 | else |
88 | else |
| 89 | { |
89 | { |
| 90 | throw std::runtime_error("Accepted unknown client!"); |
90 | throw std::runtime_error("Accepted unknown client!"); |
| 91 | } |
91 | } |
| 92 | } |
92 | } |
| 93 | 93 | ||
| 94 | this->signal_on_new_state_(client_id, server_id, client_state); |
94 | this->signal_on_new_state_(client_id, server_id, client_state); |
| 95 | } |
95 | } |
| 96 | 96 | ||
| 97 | void Manager::SlotOnNewData(ClientId client_id, ServerId server_id, type::Byteset data) |
97 | void Manager::SlotOnNewData(ClientId client_id, ServerId server_id, type::Byteset data) |
| 98 | { |
98 | { |
| 99 | this->signal_on_new_data_(client_id, server_id, data); |
99 | this->signal_on_new_data_(client_id, server_id, data); |
| 100 | } |
100 | } |
| 101 | 101 | ||
| 102 | ServerId Manager::GetFreeServerId() |
102 | ServerId Manager::GetFreeServerId() |
| 103 | { |
103 | { |
| 104 | ServerId server_id = 1; |
104 | ServerId server_id = 1; |
| 105 | 105 | ||
| 106 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
106 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 107 | { |
107 | { |
| 108 | if (it->second->GetServerId() != server_id) |
108 | if (it->second->GetServerId() != server_id) |
| 109 | { |
109 | { |
| 110 | return server_id; |
110 | return server_id; |
| 111 | } |
111 | } |
| 112 | 112 | ||
| 113 | server_id++; |
113 | server_id++; |
| 114 | } |
114 | } |
| 115 | 115 | ||
| 116 | return server_id; |
116 | return server_id; |
| 117 | } |
117 | } |
| 118 | 118 | ||
| 119 | ClientId Manager::GetFreeClientId() |
119 | ClientId Manager::GetFreeClientId() |
| 120 | { |
120 | { |
| 121 | ClientId client_id = 1; |
121 | ClientId client_id = 1; |
| 122 | 122 | ||
| 123 | while (this->clients_.find(client_id) != this->clients_.end()) |
123 | while (this->clients_.find(client_id) != this->clients_.end()) |
| 124 | { |
124 | { |
| 125 | client_id++; |
125 | client_id++; |
| 126 | } |
126 | } |
| 127 | 127 | ||
| 128 | return client_id; |
128 | return client_id; |
| 129 | } |
129 | } |
| 130 | 130 | ||
| 131 | ServerId Manager::StartServer(Protocol protocol, unsigned int port) |
131 | ServerId Manager::StartServer(Protocol protocol, unsigned int port) |
| 132 | { |
132 | { |
| 133 | if (protocol != PROTOCOL_TCP) |
133 | if (protocol != PROTOCOL_TCP) |
| 134 | { |
134 | { |
| 135 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
135 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
| 136 | return 0; |
136 | return 0; |
| 137 | } |
137 | } |
| 138 | 138 | ||
| 139 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), this->GetFreeServerId())); |
139 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), this->GetFreeServerId())); |
| 140 | 140 | ||
| 141 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
141 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 142 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
142 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 143 | 143 | ||
| 144 | 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))); |
144 | 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))); |
| 145 | 145 | ||
| 146 | client->Accept(acceptor); |
146 | client->Accept(acceptor); |
| 147 | 147 | ||
| 148 | this->clients_[client->GetId()] = client; |
148 | this->clients_[client->GetId()] = client; |
| 149 | 149 | ||
| 150 | return client->GetServerId(); |
150 | return client->GetServerId(); |
| 151 | } |
151 | } |
| 152 | 152 | ||
| 153 | ClientId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
153 | ClientId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
| 154 | { |
154 | { |
| 155 | Client::Pointer client; |
155 | Client::Pointer client; |
| 156 | 156 | ||
| 157 | switch (protocol) |
157 | switch (protocol) |
| 158 | { |
158 | { |
| 159 | case PROTOCOL_TCP: |
159 | case PROTOCOL_TCP: |
| 160 | { |
160 | { |
| 161 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0)); |
161 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 162 | break; |
162 | break; |
| 163 | } |
163 | } |
| 164 | case PROTOCOL_UDP: |
164 | case PROTOCOL_UDP: |
| 165 | { |
165 | { |
| 166 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0)); |
166 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 167 | break; |
167 | break; |
| 168 | } |
168 | } |
| 169 | case PROTOCOL_SERIAL: |
169 | case PROTOCOL_SERIAL: |
| 170 | { |
170 | { |
| 171 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0)); |
171 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 172 | break; |
172 | break; |
| 173 | } |
173 | } |
| 174 | default: |
174 | default: |
| 175 | { |
175 | { |
| 176 | throw std::runtime_error("Invalid protocol specified!"); |
176 | throw std::runtime_error("Invalid protocol specified!"); |
| 177 | return 0; |
177 | return 0; |
| 178 | } |
178 | } |
| 179 | } |
179 | } |
| 180 | 180 | ||
| 181 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
181 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 182 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
182 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 183 | 183 | ||
| 184 | try |
- | |
| 185 | { |
- | |
| 186 | client->Connect(address, port_or_baud); |
184 | client->Connect(address, port_or_baud); |
| 187 | } |
- | |
| 188 | catch (std::exception& e) |
- | |
| 189 | { |
- | |
| 190 | throw std::runtime_error(e.what()); |
- | |
| 191 | } |
- | |
| 192 | 185 | ||
| 193 | this->clients_[client->GetId()] = client; |
186 | this->clients_[client->GetId()] = client; |
| 194 | 187 | ||
| 195 | return client->GetId(); |
188 | return client->GetId(); |
| 196 | } |
189 | } |
| 197 | 190 | ||
| 198 | void Manager::SendToAll(ServerId server_id, type::Byteset data) |
191 | void Manager::SendToAll(ServerId server_id, type::Byteset data) |
| 199 | { |
192 | { |
| 200 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
193 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
| 201 | } |
194 | } |
| 202 | 195 | ||
| 203 | void Manager::SendTo(ClientId client_id, type::Byteset data) |
196 | void Manager::SendTo(ClientId client_id, type::Byteset data) |
| 204 | { |
197 | { |
| 205 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
198 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
| 206 | } |
199 | } |
| 207 | 200 | ||
| 208 | void Manager::StopServer(ServerId server_id) |
201 | void Manager::StopServer(ServerId server_id) |
| 209 | { |
202 | { |
| 210 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
203 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
| 211 | } |
204 | } |
| 212 | 205 | ||
| 213 | void Manager::Disconnect(ClientId client_id) |
206 | void Manager::Disconnect(ClientId client_id) |
| 214 | { |
207 | { |
| 215 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
208 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
| 216 | } |
209 | } |
| 217 | 210 | ||
| 218 | void Manager::SendToAllHandler(ServerId server_id, type::Byteset data) |
211 | void Manager::SendToAllHandler(ServerId server_id, type::Byteset data) |
| 219 | { |
212 | { |
| 220 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
213 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 221 | { |
214 | { |
| 222 | if (it->second->GetServerId() == server_id) |
215 | if (it->second->GetServerId() == server_id) |
| 223 | { |
216 | { |
| 224 | it->second->Send(data); |
217 | it->second->Send(data); |
| 225 | } |
218 | } |
| 226 | } |
219 | } |
| 227 | } |
220 | } |
| 228 | 221 | ||
| 229 | void Manager::SendToHandler(ClientId client_id, type::Byteset data) |
222 | void Manager::SendToHandler(ClientId client_id, type::Byteset data) |
| 230 | { |
223 | { |
| 231 | ClientList::iterator it = this->clients_.find(client_id); |
224 | ClientList::iterator it = this->clients_.find(client_id); |
| 232 | 225 | ||
| 233 | if (it != this->clients_.end()) |
226 | if (it != this->clients_.end()) |
| 234 | { |
227 | { |
| 235 | it->second->Send(data); |
228 | it->second->Send(data); |
| 236 | } |
229 | } |
| 237 | } |
230 | } |
| 238 | 231 | ||
| 239 | void Manager::StopServerHandler(ServerId server_id) |
232 | void Manager::StopServerHandler(ServerId server_id) |
| 240 | { |
233 | { |
| 241 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
234 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 242 | { |
235 | { |
| 243 | if (it->second->GetServerId() == server_id) |
236 | if (it->second->GetServerId() == server_id) |
| 244 | { |
237 | { |
| 245 | it->second->Stop(); |
238 | it->second->Stop(); |
| 246 | } |
239 | } |
| 247 | } |
240 | } |
| 248 | } |
241 | } |
| 249 | 242 | ||
| 250 | void Manager::DisconnectHandler(ClientId client_id) |
243 | void Manager::DisconnectHandler(ClientId client_id) |
| 251 | { |
244 | { |
| 252 | ClientList::iterator it = this->clients_.find(client_id); |
245 | ClientList::iterator it = this->clients_.find(client_id); |
| 253 | 246 | ||
| 254 | if (it != this->clients_.end()) |
247 | if (it != this->clients_.end()) |
| 255 | { |
248 | { |
| 256 | it->second->Disconnect(); |
249 | it->second->Disconnect(); |
| 257 | } |
250 | } |
| 258 | } |
251 | } |
| 259 | 252 | ||
| 260 | }; // namespace net |
253 | }; // namespace net |
| 261 | }; // namespace atom |
254 | }; // namespace atom |
| 262 | 255 | ||