Rev 1962 | Rev 1987 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1962 | Rev 1971 | ||
|---|---|---|---|
| Line 91... | Line 91... | ||
| 91 | } |
91 | } |
| 92 | 92 | ||
| 93 | 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) |
| 94 | { |
94 | { |
| 95 | LOG_DEBUG_ENTER; |
95 | LOG_DEBUG_ENTER; |
| - | 96 | ||
| - | 97 | log::Debug(log_module_, "SlotOnNewState, client_id %d, server_id %d, client_state %d", client_id, server_id, client_state); |
|
| 96 | 98 | ||
| 97 | if (client_state == CLIENT_STATE_DISCONNECTED) |
99 | if (client_state == CLIENT_STATE_DISCONNECTED) |
| 98 | { |
100 | { |
| 99 | this->clients_.erase(client_id); |
101 | this->clients_.erase(client_id); |
| 100 | } |
102 | } |
| 101 | 103 | ||
| 102 | this->signal_on_new_state_(client_id, server_id, client_state); |
104 | this->signal_on_new_state_(client_id, server_id, client_state); |
| 103 | 105 | ||
| 104 | LOG_DEBUG_EXIT; |
106 | LOG_DEBUG_EXIT; |
| 105 | } |
107 | } |
| 106 | 108 | ||
| 107 | void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket) |
109 | void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket) |
| 108 | { |
110 | { |
| 109 | LOG_DEBUG_ENTER; |
111 | LOG_DEBUG_ENTER; |
| 110 | 112 | ||
| 111 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, socket, this->GetFreeSocketId(), server_id)); |
113 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, socket, this->GetFreeSocketId(), server_id)); |
| 112 | 114 | ||
| 113 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
115 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 114 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
116 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 115 | 117 | ||
| 116 | this->clients_[client->GetId()] = client; |
118 | this->clients_[client->GetId()] = client; |
| 117 | 119 | ||
| 118 | log::Debug(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId()); |
120 | log::Debug(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId()); |
| 119 | 121 | ||
| 120 | this->signal_on_new_state_(client->GetId(), server_id, CLIENT_STATE_CONNECTED); |
122 | this->signal_on_new_state_(client->GetId(), server_id, CLIENT_STATE_CONNECTED); |
| 121 | 123 | ||
| 122 | LOG_DEBUG_EXIT; |
124 | LOG_DEBUG_EXIT; |
| 123 | } |
125 | } |
| 124 | 126 | ||
| 125 | void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data) |
127 | void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data) |
| 126 | { |
128 | { |
| 127 | LOG_DEBUG_ENTER; |
129 | LOG_DEBUG_ENTER; |
| 128 | 130 | ||
| 129 | this->signal_on_new_data_(client_id, server_id, data); |
131 | this->signal_on_new_data_(client_id, server_id, data); |
| 130 | 132 | ||
| 131 | LOG_DEBUG_EXIT; |
133 | LOG_DEBUG_EXIT; |
| 132 | } |
134 | } |
| 133 | 135 | ||
| 134 | SocketId Manager::GetFreeSocketId() |
136 | SocketId Manager::GetFreeSocketId() |
| 135 | { |
137 | { |
| 136 | LOG_DEBUG_ENTER; |
138 | LOG_DEBUG_ENTER; |
| 137 | 139 | ||
| 138 | SocketId id = 1; |
140 | SocketId id = 1; |
| Line 140... | Line 142... | ||
| 140 | while (this->clients_.find(id) != this->clients_.end() || this->servers_.find(id) != this->servers_.end()) |
142 | while (this->clients_.find(id) != this->clients_.end() || this->servers_.find(id) != this->servers_.end()) |
| 141 | { |
143 | { |
| 142 | id++; |
144 | id++; |
| 143 | } |
145 | } |
| 144 | 146 | ||
| 145 | LOG_DEBUG_EXIT; |
147 | LOG_DEBUG_EXIT; |
| 146 | 148 | ||
| 147 | return id; |
149 | return id; |
| 148 | } |
150 | } |
| 149 | 151 | ||
| 150 | SocketId Manager::StartServer(Protocol protocol, unsigned int port) |
152 | SocketId Manager::StartServer(Protocol protocol, unsigned int port) |
| 151 | { |
153 | { |
| 152 | LOG_DEBUG_ENTER; |
154 | LOG_DEBUG_ENTER; |
| 153 | 155 | ||
| 154 | if (protocol != PROTOCOL_TCP) |
156 | if (protocol != PROTOCOL_TCP) |
| 155 | { |
157 | { |
| 156 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
158 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
| 157 | } |
159 | } |
| 158 | 160 | ||
| 159 | TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId())); |
161 | TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId())); |
| 160 | 162 | ||
| 161 | server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_)); |
163 | server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_)); |
| 162 | 164 | ||
| 163 | server->Accept(); |
165 | server->Accept(); |
| 164 | 166 | ||
| 165 | this->servers_[server->GetId()] = server; |
167 | this->servers_[server->GetId()] = server; |
| 166 | 168 | ||
| 167 | log::Debug(log_module_, "Started server with id %d!", server->GetId()); |
169 | log::Debug(log_module_, "Started server with id %d!", server->GetId()); |
| 168 | 170 | ||
| 169 | LOG_DEBUG_EXIT; |
171 | LOG_DEBUG_EXIT; |
| 170 | 172 | ||
| 171 | return server->GetId(); |
173 | return server->GetId(); |
| 172 | } |
174 | } |
| 173 | 175 | ||
| 174 | SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
176 | SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
| 175 | { |
177 | { |
| Line 211... | Line 213... | ||
| 211 | 213 | ||
| 212 | return client->GetId(); |
214 | return client->GetId(); |
| 213 | } |
215 | } |
| 214 | 216 | ||
| 215 | void Manager::SendToAll(SocketId server_id, common::Byteset data) |
217 | void Manager::SendToAll(SocketId server_id, common::Byteset data) |
| 216 | { |
218 | { |
| 217 | LOG_DEBUG_ENTER; |
219 | LOG_DEBUG_ENTER; |
| 218 | - | ||
| 219 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
- | |
| 220 | 220 | ||
| - | 221 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
|
| - | 222 | ||
| 221 | LOG_DEBUG_EXIT; |
223 | LOG_DEBUG_EXIT; |
| 222 | } |
224 | } |
| 223 | 225 | ||
| 224 | void Manager::SendTo(SocketId client_id, common::Byteset data) |
226 | void Manager::SendTo(SocketId client_id, common::Byteset data) |
| 225 | { |
227 | { |
| 226 | LOG_DEBUG_ENTER; |
228 | LOG_DEBUG_ENTER; |
| - | 229 | ||
| - | 230 | log::Debug(log_module_, "SendTo, client_id %d", client_id); |
|
| 227 | 231 | ||
| 228 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
232 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
| 229 | 233 | ||
| 230 | LOG_DEBUG_EXIT; |
234 | LOG_DEBUG_EXIT; |
| 231 | } |
235 | } |
| 232 | 236 | ||
| 233 | void Manager::StopServer(SocketId server_id) |
237 | void Manager::StopServer(SocketId server_id) |
| 234 | { |
238 | { |
| 235 | LOG_DEBUG_ENTER; |
239 | LOG_DEBUG_ENTER; |
| 236 | 240 | ||
| Line 238... | Line 242... | ||
| 238 | 242 | ||
| 239 | LOG_DEBUG_EXIT; |
243 | LOG_DEBUG_EXIT; |
| 240 | } |
244 | } |
| 241 | 245 | ||
| 242 | void Manager::Disconnect(SocketId client_id) |
246 | void Manager::Disconnect(SocketId client_id) |
| 243 | { |
247 | { |
| 244 | LOG_DEBUG_ENTER; |
248 | LOG_DEBUG_ENTER; |
| - | 249 | ||
| - | 250 | log::Debug(log_module_, "Disconnect, client_id %d", client_id); |
|
| 245 | 251 | ||
| 246 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
252 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
| 247 | 253 | ||
| 248 | LOG_DEBUG_EXIT; |
254 | LOG_DEBUG_EXIT; |
| 249 | } |
255 | } |
| 250 | 256 | ||
| 251 | void Manager::SendToAllHandler(SocketId server_id, common::Byteset data) |
257 | void Manager::SendToAllHandler(SocketId server_id, common::Byteset data) |
| 252 | { |
258 | { |
| 253 | LOG_DEBUG_ENTER; |
259 | LOG_DEBUG_ENTER; |
| 254 | 260 | ||
| 255 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
261 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 256 | { |
262 | { |
| 257 | if (it->second->GetServerId() == server_id) |
263 | if (it->second->GetServerId() == server_id) |
| Line 262... | Line 268... | ||
| 262 | 268 | ||
| 263 | LOG_DEBUG_EXIT; |
269 | LOG_DEBUG_EXIT; |
| 264 | } |
270 | } |
| 265 | 271 | ||
| 266 | void Manager::SendToHandler(SocketId client_id, common::Byteset data) |
272 | void Manager::SendToHandler(SocketId client_id, common::Byteset data) |
| 267 | { |
273 | { |
| 268 | LOG_DEBUG_ENTER; |
274 | LOG_DEBUG_ENTER; |
| - | 275 | ||
| - | 276 | log::Debug(log_module_, "SendToHandler, client_id %d, data %s", client_id, data.ToCharString().c_str()); |
|
| 269 | 277 | ||
| 270 | ClientList::iterator it = this->clients_.find(client_id); |
278 | ClientList::iterator it = this->clients_.find(client_id); |
| 271 | 279 | ||
| 272 | if (it != this->clients_.end()) |
280 | if (it != this->clients_.end()) |
| 273 | { |
281 | { |
| 274 | it->second->Send(data); |
282 | it->second->Send(data); |
| 275 | } |
283 | } |
| 276 | 284 | ||
| 277 | LOG_DEBUG_EXIT; |
285 | LOG_DEBUG_EXIT; |
| 278 | } |
286 | } |
| 279 | 287 | ||
| 280 | void Manager::StopServerHandler(SocketId server_id) |
288 | void Manager::StopServerHandler(SocketId server_id) |
| 281 | { |
289 | { |
| 282 | LOG_DEBUG_ENTER; |
290 | LOG_DEBUG_ENTER; |
| 283 | 291 | ||
| 284 | this->clients_.erase(server_id); |
292 | this->clients_.erase(server_id); |
| 285 | 293 | ||
| 286 | LOG_DEBUG_EXIT; |
294 | LOG_DEBUG_EXIT; |
| 287 | } |
295 | } |
| 288 | 296 | ||
| 289 | void Manager::DisconnectHandler(SocketId client_id) |
297 | void Manager::DisconnectHandler(SocketId client_id) |
| 290 | { |
298 | { |
| 291 | LOG_DEBUG_ENTER; |
299 | LOG_DEBUG_ENTER; |
| - | 300 | ||
| - | 301 | log::Debug(log_module_, "DisconnectHandler, client_id %d", client_id); |
|
| 292 | 302 | ||
| 293 | ClientList::iterator it = this->clients_.find(client_id); |
303 | ClientList::iterator it = this->clients_.find(client_id); |
| 294 | 304 | ||
| 295 | if (it != this->clients_.end()) |
305 | if (it != this->clients_.end()) |
| 296 | { |
306 | { |