Subversion Repositories HomeAutomation

Rev

Rev 976 | Rev 984 | 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 = new AsyncSocket();
  30.  
  31.     mySocket->setAddress(address, port);
  32.     mySocket->setReconnectTimeout(reconnectTimeout);
  33.  
  34.     Thread<SocketThread>();
  35. }
  36.  
  37. SocketThread::~SocketThread()
  38. {
  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;
  49. }
  50.  
  51. void SocketThread::run()
  52. {
  53.     //SyslogStream &slog = SyslogStream::getInstance();
  54.     VirtualMachine &vm = VirtualMachine::getInstance();
  55.  
  56.     mySocket->start();
  57.  
  58.     mySocket->startEvent();
  59.  
  60.     while (1)
  61.     {
  62.         mySocket->waitForEvent();
  63.  
  64.         while (mySocket->availableEvent())
  65.         {
  66.             int event = mySocket->getEvent();
  67.  
  68.             if (event == ASYNCSOCKET_EVENT_DATA)
  69.             {
  70.                 while (mySocket->availableData())
  71.                 {
  72.                     string data = mySocket->getData();
  73.                     vm.queueExpression("Socket.triggerSocketCallback(" + itos(myId) + ", 'EVENT_DATA', '" + escape(data) + "');");
  74.                 }
  75.             }
  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.             }
  98.         }
  99.     }
  100.  
  101.     mySocket->stopEvent();
  102. }
  103.  
  104. void SocketThread::send(string data)
  105. {
  106.  
  107.     mySocket->sendData(data);
  108. }
  109.  
  110.