Subversion Repositories HomeAutomation

Rev

Rev 983 | 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.     myChannel->stop();
  56.     delete myChannel;
  57. }
  58.  
  59. void CanNetManager::openChannel()
  60. {
  61.     SyslogStream &slog = SyslogStream::getInstance();
  62.     VirtualMachine &vm = VirtualMachine::getInstance();
  63.     CanDebug &canDebug = CanDebug::getInstance();
  64.  
  65.     string address = Settings::get("CanNetAddress");
  66.     string port = Settings::get("CanNetPort");
  67.  
  68.     myChannel->setAddress(address);
  69.     myChannel->setPort(stoi(port));
  70.     myChannel->setReconnectTimeout(10);
  71.  
  72.     myChannel->start();
  73.  
  74.     myChannel->eventStartListen();
  75.  
  76.     bool waitingForPong = false;
  77.     vector<string> dataLines;
  78.     string data;
  79.     CanMessage canMessage;
  80.  
  81.     while (1)
  82.     {
  83.         myChannel->eventWait();
  84.  
  85.         while (myChannel->eventIsAvailable())
  86.         {
  87.             SocketEvent socketEvent = myChannel->eventFetch();
  88.  
  89.             switch (socketEvent.getType())
  90.             {
  91.                 case SocketEvent::TYPE_CONNECTED:
  92.                 slog << "Connected to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
  93.                 break;
  94.  
  95.                 case SocketEvent::TYPE_CONNECTING:
  96.                 slog << "Connecting to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
  97.                 break;
  98.  
  99.                 case SocketEvent::TYPE_CONNECTION_CLOSED:
  100.                 slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " closed.\n";
  101.                 vm.queueExpression("setAllOffline();");
  102.                 break;
  103.  
  104.                 case SocketEvent::TYPE_CONNECTION_DIED:
  105.                 slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " died :: " + socketEvent.getData() + "\n";
  106.                 vm.queueExpression("setAllOffline();");
  107.                 break;
  108.  
  109.                 case SocketEvent::TYPE_CONNECTION_RESET:
  110.                 slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " was reset :: " + socketEvent.getData() + "\n";
  111.                 vm.queueExpression("setAllOffline();");
  112.                 break;
  113.  
  114.                 case SocketEvent::TYPE_CONNECTION_FAILED:
  115.                 slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " failed :: " + socketEvent.getData() + "\n";
  116.                 vm.queueExpression("setAllOffline();");
  117.                 break;
  118.  
  119.                 case SocketEvent::TYPE_DATA:
  120.                 waitingForPong = false;
  121.                 try
  122.                 {
  123.                     dataLines = explode("\n", socketEvent.getData());
  124.  
  125.                     for (int n = 0; n < dataLines.size(); n++)
  126.                     {
  127.                         data = trim(dataLines[n], '\n');
  128.  
  129.                         if (data == "PONG")
  130.                         {
  131.                             slog << "Received pong.\n";
  132.                             continue;
  133.                         }
  134.  
  135.                         canMessage.setRaw(data);
  136.  
  137.                         if (!canMessage.isUnknown())
  138.                         {
  139.                             vm.queueCanMessage(canMessage);
  140.                             canDebug.sendCanMessage(canMessage);
  141.                         }
  142.                         else
  143.                         {
  144.                             canDebug.sendData(data + "\n");
  145.                         }
  146.                     }
  147.                 }
  148.                 catch (CanMessageException* e)
  149.                 {
  150.                     slog << "CanMessageException was caught:\n";
  151.                     slog << e->getDescription() + "\n";
  152.                 }
  153.                 break;
  154.  
  155.                 case SocketEvent::TYPE_INACTIVITY:
  156.                 if (waitingForPong)
  157.                 {
  158.                     slog << "We have not received a pong for our ping.\n";
  159.                     waitingForPong = false;
  160.                     vm.queueExpression("setAllOffline();");
  161.                     myChannel->forceReconnect();
  162.                 }
  163.                 else
  164.                 {
  165.                     //slog << "We have not received anything from the canDaemon in some time.\n";
  166.                     waitingForPong = true;
  167.                     slog << "Sending ping.\n";
  168.                     myChannel->sendData("PING");
  169.                 }
  170.                 break;
  171.  
  172.                 case SocketEvent::TYPE_WAITING_RECONNECT:
  173.                 slog << "Will try to reconnect to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " in " + itos(myChannel->getReconnectTimeout()) + " seconds.\n";
  174.                 break;
  175.             }
  176.         }
  177.     }
  178.  
  179.     myChannel->eventStopListen();
  180. }
  181.  
  182. void CanNetManager::sendMessage(CanMessage canMessage)
  183. {
  184.     CanDebug &canDebug = CanDebug::getInstance();
  185.     canDebug.sendCanMessage(canMessage);
  186.     myChannel->sendData(canMessage.getRaw());
  187. }
  188.  
  189.