Subversion Repositories HomeAutomation

Rev

Rev 1987 | 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
 
1962 runge 29
#include "common/log.h"
30
 
31
static const std::string log_module_ = "net::manager";
32
 
1592 runge 33
namespace atom {
34
namespace net {
1593 runge 35
 
1599 runge 36
Manager::Pointer Manager::instance_;
1898 runge 37
 
1962 runge 38
 
1601 runge 39
Manager::Manager()
1592 runge 40
{
1962 runge 41
  LOG_DEBUG_ENTER;
42
  LOG_DEBUG_EXIT;
1592 runge 43
}
44
 
45
Manager::~Manager()
46
{
1962 runge 47
  LOG_DEBUG_ENTER;
1939 runge 48
 
1962 runge 49
  this->Stop();
50
 
51
  this->clients_.clear();
52
 
53
  LOG_DEBUG_EXIT;
1592 runge 54
}
55
 
56
Manager::Pointer Manager::Instance()
57
{
1962 runge 58
  LOG_DEBUG_ENTER;
59
 
60
  return Manager::instance_;
61
 
62
  LOG_DEBUG_EXIT;
1592 runge 63
}
64
 
1599 runge 65
void Manager::Create()
66
{
1962 runge 67
  LOG_DEBUG_ENTER;
68
 
69
  Manager::instance_ = Manager::Pointer(new Manager());
70
 
71
  LOG_DEBUG_EXIT;
1599 runge 72
}
73
 
1592 runge 74
void Manager::Delete()
75
{
1962 runge 76
  LOG_DEBUG_ENTER;
77
 
78
  Manager::instance_.reset();
79
 
80
  LOG_DEBUG_EXIT;
1592 runge 81
}
82
 
1986 runge 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)
1592 runge 84
{
1962 runge 85
  LOG_DEBUG_ENTER;
86
 
87
  this->signal_on_new_state_.connect(slot_on_new_state);
1986 runge 88
  this->signal_on_new_client_.connect(slot_on_new_client);
1962 runge 89
  this->signal_on_new_data_.connect(slot_on_new_data);
90
 
91
  LOG_DEBUG_EXIT;
1592 runge 92
}
93
 
1959 runge 94
void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state)
1592 runge 95
{
1962 runge 96
  LOG_DEBUG_ENTER;
97
 
1971 runge 98
  log::Debug(log_module_, "SlotOnNewState, client_id %d, server_id %d, client_state %d", client_id, server_id, client_state);
99
 
1962 runge 100
  if (client_state == CLIENT_STATE_DISCONNECTED)
101
  {
102
    this->clients_.erase(client_id);
103
  }
104
 
1986 runge 105
  this->signal_on_new_state_(client_id, client_state);
1962 runge 106
 
107
  LOG_DEBUG_EXIT;
1592 runge 108
}
109
 
1962 runge 110
void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket)
111
{
112
  LOG_DEBUG_ENTER;
113
 
114
  TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, socket, this->GetFreeSocketId(), server_id));
115
 
116
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
117
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
118
 
119
  this->clients_[client->GetId()] = client;
120
 
1986 runge 121
  log::Info(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId());
1962 runge 122
 
1986 runge 123
  this->signal_on_new_client_(client->GetId(), server_id);
1962 runge 124
 
1986 runge 125
  this->signal_on_new_state_(client->GetId(), CLIENT_STATE_CONNECTED);
126
 
1962 runge 127
  LOG_DEBUG_EXIT;
128
}
129
 
1959 runge 130
void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data)
1592 runge 131
{
1962 runge 132
  LOG_DEBUG_ENTER;
133
 
1989 runge 134
  //log::Debug(log_module_, "data=\"%s\"", std::string(data.begin(), data.end()).data());
1987 runge 135
 
1986 runge 136
  this->signal_on_new_data_(client_id, data);
1962 runge 137
 
138
  LOG_DEBUG_EXIT;
1592 runge 139
}
140
 
1959 runge 141
SocketId Manager::GetFreeSocketId()
1592 runge 142
{
1962 runge 143
  LOG_DEBUG_ENTER;
144
 
145
  SocketId id = 1;
146
 
147
  while (this->clients_.find(id) != this->clients_.end() || this->servers_.find(id) != this->servers_.end())
148
  {
149
    id++;
150
  }
151
 
152
  LOG_DEBUG_EXIT;
153
 
154
  return id;
1592 runge 155
}
156
 
1989 runge 157
SocketId Manager::StartServer(TransportProtocol protocol, unsigned int port)
1592 runge 158
{
1962 runge 159
  LOG_DEBUG_ENTER;
160
 
1989 runge 161
  if (protocol != TRANSPORT_PROTOCOL_TCP)
1962 runge 162
  {
163
    throw std::runtime_error("Can not start server for the Serial or UDP protocol!");
164
  }
165
 
166
  TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId()));
167
 
168
  server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_));
1592 runge 169
 
1962 runge 170
  server->Accept();
171
 
172
  this->servers_[server->GetId()] = server;
173
 
174
  log::Debug(log_module_, "Started server with id %d!", server->GetId());
175
 
176
  LOG_DEBUG_EXIT;
177
 
178
  return server->GetId();
1592 runge 179
}
180
 
1989 runge 181
SocketId Manager::Connect(TransportProtocol protocol, std::string address, unsigned int port_or_baud)
1592 runge 182
{
1962 runge 183
  LOG_DEBUG_ENTER;
184
 
185
  Client::Pointer client;
186
 
187
  switch (protocol)
188
  {
1989 runge 189
    case TRANSPORT_PROTOCOL_TCP:
1592 runge 190
    {
1962 runge 191
      client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0));
192
      break;
1592 runge 193
    }
1989 runge 194
    case TRANSPORT_PROTOCOL_UDP:
1962 runge 195
    {
196
      client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0));
197
      break;
198
    }
1989 runge 199
    case TRANSPORT_PROTOCOL_SERIAL:
1962 runge 200
    {
201
      client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0));
202
      break;
203
    }
204
    default:
205
    {
206
      throw std::runtime_error("Invalid protocol specified!");
207
    }
208
  }
209
 
210
  client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
211
                       Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
212
 
213
  client->Connect(address, port_or_baud);
214
 
215
  this->clients_[client->GetId()] = client;
216
 
217
  LOG_DEBUG_EXIT;
218
 
219
  return client->GetId();
1592 runge 220
}
221
 
1959 runge 222
void Manager::SendToAll(SocketId server_id, common::Byteset data)
1592 runge 223
{
1962 runge 224
  LOG_DEBUG_ENTER;
225
 
226
  this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data));
227
 
228
  LOG_DEBUG_EXIT;
1592 runge 229
}
230
 
1959 runge 231
void Manager::SendTo(SocketId client_id, common::Byteset data)
1592 runge 232
{
1962 runge 233
  LOG_DEBUG_ENTER;
234
 
1971 runge 235
  log::Debug(log_module_, "SendTo, client_id %d", client_id);
236
 
1962 runge 237
  this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data));
238
 
239
  LOG_DEBUG_EXIT;
1592 runge 240
}
241
 
1959 runge 242
void Manager::StopServer(SocketId server_id)
1592 runge 243
{
1962 runge 244
  LOG_DEBUG_ENTER;
245
 
246
  this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id));
247
 
248
  LOG_DEBUG_EXIT;
1592 runge 249
}
250
 
1959 runge 251
void Manager::Disconnect(SocketId client_id)
1592 runge 252
{
1962 runge 253
  LOG_DEBUG_ENTER;
254
 
1971 runge 255
  log::Debug(log_module_, "Disconnect, client_id %d", client_id);
256
 
1962 runge 257
  this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id));
258
 
259
  LOG_DEBUG_EXIT;
1592 runge 260
}
261
 
1959 runge 262
void Manager::SendToAllHandler(SocketId server_id, common::Byteset data)
1592 runge 263
{
1962 runge 264
  LOG_DEBUG_ENTER;
265
 
266
  for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
267
  {
268
    if (it->second->GetServerId() == server_id)
1592 runge 269
    {
1962 runge 270
      it->second->Send(data);
1592 runge 271
    }
1962 runge 272
  }
273
 
274
  LOG_DEBUG_EXIT;
1592 runge 275
}
276
 
1959 runge 277
void Manager::SendToHandler(SocketId client_id, common::Byteset data)
1592 runge 278
{
1962 runge 279
  LOG_DEBUG_ENTER;
280
 
1987 runge 281
  log::Debug(log_module_, "SendToHandler, client_id %d, data %s", client_id, std::string(data.begin(), data.end()).data());
1971 runge 282
 
1962 runge 283
  ClientList::iterator it = this->clients_.find(client_id);
1599 runge 284
 
1962 runge 285
  if (it != this->clients_.end())
286
  {
287
    it->second->Send(data);
288
  }
1986 runge 289
  else
290
  {
291
    log::Error(log_module_, "Failed to find client %d to send data to!", client_id);
292
  }
1962 runge 293
 
294
  LOG_DEBUG_EXIT;
1592 runge 295
}
296
 
1959 runge 297
void Manager::StopServerHandler(SocketId server_id)
1592 runge 298
{
1962 runge 299
  LOG_DEBUG_ENTER;
300
 
301
  this->clients_.erase(server_id);
302
 
303
  LOG_DEBUG_EXIT;
1592 runge 304
}
305
 
1959 runge 306
void Manager::DisconnectHandler(SocketId client_id)
1592 runge 307
{
1962 runge 308
  LOG_DEBUG_ENTER;
309
 
1971 runge 310
  log::Debug(log_module_, "DisconnectHandler, client_id %d", client_id);
311
 
1962 runge 312
  ClientList::iterator it = this->clients_.find(client_id);
313
 
314
  if (it != this->clients_.end())
315
  {
316
    it->second->Disconnect();
317
  }
318
 
319
  LOG_DEBUG_EXIT;
1592 runge 320
}
321
 
1962 runge 322
 
1592 runge 323
}; // namespace net
324
}; // namespace atom