Subversion Repositories HomeAutomation

Rev

Rev 976 | Blame | Last modification | View Log | SVN | RSS feed

  1. /***************************************************************************
  2.  *   Copyright (C) November 29, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   cannetmanager.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 "canmessage.h"
  23. #include "cannetmanager.h"
  24.  
  25. CanNetManager* CanNetManager::myInstance = NULL;
  26.  
  27. CanNetManager& CanNetManager::getInstance()
  28. {
  29.     if (myInstance == NULL)
  30.     {
  31.         myInstance = new CanNetManager();
  32.     }
  33.  
  34.     return *myInstance;
  35. }
  36.  
  37. void CanNetManager::deleteInstance()
  38. {
  39.     if (myInstance != NULL)
  40.     {
  41.         delete myInstance;
  42.         myInstance = NULL;
  43.     }
  44. }
  45.  
  46. CanNetManager::CanNetManager()
  47. {
  48.     CanIdTranslator &translator = CanIdTranslator::getInstance();
  49.     myChannel = new AsyncSocket();
  50. }
  51.  
  52. CanNetManager::~CanNetManager()
  53. {
  54.     CanIdTranslator::deleteInstance();
  55.     delete myChannel;
  56. }
  57.  
  58. void CanNetManager::openChannel()
  59. {
  60.     SyslogStream &slog = SyslogStream::getInstance();
  61.     VirtualMachine &vm = VirtualMachine::getInstance();
  62.     CanDebug &canDebug = CanDebug::getInstance();
  63.  
  64.     string address = Settings::get("CanNetAddress");
  65.     string port = Settings::get("CanNetPort");
  66.  
  67.     myChannel->setAddress(address, stoi(port));
  68.  
  69.     myChannel->setReconnectTimeout(10);
  70.  
  71.     myChannel->start();
  72.  
  73.     myChannel->startEvent();
  74.  
  75.     bool waitingForPong = false;
  76.  
  77.     while (1)
  78.     {
  79.         myChannel->waitForEvent();
  80.  
  81.         int event = myChannel->getEvent();
  82.  
  83.         if (event == ASYNCSOCKET_EVENT_DATA)
  84.         {
  85.             while (myChannel->availableData())
  86.             {
  87.                 waitingForPong = false;
  88.  
  89.                 string data = myChannel->getData();
  90.  
  91.                 try
  92.                 {
  93.                     ///FIXME: Verify that \n is right here, have seen that it does not always work
  94.                     vector<string> dataLines = explode("\n", data);
  95.  
  96.                     for (int n = 0; n < dataLines.size(); n++)
  97.                     {
  98.                         data = trim(dataLines[n], '\n');
  99.  
  100.                         if (data == "PONG")
  101.                         {
  102.                             slog << "Received pong.\n";
  103.                             continue;
  104.                         }
  105.  
  106.                         CanMessage canMessage;
  107.                         canMessage.setRaw(data);
  108.  
  109.                         //slog << "Received: " << data;
  110.  
  111.                         if (!canMessage.isUnknown())
  112.                         {
  113.                             vm.queueCanMessage(canMessage);
  114.                             canDebug.sendCanMessage(canMessage);
  115.                         }
  116.                         else
  117.                         {
  118.                             canDebug.sendData(data + "\n");
  119.                         }
  120.                         /*else
  121.                         {
  122.                             slog << "Received unknown message. Skipping...\n";
  123.                         }*/
  124.                     }
  125.                 }
  126.                 catch (CanMessageException* e)
  127.                 {
  128.                     slog << "CanMessageException was caught:\n";
  129.                     slog << e->getDescription() + "\n";
  130.                 }
  131.             }
  132.         }
  133.         else if (event == ASYNCSOCKET_EVENT_CLOSED)
  134.         {
  135.             vm.queueExpression("setAllOffline();");
  136.         }
  137.         else if (event == ASYNCSOCKET_EVENT_DIED)
  138.         {
  139.             vm.queueExpression("setAllOffline();");
  140.             break;
  141.         }
  142.         else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
  143.         {
  144.             if (waitingForPong)
  145.             {
  146.                 slog << "We have not received a pong for our ping.\n";
  147.                 waitingForPong = false;
  148.                 vm.queueExpression("setAllOffline();");
  149.                 myChannel->forceReconnect();
  150.             }
  151.             else
  152.             {
  153.                 //slog << "We have not received anything from the canDaemon in some time.\n";
  154.                 waitingForPong = true;
  155.                 slog << "Sending ping.\n";
  156.                 myChannel->sendData("PING");
  157.             }
  158.  
  159.         }
  160.     }
  161.  
  162.     myChannel->stopEvent();
  163. }
  164.  
  165. void CanNetManager::sendMessage(CanMessage canMessage)
  166. {
  167.     CanDebug &canDebug = CanDebug::getInstance();
  168.     canDebug.sendCanMessage(canMessage);
  169.     myChannel->sendData(canMessage.getRaw());
  170. }
  171.  
  172.