Subversion Repositories HomeAutomation

Rev

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

Rev 1898 Rev 1916
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 "TcpClient.h"
21
#include "TcpClient.h"
22
 
22
 
23
#include <boost/lexical_cast.hpp>
23
#include <boost/lexical_cast.hpp>
24
 
24
 
25
namespace atom {
25
namespace atom {
26
namespace net {
26
namespace net {
27
 
27
 
28
logging::Logger TcpClient::LOG("net::TcpClient");
28
logging::Logger TcpClient::LOG("net::TcpClient");
29
 
29
 
30
TcpClient::TcpClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : socket_(io_service), Client(io_service, id, server_id)
30
TcpClient::TcpClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : Client(io_service, id, server_id), socket_(io_service)
31
{
31
{
32
}
32
}
33
 
33
 
34
TcpClient::~TcpClient()
34
TcpClient::~TcpClient()
35
{
35
{
36
}
36
}
37
 
37
 
38
void TcpClient::Accept(AcceptorPointer acceptor)
38
void TcpClient::Accept(AcceptorPointer acceptor)
39
{
39
{
40
    try
40
    try
41
    {
41
    {
42
        this->acceptor_ = acceptor;
42
        this->acceptor_ = acceptor;
43
       
43
       
44
        this->acceptor_->async_accept(this->socket_,
44
        this->acceptor_->async_accept(this->socket_,
45
                                      boost::bind(&TcpClient::AcceptHandler,
45
                                      boost::bind(&TcpClient::AcceptHandler,
46
                                                  this,
46
                                                  this,
47
                                                  boost::asio::placeholders::error));
47
                                                  boost::asio::placeholders::error));
48
    }
48
    }
49
    catch (std::exception e)
49
    catch (std::exception e)
50
    {
50
    {
51
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
51
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
52
    }
52
    }
53
}
53
}
54
 
54
 
55
void TcpClient::AcceptHandler(const boost::system::error_code& error)
55
void TcpClient::AcceptHandler(const boost::system::error_code& error)
56
{
56
{
57
    this->Read();
57
    this->Read();
58
   
58
   
59
    this->signal_on_new_state_(this->GetId(), this->GetServerId(), CLIENT_STATE_ACCEPTED);
59
    this->signal_on_new_state_(this->GetId(), this->GetServerId(), CLIENT_STATE_ACCEPTED);
60
}
60
}
61
 
61
 
62
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
62
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
63
{
63
{
64
    AcceptorPointer acceptor = this->acceptor_;
64
    AcceptorPointer acceptor = this->acceptor_;
65
    this->acceptor_.reset();
65
    this->acceptor_.reset();
66
    return acceptor;
66
    return acceptor;
67
}
67
}
68
 
68
 
69
void TcpClient::Connect(std::string address, unsigned int port)
69
void TcpClient::Connect(std::string address, unsigned int port)
70
{
70
{
71
    try
71
    try
72
    {
72
    {
73
        boost::asio::ip::tcp::resolver resolver(this->socket_.get_io_service());
73
        boost::asio::ip::tcp::resolver resolver(this->socket_.get_io_service());
74
        boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port));
74
        boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port));
75
        boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
75
        boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
76
       
76
       
77
        this->socket_.connect(*it);
77
        this->socket_.connect(*it);
78
    }
78
    }
79
    catch (std::exception e)
79
    catch (std::exception e)
80
    {
80
    {
81
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port));
81
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port));
82
    }
82
    }
83
   
83
   
84
    this->Read();
84
    this->Read();
85
}
85
}
86
 
86
 
87
void TcpClient::Stop()
87
void TcpClient::Stop()
88
{
88
{
89
    if (this->socket_.is_open())
89
    if (this->socket_.is_open())
90
    {
90
    {
91
        this->socket_.cancel();
91
        this->socket_.cancel();
92
        this->socket_.close();
92
        this->socket_.close();
93
    }
93
    }
94
   
94
   
95
    if (this->acceptor_.use_count() != 0)
95
    if (this->acceptor_.use_count() != 0)
96
    {
96
    {
97
        this->acceptor_->cancel();
97
        this->acceptor_->cancel();
98
        this->acceptor_->close();
98
        this->acceptor_->close();
99
    }
99
    }
100
}
100
}
101
 
101
 
102
void TcpClient::Send(common::Byteset data)
102
void TcpClient::Send(common::Byteset data)
103
{
103
{
104
    if (this->acceptor_.use_count() != 0)
104
    if (this->acceptor_.use_count() != 0)
105
    {
105
    {
106
        return;
106
        return;
107
    }
107
    }
108
 
108
 
109
    try
109
    try
110
    {
110
    {
111
        if (this->socket_.is_open())
111
        if (this->socket_.is_open())
112
        {
112
        {
113
            this->socket_.send(boost::asio::buffer(data.Get(), data.GetMaxSize()));
113
            this->socket_.send(boost::asio::buffer(data.Get(), data.GetMaxSize()));
114
        }
114
        }
115
    }
115
    }
116
    catch (std::exception& e)
116
    catch (std::exception& e)
117
    {
117
    {
118
        this->Disconnect();
118
        this->Disconnect();
119
        //throw std::runtime_error(e.what());
119
        //throw std::runtime_error(e.what());
120
    }
120
    }
121
}
121
}
122
 
122
 
123
void TcpClient::Read()
123
void TcpClient::Read()
124
{
124
{
125
    Client::Read();
125
    Client::Read();
126
   
126
   
127
    this->socket_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
127
    this->socket_.async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
128
                                  boost::bind(&TcpClient::ReadHandler,
128
                                  boost::bind(&TcpClient::ReadHandler,
129
                                              this,
129
                                              this,
130
                                              boost::asio::placeholders::error,
130
                                              boost::asio::placeholders::error,
131
                                              boost::asio::placeholders::bytes_transferred));
131
                                              boost::asio::placeholders::bytes_transferred));
132
}
132
}
133
 
133
 
134
   
134
   
135
}; // namespace net
135
}; // namespace net
136
}; // namespace atom
136
}; // namespace atom
137
 
137