Subversion Repositories HomeAutomation

Rev

Rev 1989 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1914 linlun 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 "CanDaemon.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
#include <boost/algorithm/string/trim.hpp>
25
 
26
#include "net/Manager.h"
1918 linlun 27
#include "broker/Manager.h"
1914 linlun 28
 
29
#include "Message.h"
30
 
31
namespace atom {
32
namespace can {
33
 
34
CanDaemon::CanDaemon(unsigned int port): broker::Subscriber(port), LOG("can::CanDaemon")
35
{
36
    try
37
    {
1989 runge 38
        this->server_id_ = net::Manager::Instance()->StartServer(net::TRANSPORT_PROTOCOL_TCP, port);
1914 linlun 39
        LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port) + ".");
40
        LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + ".");
41
    }
42
    catch (std::runtime_error& e)
43
    {
44
        LOG.Error(e.what());
45
    }
46
}
47
 
48
CanDaemon::~CanDaemon()
49
{
50
    net::Manager::Instance()->StopServer(this->server_id_);
51
    this->server_id_ = 0;
52
}
53
 
54
 
55
void CanDaemon::SlotOnMessageHandler(broker::Message::Pointer message)
56
{
57
    if (message->GetType() == broker::Message::CAN_RAW_MESSAGE)
58
    {
1987 runge 59
      std::string* payload_str;
1916 runge 60
 
1987 runge 61
      payload_str = static_cast<std::string*>(message->GetPayload().get());
62
      std::string line = *payload_str;
1916 runge 63
 
1987 runge 64
      net::Manager::Instance()->SendToAll(this->server_id_, common::Byteset(line.begin(), line.end()));
1914 linlun 65
    }
66
}
67
 
1986 runge 68
void CanDaemon::SlotOnNewDataHandler(net::SocketId id, common::Byteset data)
1914 linlun 69
{
1986 runge 70
    if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
1914 linlun 71
    {
1986 runge 72
      return;
1914 linlun 73
    }
74
 
1987 runge 75
    std::string str(data.begin(), data.end());
1914 linlun 76
 
77
    boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n"));
78
 
2038 arune 79
    LOG.Debug("Received: \"" + str + "\" from client " + boost::lexical_cast<std::string>(id) + " on server " + boost::lexical_cast<std::string>(this->server_id_));
1914 linlun 80
 
1918 linlun 81
    std::string* payload_str = new std::string(str);
82
    broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_RAW_MESSAGE, broker::Message::PayloadPointer(payload_str), this)));
83
 
1919 linlun 84
    //LOG.Info("Sent...");
1918 linlun 85
 
1914 linlun 86
    if (str == "q" || str == "quit")
87
    {
2038 arune 88
        LOG.Debug("Client " + boost::lexical_cast<std::string>(id) + " has requested to be disconnected.");
1986 runge 89
        net::Manager::Instance()->Disconnect(id);
1914 linlun 90
    }
91
}
92
 
1986 runge 93
void CanDaemon::SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
1914 linlun 94
{
1986 runge 95
  if (server_id == this->server_id_)
96
  {
97
    this->clients_.insert(id);
98
  }
99
}
100
 
101
void CanDaemon::SlotOnNewStateHandler(net::SocketId id, net::ClientState client_state)
102
{
103
    if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
1914 linlun 104
    {
1986 runge 105
      return;
1914 linlun 106
    }
1986 runge 107
 
1914 linlun 108
    if (client_state == net::CLIENT_STATE_DISCONNECTED)
109
    {
2038 arune 110
        LOG.Debug("Client " + boost::lexical_cast<std::string>(id) + " has disconnected.");
1986 runge 111
        this->clients_.erase(id);
1914 linlun 112
    }
113
    else if (client_state == net::CLIENT_STATE_CONNECTED)
114
    {
2038 arune 115
        LOG.Debug("Client " + boost::lexical_cast<std::string>(id) + " has connected.");
1914 linlun 116
    }
117
    else
118
    {
119
        LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
120
    }
121
}
122
 
123
}; // namespace can
124
}; // namespace atom