Rev 1595 | Rev 1597 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1595 | Rev 1596 | ||
|---|---|---|---|
| Line 17... | Line 17... | ||
| 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 | ||
| - | 23 | #include <iostream> |
|
| 22 | 24 | ||
| 23 | #include <boost/lexical_cast.hpp> |
25 | #include <boost/lexical_cast.hpp> |
| 24 | #include <boost/bind.hpp> |
26 | #include <boost/bind.hpp> |
| 25 | #include <boost/cast.hpp> |
27 | #include <boost/cast.hpp> |
| 26 | 28 | ||
| Line 42... | Line 44... | ||
| 42 | this->thread_.interrupt(); |
44 | this->thread_.interrupt(); |
| 43 | this->thread_.join(); |
45 | this->thread_.join(); |
| 44 | } |
46 | } |
| 45 | 47 | ||
| 46 | Manager::Pointer Manager::Instance() |
48 | Manager::Pointer Manager::Instance() |
| 47 | { |
49 | { |
| 48 | return Manager::instance_; |
50 | return Manager::instance_; |
| 49 | } |
51 | } |
| 50 | 52 | ||
| 51 | void Manager::Delete() |
53 | void Manager::Delete() |
| 52 | { |
54 | { |
| Line 66... | Line 68... | ||
| 66 | this->mutex_clients_.lock(); |
68 | this->mutex_clients_.lock(); |
| 67 | this->clients_.erase(client_id); |
69 | this->clients_.erase(client_id); |
| 68 | this->mutex_clients_.unlock(); |
70 | this->mutex_clients_.unlock(); |
| 69 | } |
71 | } |
| 70 | else if (client_state == CLIENT_STATE_ACCEPTED) |
72 | else if (client_state == CLIENT_STATE_ACCEPTED) |
| 71 | { |
73 | { |
| 72 | this->mutex_clients_.lock(); |
74 | this->mutex_clients_.lock(); |
| 73 | ClientList::iterator it = this->clients_.find(client_id); |
75 | ClientList::iterator it = this->clients_.find(client_id); |
| 74 | 76 | ||
| 75 | if (it != this->clients_.end()) |
77 | if (it != this->clients_.end()) |
| 76 | { |
78 | { |
| 77 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), it->second->GetServerId())); |
79 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), it->second->GetServerId())); |
| 78 | 80 | ||
| 79 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
81 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 80 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
82 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 81 | 83 | ||
| 82 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
84 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
| 83 | 85 | ||
| 84 | this->clients_[client->GetId()] = client; |
86 | this->clients_[client->GetId()] = client; |
| 85 | 87 | ||
| 86 | this->mutex_clients_.unlock(); |
88 | this->mutex_clients_.unlock(); |
| 87 | 89 | ||
| 88 | this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED); |
90 | this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED); |
| 89 | return; |
91 | return; |
| 90 | } |
92 | } |
| 91 | else |
93 | else |
| 92 | { |
94 | { |
| 93 | this->mutex_clients_.unlock(); |
95 | this->mutex_clients_.unlock(); |
| 94 | throw std::runtime_error("Accepted unknown client!"); |
96 | throw std::runtime_error("Accepted unknown client!"); |
| 95 | } |
97 | } |
| 96 | } |
98 | } |
| 97 | 99 | ||
| 98 | this->signal_on_new_state_(client_id, server_id, client_state); |
100 | this->signal_on_new_state_(client_id, server_id, client_state); |
| 99 | } |
101 | } |
| 100 | 102 | ||
| 101 | void Manager::SlotOnNewData(ClientId client_id, ServerId server_id, |
103 | void Manager::SlotOnNewData(ClientId client_id, ServerId server_id, type::Byteset data) |
| 102 | { |
104 | { |
| 103 | this->signal_on_new_data_(client_id, server_id, data); |
105 | this->signal_on_new_data_(client_id, server_id, data); |
| 104 | } |
106 | } |
| 105 | 107 | ||
| 106 | ServerId Manager::GetFreeServerId() |
108 | ServerId Manager::GetFreeServerId() |
| Line 115... | Line 117... | ||
| 115 | } |
117 | } |
| 116 | 118 | ||
| 117 | server_id++; |
119 | server_id++; |
| 118 | } |
120 | } |
| 119 | 121 | ||
| 120 | return |
122 | return server_id; |
| 121 | } |
123 | } |
| 122 | 124 | ||
| 123 | ClientId Manager::GetFreeClientId() |
125 | ClientId Manager::GetFreeClientId() |
| 124 | { |
126 | { |
| 125 | ClientId client_id = 1; |
127 | ClientId client_id = 1; |
| Line 207... | Line 209... | ||
| 207 | this->mutex_clients_.unlock(); |
209 | this->mutex_clients_.unlock(); |
| 208 | 210 | ||
| 209 | return client->GetId(); |
211 | return client->GetId(); |
| 210 | } |
212 | } |
| 211 | 213 | ||
| 212 | void Manager::SendToAll(ServerId server_id, |
214 | void Manager::SendToAll(ServerId server_id, type::Byteset data) |
| 213 | { |
215 | { |
| 214 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
216 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
| 215 | } |
217 | } |
| 216 | 218 | ||
| 217 | void Manager::SendTo(ClientId client_id, |
219 | void Manager::SendTo(ClientId client_id, type::Byteset data) |
| 218 | { |
220 | { |
| 219 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
221 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
| 220 | } |
222 | } |
| 221 | 223 | ||
| 222 | void Manager::StopServer(ServerId server_id) |
224 | void Manager::StopServer(ServerId server_id) |
| Line 227... | Line 229... | ||
| 227 | void Manager::Disconnect(ClientId client_id) |
229 | void Manager::Disconnect(ClientId client_id) |
| 228 | { |
230 | { |
| 229 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
231 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
| 230 | } |
232 | } |
| 231 | 233 | ||
| 232 | void Manager::SendToAllHandler(ServerId server_id, |
234 | void Manager::SendToAllHandler(ServerId server_id, type::Byteset data) |
| 233 | { |
235 | { |
| 234 | this->mutex_clients_.lock(); |
236 | this->mutex_clients_.lock(); |
| 235 | 237 | ||
| 236 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
238 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 237 | { |
239 | { |
| Line 242... | Line 244... | ||
| 242 | } |
244 | } |
| 243 | 245 | ||
| 244 | this->mutex_clients_.unlock(); |
246 | this->mutex_clients_.unlock(); |
| 245 | } |
247 | } |
| 246 | 248 | ||
| 247 | void Manager::SendToHandler(ClientId client_id, |
249 | void Manager::SendToHandler(ClientId client_id, type::Byteset data) |
| 248 | { |
250 | { |
| 249 | this->mutex_clients_.lock(); |
251 | this->mutex_clients_.lock(); |
| 250 | 252 | ||
| 251 | ClientList::iterator it = this->clients_.find(client_id); |
253 | ClientList::iterator it = this->clients_.find(client_id); |
| 252 | 254 | ||