Subversion Repositories HomeAutomation

Rev

Rev 1987 | 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 "Client.h"
22
 
1608 runge 23
#include <iostream>
24
 
1592 runge 25
#include <boost/bind.hpp>
26
#include <boost/lexical_cast.hpp>
27
 
1963 runge 28
#include "common/log.h"
29
 
1592 runge 30
namespace atom {
31
namespace net {
1963 runge 32
 
33
static const std::string log_module_ = "net::client";
1592 runge 34
 
1962 runge 35
Client::Client(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : buffer_(2048), io_service_(io_service)
1592 runge 36
{
1963 runge 37
  LOG_DEBUG_ENTER;
38
 
39
  this->server_id_ = server_id;
40
  this->id_ = id;
41
 
42
  LOG_DEBUG_EXIT;
1592 runge 43
}
44
 
45
Client::~Client()
46
{
1963 runge 47
  LOG_DEBUG_ENTER;
48
 
49
  this->Stop();
50
 
51
  LOG_DEBUG_EXIT;
1592 runge 52
}
53
 
54
void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data)
55
{
1963 runge 56
  LOG_DEBUG_ENTER;
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
  LOG_DEBUG_EXIT;
1592 runge 62
}
63
 
1959 runge 64
SocketId Client::GetId()
1592 runge 65
{
1963 runge 66
  LOG_DEBUG_ENTER;
67
  LOG_DEBUG_EXIT;
68
 
69
  return this->id_;
1592 runge 70
}
71
 
1959 runge 72
SocketId Client::GetServerId()
1592 runge 73
{
1963 runge 74
  LOG_DEBUG_ENTER;
75
  LOG_DEBUG_EXIT;
76
 
77
  return this->server_id_;
1592 runge 78
}
79
 
80
void Client::Read()
81
{
1963 runge 82
  LOG_DEBUG_ENTER;
83
 
1987 runge 84
  common::Byteset empty_vector(8192);
85
  this->buffer_.swap(empty_vector);
86
 
1963 runge 87
  LOG_DEBUG_EXIT;
1592 runge 88
}
89
 
90
void Client::ReadHandler(const boost::system::error_code& error, size_t size)
91
{
1963 runge 92
  LOG_DEBUG_ENTER;
1971 runge 93
 
1963 runge 94
 
95
  if (error)
96
  {
1971 runge 97
    log::Debug(log_module_, "error %d", error);
98
 
99
    if (boost::system::errc::operation_canceled != error)
100
    {
101
      this->Disconnect();
102
    }
1963 runge 103
  }
104
  else if (size == 0)
105
  {
2066 runge 106
    //log::Debug(log_module_, "size 0");
1971 runge 107
 
1963 runge 108
    this->Disconnect();
109
  }
110
  else
111
  {
2066 runge 112
    //log::Debug(log_module_, "size was %u", size);
1963 runge 113
 
1987 runge 114
    this->signal_on_new_data_(this->id_, this->server_id_, common::Byteset(this->buffer_.begin(), this->buffer_.begin() + size));
1963 runge 115
 
116
    this->Read();
117
  }
118
 
119
  LOG_DEBUG_EXIT;
1592 runge 120
}
121
 
1599 runge 122
void Client::Stop()
123
{
1963 runge 124
  LOG_DEBUG_ENTER;
125
  LOG_DEBUG_EXIT;
1599 runge 126
}
127
 
1592 runge 128
void Client::Disconnect()
129
{
1963 runge 130
  LOG_DEBUG_ENTER;
131
 
1939 runge 132
  if (this->id_ != 0)
133
  {
1959 runge 134
    SocketId id = this->id_;
1939 runge 135
 
136
    this->Stop();
137
 
138
    this->id_ = 0;
139
 
140
    this->signal_on_new_state_(id, this->server_id_, CLIENT_STATE_DISCONNECTED);
141
  }
1963 runge 142
 
143
  LOG_DEBUG_EXIT;
1592 runge 144
}
145
 
146
}; // namespace net
147
}; // namespace atom