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 |
|---|---|---|---|
| 1601 | 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 "Manager.h" |
||
| 22 | |||
| 23 | #include <fstream> |
||
| 24 | |||
| 25 | #include <boost/lexical_cast.hpp> |
||
| 26 | #include <boost/bind.hpp> |
||
| 27 | |||
| 28 | namespace atom { |
||
| 29 | namespace vm { |
||
| 30 | |||
| 31 | Manager::Pointer Manager::instance_; |
||
| 32 | |||
| 33 | Manager::Manager() : LOG("vm::Manager") |
||
| 34 | { |
||
| 35 | } |
||
| 36 | |||
| 37 | Manager::~Manager() |
||
| 38 | { |
||
| 39 | this->io_service_.stop(); |
||
| 40 | |||
| 41 | this->plugins_.clear(); |
||
| 42 | this->context_.Dispose(); |
||
| 43 | |||
| 44 | for (ImportFunctionList::iterator it = this->functions_.begin(); it != this->functions_.end(); it++) |
||
| 45 | { |
||
| 46 | it->second.Dispose(); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | void Manager::Create() |
||
| 51 | { |
||
| 52 | Manager::instance_ = Manager::Pointer(new Manager()); |
||
| 53 | } |
||
| 54 | |||
| 55 | Manager::Pointer Manager::Instance() |
||
| 56 | { |
||
| 57 | return Manager::instance_; |
||
| 58 | } |
||
| 59 | |||
| 60 | void Manager::Delete() |
||
| 61 | { |
||
| 62 | Manager::instance_.reset(); |
||
| 63 | } |
||
| 64 | |||
| 1619 | runge | 65 | v8::Persistent<v8::Context>& Manager::GetContext() |
| 66 | { |
||
| 67 | return this->context_; |
||
| 68 | } |
||
| 69 | |||
| 1601 | runge | 70 | void Manager::AddPlugin(Plugin::Pointer plugin) |
| 71 | { |
||
| 1603 | runge | 72 | this->plugins_.push_back(plugin); |
| 1601 | runge | 73 | } |
| 74 | |||
| 1656 | runge | 75 | void Manager::Start(std::string script_path, std::string user_script_path) |
| 1601 | runge | 76 | { |
| 77 | this->script_path_ = script_path; |
||
| 1656 | runge | 78 | this->user_script_path_ = user_script_path; |
| 1601 | runge | 79 | this->io_service_.post(boost::bind(&Manager::StartHandler, this)); |
| 80 | } |
||
| 81 | |||
| 1647 | runge | 82 | void Manager::CallOutput(std::string output) |
| 1601 | runge | 83 | { |
| 1647 | runge | 84 | if (this->current_plugin_name_ != "") |
| 1608 | runge | 85 | { |
| 1647 | runge | 86 | for (unsigned int n = 0; n < this->plugins_.size(); n++) |
| 1608 | runge | 87 | { |
| 1647 | runge | 88 | if (this->plugins_[n]->GetName() == this->current_plugin_name_) |
| 89 | { |
||
| 90 | this->plugins_[n]->CallOutput(this->current_request_id_, output); |
||
| 91 | return; |
||
| 92 | } |
||
| 1608 | runge | 93 | } |
| 94 | } |
||
| 1647 | runge | 95 | else |
| 96 | { |
||
| 97 | LOG.Info(output); |
||
| 98 | } |
||
| 1601 | runge | 99 | } |
| 100 | |||
| 1647 | runge | 101 | Value Manager::Export_Log(const v8::Arguments& args) |
| 102 | { |
||
| 1740 | runge | 103 | v8::Locker lock; |
| 1647 | runge | 104 | v8::Context::Scope context_scope(Manager::Instance()->GetContext()); |
| 105 | |||
| 106 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
||
| 107 | |||
| 108 | if (args.Length() < 1) |
||
| 109 | { |
||
| 110 | Manager::instance_->LOG.Error(std::string(__FUNCTION__) + ": To few arguments to log."); |
||
| 111 | return v8::Undefined(); |
||
| 112 | } |
||
| 113 | |||
| 114 | v8::String::AsciiValue str(args[0]); |
||
| 115 | |||
| 116 | Manager::instance_->CallOutput(*str); |
||
| 117 | |||
| 118 | return v8::Undefined(); |
||
| 119 | } |
||
| 120 | |||
| 1608 | runge | 121 | void Manager::Call(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
| 1601 | runge | 122 | { |
| 1608 | runge | 123 | this->io_service_.post(boost::bind(&Manager::CallHandler, this, plugin_name, request_id, name, arguments)); |
| 1601 | runge | 124 | } |
| 125 | |||
| 1608 | runge | 126 | void Manager::Execute(std::string plugin_name, unsigned int request_id, std::string code) |
| 1601 | runge | 127 | { |
| 1608 | runge | 128 | this->io_service_.post(boost::bind(&Manager::ExecuteHandler, this, plugin_name, request_id, code)); |
| 129 | } |
||
| 130 | |||
| 1647 | runge | 131 | bool Manager::CallHandler(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
| 1608 | runge | 132 | { |
| 1740 | runge | 133 | v8::Locker lock; |
| 1601 | runge | 134 | v8::Context::Scope context_scope(this->context_); |
| 135 | v8::TryCatch try_catch; |
||
| 136 | |||
| 1647 | runge | 137 | this->current_plugin_name_ = plugin_name; |
| 138 | this->current_request_id_ = request_id; |
||
| 139 | |||
| 1601 | runge | 140 | if (this->functions_.find(name) == this->functions_.end()) |
| 141 | { |
||
| 1647 | runge | 142 | this->CallOutput("No such function found, " + name); |
| 143 | |||
| 144 | this->current_plugin_name_ = ""; |
||
| 145 | this->current_request_id_ = 0; |
||
| 146 | return false; |
||
| 1601 | runge | 147 | } |
| 1604 | runge | 148 | |
| 1642 | runge | 149 | //LOG.Debug("Call: " + name); |
| 1625 | runge | 150 | |
| 1601 | runge | 151 | Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data()); |
| 152 | |||
| 153 | if (result.IsEmpty()) |
||
| 154 | { |
||
| 1647 | runge | 155 | this->CallOutput(this->FormatException(try_catch)); |
| 156 | |||
| 157 | this->current_plugin_name_ = ""; |
||
| 158 | this->current_request_id_ = 0; |
||
| 159 | return false; |
||
| 1601 | runge | 160 | } |
| 1608 | runge | 161 | |
| 1647 | runge | 162 | //v8::String::AsciiValue response(result->ToString()); |
| 163 | //this->CallOutput(plugin_name, request_id, std::string(*response)); |
||
| 1608 | runge | 164 | |
| 1647 | runge | 165 | this->current_plugin_name_ = ""; |
| 166 | this->current_request_id_ = 0; |
||
| 167 | return true; |
||
| 1601 | runge | 168 | } |
| 169 | |||
| 170 | void Manager::StartHandler() |
||
| 171 | { |
||
| 172 | v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New(); |
||
| 173 | |||
| 1647 | runge | 174 | raw_template->Set(v8::String::New("Log"), v8::FunctionTemplate::New(Manager::Export_Log)); |
| 175 | |||
| 1603 | runge | 176 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 1601 | runge | 177 | { |
| 1603 | runge | 178 | ExportFunctionList export_functions = this->plugins_[c]->GetExportFunctions(); |
| 1601 | runge | 179 | |
| 180 | for (ExportFunctionList::iterator it = export_functions.begin(); it != export_functions.end(); it++) |
||
| 181 | { |
||
| 182 | raw_template->Set(v8::String::New(it->first.data()), v8::FunctionTemplate::New(it->second)); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | this->context_ = v8::Context::New(NULL, raw_template); |
||
| 187 | |||
| 1740 | runge | 188 | v8::Locker lock; |
| 1601 | runge | 189 | v8::Context::Scope context_scope(this->context_); |
| 190 | |||
| 1603 | runge | 191 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 1601 | runge | 192 | { |
| 1608 | runge | 193 | this->LoadScript("plugin/" + this->plugins_[c]->GetName() + ".js"); |
| 1601 | runge | 194 | } |
| 195 | |||
| 196 | this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template); |
||
| 197 | |||
| 1603 | runge | 198 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 1601 | runge | 199 | { |
| 1603 | runge | 200 | this->plugins_[c]->InitializeDone(); |
| 1601 | runge | 201 | } |
| 202 | } |
||
| 203 | |||
| 1608 | runge | 204 | bool Manager::ImportFunction(std::string functionname) |
| 1601 | runge | 205 | { |
| 1740 | runge | 206 | v8::Locker lock; |
| 1608 | runge | 207 | v8::Context::Scope context_scope(this->context_); |
| 208 | |||
| 209 | v8::TryCatch try_catch; |
||
| 210 | |||
| 211 | v8::Handle<v8::String> process_name = v8::String::New(functionname.data()); |
||
| 212 | v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name); |
||
| 213 | |||
| 214 | if (!process_val->IsFunction()) |
||
| 215 | { |
||
| 216 | LOG.Error(functionname + " was not found to be a function!"); |
||
| 217 | return false; |
||
| 218 | } |
||
| 219 | |||
| 220 | v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val); |
||
| 221 | |||
| 222 | this->functions_[functionname] = v8::Persistent<v8::Function>::New(process_fun); |
||
| 223 | |||
| 224 | return true; |
||
| 225 | } |
||
| 226 | |||
| 227 | bool Manager::LoadScript(std::string scriptname) |
||
| 228 | { |
||
| 1644 | runge | 229 | for (unsigned int n = 0; n < this->loaded_scripts_.size(); n++) |
| 230 | { |
||
| 231 | if (this->loaded_scripts_[n] == scriptname) |
||
| 232 | { |
||
| 1657 | runge | 233 | LOG.Debug(scriptname + " is already loaded."); |
| 1644 | runge | 234 | return true; |
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 1656 | runge | 238 | std::string path = this->user_script_path_ + scriptname; |
| 1601 | runge | 239 | std::ifstream file(path.data()); |
| 240 | |||
| 241 | if (!file.is_open()) |
||
| 242 | { |
||
| 1656 | runge | 243 | path = this->script_path_ + scriptname; |
| 244 | file.open(path.data()); |
||
| 245 | |||
| 246 | if (!file.is_open()) |
||
| 247 | { |
||
| 248 | file.close(); |
||
| 249 | LOG.Warning("Could not load " + scriptname + "."); |
||
| 250 | return false; |
||
| 251 | } |
||
| 1601 | runge | 252 | } |
| 253 | |||
| 254 | std::string code = ""; |
||
| 255 | std::string line; |
||
| 256 | |||
| 257 | while (!file.eof()) |
||
| 258 | { |
||
| 259 | std::getline(file, line); |
||
| 260 | |||
| 261 | if (file.eof()) |
||
| 262 | { |
||
| 263 | break; |
||
| 264 | } |
||
| 265 | |||
| 266 | code += line + "\n"; |
||
| 267 | } |
||
| 268 | |||
| 1602 | runge | 269 | code += "\n"; |
| 270 | |||
| 1601 | runge | 271 | file.close(); |
| 272 | |||
| 1740 | runge | 273 | v8::Locker lock; |
| 1601 | runge | 274 | v8::Context::Scope context_scope(this->context_); |
| 275 | |||
| 276 | v8::TryCatch try_catch; |
||
| 277 | |||
| 278 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
||
| 279 | v8::String::New(scriptname.data(), scriptname.length())); |
||
| 280 | |||
| 281 | if (script.IsEmpty()) |
||
| 282 | { |
||
| 283 | LOG.Error(this->FormatException(try_catch)); |
||
| 1608 | runge | 284 | return false; |
| 1601 | runge | 285 | } |
| 286 | else |
||
| 287 | { |
||
| 288 | Value result = script->Run(); |
||
| 289 | |||
| 290 | if (result.IsEmpty()) |
||
| 291 | { |
||
| 292 | LOG.Error(this->FormatException(try_catch)); |
||
| 1608 | runge | 293 | return false; |
| 1601 | runge | 294 | } |
| 295 | } |
||
| 296 | |||
| 1644 | runge | 297 | this->loaded_scripts_.push_back(scriptname); |
| 298 | |||
| 1657 | runge | 299 | LOG.Info("\033[32mLoaded " + scriptname + " successfully.\033[0m"); |
| 1601 | runge | 300 | return true; |
| 301 | } |
||
| 302 | |||
| 1608 | runge | 303 | void Manager::ExecuteHandler(std::string plugin_name, unsigned int request_id, std::string code) |
| 304 | { |
||
| 305 | std::string scriptname = "execute_" + plugin_name + ".js"; |
||
| 306 | |||
| 1740 | runge | 307 | v8::Locker lock; |
| 1608 | runge | 308 | v8::Context::Scope context_scope(this->context_); |
| 309 | |||
| 310 | v8::TryCatch try_catch; |
||
| 311 | |||
| 312 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
||
| 313 | v8::String::New(scriptname.data(), scriptname.length())); |
||
| 314 | |||
| 1647 | runge | 315 | this->current_plugin_name_ = plugin_name; |
| 316 | this->current_request_id_ = request_id; |
||
| 317 | |||
| 1608 | runge | 318 | if (script.IsEmpty()) |
| 319 | { |
||
| 1647 | runge | 320 | this->CallOutput(this->FormatException(try_catch)); |
| 1608 | runge | 321 | } |
| 322 | else |
||
| 323 | { |
||
| 324 | Value result = script->Run(); |
||
| 325 | |||
| 326 | if (result.IsEmpty()) |
||
| 327 | { |
||
| 1647 | runge | 328 | this->CallOutput(this->FormatException(try_catch)); |
| 1608 | runge | 329 | } |
| 330 | else |
||
| 331 | { |
||
| 1647 | runge | 332 | //v8::String::AsciiValue response(result->ToString()); |
| 333 | //this->CallOutput(std::string(*response)); |
||
| 1608 | runge | 334 | } |
| 335 | } |
||
| 1647 | runge | 336 | |
| 337 | this->current_plugin_name_ = ""; |
||
| 338 | this->current_request_id_ = 0; |
||
| 1608 | runge | 339 | } |
| 340 | |||
| 1601 | runge | 341 | std::string Manager::FormatException(v8::TryCatch& try_catch) |
| 342 | { |
||
| 1740 | runge | 343 | v8::Locker lock; |
| 1625 | runge | 344 | v8::Context::Scope context_scope(this->context_); |
| 345 | |||
| 1601 | runge | 346 | std::string result; |
| 347 | |||
| 348 | v8::HandleScope handle_scope; |
||
| 349 | v8::String::Utf8Value exception(try_catch.Exception()); |
||
| 350 | v8::Handle<v8::Message> message = try_catch.Message(); |
||
| 351 | |||
| 352 | if (message.IsEmpty()) |
||
| 353 | { |
||
| 354 | result = *exception; |
||
| 355 | } |
||
| 356 | else |
||
| 357 | { |
||
| 358 | v8::String::Utf8Value filename(message->GetScriptResourceName()); |
||
| 359 | |||
| 360 | result = std::string(*filename) + ":" + boost::lexical_cast<std::string>(message->GetLineNumber()) + ": " + std::string(*exception); |
||
| 361 | |||
| 362 | /*v8::String::Utf8Value sourceline(message->GetSourceLine()); |
||
| 363 | string strSourceline = *sourceline; |
||
| 364 | |||
| 365 | result += strSourceline + "\n"; |
||
| 366 | |||
| 367 | string underline = rpad("", message->GetStartColumn(), ' '); |
||
| 368 | underline = rpad(underline, message->GetEndColumn(), '^'); |
||
| 369 | |||
| 370 | result += underline + "\n";*/ |
||
| 371 | } |
||
| 372 | |||
| 373 | return result; |
||
| 374 | } |
||
| 375 | |||
| 376 | }; // namespace vm |
||
| 377 | }; // namespace atom |