Subversion Repositories HomeAutomation

Rev

Rev 1987 | 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
    {
1989 runge 37
        this->server_id_ = net::Manager::Instance()->StartServer(net::TRANSPORT_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
    }
1606 runge 41
    catch (std::runtime_error& e)
1596 runge 42
    {
43
        LOG.Error(e.what());
44
    }
45
}
46
 
47
Monitor::~Monitor()
48
{
49
    net::Manager::Instance()->StopServer(this->server_id_);
1599 runge 50
    this->server_id_ = 0;
1596 runge 51
}
52
 
53
void Monitor::SlotOnMessageHandler(broker::Message::Pointer message)
54
{
55
    if (message->GetType() == broker::Message::CAN_MESSAGE)
56
    {
57
        Message* payload = static_cast<Message*>(message->GetPayload().get());
58
 
59
        std::string line = "";
60
 
61
        if (payload->GetClassName() == "nmt")
62
        {
63
            line += "NMT";
64
        }
65
        else
66
        {
67
            if (payload->GetDirectionName() == "To_Owner")
68
            {
1642 runge 69
                line += "RX ";
1596 runge 70
            }
71
            else if (payload->GetDirectionName() == "From_Owner")
72
            {
1642 runge 73
                line += "TX ";
1596 runge 74
            }
75
            else
76
            {
77
                line += "??";
78
            }
1642 runge 79
        }
80
 
81
        line += " " + payload->GetCommandName() + " ";
82
 
83
        while (line.length() < 20)
84
        {
85
            line += " ";
86
        }
87
 
88
        if (payload->GetClassName() == "nmt")
89
        {
90
            line += "-";
91
        }
92
        else
93
        {
94
            line += payload->GetClassName();
1596 runge 95
            line += "_" + payload->GetModuleName();
96
            line += ":" + boost::lexical_cast<std::string>(payload->GetId());
97
        }
98
 
1642 runge 99
        while (line.length() < 40)
100
        {
101
            line += " ";
102
        }
1596 runge 103
 
1642 runge 104
        while (line.length() < 20)
105
        {
106
            line += " ";
107
        }
1596 runge 108
 
1642 runge 109
        common::StringMap variables = payload->GetVariables();
110
 
111
        for (common::StringMap::iterator it = variables.begin(); it != variables.end(); it++)
1596 runge 112
        {
1956 runge 113
          line += " " + it->first + "=" + it->second;
1596 runge 114
        }
115
 
116
        line += "\n";
117
 
1987 runge 118
        net::Manager::Instance()->SendToAll(this->server_id_, common::Byteset(line.begin(), line.end()));
1596 runge 119
    }
120
}
121
 
1986 runge 122
void Monitor::SlotOnNewDataHandler(net::SocketId id, common::Byteset data)
1596 runge 123
{
1986 runge 124
    if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
1598 runge 125
    {
1986 runge 126
      return;
1598 runge 127
    }
1986 runge 128
 
1987 runge 129
    std::string str(data.begin(), data.end());
1596 runge 130
 
1597 runge 131
    boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n"));
1596 runge 132
 
1986 runge 133
    LOG.Debug("Received: \"" + str + "\" from client " + boost::lexical_cast<std::string>(id) + " on server " + boost::lexical_cast<std::string>(this->server_id_));
1596 runge 134
 
135
    if (str == "q" || str == "quit")
136
    {
1986 runge 137
        LOG.Info("Client " + boost::lexical_cast<std::string>(id) + " has requested to be disconnected.");
138
        net::Manager::Instance()->Disconnect(id);
1596 runge 139
    }
140
}
141
 
1986 runge 142
void Monitor::SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
1596 runge 143
{
1986 runge 144
  if (server_id == this->server_id_)
145
  {
146
    this->clients_.insert(id);
147
  }
148
}
149
 
150
void Monitor::SlotOnNewStateHandler(net::SocketId id, net::ClientState client_state)
151
{
152
    if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
1598 runge 153
    {
1986 runge 154
      return;
1598 runge 155
    }
1986 runge 156
 
1596 runge 157
    if (client_state == net::CLIENT_STATE_DISCONNECTED)
158
    {
1986 runge 159
        LOG.Info("Client " + boost::lexical_cast<std::string>(id) + " has disconnected.");
160
        this->clients_.erase(id);
1596 runge 161
    }
162
    else if (client_state == net::CLIENT_STATE_CONNECTED)
163
    {
1986 runge 164
        LOG.Info("Client " + boost::lexical_cast<std::string>(id) + " has connected.");
1987 runge 165
 
166
        std::string message = "Welcome to Atom CAN monitoring\n";
167
 
168
        net::Manager::Instance()->SendTo(id, common::Byteset(message.begin(), message.end()));
1596 runge 169
    }
170
    else
171
    {
172
        LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
173
    }
174
}
175
 
176
}; // namespace can
177
}; // namespace atom