Subversion Repositories HomeAutomation

Rev

Rev 1593 | Rev 1595 | 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
 
23
#include <boost/lexical_cast.hpp>
24
#include <boost/bind.hpp>
1594 runge 25
#include <boost/cast.hpp>
1592 runge 26
 
27
namespace atom {
28
namespace net {
1593 runge 29
 
1592 runge 30
Manager::Pointer Manager::instance_ = Manager::Pointer(new Manager());
31
 
32
Manager::Manager() : io_service_work_(io_service_)
33
{
34
    boost::thread thread(boost::bind(&boost::asio::io_service::run, &this->io_service_));
35
    this->thread_ = thread.move();
36
}
37
 
38
Manager::~Manager()
39
{
40
    this->clients_.clear();
41
 
42
    this->thread_.interrupt();
43
    this->thread_.join();
44
}
45
 
46
Manager::Pointer Manager::Instance()
47
{
48
    return Manager::instance_;
49
}
50
 
51
void Manager::Delete()
52
{
53
    Manager::instance_.reset();
54
}
55
 
56
void Manager::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data)
57
{
58
    this->signal_on_new_state_.connect(slot_on_new_state);
59
    this->signal_on_new_data_.connect(slot_on_new_data);
60
}
61
 
1593 runge 62
void Manager::SlotOnNewState(ClientId client_id, ServerId server_id, ClientState client_state)
1592 runge 63
{
64
    if (client_state == CLIENT_STATE_DISCONNECTED)
65
    {
66
        this->clients_.erase(client_id);
67
    }
68
    else if (client_state == CLIENT_STATE_ACCEPTED)
69
    {
70
        ClientList::iterator it = this->clients_.find(client_id);
71
 
72
        if (it != this->clients_.end())
73
        {
74
            TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), it->second->GetServerId()));
75
 
1593 runge 76
            client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
77
                                 Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
1592 runge 78
 
1594 runge 79
            client->Accept(boost::polymorphic_downcast<TcpClient*>(it->second.get())->ReleaseAcceptor());
1592 runge 80
 
81
            this->clients_[client->GetId()] = client;
82
 
1593 runge 83
            this->signal_on_new_state_(client_id, server_id, CLIENT_STATE_CONNECTED);
1592 runge 84
            return;
85
        }
86
        else
87
        {
88
            throw std::runtime_error("Accepted unknown client!");
89
        }
90
    }
91
 
1593 runge 92
    this->signal_on_new_state_(client_id, server_id, client_state);
1592 runge 93
}
94
 
1593 runge 95
void Manager::SlotOnNewData(ClientId client_id, ServerId server_id, Buffer data)
1592 runge 96
{
1593 runge 97
    this->signal_on_new_data_(client_id, server_id, data);
1592 runge 98
}
99
 
100
ServerId Manager::GetFreeServerId()
101
{
102
    ServerId server_id = 1;
103
 
104
    for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
105
    {
106
        if (it->second->GetServerId() != server_id)
107
        {
108
            return server_id;
109
        }
110
 
111
        server_id++;
112
    }
113
 
114
    return 0;
115
}
116
 
117
ClientId Manager::GetFreeClientId()
118
{
119
    ClientId client_id = 1;
120
 
121
    while (this->clients_.find(client_id) != this->clients_.end())
122
    {
123
        client_id++;
124
    }
125
 
126
    return client_id;
127
}
128
 
129
ServerId Manager::StartServer(Protocol protocol, unsigned int port)
130
{
131
    if (protocol != PROTOCOL_TCP)
132
    {
133
        throw std::runtime_error("Can not start server for the Serial or UDP protocol!");
134
        return 0;
135
    }
136
 
137
    TcpClient::Pointer client = TcpClient::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), this->GetFreeServerId()));
138
 
1593 runge 139
    client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
140
                         Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
1592 runge 141
 
142
    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)));    
143
 
144
    client->Accept(acceptor);
145
 
146
    this->clients_[client->GetId()] = client;
147
 
148
    return client->GetServerId();
149
}
150
 
151
ClientId Manager::Connect(Protocol protocol, std::string address, unsigned int port_or_baud)
152
{
153
    Client::Pointer client;
154
 
155
    switch (protocol)
156
    {
157
        case PROTOCOL_TCP:
158
        {
159
            client = Client::Pointer(new TcpClient(this->io_service_, this->GetFreeClientId(), 0));
160
            break;
161
        }
162
        case PROTOCOL_UDP:
163
        {
164
            client = Client::Pointer(new UdpClient(this->io_service_, this->GetFreeClientId(), 0));
165
            break;
166
        }
167
        case PROTOCOL_SERIAL:
168
        {
169
            client = Client::Pointer(new SerialClient(this->io_service_, this->GetFreeClientId(), 0));
170
            break;
171
        }
172
        default:
173
        {
174
            throw std::runtime_error("Invalid protocol specified!");
175
            return 0;
176
        }
177
    }
178
 
1593 runge 179
    client->ConnectSlots(Client::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(Manager::instance_),
180
                         Client::SignalOnNewData::slot_type(&Manager::SlotOnNewData, this, _1, _2, _3).track(Manager::instance_));
1592 runge 181
 
182
    try
183
    {
184
        client->Connect(address, port_or_baud);
185
    }
186
    catch (std::exception e)
187
    {
188
        throw e;
189
    }
190
 
191
    this->clients_[client->GetId()] = client;
192
 
193
    return client->GetId();
194
}
195
 
196
void Manager::SendToAll(ServerId server_id, Buffer data)
197
{
198
    this->io_service_.post(boost::bind(&Manager::SendToAllHandler, this, server_id, data));
199
}
200
 
201
void Manager::SendTo(ClientId client_id, Buffer data)
202
{
203
    this->io_service_.post(boost::bind(&Manager::SendToHandler, this, client_id, data));
204
}
205
 
206
void Manager::StopServer(ServerId server_id)
207
{
208
    this->io_service_.post(boost::bind(&Manager::StopServerHandler, this, server_id));
209
}
210
 
211
void Manager::Disconnect(ClientId client_id)
212
{
213
    this->io_service_.post(boost::bind(&Manager::DisconnectHandler, this, client_id));
214
}
215
 
216
void Manager::SendToAllHandler(ServerId server_id, Buffer data)
217
{
218
    for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
219
    {
220
        if (it->second->GetServerId() == server_id)
221
        {
222
            it->second->Send(data);
223
        }
224
    }
225
}
226
 
227
void Manager::SendToHandler(ClientId client_id, Buffer data)
228
{
229
    ClientList::iterator it = this->clients_.find(client_id);
230
 
231
    if (it != this->clients_.end())
232
    {
233
        it->second->Send(data);
234
    }
235
}
236
 
237
void Manager::StopServerHandler(ServerId server_id)
238
{
239
    for (ClientList::iterator it = this->clients_.begin(); it != this->clients_.end(); it++)
240
    {
241
        if (it->second->GetServerId() == server_id)
242
        {
243
            it->second->Disconnect();
244
        }
245
    }
246
}
247
 
248
void Manager::DisconnectHandler(ClientId client_id)
249
{
250
    ClientList::iterator it = this->clients_.find(client_id);
251
 
252
    if (it != this->clients_.end())
253
    {
254
        it->second->Disconnect();
255
    }
256
}
257
 
258
}; // namespace net
259
}; // namespace atom