Subversion Repositories HomeAutomation

Rev

Rev 1962 | Rev 1987 | 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 "UdpClient.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
 
1963 runge 25
#include "common/log.h"
26
 
1592 runge 27
namespace atom {
28
namespace net {
1963 runge 29
 
30
static const std::string log_module_ = "net::udpclient";
1592 runge 31
 
1962 runge 32
UdpClient::UdpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
1592 runge 33
{
1963 runge 34
  LOG_DEBUG_ENTER;
35
  LOG_DEBUG_EXIT;
1592 runge 36
}
37
 
38
UdpClient::~UdpClient()
39
{
1963 runge 40
  LOG_DEBUG_ENTER;
41
  LOG_DEBUG_EXIT;
1592 runge 42
}
43
 
44
void UdpClient::Connect(std::string address, unsigned int port)
45
{
1963 runge 46
  LOG_DEBUG_ENTER;
47
 
48
  try
49
  {
50
    boost::asio::ip::udp::resolver resolver(this->io_service_);
51
    boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), address, boost::lexical_cast<std::string>(port));
52
    boost::asio::ip::udp::resolver::iterator it = resolver.resolve(query);
1592 runge 53
 
1963 runge 54
    this->endpoint_ = *it;
55
 
56
    this->socket_.reset(new boost::asio::ip::udp::socket(this->io_service_, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
57
  }
58
  catch (std::exception e)
59
  {
60
    throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + e.what());
61
  }
62
 
63
  this->Read();
64
 
65
  LOG_DEBUG_EXIT;
1592 runge 66
}
67
 
1599 runge 68
void UdpClient::Stop()
1592 runge 69
{
1963 runge 70
  LOG_DEBUG_ENTER;
71
 
72
  if (this->socket_.use_count() != 0)
73
  {
74
    this->socket_->cancel();
75
    this->socket_->close();
76
    this->socket_.reset();
77
  }
78
 
79
  LOG_DEBUG_EXIT;
1592 runge 80
}
81
 
1642 runge 82
void UdpClient::Send(common::Byteset data)
1592 runge 83
{
1963 runge 84
  LOG_DEBUG_ENTER;
85
 
86
  try
87
  {
88
    if (this->socket_->is_open())
1593 runge 89
    {
1963 runge 90
      this->socket_->send_to(boost::asio::buffer(data.Get(), data.GetMaxSize()), this->endpoint_);
1593 runge 91
    }
1963 runge 92
  }
93
  catch (std::exception& e)
94
  {
95
    this->Disconnect();
96
    //throw std::runtime_error(e.what());
97
  }
98
 
99
  LOG_DEBUG_EXIT;
1592 runge 100
}
101
 
102
void UdpClient::Read()
103
{
1963 runge 104
  LOG_DEBUG_ENTER;
105
 
106
  Client::Read();
1599 runge 107
 
1963 runge 108
  this->socket_->async_receive(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
109
                               boost::bind(&UdpClient::ReadHandler,
110
                                           this,
111
                                           boost::asio::placeholders::error,
112
                                           boost::asio::placeholders::bytes_transferred));
113
 
114
  LOG_DEBUG_EXIT;
1592 runge 115
}
116
 
117
}; // namespace net
118
}; // namespace atom