Subversion Repositories HomeAutomation

Rev

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

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