Subversion Repositories HomeAutomation

Rev

Rev 1962 | Rev 1986 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1962 Rev 1971
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
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
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
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
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.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
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
 
22
 
23
#include <iostream>
23
#include <iostream>
24
 
24
 
25
#include <boost/lexical_cast.hpp>
25
#include <boost/lexical_cast.hpp>
26
#include <boost/bind.hpp>
26
#include <boost/bind.hpp>
27
#include <boost/cast.hpp>
27
#include <boost/cast.hpp>
28
 
28
 
29
#include "common/log.h"
29
#include "common/log.h"
30
 
30
 
31
static const std::string log_module_ = "net::manager";
31
static const std::string log_module_ = "net::manager";
32
 
32
 
33
namespace atom {
33
namespace atom {
34
namespace net {
34
namespace net {
35
 
35
 
36
Manager::Pointer Manager::instance_;
36
Manager::Pointer Manager::instance_;
37
 
37
 
38
 
38
 
39
Manager::Manager()
39
Manager::Manager()
40
{
40
{
41
  LOG_DEBUG_ENTER;
41
  LOG_DEBUG_ENTER;
42
  LOG_DEBUG_EXIT;
42
  LOG_DEBUG_EXIT;
43
}
43
}
44
 
44
 
45
Manager::~Manager()
45
Manager::~Manager()
46
{
46
{
47
  LOG_DEBUG_ENTER;
47
  LOG_DEBUG_ENTER;
48
 
48
 
49
  this->Stop();
49
  this->Stop();
50
 
50
 
51
  this->clients_.clear();
51
  this->clients_.clear();
52
 
52
 
53
  LOG_DEBUG_EXIT;
53
  LOG_DEBUG_EXIT;
54
}
54
}
55
 
55
 
56
Manager::Pointer Manager::Instance()
56
Manager::Pointer Manager::Instance()
57
{
57
{
58
  LOG_DEBUG_ENTER;
58
  LOG_DEBUG_ENTER;
59
 
59
 
60
  return Manager::instance_;
60
  return Manager::instance_;
61
 
61
 
62
  LOG_DEBUG_EXIT;
62
  LOG_DEBUG_EXIT;
63
}
63
}
64
 
64
 
65
void Manager::Create()
65
void Manager::Create()
66
{
66
{
67
  LOG_DEBUG_ENTER;
67
  LOG_DEBUG_ENTER;
68
 
68
 
69
  Manager::instance_ = Manager::Pointer(new Manager());
69
  Manager::instance_ = Manager::Pointer(new Manager());
70
 
70
 
71
  LOG_DEBUG_EXIT;
71
  LOG_DEBUG_EXIT;
72
}
72
}
73
 
73
 
74
void Manager::Delete()
74
void Manager::Delete()
75
{
75
{
76
  LOG_DEBUG_ENTER;
76
  LOG_DEBUG_ENTER;
77
 
77
 
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 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_data_.connect(slot_on_new_data);
88
  this->signal_on_new_data_.connect(slot_on_new_data);
89
 
89
 
90
  LOG_DEBUG_EXIT;
90
  LOG_DEBUG_EXIT;
91
}
91
}
92
 
92
 
93
void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state)
93
void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state)
94
{
94
{
95
  LOG_DEBUG_ENTER;
95
  LOG_DEBUG_ENTER;
-
 
96
 
-
 
97
  log::Debug(log_module_, "SlotOnNewState, client_id %d, server_id %d, client_state %d", client_id, server_id, client_state);
96
 
98
 
97
  if (client_state == CLIENT_STATE_DISCONNECTED)
99
  if (client_state == CLIENT_STATE_DISCONNECTED)
98
  {
100
  {
99
    this->clients_.erase(client_id);
101
    this->clients_.erase(client_id);
100
  }
102
  }
101
 
103
 
102
  this->signal_on_new_state_(client_id, server_id, client_state);
104
  this->signal_on_new_state_(client_id, server_id, client_state);
103
 
105
 
104
  LOG_DEBUG_EXIT;
106
  LOG_DEBUG_EXIT;
105
}
107
}
106
 
108
 
107
void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket)
109
void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket)
108
{
110
{
109
  LOG_DEBUG_ENTER;
111
  LOG_DEBUG_ENTER;
110
 
112
 
111
  TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, socket, this->GetFreeSocketId(), server_id));
113
  TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, socket, this->GetFreeSocketId(), server_id));
112
 
114
 
113
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
115
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
114
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
116
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
115
 
117
 
116
  this->clients_[client->GetId()] = client;
118
  this->clients_[client->GetId()] = client;
117
 
119
 
118
  log::Debug(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId());
120
  log::Debug(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId());
119
 
121
 
120
  this->signal_on_new_state_(client->GetId(), server_id, CLIENT_STATE_CONNECTED);
122
  this->signal_on_new_state_(client->GetId(), server_id, CLIENT_STATE_CONNECTED);
121
 
123
 
122
  LOG_DEBUG_EXIT;
124
  LOG_DEBUG_EXIT;
123
}
125
}
124
 
126
 
125
void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data)
127
void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data)
126
{
128
{
127
  LOG_DEBUG_ENTER;
129
  LOG_DEBUG_ENTER;
128
 
130
 
129
  this->signal_on_new_data_(client_id, server_id, data);
131
  this->signal_on_new_data_(client_id, server_id, data);
130
 
132
 
131
  LOG_DEBUG_EXIT;
133
  LOG_DEBUG_EXIT;
132
}
134
}
133
 
135
 
134
SocketId Manager::GetFreeSocketId()
136
SocketId Manager::GetFreeSocketId()
135
{
137
{
136
  LOG_DEBUG_ENTER;
138
  LOG_DEBUG_ENTER;
137
 
139
 
138
  SocketId id = 1;
140
  SocketId id = 1;
139
 
141
 
140
  while (this->clients_.find(id) != this->clients_.end() || this->servers_.find(id) != this->servers_.end())
142
  while (this->clients_.find(id) != this->clients_.end() || this->servers_.find(id) != this->servers_.end())
141
  {
143
  {
142
    id++;
144
    id++;
143
  }
145
  }
144
 
146
 
145
  LOG_DEBUG_EXIT;
147
  LOG_DEBUG_EXIT;
146
 
148
 
147
  return id;
149
  return id;
148
}
150
}
149
 
151
 
150
SocketId Manager::StartServer(Protocol protocol, unsigned int port)
152
SocketId Manager::StartServer(Protocol protocol, unsigned int port)
151
{
153
{
152
  LOG_DEBUG_ENTER;
154
  LOG_DEBUG_ENTER;
153
 
155
 
154
  if (protocol != PROTOCOL_TCP)
156
  if (protocol != PROTOCOL_TCP)
155
  {
157
  {
156
    throw std::runtime_error("Can not start server for the Serial or UDP protocol!");
158
    throw std::runtime_error("Can not start server for the Serial or UDP protocol!");
157
  }
159
  }
158
 
160
 
159
  TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId()));
161
  TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId()));
160
 
162
 
161
  server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_));
163
  server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_));
162
 
164
 
163
  server->Accept();
165
  server->Accept();
164
 
166
 
165
  this->servers_[server->GetId()] = server;
167
  this->servers_[server->GetId()] = server;
166
 
168
 
167
  log::Debug(log_module_, "Started server with id %d!", server->GetId());
169
  log::Debug(log_module_, "Started server with id %d!", server->GetId());
168
 
170
 
169
  LOG_DEBUG_EXIT;
171
  LOG_DEBUG_EXIT;
170
 
172
 
171
  return server->GetId();
173
  return server->GetId();
172
}
174
}
173
 
175
 
174
SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud)
176
SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud)
175
{
177
{
176
  LOG_DEBUG_ENTER;
178
  LOG_DEBUG_ENTER;
177
 
179
 
178
  Client::Pointer client;
180
  Client::Pointer client;
179
 
181
 
180
  switch (protocol)
182
  switch (protocol)
181
  {
183
  {
182
    case PROTOCOL_TCP:
184
    case PROTOCOL_TCP:
183
    {
185
    {
184
      client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0));
186
      client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0));
185
      break;
187
      break;
186
    }
188
    }
187
    case PROTOCOL_UDP:
189
    case PROTOCOL_UDP:
188
    {
190
    {
189
      client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0));
191
      client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0));
190
      break;
192
      break;
191
    }
193
    }
192
    case PROTOCOL_SERIAL:
194
    case PROTOCOL_SERIAL:
193
    {
195
    {
194
      client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0));
196
      client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0));
195
      break;
197
      break;
196
    }
198
    }
197
    default:
199
    default:
198
    {
200
    {
199
      throw std::runtime_error("Invalid protocol specified!");
201
      throw std::runtime_error("Invalid protocol specified!");
200
    }
202
    }
201
  }
203
  }
202
 
204
 
203
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
205
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
204
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
206
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
205
 
207
 
206
  client->Connect(address, port_or_baud);
208
  client->Connect(address, port_or_baud);
207
 
209
 
208
  this->clients_[client->GetId()] = client;
210
  this->clients_[client->GetId()] = client;
209
 
211
 
210
  LOG_DEBUG_EXIT;
212
  LOG_DEBUG_EXIT;
211
 
213
 
212
  return client->GetId();
214
  return client->GetId();
213
}
215
}
214
 
216
 
215
void Manager::SendToAll(SocketId server_id, common::Byteset data)
217
void Manager::SendToAll(SocketId server_id, common::Byteset data)
216
{
218
{
217
  LOG_DEBUG_ENTER;
219
  LOG_DEBUG_ENTER;
218
 
-
 
219
  this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data));
-
 
220
 
220
 
-
 
221
  this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data));
-
 
222
 
221
  LOG_DEBUG_EXIT;
223
  LOG_DEBUG_EXIT;
222
}
224
}
223
 
225
 
224
void Manager::SendTo(SocketId client_id, common::Byteset data)
226
void Manager::SendTo(SocketId client_id, common::Byteset data)
225
{
227
{
226
  LOG_DEBUG_ENTER;
228
  LOG_DEBUG_ENTER;
-
 
229
 
-
 
230
  log::Debug(log_module_, "SendTo, client_id %d", client_id);
227
 
231
 
228
  this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data));
232
  this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data));
229
 
233
 
230
  LOG_DEBUG_EXIT;
234
  LOG_DEBUG_EXIT;
231
}
235
}
232
 
236
 
233
void Manager::StopServer(SocketId server_id)
237
void Manager::StopServer(SocketId server_id)
234
{
238
{
235
  LOG_DEBUG_ENTER;
239
  LOG_DEBUG_ENTER;
236
 
240
 
237
  this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id));
241
  this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id));
238
 
242
 
239
  LOG_DEBUG_EXIT;
243
  LOG_DEBUG_EXIT;
240
}
244
}
241
 
245
 
242
void Manager::Disconnect(SocketId client_id)
246
void Manager::Disconnect(SocketId client_id)
243
{
247
{
244
  LOG_DEBUG_ENTER;
248
  LOG_DEBUG_ENTER;
-
 
249
 
-
 
250
  log::Debug(log_module_, "Disconnect, client_id %d", client_id);
245
 
251
 
246
  this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id));
252
  this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id));
247
 
253
 
248
  LOG_DEBUG_EXIT;
254
  LOG_DEBUG_EXIT;
249
}
255
}
250
 
256
 
251
void Manager::SendToAllHandler(SocketId server_id, common::Byteset data)
257
void Manager::SendToAllHandler(SocketId server_id, common::Byteset data)
252
{
258
{
253
  LOG_DEBUG_ENTER;
259
  LOG_DEBUG_ENTER;
254
 
260
 
255
  for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
261
  for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
256
  {
262
  {
257
    if (it->second->GetServerId() == server_id)
263
    if (it->second->GetServerId() == server_id)
258
    {
264
    {
259
      it->second->Send(data);
265
      it->second->Send(data);
260
    }
266
    }
261
  }
267
  }
262
 
268
 
263
  LOG_DEBUG_EXIT;
269
  LOG_DEBUG_EXIT;
264
}
270
}
265
 
271
 
266
void Manager::SendToHandler(SocketId client_id, common::Byteset data)
272
void Manager::SendToHandler(SocketId client_id, common::Byteset data)
267
{
273
{
268
  LOG_DEBUG_ENTER;
274
  LOG_DEBUG_ENTER;
-
 
275
 
-
 
276
  log::Debug(log_module_, "SendToHandler, client_id %d, data %s", client_id, data.ToCharString().c_str());
269
 
277
 
270
  ClientList::iterator it = this->clients_.find(client_id);
278
  ClientList::iterator it = this->clients_.find(client_id);
271
 
279
 
272
  if (it != this->clients_.end())
280
  if (it != this->clients_.end())
273
  {
281
  {
274
    it->second->Send(data);
282
    it->second->Send(data);
275
  }
283
  }
276
 
284
 
277
  LOG_DEBUG_EXIT;
285
  LOG_DEBUG_EXIT;
278
}
286
}
279
 
287
 
280
void Manager::StopServerHandler(SocketId server_id)
288
void Manager::StopServerHandler(SocketId server_id)
281
{
289
{
282
  LOG_DEBUG_ENTER;
290
  LOG_DEBUG_ENTER;
283
 
291
 
284
  this->clients_.erase(server_id);
292
  this->clients_.erase(server_id);
285
 
293
 
286
  LOG_DEBUG_EXIT;
294
  LOG_DEBUG_EXIT;
287
}
295
}
288
 
296
 
289
void Manager::DisconnectHandler(SocketId client_id)
297
void Manager::DisconnectHandler(SocketId client_id)
290
{
298
{
291
  LOG_DEBUG_ENTER;
299
  LOG_DEBUG_ENTER;
-
 
300
 
-
 
301
  log::Debug(log_module_, "DisconnectHandler, client_id %d", client_id);
292
 
302
 
293
  ClientList::iterator it = this->clients_.find(client_id);
303
  ClientList::iterator it = this->clients_.find(client_id);
294
 
304
 
295
  if (it != this->clients_.end())
305
  if (it != this->clients_.end())
296
  {
306
  {
297
    it->second->Disconnect();
307
    it->second->Disconnect();
298
  }
308
  }
299
 
309
 
300
  LOG_DEBUG_EXIT;
310
  LOG_DEBUG_EXIT;
301
}
311
}
302
 
312
 
303
 
313
 
304
}; // namespace net
314
}; // namespace net
305
}; // namespace atom
315
}; // namespace atom
306
 
316