Subversion Repositories HomeAutomation

Rev

Rev 983 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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.  
  29.     mySocket.setAddress(address, port);
  30.     mySocket.setReconnectTimeout(reconnectTimeout);
  31.  
  32.     Thread<SocketThread>();
  33. }
  34.  
  35. SocketThread::~SocketThread()
  36. {
  37.     stop();
  38. }
  39.  
  40. void SocketThread::run()
  41. {
  42.     SyslogStream &slog = SyslogStream::getInstance();
  43.     VirtualMachine &vm = VirtualMachine::getInstance();
  44.  
  45.     mySocket.start();
  46.  
  47.     mySocket.startEvent();
  48.  
  49.     while (1)
  50.     {
  51.         mySocket.waitForEvent();
  52.  
  53.         int event = mySocket.getEvent();
  54.  
  55.         if (event == ASYNCSOCKET_EVENT_DATA)
  56.         {
  57.             while (mySocket.availableData())
  58.             {
  59.                 string data = mySocket.getData();
  60.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_DATA', '" + escape(data) + "');");
  61.             }
  62.         }
  63.         else if (event == ASYNCSOCKET_EVENT_CLOSED)
  64.         {
  65.             vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_CLOSED', '');");
  66.         }
  67.         else if (event == ASYNCSOCKET_EVENT_DIED)
  68.         {
  69.             vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_DIED', '');");
  70.             break;
  71.         }
  72.         else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
  73.         {
  74.             vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_INACTIVITY', '');");
  75.         }
  76.     }
  77.  
  78.     mySocket.stopEvent();
  79. }
  80.  
  81. void SocketThread::send(string data)
  82. {
  83.     mySocket.sendData(data);
  84. }
  85.  
  86.