Subversion Repositories HomeAutomation

Rev

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

Rev 976 Rev 983
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) December 26, 2008 by Mattias Runge                             *
2
 *   Copyright (C) December 26, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   socketthread.cpp                                            *
4
 *   socketthread.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 "socketthread.h"
22
#include "socketthread.h"
23
#include "virtualmachine.h"
23
#include "virtualmachine.h"
24
 
24
 
25
SocketThread::SocketThread(string address, int port, unsigned int reconnectTimeout)
25
SocketThread::SocketThread(string address, int port, unsigned int reconnectTimeout)
26
{
26
{
27
    myId = time(NULL);
27
    myId = time(NULL);
28
 
28
 
-
 
29
    mySocket = new AsyncSocket();
-
 
30
 
29
    mySocket.setAddress(address, port);
31
    mySocket->setAddress(address, port);
30
    mySocket.setReconnectTimeout(reconnectTimeout);
32
    mySocket->setReconnectTimeout(reconnectTimeout);
31
 
33
 
32
    Thread<SocketThread>();
34
    Thread<SocketThread>();
33
}
35
}
34
 
36
 
35
SocketThread::~SocketThread()
37
SocketThread::~SocketThread()
36
{
38
{
37
    stop();
39
    if (!stop())
-
 
40
    {
-
 
41
        cout << "~SocketThread() :: Failed to stop thread :: Error code is " + itos(getError()) + "\n";
-
 
42
 
-
 
43
    }
-
 
44
    else
-
 
45
    {
-
 
46
    //  cout << "~SocketThread() :: Successfully stopped thread\n";
-
 
47
    }
-
 
48
    delete mySocket;
38
}
49
}
39
 
50
 
40
void SocketThread::run()
51
void SocketThread::run()
41
{
52
{
42
    SyslogStream &slog = SyslogStream::getInstance();
53
    //SyslogStream &slog = SyslogStream::getInstance();
43
    VirtualMachine &vm = VirtualMachine::getInstance();
54
    VirtualMachine &vm = VirtualMachine::getInstance();
44
 
55
 
45
    mySocket.start();
56
    mySocket->start();
46
 
57
 
47
    mySocket.startEvent();
58
    mySocket->startEvent();
48
 
59
 
49
    while (1)
60
    while (1)
50
    {
61
    {
51
        mySocket.waitForEvent();
62
        mySocket->waitForEvent();
52
 
63
 
-
 
64
        while (mySocket->availableEvent())
-
 
65
        {
53
        int event = mySocket.getEvent();
66
            int event = mySocket->getEvent();
54
 
67
 
55
        if (event == ASYNCSOCKET_EVENT_DATA)
68
            if (event == ASYNCSOCKET_EVENT_DATA)
56
        {
69
            {
57
            while (mySocket.availableData())
70
                while (mySocket->availableData())
58
            {
71
                {
59
                string data = mySocket.getData();
72
                    string data = mySocket->getData();
60
                vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_DATA', '" + escape(data) + "');");
73
                    vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_DATA', '" + escape(data) + "');");
61
            }
74
                }
-
 
75
            }
-
 
76
            else if (event == ASYNCSOCKET_EVENT_CONNECTED)
-
 
77
            {
-
 
78
                vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_CONNECTED', '');");
62
        }
79
            }
63
        else if (event == ASYNCSOCKET_EVENT_CLOSED)
80
            else if (event == ASYNCSOCKET_EVENT_CLOSED)
64
        {
81
            {
65
            vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_CLOSED', '');");
82
                vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_CLOSED', '');");
66
        }
83
            }
67
        else if (event == ASYNCSOCKET_EVENT_DIED)
84
            else if (event == ASYNCSOCKET_EVENT_DIED)
68
        {
85
            {
69
            vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_DIED', '');");
86
                vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_DIED', '');");
70
            break;
87
                break;
-
 
88
            }
-
 
89
            else if (event == ASYNCSOCKET_EVENT_RESET)
-
 
90
            {
-
 
91
                vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_RESET', '');");
-
 
92
                break;
71
        }
93
            }
72
        else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
94
            else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
73
        {
95
            {
74
            vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_INACTIVITY', '');");
96
                vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_INACTIVITY', '');");
-
 
97
            }
75
        }
98
        }
76
    }
99
    }
77
 
100
 
78
    mySocket.stopEvent();
101
    mySocket->stopEvent();
79
}
102
}
80
 
103
 
81
void SocketThread::send(string data)
104
void SocketThread::send(string data)
82
{
105
{
-
 
106
 
83
    mySocket.sendData(data);
107
    mySocket->sendData(data);
84
}
108
}
85
 
109
 
86
 
110