Rev 1898 | Rev 1959 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1592 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 5 | * This program is free software; you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU General Public License along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "Manager.h" |
||
| 22 | |||
| 1596 | runge | 23 | #include <iostream> |
| 24 | |||
| 1592 | runge | 25 | #include <boost/lexical_cast.hpp> |
| 26 | #include <boost/bind.hpp> |
||
| 1594 | runge | 27 | #include <boost/cast.hpp> |
| 1592 | runge | 28 | |
| 29 | namespace atom { |
||
| 30 | namespace net { |
||
| 1593 | runge | 31 | |
| 1599 | runge | 32 | Manager::Pointer Manager::instance_; |
| 1898 | runge | 33 | |
| 34 | logging::Logger Manager::LOG("net::Manager"); |
||
| 35 | |||
| 1601 | runge | 36 | Manager::Manager() |
| 1592 | runge | 37 | { |
| 1627 | runge | 38 | |
| 1592 | runge | 39 | } |
| 40 | |||
| 41 | Manager::~Manager() |
||
| 42 | { |
||
| 1939 | runge | 43 | this->Stop(); |
| 44 | |||
| 1592 | runge | 45 | this->clients_.clear(); |
| 46 | } |
||
| 47 | |||
| 48 | Manager::Pointer Manager::Instance() |
||
| 49 | { |
||
| 50 | return Manager::instance_; |
||
| 51 | } |
||
| 52 | |||
| 1599 | runge | 53 | void Manager::Create() |
| 54 | { |
||
| 55 | Manager::instance_ = Manager::Pointer(new Manager()); |
||
| 56 | } |
||
| 57 | |||
| 1592 | runge | 58 | void Manager::Delete() |
| 59 | { |
||
| 60 | Manager::instance_.reset(); |
||
| 61 | } |
||
| 62 | |||
| 63 | void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
||
| 64 | { |
||
| 65 | this->signal_on_new_state_.connect(slot_on_new_state); |
||
| 66 | this->signal_on_new_data_.connect(slot_on_new_data); |
||
| 67 | } |
||
| 68 | |||
| 1593 | runge | 69 | void Manager::SlotOnNewState(ClientId client_id, ServerId server_id, ClientState client_state) |
| 1592 | runge | 70 | { |
| 71 | if (client_state == CLIENT_STATE_DISCONNECTED) |
||
| 72 | { |
||
| 73 | this->clients_.erase(client_id); |
||
| 74 | } |
||
| 75 | else if (client_state == CLIENT_STATE_ACCEPTED) |
||
| 76 | { |
||
| 77 | ClientList::iterator it = this->clients_.find(client_id); |
||
| 78 | |||
| 79 | if (it != this->clients_.end()) |
||
| 80 | { |
||
| 81 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), it->second->GetServerId())); |
||
| 82 | |||
| 1593 | runge | 83 | 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_)); |
||
| 1592 | runge | 85 | |
| 1594 | runge | 86 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
| 1592 | runge | 87 | |
| 88 | this->clients_[client->GetId()] = client; |
||
| 89 | |||
| 1593 | runge | 90 | this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED); |
| 1592 | runge | 91 | return; |
| 92 | } |
||
| 93 | else |
||
| 94 | { |
||
| 95 | throw std::runtime_error("Accepted unknown client!"); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 1593 | runge | 99 | this->signal_on_new_state_(client_id, server_id, client_state); |
| 1592 | runge | 100 | } |
| 101 | |||
| 1642 | runge | 102 | void Manager::SlotOnNewData(ClientId client_id, ServerId server_id, common::Byteset data) |
| 1592 | runge | 103 | { |
| 1593 | runge | 104 | this->signal_on_new_data_(client_id, server_id, data); |
| 1592 | runge | 105 | } |
| 106 | |||
| 107 | ServerId Manager::GetFreeServerId() |
||
| 108 | { |
||
| 109 | ServerId server_id = 1; |
||
| 110 | |||
| 111 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
||
| 112 | { |
||
| 113 | if (it->second->GetServerId() != server_id) |
||
| 114 | { |
||
| 115 | return server_id; |
||
| 116 | } |
||
| 117 | |||
| 118 | server_id++; |
||
| 119 | } |
||
| 120 | |||
| 1596 | runge | 121 | return server_id; |
| 1592 | runge | 122 | } |
| 123 | |||
| 124 | ClientId Manager::GetFreeClientId() |
||
| 125 | { |
||
| 126 | ClientId client_id = 1; |
||
| 127 | |||
| 128 | while (this->clients_.find(client_id) != this->clients_.end()) |
||
| 129 | { |
||
| 130 | client_id++; |
||
| 131 | } |
||
| 132 | |||
| 133 | return client_id; |
||
| 134 | } |
||
| 135 | |||
| 136 | ServerId Manager::StartServer(Protocol protocol, unsigned int port) |
||
| 137 | { |
||
| 138 | if (protocol != PROTOCOL_TCP) |
||
| 139 | { |
||
| 140 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
||
| 141 | return 0; |
||
| 142 | } |
||
| 143 | |||
| 144 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), this->GetFreeServerId())); |
||
| 145 | |||
| 1593 | runge | 146 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 147 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
||
| 1592 | runge | 148 | |
| 149 | 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))); |
||
| 150 | |||
| 151 | client->Accept(acceptor); |
||
| 152 | |||
| 153 | this->clients_[client->GetId()] = client; |
||
| 154 | |||
| 155 | return client->GetServerId(); |
||
| 156 | } |
||
| 157 | |||
| 158 | ClientId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
||
| 159 | { |
||
| 160 | Client::Pointer client; |
||
| 161 | |||
| 162 | switch (protocol) |
||
| 163 | { |
||
| 164 | case PROTOCOL_TCP: |
||
| 165 | { |
||
| 166 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0)); |
||
| 167 | break; |
||
| 168 | } |
||
| 169 | case PROTOCOL_UDP: |
||
| 170 | { |
||
| 171 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0)); |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | case PROTOCOL_SERIAL: |
||
| 175 | { |
||
| 176 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0)); |
||
| 177 | break; |
||
| 178 | } |
||
| 179 | default: |
||
| 180 | { |
||
| 181 | throw std::runtime_error("Invalid protocol specified!"); |
||
| 182 | return 0; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 1593 | runge | 186 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
| 187 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
||
| 1592 | runge | 188 | |
| 1606 | runge | 189 | client->Connect(address, port_or_baud); |
| 1592 | runge | 190 | |
| 191 | this->clients_[client->GetId()] = client; |
||
| 192 | |||
| 193 | return client->GetId(); |
||
| 194 | } |
||
| 195 | |||
| 1642 | runge | 196 | void Manager::SendToAll(ServerId server_id, common::Byteset data) |
| 1592 | runge | 197 | { |
| 198 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
||
| 199 | } |
||
| 200 | |||
| 1642 | runge | 201 | void Manager::SendTo(ClientId client_id, common::Byteset data) |
| 1592 | runge | 202 | { |
| 203 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
||
| 204 | } |
||
| 205 | |||
| 206 | void Manager::StopServer(ServerId server_id) |
||
| 207 | { |
||
| 208 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
||
| 209 | } |
||
| 210 | |||
| 211 | void Manager::Disconnect(ClientId client_id) |
||
| 212 | { |
||
| 213 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
||
| 214 | } |
||
| 215 | |||
| 1642 | runge | 216 | void Manager::SendToAllHandler(ServerId server_id, common::Byteset data) |
| 1592 | runge | 217 | { |
| 218 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
||
| 219 | { |
||
| 220 | if (it->second->GetServerId() == server_id) |
||
| 221 | { |
||
| 222 | it->second->Send(data); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 1642 | runge | 227 | void Manager::SendToHandler(ClientId client_id, common::Byteset data) |
| 1592 | runge | 228 | { |
| 229 | ClientList::iterator it = this->clients_.find(client_id); |
||
| 1599 | runge | 230 | |
| 1592 | runge | 231 | if (it != this->clients_.end()) |
| 232 | { |
||
| 233 | it->second->Send(data); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | void Manager::StopServerHandler(ServerId server_id) |
||
| 238 | { |
||
| 239 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
||
| 240 | { |
||
| 241 | if (it->second->GetServerId() == server_id) |
||
| 242 | { |
||
| 1599 | runge | 243 | it->second->Stop(); |
| 1592 | runge | 244 | } |
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | void Manager::DisconnectHandler(ClientId client_id) |
||
| 249 | { |
||
| 250 | ClientList::iterator it = this->clients_.find(client_id); |
||
| 251 | |||
| 252 | if (it != this->clients_.end()) |
||
| 253 | { |
||
| 254 | it->second->Disconnect(); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | }; // namespace net |
||
| 259 | }; // namespace atom |