Subversion Repositories HomeAutomation

Rev

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

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