Rev 1596 | Rev 1599 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1596 | Rev 1597 | ||
|---|---|---|---|
| Line 63... | Line 63... | ||
| 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->mutex_clients_.lock(); |
- | |
| 69 | this->clients_.erase(client_id); |
68 | this->clients_.erase(client_id); |
| 70 | this->mutex_clients_.unlock(); |
- | |
| 71 | } |
69 | } |
| 72 | else if (client_state == CLIENT_STATE_ACCEPTED) |
70 | else if (client_state == CLIENT_STATE_ACCEPTED) |
| 73 | { |
71 | { |
| 74 | this->mutex_clients_.lock(); |
- | |
| 75 | ClientList::iterator it = this->clients_.find(client_id); |
72 | ClientList::iterator it = this->clients_.find(client_id); |
| 76 | 73 | ||
| 77 | if (it != this->clients_.end()) |
74 | if (it != this->clients_.end()) |
| 78 | { |
75 | { |
| 79 | 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())); |
| Line 82... | Line 79... | ||
| 82 | 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_)); |
| 83 | 80 | ||
| 84 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
81 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
| 85 | 82 | ||
| 86 | this->clients_[client->GetId()] = client; |
83 | this->clients_[client->GetId()] = client; |
| 87 | - | ||
| 88 | this->mutex_clients_.unlock(); |
- | |
| 89 | 84 | ||
| 90 | 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); |
| 91 | return; |
86 | return; |
| 92 | } |
87 | } |
| 93 | else |
88 | else |
| 94 | { |
89 | { |
| 95 | this->mutex_clients_.unlock(); |
- | |
| 96 | throw std::runtime_error("Accepted unknown client!"); |
90 | throw std::runtime_error("Accepted unknown client!"); |
| 97 | } |
91 | } |
| 98 | } |
92 | } |
| 99 | 93 | ||
| 100 | this->signal_on_new_state_(client_id, server_id, client_state); |
94 | this->signal_on_new_state_(client_id, server_id, client_state); |
| 101 | } |
95 | } |
| 102 | 96 | ||
| 103 | 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) |
| Line 106... | Line 100... | ||
| 106 | } |
100 | } |
| 107 | 101 | ||
| 108 | ServerId Manager::GetFreeServerId() |
102 | ServerId Manager::GetFreeServerId() |
| 109 | { |
103 | { |
| 110 | ServerId server_id = 1; |
104 | ServerId server_id = 1; |
| 111 | 105 | ||
| 112 | 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++) |
| 113 | { |
107 | { |
| 114 | if (it->second->GetServerId() != server_id) |
108 | if (it->second->GetServerId() != server_id) |
| 115 | { |
109 | { |
| 116 | return server_id; |
110 | return server_id; |
| 117 | } |
111 | } |
| 118 | 112 | ||
| 119 | server_id++; |
113 | server_id++; |
| 120 | } |
114 | } |
| 121 | 115 | ||
| 122 | return server_id; |
116 | return server_id; |
| 123 | } |
117 | } |
| 124 | 118 | ||
| 125 | ClientId Manager::GetFreeClientId() |
119 | ClientId Manager::GetFreeClientId() |
| 126 | { |
120 | { |
| 127 | ClientId client_id = 1; |
121 | ClientId client_id = 1; |
| 128 | 122 | ||
| 129 | while (this->clients_.find(client_id) != this->clients_.end()) |
123 | while (this->clients_.find(client_id) != this->clients_.end()) |
| 130 | { |
124 | { |
| 131 | client_id++; |
125 | client_id++; |
| 132 | } |
126 | } |
| 133 | 127 | ||
| 134 | return client_id; |
128 | return client_id; |
| 135 | } |
129 | } |
| 136 | 130 | ||
| 137 | ServerId Manager::StartServer(Protocol protocol, unsigned int port) |
131 | ServerId Manager::StartServer(Protocol protocol, unsigned int port) |
| 138 | { |
132 | { |
| 139 | if (protocol != PROTOCOL_TCP) |
133 | if (protocol != PROTOCOL_TCP) |
| 140 | { |
134 | { |
| 141 | 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!"); |
| 142 | return 0; |
136 | return 0; |
| 143 | } |
137 | } |
| 144 | - | ||
| 145 | this->mutex_clients_.lock(); |
- | |
| 146 | 138 | ||
| 147 | 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())); |
| 148 | 140 | ||
| 149 | 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_), |
| 150 | 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_)); |
| 151 | 143 | ||
| 152 | 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))); |
| 153 | 145 | ||
| 154 | client->Accept(acceptor); |
146 | client->Accept(acceptor); |
| 155 | 147 | ||
| 156 | this->clients_[client->GetId()] = client; |
148 | this->clients_[client->GetId()] = client; |
| 157 | - | ||
| 158 | this->mutex_clients_.unlock(); |
- | |
| 159 | 149 | ||
| 160 | return client->GetServerId(); |
150 | return client->GetServerId(); |
| 161 | } |
151 | } |
| 162 | 152 | ||
| 163 | 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) |
| 164 | { |
154 | { |
| 165 | Client::Pointer client; |
155 | Client::Pointer client; |
| 166 | - | ||
| 167 | this->mutex_clients_.lock(); |
- | |
| 168 | 156 | ||
| 169 | switch (protocol) |
157 | switch (protocol) |
| 170 | { |
158 | { |
| 171 | case PROTOCOL_TCP: |
159 | case PROTOCOL_TCP: |
| 172 | { |
160 | { |
| 173 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0)); |
161 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 174 | break; |
162 | break; |
| 175 | } |
163 | } |
| 176 | case PROTOCOL_UDP: |
164 | case PROTOCOL_UDP: |
| 177 | { |
165 | { |
| 178 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0)); |
166 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 179 | break; |
167 | break; |
| 180 | } |
168 | } |
| 181 | case PROTOCOL_SERIAL: |
169 | case PROTOCOL_SERIAL: |
| 182 | { |
170 | { |
| 183 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0)); |
171 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 184 | break; |
172 | break; |
| 185 | } |
173 | } |
| 186 | default: |
174 | default: |
| 187 | { |
175 | { |
| 188 | this->mutex_clients_.unlock(); |
- | |
| 189 | throw std::runtime_error("Invalid protocol specified!"); |
176 | throw std::runtime_error("Invalid protocol specified!"); |
| 190 | return 0; |
177 | return 0; |
| 191 | } |
178 | } |
| 192 | } |
179 | } |
| 193 | 180 | ||
| Line 195... | Line 182... | ||
| 195 | 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_)); |
| 196 | 183 | ||
| 197 | try |
184 | try |
| 198 | { |
185 | { |
| 199 | client->Connect(address, port_or_baud); |
186 | client->Connect(address, port_or_baud); |
| 200 | } |
187 | } |
| 201 | catch (std::exception e) |
188 | catch (std::exception& e) |
| 202 | { |
189 | { |
| 203 |
|
190 | throw std::runtime_error(e.what()); |
| 204 | throw e; |
- | |
| 205 | } |
191 | } |
| 206 | 192 | ||
| 207 | this->clients_[client->GetId()] = client; |
193 | this->clients_[client->GetId()] = client; |
| 208 | 194 | ||
| 209 | this->mutex_clients_.unlock(); |
- | |
| 210 | - | ||
| 211 | return client->GetId(); |
195 | return client->GetId(); |
| 212 | } |
196 | } |
| 213 | 197 | ||
| 214 | void Manager::SendToAll(ServerId server_id, type::Byteset data) |
198 | void Manager::SendToAll(ServerId server_id, type::Byteset data) |
| 215 | { |
199 | { |
| 216 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
200 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
| 217 | } |
201 | } |
| 218 | 202 | ||
| 219 | void Manager::SendTo(ClientId client_id, type::Byteset data) |
203 | void Manager::SendTo(ClientId client_id, type::Byteset data) |
| 220 | { |
204 | { |
| 221 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
205 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
| 222 | } |
206 | } |
| 223 | 207 | ||
| 224 | void Manager::StopServer(ServerId server_id) |
208 | void Manager::StopServer(ServerId server_id) |
| 225 | { |
209 | { |
| 226 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
210 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
| 227 | } |
211 | } |
| 228 | 212 | ||
| 229 | void Manager::Disconnect(ClientId client_id) |
213 | void Manager::Disconnect(ClientId client_id) |
| 230 | { |
214 | { |
| 231 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
215 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
| 232 | } |
216 | } |
| 233 | 217 | ||
| 234 | void Manager::SendToAllHandler(ServerId server_id, type::Byteset data) |
218 | void Manager::SendToAllHandler(ServerId server_id, type::Byteset data) |
| 235 | { |
219 | { |
| 236 | this->mutex_clients_.lock(); |
- | |
| 237 | - | ||
| 238 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
220 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 239 | { |
221 | { |
| 240 | if (it->second->GetServerId() == server_id) |
222 | if (it->second->GetServerId() == server_id) |
| 241 | { |
223 | { |
| 242 | it->second->Send(data); |
224 | it->second->Send(data); |
| 243 | } |
225 | } |
| 244 | } |
226 | } |
| 245 | - | ||
| 246 | this->mutex_clients_.unlock(); |
- | |
| 247 | } |
227 | } |
| 248 | 228 | ||
| 249 | void Manager::SendToHandler(ClientId client_id, type::Byteset data) |
229 | void Manager::SendToHandler(ClientId client_id, type::Byteset data) |
| 250 | { |
230 | { |
| 251 | this->mutex_clients_.lock(); |
- | |
| 252 | - | ||
| 253 | ClientList::iterator it = this->clients_.find(client_id); |
231 | ClientList::iterator it = this->clients_.find(client_id); |
| 254 | 232 | ||
| 255 | if (it != this->clients_.end()) |
233 | if (it != this->clients_.end()) |
| 256 | { |
234 | { |
| 257 | it->second->Send(data); |
235 | it->second->Send(data); |
| 258 | } |
236 | } |
| 259 | - | ||
| 260 | this->mutex_clients_.unlock(); |
- | |
| 261 | } |
237 | } |
| 262 | 238 | ||
| 263 | void Manager::StopServerHandler(ServerId server_id) |
239 | void Manager::StopServerHandler(ServerId server_id) |
| 264 | { |
240 | { |
| 265 | this->mutex_clients_.lock(); |
- | |
| 266 | - | ||
| 267 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
241 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 268 | { |
242 | { |
| 269 | if (it->second->GetServerId() == server_id) |
243 | if (it->second->GetServerId() == server_id) |
| 270 | { |
244 | { |
| 271 | this->mutex_clients_.unlock(); |
- | |
| 272 | it->second->Disconnect(); |
245 | it->second->Disconnect(); |
| 273 | this->mutex_clients_.lock(); |
- | |
| 274 | } |
246 | } |
| 275 | } |
247 | } |
| 276 | - | ||
| 277 | this->mutex_clients_.unlock(); |
- | |
| 278 | } |
248 | } |
| 279 | 249 | ||
| 280 | void Manager::DisconnectHandler(ClientId client_id) |
250 | void Manager::DisconnectHandler(ClientId client_id) |
| 281 | { |
251 | { |
| 282 | this->mutex_clients_.lock(); |
- | |
| 283 | - | ||
| 284 | ClientList::iterator it = this->clients_.find(client_id); |
252 | ClientList::iterator it = this->clients_.find(client_id); |
| 285 | - | ||
| 286 | this->mutex_clients_.unlock(); |
- | |
| 287 | 253 | ||
| 288 | if (it != this->clients_.end()) |
254 | if (it != this->clients_.end()) |
| 289 | { |
255 | { |
| 290 | it->second->Disconnect(); |
256 | it->second->Disconnect(); |
| 291 | } |
257 | } |