Subversion Repositories HomeAutomation

Rev

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

Rev 1608 Rev 1939
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 "Client.h"
21
#include "Client.h"
22
 
22
 
23
#include <iostream>
23
#include <iostream>
24
 
24
 
25
#include <boost/bind.hpp>
25
#include <boost/bind.hpp>
26
#include <boost/lexical_cast.hpp>
26
#include <boost/lexical_cast.hpp>
27
 
27
 
28
namespace atom {
28
namespace atom {
29
namespace net {
29
namespace net {
30
 
30
 
31
Client::Client(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : buffer_(2048)
31
Client::Client(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : buffer_(2048)
32
{
32
{
33
    this->server_id_ = server_id;
33
    this->server_id_ = server_id;
34
    this->id_ = id;
34
    this->id_ = id;
35
}
35
}
36
 
36
 
37
Client::~Client()
37
Client::~Client()
38
{
38
{
39
    this->Stop();
39
    this->Stop();
40
}
40
}
41
 
41
 
42
void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data)
42
void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data)
43
{
43
{
44
    this->signal_on_new_state_.connect(slot_on_new_state);
44
    this->signal_on_new_state_.connect(slot_on_new_state);
45
    this->signal_on_new_data_.connect(slot_on_new_data);
45
    this->signal_on_new_data_.connect(slot_on_new_data);
46
}
46
}
47
 
47
 
48
ClientId Client::GetId()
48
ClientId Client::GetId()
49
{
49
{
50
    return this->id_;
50
    return this->id_;
51
}
51
}
52
 
52
 
53
ServerId Client::GetServerId()
53
ServerId Client::GetServerId()
54
{
54
{
55
    return this->server_id_;
55
    return this->server_id_;
56
}
56
}
57
 
57
 
58
void Client::Read()
58
void Client::Read()
59
{
59
{
60
    this->buffer_.Clear();
60
    this->buffer_.Clear();
61
}
61
}
62
 
62
 
63
void Client::ReadHandler(const boost::system::error_code& error, size_t size)
63
void Client::ReadHandler(const boost::system::error_code& error, size_t size)
64
{
64
{
65
    if (error)
65
    if (error)
66
    {
66
    {
67
        this->Disconnect();
67
        this->Disconnect();
68
    }
68
    }
69
    else if (size == 0)
69
    else if (size == 0)
70
    {
70
    {
71
        this->Disconnect();
71
        this->Disconnect();
72
    }
72
    }
73
    else
73
    else
74
    {
74
    {
75
        this->buffer_.SetSize(size);
75
        this->buffer_.SetSize(size);
76
       
76
       
77
        this->signal_on_new_data_(this->id_, this->server_id_, this->buffer_);
77
        this->signal_on_new_data_(this->id_, this->server_id_, this->buffer_);
78
       
78
       
79
        this->Read();
79
        this->Read();
80
    }
80
    }
81
}
81
}
82
 
82
 
83
void Client::Stop()
83
void Client::Stop()
84
{
84
{
85
}
85
}
86
 
86
 
87
void Client::Disconnect()
87
void Client::Disconnect()
88
{
88
{
89
    if (this->id_ != 0)
89
  if (this->id_ != 0)
90
    {
90
  {
-
 
91
    ClientId id = this->id_;
-
 
92
     
91
        this->Stop();
93
    this->Stop();
92
        this->signal_on_new_state_(this->id_, this->server_id_, CLIENT_STATE_DISCONNECTED);
-
 
93
 
94
       
94
        this->id_ = 0;
95
    this->id_ = 0;
-
 
96
       
-
 
97
    this->signal_on_new_state_(id, this->server_id_, CLIENT_STATE_DISCONNECTED);
95
    }
98
  }
96
}
99
}
97
   
100
   
98
}; // namespace net
101
}; // namespace net
99
}; // namespace atom
102
}; // namespace atom
100
 
103