Subversion Repositories HomeAutomation

Rev

Rev 984 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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