Rev 1642 | Rev 1647 | 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); |
||
| 40 | this->ExportFunction("ModuleExport_ProgramNode", Module::Export_ProgramNode); |
||
| 1602 | runge | 41 | } |
| 42 | |||
| 43 | Module::~Module() |
||
| 44 | { |
||
| 45 | this->tracker_.reset(); |
||
| 46 | } |
||
| 47 | |||
| 48 | void Module::InitializeDone() |
||
| 49 | { |
||
| 50 | Plugin::InitializeDone(); |
||
| 51 | |||
| 1608 | runge | 52 | this->ImportFunction("Module_OnChange"); |
| 53 | this->ImportFunction("Module_OnMessage"); |
||
| 54 | |||
| 1642 | runge | 55 | control::Manager::Instance()->ConnectSlots(control::Manager::SignalOnNodeChange::slot_type(&Module::SlotOnNodeChange, this, _1, _2).track(this->tracker_), |
| 56 | control::Manager::SignalOnModuleChange::slot_type(&Module::SlotOnModuleChange, this, _1, _2).track(this->tracker_), |
||
| 57 | control::Manager::SignalOnModuleMessage::slot_type(&Module::SlotOnModuleMessage, this, _1, _2, _3).track(this->tracker_)); |
||
| 1602 | runge | 58 | } |
| 59 | |||
| 1614 | runge | 60 | void Module::SlotOnNodeChange(unsigned int node_id, bool available) |
| 61 | { |
||
| 1625 | runge | 62 | this->io_service_.post(boost::bind(&Module::SlotOnNodeChangeHandler, this, node_id, available)); |
| 1614 | runge | 63 | } |
| 64 | |||
| 1602 | runge | 65 | void Module::SlotOnModuleChange(std::string full_id, bool available) |
| 66 | { |
||
| 1625 | runge | 67 | this->io_service_.post(boost::bind(&Module::SlotOnModuleChangeHandler, this, full_id, available)); |
| 68 | } |
||
| 69 | |||
| 1642 | runge | 70 | void Module::SlotOnModuleMessage(std::string full_id, std::string command, common::StringMap variables) |
| 1625 | runge | 71 | { |
| 72 | this->io_service_.post(boost::bind(&Module::SlotOnModuleMessageHandler, this, full_id, command, variables)); |
||
| 73 | } |
||
| 74 | |||
| 75 | void Module::SlotOnNodeChangeHandler(unsigned int node_id, bool available) |
||
| 76 | { |
||
| 77 | |||
| 78 | } |
||
| 79 | |||
| 80 | void Module::SlotOnModuleChangeHandler(std::string full_id, bool available) |
||
| 81 | { |
||
| 82 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
||
| 83 | |||
| 1642 | runge | 84 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1625 | runge | 85 | |
| 1602 | runge | 86 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 87 | arguments->push_back(v8::String::New(full_id.data())); |
||
| 88 | arguments->push_back(v8::Boolean::New(available)); |
||
| 89 | |||
| 1608 | runge | 90 | this->Call(0, "Module_OnChange", arguments); |
| 1602 | runge | 91 | } |
| 92 | |||
| 1642 | runge | 93 | void Module::SlotOnModuleMessageHandler(std::string full_id, std::string command, common::StringMap variables) |
| 1602 | runge | 94 | { |
| 1619 | runge | 95 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 96 | |||
| 1642 | runge | 97 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1609 | runge | 98 | |
| 1602 | runge | 99 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 100 | |||
| 101 | arguments->push_back(v8::String::New(full_id.data())); |
||
| 102 | arguments->push_back(v8::String::New(command.data())); |
||
| 103 | |||
| 1619 | runge | 104 | v8::Local<v8::Array> vars = v8::Array::New(variables.size()); |
| 105 | |||
| 1642 | runge | 106 | for (common::StringMap::iterator it = variables.begin(); it != variables.end(); it++) |
| 1602 | runge | 107 | { |
| 1619 | runge | 108 | vars->Set(v8::String::New(it->first.data()), v8::String::New(it->second.data())); |
| 1602 | runge | 109 | } |
| 110 | |||
| 1619 | runge | 111 | arguments->push_back(vars); |
| 112 | |||
| 1608 | runge | 113 | this->Call(0, "Module_OnMessage", arguments); |
| 1602 | runge | 114 | } |
| 115 | |||
| 1604 | runge | 116 | Value Module::Export_SendModuleMessage(const v8::Arguments& args) |
| 1602 | runge | 117 | { |
| 1625 | runge | 118 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 1604 | runge | 119 | |
| 1642 | runge | 120 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1625 | runge | 121 | |
| 1604 | runge | 122 | if (args.Length() < 3) |
| 123 | { |
||
| 124 | LOG.Error("To few arguments."); |
||
| 125 | } |
||
| 126 | |||
| 1642 | runge | 127 | common::StringMap variables; |
| 1602 | runge | 128 | |
| 129 | v8::String::AsciiValue full_id(args[0]); |
||
| 130 | v8::String::AsciiValue command(args[1]); |
||
| 131 | unsigned int length = args[2]->Uint32Value(); |
||
| 132 | |||
| 133 | for (unsigned int n = 3; n < length * 2 + 3; n += 2) |
||
| 134 | { |
||
| 135 | v8::String::AsciiValue name(args[n]); |
||
| 136 | v8::String::AsciiValue value(args[n + 1]); |
||
| 137 | |||
| 1607 | runge | 138 | //LOG.Debug(std::string(*name) + "====" + std::string(*value)); |
| 139 | |||
| 1602 | runge | 140 | variables[std::string(*name)] = std::string(*value); |
| 141 | } |
||
| 142 | |||
| 1642 | runge | 143 | control::Manager::Instance()->SendMessage(std::string(*full_id), std::string(*command), variables); |
| 1604 | runge | 144 | |
| 1644 | runge | 145 | return v8::Boolean::New(true); |
| 1602 | runge | 146 | } |
| 1604 | runge | 147 | |
| 1644 | runge | 148 | Value Module::Export_GetAvailableModules(const v8::Arguments& args) |
| 1604 | runge | 149 | { |
| 1625 | runge | 150 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 151 | |||
| 1642 | runge | 152 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1602 | runge | 153 | |
| 1604 | runge | 154 | if (args.Length() < 1) |
| 155 | { |
||
| 1644 | runge | 156 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 1604 | runge | 157 | } |
| 158 | |||
| 1644 | runge | 159 | common::StringList available_modules = control::Manager::Instance()->GetAvailableModules(); |
| 1604 | runge | 160 | |
| 1644 | runge | 161 | v8::Local<v8::Array> result = v8::Array::New(available_modules.size()); |
| 162 | |||
| 163 | for (unsigned int n = 0; n < available_modules.size(); n++) |
||
| 164 | { |
||
| 165 | result->Set(n, v8::String::New(available_modules[n].data())); |
||
| 166 | } |
||
| 167 | |||
| 168 | return result; |
||
| 1604 | runge | 169 | } |
| 1642 | runge | 170 | |
| 171 | Value Module::Export_ProgramNode(const v8::Arguments& args) |
||
| 172 | { |
||
| 173 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
||
| 1604 | runge | 174 | |
| 1642 | runge | 175 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 176 | |||
| 177 | if (args.Length() < 3) |
||
| 178 | { |
||
| 1644 | runge | 179 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 1642 | runge | 180 | } |
| 181 | |||
| 182 | v8::String::AsciiValue filename(args[2]); |
||
| 183 | |||
| 184 | return v8::Boolean::New(control::Manager::Instance()->ProgramNode(args[0]->Uint32Value(), args[1]->BooleanValue(), std::string(*filename))); |
||
| 185 | } |
||
| 186 | |||
| 1602 | runge | 187 | }; // namespace plugin |
| 188 | }; // namespace vm |
||
| 189 | }; // namespace atom |