Subversion Repositories HomeAutomation

Rev

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

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