Subversion Repositories HomeAutomation

Rev

Rev 1971 | Rev 1987 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1971 Rev 1986
Line 78... Line 78...
78
  Manager::instance_.reset();
78
  Manager::instance_.reset();
79
 
79
 
80
  LOG_DEBUG_EXIT;
80
  LOG_DEBUG_EXIT;
81
}
81
}
82
 
82
 
83
void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data)
83
void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewClient::slot_type& slot_on_new_client, const SignalOnNewData::slot_type& slot_on_new_data)
84
{
84
{
85
  LOG_DEBUG_ENTER;
85
  LOG_DEBUG_ENTER;
86
 
86
 
87
  this->signal_on_new_state_.connect(slot_on_new_state);
87
  this->signal_on_new_state_.connect(slot_on_new_state);
-
 
88
  this->signal_on_new_client_.connect(slot_on_new_client);
88
  this->signal_on_new_data_.connect(slot_on_new_data);
89
  this->signal_on_new_data_.connect(slot_on_new_data);
89
 
90
 
90
  LOG_DEBUG_EXIT;
91
  LOG_DEBUG_EXIT;
91
}
92
}
92
 
93
 
Line 99... Line 100...
99
  if (client_state == CLIENT_STATE_DISCONNECTED)
100
  if (client_state == CLIENT_STATE_DISCONNECTED)
100
  {
101
  {
101
    this->clients_.erase(client_id);
102
    this->clients_.erase(client_id);
102
  }
103
  }
103
 
104
 
104
  this->signal_on_new_state_(client_id, server_id, client_state);
105
  this->signal_on_new_state_(client_id, client_state);
105
 
106
 
106
  LOG_DEBUG_EXIT;
107
  LOG_DEBUG_EXIT;
107
}
108
}
108
 
109
 
109
void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket)
110
void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket)
Line 115... Line 116...
115
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
116
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
116
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
117
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
117
 
118
 
118
  this->clients_[client->GetId()] = client;
119
  this->clients_[client->GetId()] = client;
119
 
120
 
120
  log::Debug(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId());
121
  log::Info(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId());
121
 
122
 
-
 
123
  this->signal_on_new_client_(client->GetId(), server_id);
-
 
124
 
122
  this->signal_on_new_state_(client->GetId(), server_id, CLIENT_STATE_CONNECTED);
125
  this->signal_on_new_state_(client->GetId(), CLIENT_STATE_CONNECTED);
123
 
126
 
124
  LOG_DEBUG_EXIT;
127
  LOG_DEBUG_EXIT;
125
}
128
}
126
 
129
 
127
void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data)
130
void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data)
128
{
131
{
129
  LOG_DEBUG_ENTER;
132
  LOG_DEBUG_ENTER;
130
 
133
 
131
  this->signal_on_new_data_(client_id, server_id, data);
134
  this->signal_on_new_data_(client_id, data);
132
 
135
 
133
  LOG_DEBUG_EXIT;
136
  LOG_DEBUG_EXIT;
134
}
137
}
135
 
138
 
136
SocketId Manager::GetFreeSocketId()
139
SocketId Manager::GetFreeSocketId()
Line 159... Line 162...
159
  }
162
  }
160
 
163
 
161
  TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId()));
164
  TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId()));
162
 
165
 
163
  server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_));
166
  server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_));
164
 
167
 
165
  server->Accept();
168
  server->Accept();
166
 
169
 
167
  this->servers_[server->GetId()] = server;
170
  this->servers_[server->GetId()] = server;
168
 
171
 
169
  log::Debug(log_module_, "Started server with id %d!", server->GetId());
172
  log::Debug(log_module_, "Started server with id %d!", server->GetId());
170
 
173
 
171
  LOG_DEBUG_EXIT;
174
  LOG_DEBUG_EXIT;
172
 
175
 
173
  return server->GetId();
176
  return server->GetId();
174
}
177
}
175
 
178
 
176
SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud)
179
SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud)
177
{
180
{
178
  LOG_DEBUG_ENTER;
181
  LOG_DEBUG_ENTER;
179
 
182
 
180
  Client::Pointer client;
183
  Client::Pointer client;
181
 
184
 
182
  switch (protocol)
185
  switch (protocol)
183
  {
186
  {
184
    case PROTOCOL_TCP:
187
    case PROTOCOL_TCP:
185
    {
188
    {
186
      client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0));
189
      client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0));
187
      break;
190
      break;
188
    }
191
    }
189
    case PROTOCOL_UDP:
192
    case PROTOCOL_UDP:
190
    {
193
    {
191
      client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0));
194
      client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0));
192
      break;
195
      break;
193
    }
196
    }
194
    case PROTOCOL_SERIAL:
197
    case PROTOCOL_SERIAL:
195
    {
198
    {
196
      client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0));
199
      client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0));
197
      break;
200
      break;
198
    }
201
    }
199
    default:
202
    default:
200
    {
203
    {
201
      throw std::runtime_error("Invalid protocol specified!");
204
      throw std::runtime_error("Invalid protocol specified!");
202
    }
205
    }
203
  }
206
  }
204
 
207
 
205
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
208
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
206
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
209
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
207
 
210
 
208
  client->Connect(address, port_or_baud);
211
  client->Connect(address, port_or_baud);
209
 
212
 
210
  this->clients_[client->GetId()] = client;
213
  this->clients_[client->GetId()] = client;
211
 
214
 
212
  LOG_DEBUG_EXIT;
215
  LOG_DEBUG_EXIT;
213
 
216
 
214
  return client->GetId();
217
  return client->GetId();
215
}
218
}
216
 
219
 
217
void Manager::SendToAll(SocketId server_id, common::Byteset data)
220
void Manager::SendToAll(SocketId server_id, common::Byteset data)
218
{
221
{
219
  LOG_DEBUG_ENTER;
222
  LOG_DEBUG_ENTER;
220
 
223
 
221
  this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data));
224
  this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data));
222
 
225
 
223
  LOG_DEBUG_EXIT;
226
  LOG_DEBUG_EXIT;
224
}
227
}
225
 
228
 
226
void Manager::SendTo(SocketId client_id, common::Byteset data)
229
void Manager::SendTo(SocketId client_id, common::Byteset data)
227
{
230
{
228
  LOG_DEBUG_ENTER;
231
  LOG_DEBUG_ENTER;
229
 
232
 
230
  log::Debug(log_module_, "SendTo, client_id %d", client_id);
233
  log::Debug(log_module_, "SendTo, client_id %d", client_id);
231
 
234
 
232
  this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data));
235
  this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data));
233
 
236
 
234
  LOG_DEBUG_EXIT;
237
  LOG_DEBUG_EXIT;
235
}
238
}
236
 
239
 
237
void Manager::StopServer(SocketId server_id)
240
void Manager::StopServer(SocketId server_id)
238
{
241
{
239
  LOG_DEBUG_ENTER;
242
  LOG_DEBUG_ENTER;
240
 
243
 
241
  this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id));
244
  this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id));
242
 
245
 
243
  LOG_DEBUG_EXIT;
246
  LOG_DEBUG_EXIT;
244
}
247
}
245
 
248
 
246
void Manager::Disconnect(SocketId client_id)
249
void Manager::Disconnect(SocketId client_id)
247
{
250
{
248
  LOG_DEBUG_ENTER;
251
  LOG_DEBUG_ENTER;
249
 
252
 
250
  log::Debug(log_module_, "Disconnect, client_id %d", client_id);
253
  log::Debug(log_module_, "Disconnect, client_id %d", client_id);
251
 
254
 
252
  this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id));
255
  this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id));
253
 
256
 
254
  LOG_DEBUG_EXIT;
257
  LOG_DEBUG_EXIT;
255
}
258
}
256
 
259
 
257
void Manager::SendToAllHandler(SocketId server_id, common::Byteset data)
260
void Manager::SendToAllHandler(SocketId server_id, common::Byteset data)
258
{
261
{
259
  LOG_DEBUG_ENTER;
262
  LOG_DEBUG_ENTER;
260
 
263
 
261
  for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
264
  for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
262
  {
265
  {
263
    if (it->second->GetServerId() == server_id)
266
    if (it->second->GetServerId() == server_id)
264
    {
267
    {
265
      it->second->Send(data);
268
      it->second->Send(data);
266
    }
269
    }
267
  }
270
  }
268
 
271
 
269
  LOG_DEBUG_EXIT;
272
  LOG_DEBUG_EXIT;
270
}
273
}
271
 
274
 
272
void Manager::SendToHandler(SocketId client_id, common::Byteset data)
275
void Manager::SendToHandler(SocketId client_id, common::Byteset data)
273
{
276
{
274
  LOG_DEBUG_ENTER;
277
  LOG_DEBUG_ENTER;
275
 
278
 
276
  log::Debug(log_module_, "SendToHandler, client_id %d, data %s", client_id, data.ToCharString().c_str());
279
  log::Debug(log_module_, "SendToHandler, client_id %d, data %s", client_id, data.ToCharString().c_str());
277
 
280
 
278
  ClientList::iterator it = this->clients_.find(client_id);
281
  ClientList::iterator it = this->clients_.find(client_id);
279
 
282
 
280
  if (it != this->clients_.end())
283
  if (it != this->clients_.end())
281
  {
284
  {
282
    it->second->Send(data);
285
    it->second->Send(data);
-
 
286
  }
-
 
287
  else
-
 
288
  {
-
 
289
    log::Error(log_module_, "Failed to find client %d to send data to!", client_id);
283
  }
290
  }
284
 
291
 
285
  LOG_DEBUG_EXIT;
292
  LOG_DEBUG_EXIT;
286
}
293
}
287
 
294