Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
981 runge 1
/***************************************************************************
2
 *   Copyright (C) December 27, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
4
 *   candebug.cpp                                            *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
21
 
22
#include "candebug.h"
23
 
24
CanDebug* CanDebug::myInstance = NULL;
25
 
26
CanDebug& CanDebug::getInstance()
27
{
28
    if (myInstance == NULL)
29
    {
30
        myInstance = new CanDebug();
31
    }
32
 
33
    return *myInstance;
34
}
35
 
36
void CanDebug::deleteInstance()
37
{
38
    if (myInstance != NULL)
39
    {
40
        delete myInstance;
41
        myInstance = NULL;
42
    }
43
}
44
 
45
CanDebug::CanDebug()
46
{
47
    Thread<CanDebug>();
48
}
49
 
50
CanDebug::~CanDebug()
51
{
983 runge 52
    stop();
53
 
981 runge 54
    map<int, AsyncSocket*>::iterator iter;
55
 
56
    for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
57
    {
58
        if (iter->second->isConnected())
59
        {
60
            iter->second->stop();
61
        }
62
 
63
        delete iter->second;
64
    }
65
}
66
 
67
void CanDebug::run()
68
{
69
    SyslogStream &slog = SyslogStream::getInstance();
70
 
71
    string port = Settings::get("CanDebugPort");
72
 
985 runge 73
    if (port == "")
74
    {
75
        slog << "CanDebugPort is not defined in the config file, will not start debug interface\n";
76
        return;
77
    }
78
 
981 runge 79
    try
80
    {
81
        mySocket.setPort(stoi(port));
82
 
83
        mySocket.startListen();
84
 
85
        slog << "CanDebug is listening for connections on port " + port + ".\n";
86
 
87
        while (true)
88
        {
89
            AsyncSocket *newSocket = new AsyncSocket();
90
 
91
            if (mySocket.accept(newSocket))
92
            {
984 runge 93
                newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - CanDebug output\n");
981 runge 94
 
95
                slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n";
96
                myClientSockets[newSocket->getSocket()] = newSocket;
984 runge 97
                myClientSockets[newSocket->getSocket()]->start();
981 runge 98
            }
99
        }
100
    }
101
    catch (SocketException* e)
102
    {
103
        slog << "Exception was caught:" + e->getDescription() + ".\n";
104
    }
105
}
106
 
107
void CanDebug::sendData(string data)
108
{
109
    SyslogStream &slog = SyslogStream::getInstance();
110
 
111
    map<int, AsyncSocket*>::iterator iter;
112
 
113
    for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
114
    {
115
        if (iter->second->isConnected())
116
        {
984 runge 117
            //slog << "CanDebug: sent data to client " + itos(iter->second->getSocket()) + ".\n";
118
 
119
            try
120
            {
121
                iter->second->sendData(data);
122
            }
123
            catch (SocketException* e)
124
            {
125
                slog << "CanDebug caught an exception:" + e->getDescription() + ".\n";
126
            }
981 runge 127
        }
128
        else
129
        {
984 runge 130
            slog << "CanDebug had a client disconnect.\n";
981 runge 131
            delete iter->second;
132
            myClientSockets.erase(iter);
133
        }
134
    }
135
}
136
 
137
void CanDebug::sendCanMessage(CanMessage canMessage)
138
{
983 runge 139
    if (!canMessage.isHeartbeat() && myClientSockets.size() > 0)
981 runge 140
    {
141
        string debugData = "";
142
 
143
        if (canMessage.getDirectionFlag() == "To_Owner")
144
        {
145
            debugData += "RX";
146
        }
147
        else if (canMessage.getDirectionFlag() == "From_Owner")
148
        {
149
            debugData += "TX";
150
        }
151
        else
152
        {
153
            debugData += "??";
154
        }
155
 
156
        debugData += " " + canMessage.getClassName();
157
        debugData += "_" + canMessage.getModuleName();
158
        debugData += ":" + itos(canMessage.getModuleId());
159
        debugData += " CMD=" + canMessage.getCommandName() + " ";
160
 
161
        map<string, CanVariable> vars = canMessage.getData();
162
 
163
        map<string, CanVariable>::iterator iter;
164
 
165
        for (iter = vars.begin(); iter != vars.end(); iter++)
166
        {
167
            debugData += " " + iter->second.getName() + "=" + iter->second.getValue();
168
        }
169
 
170
        sendData("[" + niceTime() + "] " + debugData + "\n");
171
    }
172
}