Rev 1597 | Rev 1601 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1597 | Rev 1599 | ||
|---|---|---|---|
| Line 27... | Line 27... | ||
| 27 | #include <boost/cast.hpp> |
27 | #include <boost/cast.hpp> |
| 28 | 28 | ||
| 29 | namespace atom { |
29 | namespace atom { |
| 30 | namespace net { |
30 | namespace net { |
| 31 | 31 | ||
| 32 | Manager::Pointer Manager::instance_ |
32 | Manager::Pointer Manager::instance_; |
| 33 | 33 | ||
| 34 | Manager::Manager() : io_service_work_(io_service_) |
34 | Manager::Manager() : io_service_work_(io_service_) |
| 35 | { |
35 | { |
| 36 | boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_)); |
36 | boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_)); |
| 37 | this->thread_ = thread.move(); |
37 | this->thread_ = thread.move(); |
| 38 | } |
38 | } |
| 39 | 39 | ||
| 40 | Manager::~Manager() |
40 | Manager::~Manager() |
| 41 | { |
41 | { |
| 42 | this->clients_.clear(); |
42 | this->clients_.clear(); |
| - | 43 | ||
| - | 44 | this->io_service_.stop(); |
|
| 43 | 45 | ||
| 44 | this->thread_.interrupt(); |
46 | this->thread_.interrupt(); |
| 45 | this->thread_.join(); |
47 | this->thread_.join(); |
| 46 | } |
48 | } |
| 47 | 49 | ||
| 48 | Manager::Pointer Manager::Instance() |
50 | Manager::Pointer Manager::Instance() |
| - | 51 | { |
|
| - | 52 | return Manager::instance_; |
|
| - | 53 | } |
|
| - | 54 | ||
| - | 55 | void Manager::Create() |
|
| 49 | { |
56 | { |
| 50 |
|
57 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 51 | } |
58 | } |
| 52 | 59 | ||
| 53 | void Manager::Delete() |
60 | void Manager::Delete() |
| 54 | { |
61 | { |
| 55 | Manager::instance_.reset(); |
62 | Manager::instance_.reset(); |
| Line 119... | Line 126... | ||
| 119 | ClientId Manager::GetFreeClientId() |
126 | ClientId Manager::GetFreeClientId() |
| 120 | { |
127 | { |
| 121 | ClientId client_id = 1; |
128 | ClientId client_id = 1; |
| 122 | 129 | ||
| 123 | while (this->clients_.find(client_id) != this->clients_.end()) |
130 | while (this->clients_.find(client_id) != this->clients_.end()) |
| 124 | { |
131 | { |
| 125 | client_id++; |
132 | client_id++; |
| 126 | } |
133 | } |
| 127 | 134 | ||
| 128 | return client_id; |
135 | return client_id; |
| 129 | } |
136 | } |
| Line 133... | Line 140... | ||
| 133 | if (protocol != PROTOCOL_TCP) |
140 | if (protocol != PROTOCOL_TCP) |
| 134 | { |
141 | { |
| 135 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
142 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
| 136 | return 0; |
143 | return 0; |
| 137 | } |
144 | } |
| 138 | 145 | ||
| 139 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), this->GetFreeServerId())); |
146 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), this->GetFreeServerId())); |
| 140 | 147 | ||
| 141 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
148 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 142 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
149 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 143 | 150 | ||
| 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))); |
151 | 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))); |
| 145 | 152 | ||
| 146 | client->Accept(acceptor); |
153 | client->Accept(acceptor); |
| 147 | 154 | ||
| 148 | this->clients_[client->GetId()] = client; |
155 | this->clients_[client->GetId()] = client; |
| 149 | 156 | ||
| 150 | return client->GetServerId(); |
157 | return client->GetServerId(); |
| 151 | } |
158 | } |
| 152 | 159 | ||
| 153 | ClientId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
160 | ClientId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
| 154 | { |
161 | { |
| 155 | Client::Pointer client; |
162 | Client::Pointer client; |
| 156 | 163 | ||
| 157 | switch (protocol) |
164 | switch (protocol) |
| 158 | { |
165 | { |
| 159 | case PROTOCOL_TCP: |
166 | case PROTOCOL_TCP: |
| 160 | { |
167 | { |
| 161 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0)); |
168 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 162 | break; |
169 | break; |
| 163 | } |
170 | } |
| 164 | case PROTOCOL_UDP: |
171 | case PROTOCOL_UDP: |
| 165 | { |
172 | { |
| 166 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0)); |
173 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 167 | break; |
174 | break; |
| 168 | } |
175 | } |
| 169 | case PROTOCOL_SERIAL: |
176 | case PROTOCOL_SERIAL: |
| 170 | { |
177 | { |
| 171 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0)); |
178 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0)); |
| 172 | break; |
179 | break; |
| 173 | } |
180 | } |
| 174 | default: |
181 | default: |
| 175 | { |
182 | { |
| 176 | throw std::runtime_error("Invalid protocol specified!"); |
183 | throw std::runtime_error("Invalid protocol specified!"); |
| 177 | return 0; |
184 | return 0; |
| 178 | } |
185 | } |
| 179 | } |
186 | } |
| 180 | 187 | ||
| 181 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
188 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 182 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
189 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 183 | 190 | ||
| 184 | try |
191 | try |
| Line 225... | Line 232... | ||
| 225 | } |
232 | } |
| 226 | } |
233 | } |
| 227 | } |
234 | } |
| 228 | 235 | ||
| 229 | void Manager::SendToHandler(ClientId client_id, type::Byteset data) |
236 | void Manager::SendToHandler(ClientId client_id, type::Byteset data) |
| 230 | { |
237 | { |
| 231 | ClientList::iterator it = this->clients_.find(client_id); |
238 | ClientList::iterator it = this->clients_.find(client_id); |
| 232 | 239 | ||
| 233 | if (it != this->clients_.end()) |
240 | if (it != this->clients_.end()) |
| 234 | { |
241 | { |
| 235 | it->second->Send(data); |
242 | it->second->Send(data); |
| 236 | } |
243 | } |
| 237 | } |
244 | } |
| 238 | 245 | ||
| 239 | void Manager::StopServerHandler(ServerId server_id) |
246 | void Manager::StopServerHandler(ServerId server_id) |
| 240 | { |
247 | { |
| 241 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
248 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 242 | { |
249 | { |
| 243 | if (it->second->GetServerId() == server_id) |
250 | if (it->second->GetServerId() == server_id) |
| 244 | { |
251 | { |
| 245 | it->second-> |
252 | it->second->Stop(); |
| 246 | } |
253 | } |
| 247 | } |
- | |
| 248 | } |
254 | } |
| - | 255 | } |
|
| 249 | 256 | ||
| 250 | void Manager::DisconnectHandler(ClientId client_id) |
257 | void Manager::DisconnectHandler(ClientId client_id) |
| 251 | { |
258 | { |
| 252 | ClientList::iterator it = this->clients_.find(client_id); |
259 | ClientList::iterator it = this->clients_.find(client_id); |
| 253 | 260 | ||
| 254 | if (it != this->clients_.end()) |
261 | if (it != this->clients_.end()) |
| 255 | { |
262 | { |
| 256 | it->second->Disconnect(); |
263 | it->second->Disconnect(); |
| 257 | } |
264 | } |
| 258 | - | ||
| 259 | - | ||
| 260 | } |
265 | } |
| 261 | 266 | ||
| 262 | }; // namespace net |
267 | }; // namespace net |
| 263 | }; // namespace atom |
268 | }; // namespace atom |