Subversion Repositories HomeAutomation

Rev

Rev 1959 | Rev 1971 | 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
 
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
 
83
void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data)
84
{
1962 runge 85
  LOG_DEBUG_ENTER;
86
 
87
  this->signal_on_new_state_.connect(slot_on_new_state);
88
  this->signal_on_new_data_.connect(slot_on_new_data);
89
 
90
  LOG_DEBUG_EXIT;
1592 runge 91
}
92
 
1959 runge 93
void Manager::SlotOnNewState(SocketId client_id, SocketId server_id, ClientState client_state)
1592 runge 94
{
1962 runge 95
  LOG_DEBUG_ENTER;
96
 
97
  if (client_state == CLIENT_STATE_DISCONNECTED)
98
  {
99
    this->clients_.erase(client_id);
100
  }
101
 
102
  this->signal_on_new_state_(client_id, server_id, client_state);
103
 
104
  LOG_DEBUG_EXIT;
1592 runge 105
}
106
 
1962 runge 107
void Manager::SlotOnNewClient(SocketId server_id, TcpSocketPointer socket)
108
{
109
  LOG_DEBUG_ENTER;
110
 
111
  TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, socket, this->GetFreeSocketId(), server_id));
112
 
113
  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_));
115
 
116
  this->clients_[client->GetId()] = client;
117
 
118
  log::Debug(log_module_, "Client connected on server id %d with client id %d!", server_id, client->GetId());
119
 
120
  this->signal_on_new_state_(client->GetId(), server_id, CLIENT_STATE_CONNECTED);
121
 
122
  LOG_DEBUG_EXIT;
123
}
124
 
1959 runge 125
void Manager::SlotOnNewData(SocketId client_id, SocketId server_id, common::Byteset data)
1592 runge 126
{
1962 runge 127
  LOG_DEBUG_ENTER;
128
 
129
  this->signal_on_new_data_(client_id, server_id, data);
130
 
131
  LOG_DEBUG_EXIT;
1592 runge 132
}
133
 
1959 runge 134
SocketId Manager::GetFreeSocketId()
1592 runge 135
{
1962 runge 136
  LOG_DEBUG_ENTER;
137
 
138
  SocketId id = 1;
139
 
140
  while (this->clients_.find(id) != this->clients_.end() || this->servers_.find(id) != this->servers_.end())
141
  {
142
    id++;
143
  }
144
 
145
  LOG_DEBUG_EXIT;
146
 
147
  return id;
1592 runge 148
}
149
 
1959 runge 150
SocketId Manager::StartServer(Protocol protocol, unsigned int port)
1592 runge 151
{
1962 runge 152
  LOG_DEBUG_ENTER;
153
 
154
  if (protocol != PROTOCOL_TCP)
155
  {
156
    throw std::runtime_error("Can not start server for the Serial or UDP protocol!");
157
  }
158
 
159
  TcpServer::Pointer server = TcpServer::Pointer(new TcpServer(this->io_service_, port, this->GetFreeSocketId()));
160
 
161
  server->ConnectSlots(TcpServer::SignalOnNewClient::slot_type(&Manager::SlotOnNewClient, this, _1, _2).track(Manager::instance_));
1592 runge 162
 
1962 runge 163
  server->Accept();
164
 
165
  this->servers_[server->GetId()] = server;
166
 
167
  log::Debug(log_module_, "Started server with id %d!", server->GetId());
168
 
169
  LOG_DEBUG_EXIT;
170
 
171
  return server->GetId();
1592 runge 172
}
173
 
1959 runge 174
SocketId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud)
1592 runge 175
{
1962 runge 176
  LOG_DEBUG_ENTER;
177
 
178
  Client::Pointer client;
179
 
180
  switch (protocol)
181
  {
182
    case PROTOCOL_TCP:
1592 runge 183
    {
1962 runge 184
      client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeSocketId(), 0));
185
      break;
1592 runge 186
    }
1962 runge 187
    case PROTOCOL_UDP:
188
    {
189
      client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeSocketId(), 0));
190
      break;
191
    }
192
    case PROTOCOL_SERIAL:
193
    {
194
      client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeSocketId(), 0));
195
      break;
196
    }
197
    default:
198
    {
199
      throw std::runtime_error("Invalid protocol specified!");
200
    }
201
  }
202
 
203
  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_));
205
 
206
  client->Connect(address, port_or_baud);
207
 
208
  this->clients_[client->GetId()] = client;
209
 
210
  LOG_DEBUG_EXIT;
211
 
212
  return client->GetId();
1592 runge 213
}
214
 
1959 runge 215
void Manager::SendToAll(SocketId server_id, common::Byteset data)
1592 runge 216
{
1962 runge 217
  LOG_DEBUG_ENTER;
218
 
219
  this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data));
220
 
221
  LOG_DEBUG_EXIT;
1592 runge 222
}
223
 
1959 runge 224
void Manager::SendTo(SocketId client_id, common::Byteset data)
1592 runge 225
{
1962 runge 226
  LOG_DEBUG_ENTER;
227
 
228
  this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data));
229
 
230
  LOG_DEBUG_EXIT;
1592 runge 231
}
232
 
1959 runge 233
void Manager::StopServer(SocketId server_id)
1592 runge 234
{
1962 runge 235
  LOG_DEBUG_ENTER;
236
 
237
  this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id));
238
 
239
  LOG_DEBUG_EXIT;
1592 runge 240
}
241
 
1959 runge 242
void Manager::Disconnect(SocketId client_id)
1592 runge 243
{
1962 runge 244
  LOG_DEBUG_ENTER;
245
 
246
  this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id));
247
 
248
  LOG_DEBUG_EXIT;
1592 runge 249
}
250
 
1959 runge 251
void Manager::SendToAllHandler(SocketId server_id, common::Byteset data)
1592 runge 252
{
1962 runge 253
  LOG_DEBUG_ENTER;
254
 
255
  for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
256
  {
257
    if (it->second->GetServerId() == server_id)
1592 runge 258
    {
1962 runge 259
      it->second->Send(data);
1592 runge 260
    }
1962 runge 261
  }
262
 
263
  LOG_DEBUG_EXIT;
1592 runge 264
}
265
 
1959 runge 266
void Manager::SendToHandler(SocketId client_id, common::Byteset data)
1592 runge 267
{
1962 runge 268
  LOG_DEBUG_ENTER;
269
 
270
  ClientList::iterator it = this->clients_.find(client_id);
1599 runge 271
 
1962 runge 272
  if (it != this->clients_.end())
273
  {
274
    it->second->Send(data);
275
  }
276
 
277
  LOG_DEBUG_EXIT;
1592 runge 278
}
279
 
1959 runge 280
void Manager::StopServerHandler(SocketId server_id)
1592 runge 281
{
1962 runge 282
  LOG_DEBUG_ENTER;
283
 
284
  this->clients_.erase(server_id);
285
 
286
  LOG_DEBUG_EXIT;
1592 runge 287
}
288
 
1959 runge 289
void Manager::DisconnectHandler(SocketId client_id)
1592 runge 290
{
1962 runge 291
  LOG_DEBUG_ENTER;
292
 
293
  ClientList::iterator it = this->clients_.find(client_id);
294
 
295
  if (it != this->clients_.end())
296
  {
297
    it->second->Disconnect();
298
  }
299
 
300
  LOG_DEBUG_EXIT;
1592 runge 301
}
302
 
1962 runge 303
 
1592 runge 304
}; // namespace net
305
}; // namespace atom