Subversion Repositories HomeAutomation

Rev

Rev 984 | Rev 1026 | 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, unsigned int port, unsigned int reconnectTimeout)
  26. {
  27.     myId = time(NULL);
  28.  
  29.     mySocket = new AsyncSocket();
  30.  
  31.     mySocket->setAddress(address);
  32.     mySocket->setPort(port);
  33.     mySocket->setReconnectTimeout(reconnectTimeout);
  34. }
  35.  
  36. SocketThread::~SocketThread()
  37. {
  38.     if (!stop())
  39.     {
  40.         cout << "~SocketThread() :: Failed to stop thread :: Error code is " + itos(getError()) + "\n";
  41.  
  42.     }
  43. /*  else
  44.     {
  45.         cout << "~SocketThread() :: Successfully stopped thread\n";
  46.     }*/
  47.     delete mySocket;
  48. }
  49.  
  50. void SocketThread::run()
  51. {
  52.     VirtualMachine &vm = VirtualMachine::getInstance();
  53.  
  54.     mySocket->start();
  55.  
  56.     mySocket->eventStartListen();
  57.  
  58.     while (true)
  59.     {
  60.         mySocket->eventWait();
  61.  
  62.         while (mySocket->eventIsAvailable())
  63.         {
  64.             SocketEvent socketEvent = mySocket->eventFetch();
  65.  
  66.             switch (socketEvent.getType())
  67.             {
  68.                 case SocketEvent::TYPE_CONNECTED:
  69.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'CONNECTED', '" + escape(socketEvent.getData()) + "');");
  70.                 break;
  71.  
  72.                 case SocketEvent::TYPE_CONNECTING:
  73.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'CONNECTING', '" + escape(socketEvent.getData()) + "');");
  74.                 break;
  75.  
  76.                 case SocketEvent::TYPE_CONNECTION_CLOSED:
  77.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'CLOSED', '" + escape(socketEvent.getData()) + "');");
  78.                 break;
  79.  
  80.                 case SocketEvent::TYPE_CONNECTION_DIED:
  81.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'DIED', '" + escape(socketEvent.getData()) + "');");
  82.                 break;
  83.  
  84.                 case SocketEvent::TYPE_CONNECTION_RESET:
  85.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'RESET', '" + escape(socketEvent.getData()) + "');");
  86.                 break;
  87.  
  88.                 case SocketEvent::TYPE_CONNECTION_FAILED:
  89.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'CONNECTION_FAILED', '" + escape(socketEvent.getData()) + "');");
  90.                 break;
  91.  
  92.                 case SocketEvent::TYPE_DATA:
  93.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'DATA', '" + escape(socketEvent.getData()) + "');");
  94.                 break;
  95.  
  96.                 case SocketEvent::TYPE_INACTIVITY:
  97.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'INACTIVITY', '" + escape(socketEvent.getData()) + "');");
  98.                 break;
  99.                
  100.                 case SocketEvent::TYPE_WAITING_RECONNECT:
  101.                 vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'WAITING_RECONNECT', '" + escape(socketEvent.getData()) + "');");
  102.                 break;
  103.             }
  104.         }
  105.     }
  106.  
  107.     mySocket->eventStopListen();
  108. }
  109.  
  110. void SocketThread::send(string data)
  111. {
  112.     mySocket->sendData(data);
  113. }
  114.  
  115.