Subversion Repositories HomeAutomation

Rev

Rev 1597 | Rev 1599 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1597 Rev 1598
Line 28... Line 28...
28
#include "Message.h"
28
#include "Message.h"
29
 
29
 
30
namespace atom {
30
namespace atom {
31
namespace can {
31
namespace can {
32
   
32
   
33
Monitor::Monitor(unsigned int port): Subscriber(port), LOG("can::Monitor")
33
Monitor::Monitor(unsigned int port): broker::Subscriber(port), LOG("can::Monitor")
34
{
34
{
35
    net::Manager::Instance()->ConnectSlots(net::Client::SignalOnNewState::slot_type(&Monitor::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
-
 
36
                                           net::Client::SignalOnNewData::slot_type(&Monitor::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
-
 
37
   
-
 
38
    try
35
    try
39
    {
36
    {
40
        this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port);
37
        this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port);
41
        LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port));
38
        LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port) + ".");
42
        LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_));
39
        LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + ".");
43
    }
40
    }
44
    catch (std::exception e)
41
    catch (std::exception e)
45
    {
42
    {
46
        LOG.Error(e.what());
43
        LOG.Error(e.what());
47
    }
44
    }
48
}
45
}
49
 
46
 
50
Monitor::~Monitor()
47
Monitor::~Monitor()
51
{
48
{
52
    net::Manager::Instance()->StopServer(this->server_id_);
49
    net::Manager::Instance()->StopServer(this->server_id_);
53
}
-
 
54
 
-
 
55
 
-
 
56
void Monitor::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
-
 
57
{
-
 
58
    if (server_id == this->server_id_)
-
 
59
    {
-
 
60
        type::Byteset temp_buffer = data;
-
 
61
        this->io_service_.post(boost::bind(&Monitor::SlotOnNewDataHandler, this, client_id, server_id, temp_buffer));
-
 
62
    }
-
 
63
}
-
 
64
 
-
 
65
void Monitor::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
-
 
66
{
-
 
67
    if (server_id == this->server_id_)
-
 
68
    {
-
 
69
        this->io_service_.post(boost::bind(&Monitor::SlotOnNewStateHandler, this, client_id, server_id, client_state));
-
 
70
    }
-
 
71
}
50
}
72
 
51
 
73
void Monitor::SlotOnMessageHandler(broker::Message::Pointer message)
52
void Monitor::SlotOnMessageHandler(broker::Message::Pointer message)
74
{
53
{
75
    if (message->GetType() == broker::Message::CAN_MESSAGE)
54
    if (message->GetType() == broker::Message::CAN_MESSAGE)
Line 117... Line 96...
117
    }
96
    }
118
}
97
}
119
 
98
 
120
void Monitor::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
99
void Monitor::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
121
{
100
{
-
 
101
    if (server_id != this->server_id_)
-
 
102
    {
-
 
103
        return;
-
 
104
    }
-
 
105
   
122
    std::string str = data.ToCharString();
106
    std::string str = data.ToCharString();
123
   
107
   
124
    boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n"));
108
    boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n"));
125
   
109
   
126
    LOG.Debug("Received: \"" + str + "\" from client " + boost::lexical_cast<std::string>(client_id) + " on server " + boost::lexical_cast<std::string>(server_id));
110
    LOG.Debug("Received: \"" + str + "\" from client " + boost::lexical_cast<std::string>(client_id) + " on server " + boost::lexical_cast<std::string>(server_id));
127
   
111
   
128
    if (str == "q" || str == "quit")
112
    if (str == "q" || str == "quit")
129
    {
113
    {
130
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has requested to be disconnected.");
114
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has requested to be disconnected.");
131
        net::Manager::Instance()->Disconnect(client_id);
115
        net::Manager::Instance()->Disconnect(client_id);
132
    }
116
    }
133
}
117
}
134
 
118
 
135
void Monitor::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
119
void Monitor::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
136
{
120
{
-
 
121
    if (server_id != this->server_id_)
-
 
122
    {
-
 
123
        return;
-
 
124
    }
-
 
125
   
137
    if (client_state == net::CLIENT_STATE_DISCONNECTED)
126
    if (client_state == net::CLIENT_STATE_DISCONNECTED)
138
    {
127
    {
139
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected.");
128
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected.");
140
    }
129
    }
141
    else if (client_state == net::CLIENT_STATE_CONNECTED)
130
    else if (client_state == net::CLIENT_STATE_CONNECTED)