Rev 1939 | Rev 1962 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1939 | Rev 1959 | ||
|---|---|---|---|
| Line 64... | Line 64... | ||
| 64 | { |
64 | { |
| 65 | this->signal_on_new_state_.connect(slot_on_new_state); |
65 | this->signal_on_new_state_.connect(slot_on_new_state); |
| 66 | this->signal_on_new_data_.connect(slot_on_new_data); |
66 | this->signal_on_new_data_.connect(slot_on_new_data); |
| 67 | } |
67 | } |
| 68 | 68 | ||
| 69 | void Manager::SlotOnNewState( |
69 | void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state) |
| 70 | { |
70 | { |
| 71 | if (client_state == CLIENT_STATE_DISCONNECTED) |
71 | if (client_state == CLIENT_STATE_DISCONNECTED) |
| 72 | { |
72 | { |
| 73 | this->clients_.erase(client_id); |
73 | this->clients_.erase(client_id); |
| 74 | } |
74 | } |
| Line 76... | Line 76... | ||
| 76 | { |
76 | { |
| 77 | ClientList::iterator it = this->clients_.find(client_id); |
77 | ClientList::iterator it = this->clients_.find(client_id); |
| 78 | 78 | ||
| 79 | if (it != this->clients_.end()) |
79 | if (it != this->clients_.end()) |
| 80 | { |
80 | { |
| 81 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this-> |
81 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), it->second->GetServerId())); |
| 82 | 82 | ||
| 83 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
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_)); |
84 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 85 | 85 | ||
| 86 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
86 | client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor()); |
| Line 97... | Line 97... | ||
| 97 | } |
97 | } |
| 98 | 98 | ||
| 99 | this->signal_on_new_state_(client_id, server_id, client_state); |
99 | this->signal_on_new_state_(client_id, server_id, client_state); |
| 100 | } |
100 | } |
| 101 | 101 | ||
| 102 | void Manager::SlotOnNewData( |
102 | void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data) |
| 103 | { |
103 | { |
| 104 | this->signal_on_new_data_(client_id, server_id, data); |
104 | this->signal_on_new_data_(client_id, server_id, data); |
| 105 | } |
105 | } |
| 106 | 106 | ||
| 107 |
|
107 | SocketId Manager::GetFreeSocketId() |
| 108 | { |
108 | { |
| 109 |
|
109 | SocketId id = 1; |
| 110 | 110 | ||
| 111 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
111 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 112 | { |
112 | { |
| 113 | if (it->second->GetServerId() != |
113 | if (it->second->GetServerId() != id && this->clients_.find(id) == this->clients_.end()) |
| 114 | { |
114 | { |
| 115 | return |
115 | return id; |
| 116 | } |
116 | } |
| 117 | 117 | ||
| 118 |
|
118 | id++; |
| 119 | } |
119 | } |
| 120 | 120 | ||
| 121 | return |
121 | return id; |
| 122 | } |
122 | } |
| 123 | 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 |
|
124 | SocketId Manager::StartServer(Protocol protocol, unsigned int port) |
| 137 | { |
125 | { |
| 138 | if (protocol != PROTOCOL_TCP) |
126 | if (protocol != PROTOCOL_TCP) |
| 139 | { |
127 | { |
| 140 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
128 | throw std::runtime_error("Can not start server for the Serial or UDP protocol!"); |
| 141 | return 0; |
129 | return 0; |
| 142 | } |
130 | } |
| 143 | 131 | ||
| 144 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this-> |
132 | TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), this->GetFreeSocketId())); |
| 145 | 133 | ||
| 146 | client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_), |
134 | 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_)); |
135 | Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_)); |
| 148 | 136 | ||
| 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))); |
137 | 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))); |
| Line 153... | Line 141... | ||
| 153 | this->clients_[client->GetId()] = client; |
141 | this->clients_[client->GetId()] = client; |
| 154 | 142 | ||
| 155 | return client->GetServerId(); |
143 | return client->GetServerId(); |
| 156 | } |
144 | } |
| 157 | 145 | ||
| 158 |
|
146 | SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud) |
| 159 | { |
147 | { |
| 160 | Client::Pointer client; |
148 | Client::Pointer client; |
| 161 | 149 | ||
| 162 | switch (protocol) |
150 | switch (protocol) |
| 163 | { |
151 | { |
| 164 | case PROTOCOL_TCP: |
152 | case PROTOCOL_TCP: |
| 165 | { |
153 | { |
| 166 | client = Client::Pointer(new TcpClient(this->io_service_, this-> |
154 | client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0)); |
| 167 | break; |
155 | break; |
| 168 | } |
156 | } |
| 169 | case PROTOCOL_UDP: |
157 | case PROTOCOL_UDP: |
| 170 | { |
158 | { |
| 171 | client = Client::Pointer(new UdpClient(this->io_service_, this-> |
159 | client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0)); |
| 172 | break; |
160 | break; |
| 173 | } |
161 | } |
| 174 | case PROTOCOL_SERIAL: |
162 | case PROTOCOL_SERIAL: |
| 175 | { |
163 | { |
| 176 | client = Client::Pointer(new SerialClient(this->io_service_, this-> |
164 | client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0)); |
| 177 | break; |
165 | break; |
| 178 | } |
166 | } |
| 179 | default: |
167 | default: |
| 180 | { |
168 | { |
| 181 | throw std::runtime_error("Invalid protocol specified!"); |
169 | throw std::runtime_error("Invalid protocol specified!"); |
| Line 191... | Line 179... | ||
| 191 | this->clients_[client->GetId()] = client; |
179 | this->clients_[client->GetId()] = client; |
| 192 | 180 | ||
| 193 | return client->GetId(); |
181 | return client->GetId(); |
| 194 | } |
182 | } |
| 195 | 183 | ||
| 196 | void Manager::SendToAll( |
184 | void Manager::SendToAll(SocketId server_id, common::Byteset data) |
| 197 | { |
185 | { |
| 198 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
186 | this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data)); |
| 199 | } |
187 | } |
| 200 | 188 | ||
| 201 | void Manager::SendTo( |
189 | void Manager::SendTo(SocketId client_id, common::Byteset data) |
| 202 | { |
190 | { |
| 203 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
191 | this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data)); |
| 204 | } |
192 | } |
| 205 | 193 | ||
| 206 | void Manager::StopServer( |
194 | void Manager::StopServer(SocketId server_id) |
| 207 | { |
195 | { |
| 208 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
196 | this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id)); |
| 209 | } |
197 | } |
| 210 | 198 | ||
| 211 | void Manager::Disconnect( |
199 | void Manager::Disconnect(SocketId client_id) |
| 212 | { |
200 | { |
| 213 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
201 | this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id)); |
| 214 | } |
202 | } |
| 215 | 203 | ||
| 216 | void Manager::SendToAllHandler( |
204 | void Manager::SendToAllHandler(SocketId server_id, common::Byteset data) |
| 217 | { |
205 | { |
| 218 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
206 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 219 | { |
207 | { |
| 220 | if (it->second->GetServerId() == server_id) |
208 | if (it->second->GetServerId() == server_id) |
| 221 | { |
209 | { |
| 222 | it->second->Send(data); |
210 | it->second->Send(data); |
| 223 | } |
211 | } |
| 224 | } |
212 | } |
| 225 | } |
213 | } |
| 226 | 214 | ||
| 227 | void Manager::SendToHandler( |
215 | void Manager::SendToHandler(SocketId client_id, common::Byteset data) |
| 228 | { |
216 | { |
| 229 | ClientList::iterator it = this->clients_.find(client_id); |
217 | ClientList::iterator it = this->clients_.find(client_id); |
| 230 | 218 | ||
| 231 | if (it != this->clients_.end()) |
219 | if (it != this->clients_.end()) |
| 232 | { |
220 | { |
| 233 | it->second->Send(data); |
221 | it->second->Send(data); |
| 234 | } |
222 | } |
| 235 | } |
223 | } |
| 236 | 224 | ||
| 237 | void Manager::StopServerHandler( |
225 | void Manager::StopServerHandler(SocketId server_id) |
| 238 | { |
226 | { |
| 239 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
227 | for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++) |
| 240 | { |
228 | { |
| 241 | if (it->second->GetServerId() == server_id) |
229 | if (it->second->GetServerId() == server_id) |
| 242 | { |
230 | { |
| 243 | it->second->Stop(); |
231 | it->second->Stop(); |
| 244 | } |
232 | } |
| 245 | } |
233 | } |
| 246 | } |
234 | } |
| 247 | 235 | ||
| 248 | void Manager::DisconnectHandler( |
236 | void Manager::DisconnectHandler(SocketId client_id) |
| 249 | { |
237 | { |
| 250 | ClientList::iterator it = this->clients_.find(client_id); |
238 | ClientList::iterator it = this->clients_.find(client_id); |
| 251 | 239 | ||
| 252 | if (it != this->clients_.end()) |
240 | if (it != this->clients_.end()) |
| 253 | { |
241 | { |