Subversion Repositories HomeAutomation

Rev

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

Rev 969 Rev 1026
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 27, 2008 by Mattias Runge                             *
2
 *   Copyright (C) November 27, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   cannetchannel.cpp                                            *
4
 *   cannetchannel.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 "cannetmanager.h"
22
#include "cannetmanager.h"
23
 
23
 
24
 
24
 
25
#include "cannetchannel.h"
25
#include "cannetchannel.h"
26
 
26
 
27
CanNetChannel::CanNetChannel()
27
CanNetChannel::CanNetChannel()
28
{
28
{
29
}
29
}
30
 
30
 
31
CanNetChannel::~CanNetChannel()
31
CanNetChannel::~CanNetChannel()
32
{
32
{
33
}
33
}
34
 
34
 
35
void CanNetChannel::sendString(string data)
35
void CanNetChannel::sendString(string data)
36
{
36
{
37
    SyslogStream &slog = SyslogStream::getInstance();
37
    SyslogStream &slog = SyslogStream::getInstance();
38
 
38
 
39
    try
39
    try
40
    {
40
    {
41
        myClientSocket << data;
41
        myClientSocket << data;
42
    }
42
    }
43
    catch (SocketException& e)
43
    catch (SocketException& e)
44
    {
44
    {
45
        slog << "SocketException was caught:\n";
45
        slog << "SocketException was caught:\n";
46
        slog << e.getDescription() << "\n";
46
        slog << e.getDescription() << "\n";
47
    }
47
    }
48
}
48
}
49
 
49
 
50
void CanNetChannel::run()
50
void CanNetChannel::run()
51
{
51
{
52
    CanNetManager &canMan = CanNetManager::getInstance();
52
    CanNetManager &canMan = CanNetManager::getInstance();
53
    SyslogStream &slog = SyslogStream::getInstance();
53
    SyslogStream &slog = SyslogStream::getInstance();
54
 
54
 
55
    try
55
    try
56
    {
56
    {
57
        string address = Settings::get("CanNetAddress");
57
        string address = Settings::get("CanNetAddress");
58
        string port = Settings::get("CanNetPort");
58
        string port = Settings::get("CanNetPort");
59
 
59
 
60
        slog << "Trying to connect to canDaemon on " << address << ":" << port << "...\n";
60
        slog << "Trying to connect to canDaemon on " << address << ":" << port << "...\n";
61
 
61
 
62
        myClientSocket.start(address, atoi(port.c_str()));
62
        myClientSocket.start(address, atoi(port.c_str()));
63
 
63
 
64
        slog << "Connected to canDaemon.\n";
64
        slog << "Connected to canDaemon.\n";
65
 
65
 
66
        string data;
66
        string data;
67
 
67
 
68
        while (1)
68
        while (1)
69
        {
69
        {
70
            myClientSocket >> data;
70
            myClientSocket >> data;
71
 
71
 
72
            try
72
            try
73
            {
73
            {
74
                CanMessage canMessage;
74
                CanMessage canMessage;
75
                canMessage.setRaw(data);
75
                canMessage.setRaw(data);
76
 
76
 
77
                if (canMessage.isUnknown())
77
                if (canMessage.isUnknown())
78
                {
78
                {
79
                    slog << "Received unknown message. Skipping...\n";
79
                    slog << "Received unknown message. Skipping...\n";
80
                }
80
                }
81
                else
81
                else
82
                {
82
                {
83
                    canMan.addToInQueue(canMessage);
83
                    canMan.addToInQueue(canMessage);
84
                }
84
                }
85
            }
85
            }
86
            catch (CanMessageException& e)
86
            catch (CanMessageException& e)
87
            {
87
            {
88
                slog << "CanMessageException was caught:\n";
88
                slog << "CanMessageException was caught:\n";
89
                slog << e.getDescription() << "\n";
89
                slog << e.getDescription() << "\n";
90
            }
90
            }
91
 
91
 
92
            //myClientSocket << "TEST\n";
92
            //myClientSocket << "TEST\n";
93
 
93
 
94
            data = "";
94
            data = "";
95
        }
95
        }
96
    }
96
    }
97
    catch (SocketException& e)
97
    catch (SocketException& e)
98
    {
98
    {
99
        slog << "SocketException was caught:\n";
99
        slog << "SocketException was caught:\n";
100
        slog << e.getDescription() << "\n";
100
        slog << e.getDescription() << "\n";
101
    }
101
    }
102
}
102
}
103
 
103
 
104
 
104