Subversion Repositories HomeAutomation

Rev

Rev 1597 | Rev 1599 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1596 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 "Monitor.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
#include <boost/algorithm/string/trim.hpp>
25
 
26
#include "net/Manager.h"
27
 
28
#include "Message.h"
29
 
30
namespace atom {
31
namespace can {
32
 
1598 runge 33
Monitor::Monitor(unsigned int port): broker::Subscriber(port), LOG("can::Monitor")
1596 runge 34
{
35
    try
36
    {
37
        this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port);
1598 runge 38
        LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port) + ".");
39
        LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + ".");
1596 runge 40
    }
41
    catch (std::exception e)
42
    {
43
        LOG.Error(e.what());
44
    }
45
}
46
 
47
Monitor::~Monitor()
48
{
49
    net::Manager::Instance()->StopServer(this->server_id_);
50
}
51
 
52
void Monitor::SlotOnMessageHandler(broker::Message::Pointer message)
53
{
54
    if (message->GetType() == broker::Message::CAN_MESSAGE)
55
    {
56
        Message* payload = static_cast<Message*>(message->GetPayload().get());
57
 
58
        std::string line = "";
59
 
60
        if (payload->GetClassName() == "nmt")
61
        {
62
            line += "NMT";
63
        }
64
        else
65
        {
66
            if (payload->GetDirectionName() == "To_Owner")
67
            {
68
                line += "RX";
69
            }
70
            else if (payload->GetDirectionName() == "From_Owner")
71
            {
72
                line += "TX";
73
            }
74
            else
75
            {
76
                line += "??";
77
            }
78
 
79
            line += " " + payload->GetClassName();
80
            line += "_" + payload->GetModuleName();
81
            line += ":" + boost::lexical_cast<std::string>(payload->GetId());
82
        }
83
 
84
        line += " CMD=" + payload->GetCommandName() + " ";
85
 
86
        Message::VariableList variables = payload->GetVariables();
87
 
88
        for (Message::VariableList::iterator it = variables.begin(); it != variables.end(); it++)
89
        {
90
            line += " " + it->first + "=" + it->second;
91
        }
92
 
93
        line += "\n";
94
 
95
        net::Manager::Instance()->SendToAll(this->server_id_, type::Byteset(line));
96
    }
97
}
98
 
99
void Monitor::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
100
{
1598 runge 101
    if (server_id != this->server_id_)
102
    {
103
        return;
104
    }
105
 
1597 runge 106
    std::string str = data.ToCharString();
1596 runge 107
 
1597 runge 108
    boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n"));
1596 runge 109
 
110
    LOG.Debug("Received: \"" + str + "\" from client " + boost::lexical_cast<std::string>(client_id) + " on server " + boost::lexical_cast<std::string>(server_id));
111
 
112
    if (str == "q" || str == "quit")
113
    {
114
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has requested to be disconnected.");
115
        net::Manager::Instance()->Disconnect(client_id);
116
    }
117
}
118
 
119
void Monitor::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
120
{
1598 runge 121
    if (server_id != this->server_id_)
122
    {
123
        return;
124
    }
125
 
1596 runge 126
    if (client_state == net::CLIENT_STATE_DISCONNECTED)
127
    {
128
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected.");
129
    }
130
    else if (client_state == net::CLIENT_STATE_CONNECTED)
131
    {
132
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected.");
133
        net::Manager::Instance()->SendTo(client_id, type::Byteset("Welcome to Atom CAN monitoring\n"));
134
    }
135
    else
136
    {
137
        LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
138
    }
139
}
140
 
141
}; // namespace can
142
}; // namespace atom