Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

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