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