Rev 1959 | Rev 1971 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1959 | Rev 1962 | ||
|---|---|---|---|
| Line 23... | Line 23... | ||
| 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 | ||
| - | 29 | #include "common/log.h" |
|
| - | 30 | ||
| - | 31 | static const std::string log_module_ = "net::manager"; |
|
| 28 | 32 | ||
| 29 | namespace atom { |
33 | namespace atom { |
| 30 | namespace net { |
34 | namespace net { |
| 31 | 35 | ||
| 32 | Manager::Pointer Manager::instance_; |
36 | Manager::Pointer Manager::instance_; |
| 33 | 37 | ||
| 34 | logging::Logger Manager::LOG("net::Manager"); |
- | |
| 35 | 38 | ||
| 36 | Manager::Manager() |
39 | Manager::Manager() |
| 37 | { |
40 | { |
| 38 | - | ||
| - | 41 | LOG_DEBUG_ENTER; |
|
| - | 42 | LOG_DEBUG_EXIT; |
|
| 39 | } |
43 | } |
| 40 | 44 | ||
| 41 | Manager::~Manager() |
45 | Manager::~Manager() |
| 42 | { |
46 | { |
| - | 47 | LOG_DEBUG_ENTER; |
|
| - | 48 | ||
| 43 | this->Stop(); |
49 | this->Stop(); |
| 44 | 50 | ||
| 45 | this->clients_.clear(); |
51 | this->clients_.clear(); |
| - | 52 | ||
| - | 53 | LOG_DEBUG_EXIT; |
|
| 46 | } |
54 | } |
| 47 | 55 | ||
| 48 | Manager::Pointer Manager::Instance() |
56 | Manager::Pointer Manager::Instance() |
| 49 | { |
57 | { |
| - | 58 | LOG_DEBUG_ENTER; |
|
| - | 59 | ||
| 50 | return Manager::instance_; |
60 | return Manager::instance_; |
| - | 61 | ||
| - | 62 | LOG_DEBUG_EXIT; |
|
| 51 | } |
63 | } |
| 52 | 64 | ||
| 53 | void Manager::Create() |
65 | void Manager::Create() |
| 54 | { |
66 | { |
| - | 67 | LOG_DEBUG_ENTER; |
|
| - | 68 | ||
| 55 | Manager::instance_ = Manager::Pointer(new Manager()); |
69 | Manager::instance_ = Manager::Pointer(new Manager()); |
| - | 70 | ||
| - | 71 | LOG_DEBUG_EXIT; |
|
| 56 | } |
72 | } |
| 57 | 73 | ||
| 58 | void Manager::Delete() |
74 | void Manager::Delete() |
| 59 | { |
75 | { |
| - | 76 | LOG_DEBUG_ENTER; |
|
| - | 77 | ||
| 60 | Manager::instance_.reset(); |
78 | Manager::instance_.reset(); |
| - | 79 | ||
| - | 80 | LOG_DEBUG_EXIT; |
|
| 61 | } |
81 | } |
| 62 | 82 | ||
| 63 | void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
83 | void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
| 64 | { |
84 | { |
| - | 85 | LOG_DEBUG_ENTER; |
|
| - | 86 | ||
| 65 | this->signal_on_new_state_.connect(slot_on_new_state); |
87 | this->signal_on_new_state_.connect(slot_on_new_state); |
| 66 | this->signal_on_new_data_.connect(slot_on_new_data); |
88 | this->signal_on_new_data_.connect(slot_on_new_data); |
| - | 89 | ||
| - | 90 | LOG_DEBUG_EXIT; |
|
| 67 | } |
91 | } |
| 68 | 92 | ||
| 69 | void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state) |
93 | void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state) |
| 70 | { |
94 | { |
| 71 | if (client_state == CLIENT_STATE_DISCONNECTED) |
- | |
| 72 | { |
- | |
| 73 |
|
95 | LOG_DEBUG_ENTER; |
| 74 | } |
- | |
| 75 | else if (client_state == CLIENT_STATE_ACCEPTED) |
- | |
| 76 | { |
- | |
| 77 | ClientList::iterator it = this->clients_.find(client_id); |
- | |
| 78 | 96 | ||
| 79 |
|
97 | if (client_state == CLIENT_STATE_DISCONNECTED) |
| 80 | { |
98 | { |
| - | 99 | this->clients_.erase(client_id); |
|
| - | 100 | } |
|
| - | 101 | ||
| - | 102 | this->signal_on_new_state_(client_id, server_id, client_state); |
|
| - | 103 | ||
| - | 104 | LOG_DEBUG_EXIT; |
|
| - | 105 | } |
|
| - | 106 | ||
| - | 107 | void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket) |
|
| - | 108 | { |
|
| - | 109 | LOG_DEBUG_ENTER; |
|
| - | 110 | ||
| 81 |
|
111 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, socket, this->GetFreeSocketId(), server_id)); |
| 82 | 112 | ||
| 83 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
113 | 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_)); |
114 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 85 | - | ||
| 86 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
- | |
| 87 | 115 | ||
| 88 | this->clients_[client->GetId()] = client; |
116 | this->clients_[client->GetId()] = client; |
| 89 | 117 | ||
| 90 |
|
118 | log::Debug(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId()); |
| 91 | return; |
- | |
| 92 |
|
119 | |
| 93 | else |
- | |
| 94 | { |
- | |
| 95 |
|
120 | this->signal_on_new_state_(client->GetId(), server_id, CLIENT_STATE_CONNECTED); |
| 96 | } |
- | |
| 97 |
|
121 | |
| 98 | 122 | LOG_DEBUG_EXIT; |
|
| 99 | this->signal_on_new_state_(client_id, server_id, client_state); |
- | |
| 100 | } |
123 | } |
| 101 | 124 | ||
| 102 | void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data) |
125 | void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data) |
| 103 | { |
126 | { |
| - | 127 | LOG_DEBUG_ENTER; |
|
| - | 128 | ||
| 104 | this->signal_on_new_data_(client_id, server_id, data); |
129 | this->signal_on_new_data_(client_id, server_id, data); |
| - | 130 | ||
| - | 131 | LOG_DEBUG_EXIT; |
|
| 105 | } |
132 | } |
| 106 | 133 | ||
| 107 | SocketId Manager::GetFreeSocketId() |
134 | SocketId Manager::GetFreeSocketId() |
| 108 | { |
135 | { |
| 109 |
|
136 | LOG_DEBUG_ENTER; |
| 110 | 137 | ||
| 111 |
|
138 | SocketId id = 1; |
| 112 |
|
139 | |
| 113 |
|
140 | while (this->clients_.find(id) != this->clients_.end() || this->servers_.find(id) != this->servers_.end()) |
| 114 | { |
141 | { |
| 115 | return id; |
- | |
| 116 | } |
- | |
| 117 | - | ||
| 118 | id++; |
142 | id++; |
| 119 | } |
143 | } |
| - | 144 | ||
| - | 145 | LOG_DEBUG_EXIT; |
|
| 120 | 146 | ||
| 121 | return id; |
147 | return id; |
| 122 | } |
148 | } |
| 123 | 149 | ||
| 124 | SocketId Manager::StartServer(Protocol protocol, unsigned int port) |
150 | SocketId Manager::StartServer(Protocol protocol, unsigned int port) |
| 125 | { |
151 | { |
| - | 152 | LOG_DEBUG_ENTER; |
|
| - | 153 | ||
| 126 | if (protocol != PROTOCOL_TCP) |
154 | if (protocol != PROTOCOL_TCP) |
| 127 | { |
155 | { |
| 128 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
156 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
| 129 | return 0; |
- | |
| 130 | } |
- | |
| 131 | - | ||
| 132 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), this->GetFreeSocketId())); |
- | |
| 133 | - | ||
| 134 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
- | |
| 135 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
- | |
| 136 | - | ||
| 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))); |
- | |
| 138 | - | ||
| 139 | client->Accept(acceptor); |
- | |
| 140 | - | ||
| 141 | this->clients_[client->GetId()] = client; |
- | |
| 142 | - | ||
| 143 | return client->GetServerId(); |
- | |
| 144 | } |
157 | } |
| - | 158 | ||
| - | 159 | TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId())); |
|
| - | 160 | ||
| - | 161 | server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_)); |
|
| 145 | 162 | ||
| - | 163 | server->Accept(); |
|
| - | 164 | ||
| - | 165 | this->servers_[server->GetId()] = server; |
|
| - | 166 | ||
| - | 167 | log::Debug(log_module_, "Started server with id %d!", server->GetId()); |
|
| - | 168 | ||
| - | 169 | LOG_DEBUG_EXIT; |
|
| - | 170 | ||
| - | 171 | return server->GetId(); |
|
| - | 172 | } |
|
| - | 173 | ||
| 146 | SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
174 | SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
| 147 | { |
175 | { |
| - | 176 | LOG_DEBUG_ENTER; |
|
| - | 177 | ||
| 148 | Client::Pointer client; |
178 | Client::Pointer client; |
| 149 | 179 | ||
| 150 | switch (protocol) |
180 | switch (protocol) |
| 151 | { |
181 | { |
| 152 | case PROTOCOL_TCP: |
182 | case PROTOCOL_TCP: |
| Line 165... | Line 195... | ||
| 165 | break; |
195 | break; |
| 166 | } |
196 | } |
| 167 | default: |
197 | default: |
| 168 | { |
198 | { |
| 169 | throw std::runtime_error("Invalid protocol specified!"); |
199 | throw std::runtime_error("Invalid protocol specified!"); |
| 170 | return 0; |
- | |
| 171 | } |
200 | } |
| 172 | } |
201 | } |
| 173 | 202 | ||
| 174 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
203 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 175 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
204 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 176 | 205 | ||
| 177 | client->Connect(address, port_or_baud); |
206 | client->Connect(address, port_or_baud); |
| 178 | 207 | ||
| 179 | this->clients_[client->GetId()] = client; |
208 | this->clients_[client->GetId()] = client; |
| - | 209 | ||
| - | 210 | LOG_DEBUG_EXIT; |
|
| 180 | 211 | ||
| 181 | return client->GetId(); |
212 | return client->GetId(); |
| 182 | } |
213 | } |
| 183 | 214 | ||
| 184 | void Manager::SendToAll(SocketId server_id, common::Byteset data) |
215 | void Manager::SendToAll(SocketId server_id, common::Byteset data) |
| 185 | { |
216 | { |
| - | 217 | LOG_DEBUG_ENTER; |
|
| - | 218 | ||
| 186 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
219 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
| - | 220 | ||
| - | 221 | LOG_DEBUG_EXIT; |
|
| 187 | } |
222 | } |
| 188 | 223 | ||
| 189 | void Manager::SendTo(SocketId client_id, common::Byteset data) |
224 | void Manager::SendTo(SocketId client_id, common::Byteset data) |
| 190 | { |
225 | { |
| - | 226 | LOG_DEBUG_ENTER; |
|
| - | 227 | ||
| 191 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
228 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
| - | 229 | ||
| - | 230 | LOG_DEBUG_EXIT; |
|
| 192 | } |
231 | } |
| 193 | 232 | ||
| 194 | void Manager::StopServer(SocketId server_id) |
233 | void Manager::StopServer(SocketId server_id) |
| 195 | { |
234 | { |
| - | 235 | LOG_DEBUG_ENTER; |
|
| - | 236 | ||
| 196 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
237 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
| - | 238 | ||
| - | 239 | LOG_DEBUG_EXIT; |
|
| 197 | } |
240 | } |
| 198 | 241 | ||
| 199 | void Manager::Disconnect(SocketId client_id) |
242 | void Manager::Disconnect(SocketId client_id) |
| 200 | { |
243 | { |
| - | 244 | LOG_DEBUG_ENTER; |
|
| - | 245 | ||
| 201 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
246 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
| - | 247 | ||
| - | 248 | LOG_DEBUG_EXIT; |
|
| 202 | } |
249 | } |
| 203 | 250 | ||
| 204 | void Manager::SendToAllHandler(SocketId server_id, common::Byteset data) |
251 | void Manager::SendToAllHandler(SocketId server_id, common::Byteset data) |
| 205 | { |
252 | { |
| - | 253 | LOG_DEBUG_ENTER; |
|
| - | 254 | ||
| 206 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
255 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 207 | { |
256 | { |
| 208 | if (it->second->GetServerId() == server_id) |
257 | if (it->second->GetServerId() == server_id) |
| 209 | { |
258 | { |
| 210 | it->second->Send(data); |
259 | it->second->Send(data); |
| 211 | } |
260 | } |
| 212 | } |
261 | } |
| - | 262 | ||
| - | 263 | LOG_DEBUG_EXIT; |
|
| 213 | } |
264 | } |
| 214 | 265 | ||
| 215 | void Manager::SendToHandler(SocketId client_id, common::Byteset data) |
266 | void Manager::SendToHandler(SocketId client_id, common::Byteset data) |
| 216 | { |
267 | { |
| - | 268 | LOG_DEBUG_ENTER; |
|
| - | 269 | ||
| 217 | ClientList::iterator it = this->clients_.find(client_id); |
270 | ClientList::iterator it = this->clients_.find(client_id); |
| 218 | 271 | ||
| 219 | if (it != this->clients_.end()) |
272 | if (it != this->clients_.end()) |
| 220 | { |
273 | { |
| 221 | it->second->Send(data); |
274 | it->second->Send(data); |
| 222 | } |
275 | } |
| - | 276 | ||
| - | 277 | LOG_DEBUG_EXIT; |
|
| 223 | } |
278 | } |
| 224 | 279 | ||
| 225 | void Manager::StopServerHandler(SocketId server_id) |
280 | void Manager::StopServerHandler(SocketId server_id) |
| 226 | { |
281 | { |
| 227 |
|
282 | LOG_DEBUG_ENTER; |
| 228 |
|
283 | |
| 229 |
|
284 | this->clients_.erase(server_id); |
| 230 |
|
285 | |
| 231 | it->second->Stop(); |
- | |
| 232 |
|
286 | LOG_DEBUG_EXIT; |
| 233 | } |
- | |
| 234 | } |
287 | } |
| 235 | 288 | ||
| 236 | void Manager::DisconnectHandler(SocketId client_id) |
289 | void Manager::DisconnectHandler(SocketId client_id) |
| 237 | { |
290 | { |
| - | 291 | LOG_DEBUG_ENTER; |
|
| - | 292 | ||
| 238 | ClientList::iterator it = this->clients_.find(client_id); |
293 | ClientList::iterator it = this->clients_.find(client_id); |
| 239 | 294 | ||
| 240 | if (it != this->clients_.end()) |
295 | if (it != this->clients_.end()) |
| 241 | { |
296 | { |
| 242 | it->second->Disconnect(); |
297 | it->second->Disconnect(); |
| 243 | } |
298 | } |
| - | 299 | ||
| - | 300 | LOG_DEBUG_EXIT; |
|
| 244 | } |
301 | } |
| - | 302 | ||
| 245 | 303 | ||
| 246 | }; // namespace net |
304 | }; // namespace net |
| 247 | }; // namespace atom |
305 | }; // namespace atom |