Subversion Repositories HomeAutomation

Rev

Rev 975 | Rev 981 | Go to most recent revision | Blame | Compare with Previous | 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.  
  63.     string address = Settings::get("CanNetAddress");
  64.     string port = Settings::get("CanNetPort");
  65.  
  66.     myChannel->setAddress(address, stoi(port));
  67.  
  68.     myChannel->setReconnectTimeout(10);
  69.  
  70.     myChannel->start();
  71.  
  72.     myChannel->startEvent();
  73.  
  74.     bool waitingForPong = false;
  75.  
  76.     while (1)
  77.     {
  78.         myChannel->waitForEvent();
  79.  
  80.         int event = myChannel->getEvent();
  81.  
  82.         if (event == ASYNCSOCKET_EVENT_DATA)
  83.         {
  84.             while (myChannel->availableData())
  85.             {
  86.                 waitingForPong = false;
  87.  
  88.                 string data = myChannel->getData();
  89.  
  90.                 try
  91.                 {
  92.                     ///FIXME: Verify that \n is right here, have seen that it does not always work
  93.                     vector<string> dataLines = explode("\n", data);
  94.  
  95.                     for (int n = 0; n < dataLines.size(); n++)
  96.                     {
  97.                         data = trim(data, '\n');
  98.  
  99.                         if (data == "PONG")
  100.                         {
  101.                             slog << "Received pong.\n";
  102.                             continue;
  103.                         }
  104.  
  105.                         CanMessage canMessage;
  106.                         canMessage.setRaw(data);
  107.  
  108.                         //slog << "Received: " << data;
  109.  
  110.                         if (!canMessage.isUnknown())
  111.                         {
  112.                             vm.queueCanMessage(canMessage);
  113.                         }
  114.                         /*else
  115.                         {
  116.                             slog << "Received unknown message. Skipping...\n";
  117.                         }*/
  118.                     }
  119.                 }
  120.                 catch (CanMessageException* e)
  121.                 {
  122.                     slog << "CanMessageException was caught:\n";
  123.                     slog << e->getDescription() + "\n";
  124.                 }
  125.             }
  126.         }
  127.         else if (event == ASYNCSOCKET_EVENT_CLOSED)
  128.         {
  129.             vm.queueExpression("setAllOffline();");
  130.         }
  131.         else if (event == ASYNCSOCKET_EVENT_DIED)
  132.         {
  133.             vm.queueExpression("setAllOffline();");
  134.             break;
  135.         }
  136.         else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
  137.         {
  138.             if (waitingForPong)
  139.             {
  140.                 slog << "We have not received a pong for our ping.\n";
  141.                 waitingForPong = false;
  142.                 vm.queueExpression("setAllOffline();");
  143.                 myChannel->forceReconnect();
  144.             }
  145.             else
  146.             {
  147.                 //slog << "We have not received anything from the canDaemon in some time.\n";
  148.                 waitingForPong = true;
  149.                 slog << "Sending ping.\n";
  150.                 myChannel->sendData("PING");
  151.             }
  152.  
  153.         }
  154.     }
  155.  
  156.     myChannel->stopEvent();
  157. }
  158.  
  159. void CanNetManager::sendMessage(CanMessage canMessage)
  160. {
  161.     myChannel->sendData(canMessage.getRaw());
  162. }
  163.  
  164.