Rev 1657 | Rev 1741 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1602 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 5 | * This program is free software; you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU General Public License along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "Module.h" |
||
| 22 | |||
| 23 | #include <boost/lexical_cast.hpp> |
||
| 24 | |||
| 1642 | runge | 25 | #include "control/Manager.h" |
| 1619 | runge | 26 | #include "vm/Manager.h" |
| 1602 | runge | 27 | |
| 28 | namespace atom { |
||
| 29 | namespace vm { |
||
| 30 | namespace plugin { |
||
| 31 | |||
| 32 | logging::Logger Module::LOG("vm::plugin::module"); |
||
| 33 | |||
| 1625 | runge | 34 | Module::Module(boost::asio::io_service& io_service) : Plugin(io_service) |
| 1602 | runge | 35 | { |
| 36 | this->name_ = "module"; |
||
| 37 | |||
| 1644 | runge | 38 | this->ExportFunction("ModuleExport_SendMessage", Module::Export_SendModuleMessage); |
| 39 | this->ExportFunction("ModuleExport_GetAvailableModules", Module::Export_GetAvailableModules); |
||
| 1602 | runge | 40 | } |
| 41 | |||
| 42 | Module::~Module() |
||
| 43 | { |
||
| 44 | this->tracker_.reset(); |
||
| 45 | } |
||
| 46 | |||
| 47 | void Module::InitializeDone() |
||
| 48 | { |
||
| 49 | Plugin::InitializeDone(); |
||
| 50 | |||
| 1608 | runge | 51 | this->ImportFunction("Module_OnChange"); |
| 52 | this->ImportFunction("Module_OnMessage"); |
||
| 53 | |||
| 1657 | runge | 54 | control::Manager::Instance()->ConnectSlotModule(control::Manager::SignalOnModuleChange::slot_type(&Module::SlotOnModuleChange, this, _1, _2).track(this->tracker_)); |
| 55 | control::Manager::Instance()->ConnectSlotModule(control::Manager::SignalOnModuleMessage::slot_type(&Module::SlotOnModuleMessage, this, _1, _2, _3).track(this->tracker_)); |
||
| 1602 | runge | 56 | } |
| 57 | |||
| 1647 | runge | 58 | void Module::CallOutput(unsigned int request_id, std::string output) |
| 59 | { |
||
| 60 | LOG.Info(output); |
||
| 61 | } |
||
| 62 | |||
| 1602 | runge | 63 | void Module::SlotOnModuleChange(std::string full_id, bool available) |
| 64 | { |
||
| 1625 | runge | 65 | this->io_service_.post(boost::bind(&Module::SlotOnModuleChangeHandler, this, full_id, available)); |
| 66 | } |
||
| 67 | |||
| 1642 | runge | 68 | void Module::SlotOnModuleMessage(std::string full_id, std::string command, common::StringMap variables) |
| 1625 | runge | 69 | { |
| 70 | this->io_service_.post(boost::bind(&Module::SlotOnModuleMessageHandler, this, full_id, command, variables)); |
||
| 71 | } |
||
| 72 | |||
| 73 | void Module::SlotOnModuleChangeHandler(std::string full_id, bool available) |
||
| 74 | { |
||
| 1740 | runge | 75 | v8::Locker lock; |
| 1625 | runge | 76 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 77 | |||
| 1642 | runge | 78 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1625 | runge | 79 | |
| 1602 | runge | 80 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 81 | arguments->push_back(v8::String::New(full_id.data())); |
||
| 82 | arguments->push_back(v8::Boolean::New(available)); |
||
| 83 | |||
| 1608 | runge | 84 | this->Call(0, "Module_OnChange", arguments); |
| 1602 | runge | 85 | } |
| 86 | |||
| 1642 | runge | 87 | void Module::SlotOnModuleMessageHandler(std::string full_id, std::string command, common::StringMap variables) |
| 1602 | runge | 88 | { |
| 1740 | runge | 89 | v8::Locker lock; |
| 1619 | runge | 90 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 91 | |||
| 1642 | runge | 92 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1609 | runge | 93 | |
| 1602 | runge | 94 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 95 | |||
| 96 | arguments->push_back(v8::String::New(full_id.data())); |
||
| 97 | arguments->push_back(v8::String::New(command.data())); |
||
| 98 | |||
| 1619 | runge | 99 | v8::Local<v8::Array> vars = v8::Array::New(variables.size()); |
| 100 | |||
| 1642 | runge | 101 | for (common::StringMap::iterator it = variables.begin(); it != variables.end(); it++) |
| 1602 | runge | 102 | { |
| 1619 | runge | 103 | vars->Set(v8::String::New(it->first.data()), v8::String::New(it->second.data())); |
| 1602 | runge | 104 | } |
| 105 | |||
| 1619 | runge | 106 | arguments->push_back(vars); |
| 107 | |||
| 1608 | runge | 108 | this->Call(0, "Module_OnMessage", arguments); |
| 1602 | runge | 109 | } |
| 110 | |||
| 1604 | runge | 111 | Value Module::Export_SendModuleMessage(const v8::Arguments& args) |
| 1602 | runge | 112 | { |
| 1740 | runge | 113 | v8::Locker lock; |
| 1625 | runge | 114 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 1604 | runge | 115 | |
| 1642 | runge | 116 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1625 | runge | 117 | |
| 1604 | runge | 118 | if (args.Length() < 3) |
| 119 | { |
||
| 1647 | runge | 120 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 121 | return v8::Boolean::New(false); |
||
| 1604 | runge | 122 | } |
| 123 | |||
| 1642 | runge | 124 | common::StringMap variables; |
| 1602 | runge | 125 | |
| 126 | v8::String::AsciiValue full_id(args[0]); |
||
| 127 | v8::String::AsciiValue command(args[1]); |
||
| 128 | unsigned int length = args[2]->Uint32Value(); |
||
| 129 | |||
| 130 | for (unsigned int n = 3; n < length * 2 + 3; n += 2) |
||
| 131 | { |
||
| 132 | v8::String::AsciiValue name(args[n]); |
||
| 133 | v8::String::AsciiValue value(args[n + 1]); |
||
| 134 | |||
| 1607 | runge | 135 | //LOG.Debug(std::string(*name) + "====" + std::string(*value)); |
| 136 | |||
| 1602 | runge | 137 | variables[std::string(*name)] = std::string(*value); |
| 138 | } |
||
| 139 | |||
| 1642 | runge | 140 | control::Manager::Instance()->SendMessage(std::string(*full_id), std::string(*command), variables); |
| 1604 | runge | 141 | |
| 1644 | runge | 142 | return v8::Boolean::New(true); |
| 1602 | runge | 143 | } |
| 1604 | runge | 144 | |
| 1644 | runge | 145 | Value Module::Export_GetAvailableModules(const v8::Arguments& args) |
| 1604 | runge | 146 | { |
| 1740 | runge | 147 | v8::Locker lock; |
| 1625 | runge | 148 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 149 | |||
| 1642 | runge | 150 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1602 | runge | 151 | |
| 1644 | runge | 152 | common::StringList available_modules = control::Manager::Instance()->GetAvailableModules(); |
| 1604 | runge | 153 | |
| 1644 | runge | 154 | v8::Local<v8::Array> result = v8::Array::New(available_modules.size()); |
| 155 | |||
| 156 | for (unsigned int n = 0; n < available_modules.size(); n++) |
||
| 157 | { |
||
| 158 | result->Set(n, v8::String::New(available_modules[n].data())); |
||
| 159 | } |
||
| 160 | |||
| 161 | return result; |
||
| 1604 | runge | 162 | } |
| 1642 | runge | 163 | |
| 1602 | runge | 164 | }; // namespace plugin |
| 165 | }; // namespace vm |
||
| 166 | }; // namespace atom |