Subversion Repositories HomeAutomation

Rev

Rev 1196 | Rev 1229 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /***************************************************************************
  2.  *   Copyright (C) December 10, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   virtualmachine.h                                            *
  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. #ifndef _VIRTUALMACHINE_H
  23. #define _VIRTUALMACHINE_H
  24.  
  25. using namespace std;
  26.  
  27. #include <map>
  28. #include <string>
  29. #include <queue>
  30.  
  31. #include "../v8/include/v8-debug.h"
  32. #include "../Logger/logger.h"
  33. #include "../Settings/settings.h"
  34. #include "../CanNet/cannetmanager.h"
  35. #include "../CanNet/canmessage.h"
  36. #include "../Threads/semaphore.h"
  37. #include "../Threads/threadsafequeue.h"
  38. #include "intervalthread.h"
  39. #include "socketthread.h"
  40. #include "commandthread.h"
  41. #include "expression.h"
  42.  
  43. using namespace v8;
  44.  
  45. class VirtualMachine : public Thread<VirtualMachine>
  46. {
  47. public:
  48.     static VirtualMachine& getInstance();
  49.     static void deleteInstance();
  50.  
  51.     void run();
  52.  
  53.     void queueExpression(Expression expression);
  54.     void queueExpression(string script) { queueExpression(Expression(script)); };
  55.  
  56.     bool loadScript(string scriptName);
  57.     bool loadDataStore(string storeName);
  58.  
  59.     unsigned int startIntervalThread(unsigned int timeout);
  60.     bool setIntervalThreadTimeout(unsigned int id, unsigned int timeout);
  61.     bool stopIntervalThread(unsigned int id);
  62.  
  63.     unsigned int startSocketThread(string address, int port, unsigned int reconnectTimeout);
  64.     bool stopSocketThread(unsigned int id);
  65.     void sendToSocketThread(unsigned int id, string data);
  66.  
  67.     void sendToCommandClient(unsigned int id, string data);
  68.     void disconnectCommandClient(unsigned int id);
  69.  
  70.     void print(unsigned int id, string data);
  71.  
  72.     static Handle<Value> _quit(const Arguments& args);
  73.     static Handle<Value> _print(const Arguments& args);
  74.     static Handle<Value> _log(const Arguments& args);
  75.     static Handle<Value> _sendCanMessage(const Arguments& args);
  76.     static Handle<Value> _sendCanNMTMessage(const Arguments& args);
  77.     static Handle<Value> _loadScript(const Arguments& args);
  78.     static Handle<Value> _stopIntervalThread(const Arguments& args);
  79.     static Handle<Value> _startIntervalThread(const Arguments& args);
  80.     static Handle<Value> _setIntervalThreadTimeout(const Arguments& args);
  81.     static Handle<Value> _stopSocketThread(const Arguments& args);
  82.     static Handle<Value> _startSocketThread(const Arguments& args);
  83.     static Handle<Value> _sendToSocketThread(const Arguments& args);
  84.  
  85.     static Handle<Value> _uint2hex(const Arguments& args);
  86.     static Handle<Value> _hex2bin(const Arguments& args);
  87.     static Handle<Value> _bin2hex(const Arguments& args);
  88.     static Handle<Value> _bin2float(const Arguments& args);
  89.     static Handle<Value> _float2bin(const Arguments& args);
  90.     static Handle<Value> _bin2uint(const Arguments& args);
  91.     static Handle<Value> _uint2bin(const Arguments& args);
  92.     static Handle<Value> _hex2uint(const Arguments& args);
  93.  
  94.     static Handle<Value> _sleep(const Arguments& args);
  95.  
  96.     static Handle<Value> _loadDataStore(const Arguments& args);
  97.     static Handle<Value> _getFileContents(const Arguments& args);
  98.  
  99. private:
  100.     bool runExpression(Expression expression);
  101.     string formatException(TryCatch* tryCatch);
  102.  
  103.     VirtualMachine();
  104.     ~VirtualMachine();
  105.     static VirtualMachine* myInstance;
  106.  
  107.     Handle<Function> myFunctionStartup;
  108.  
  109.     Handle<ObjectTemplate> myGlobal;
  110.     HandleScope myHandleScope;
  111.     Handle<Context> myContext;
  112.  
  113.     Semaphore mySemaphore;
  114.  
  115.     ThreadSafeQueue<Expression> myExpressions;
  116.  
  117.     map<int, IntervalThread*> myIntervalThreads;
  118.     map<unsigned long int, SocketThread*> mySocketThreads;
  119.  
  120.     CommandThread myCommandThread;
  121. };
  122.  
  123. #endif  /* _VIRTUALMACHINE_H */
  124.  
  125.