Subversion Repositories HomeAutomation

Rev

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

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