Rev 1741 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1741 | Rev 2107 | ||
|---|---|---|---|
| Line 86... | Line 86... | ||
| 86 | for (unsigned int n = 0; n < this->plugins_.size(); n++) |
86 | for (unsigned int n = 0; n < this->plugins_.size(); n++) |
| 87 | { |
87 | { |
| 88 | if (this->plugins_[n]->GetName() == this->current_plugin_name_) |
88 | if (this->plugins_[n]->GetName() == this->current_plugin_name_) |
| 89 | { |
89 | { |
| 90 | this->plugins_[n]->CallOutput(this->current_request_id_, output); |
90 | this->plugins_[n]->CallOutput(this->current_request_id_, output); |
| - | 91 | return; |
|
| - | 92 | } |
|
| - | 93 | } |
|
| - | 94 | } |
|
| - | 95 | else |
|
| - | 96 | { |
|
| - | 97 | LOG.Info(output); |
|
| - | 98 | } |
|
| - | 99 | } |
|
| - | 100 | ||
| - | 101 | void Manager::CallResult(std::string output) |
|
| - | 102 | { |
|
| - | 103 | if (this->current_plugin_name_ != "") |
|
| - | 104 | { |
|
| - | 105 | for (unsigned int n = 0; n < this->plugins_.size(); n++) |
|
| - | 106 | { |
|
| - | 107 | if (this->plugins_[n]->GetName() == this->current_plugin_name_) |
|
| - | 108 | { |
|
| - | 109 | this->plugins_[n]->CallResult(this->current_request_id_, output); |
|
| 91 | return; |
110 | return; |
| 92 | } |
111 | } |
| 93 | } |
112 | } |
| 94 | } |
113 | } |
| 95 | else |
114 | else |
| Line 103... | Line 122... | ||
| 103 | v8::Locker lock; |
122 | v8::Locker lock; |
| 104 | v8::Context::Scope context_scope(Manager::Instance()->GetContext()); |
123 | v8::Context::Scope context_scope(Manager::Instance()->GetContext()); |
| 105 | v8::HandleScope handle_scope; |
124 | v8::HandleScope handle_scope; |
| 106 | 125 | ||
| 107 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
126 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 108 | 127 | ||
| 109 | if (args.Length() < 1) |
128 | if (args.Length() < 1) |
| 110 | { |
129 | { |
| 111 | Manager::instance_->LOG.Error(std::string(__FUNCTION__) + ": To few arguments to log."); |
130 | Manager::instance_->LOG.Error(std::string(__FUNCTION__) + ": To few arguments to log."); |
| 112 | return v8::Undefined(); |
131 | return v8::Undefined(); |
| 113 | } |
132 | } |
| 114 | 133 | ||
| 115 | v8::String::AsciiValue str(args[0]); |
134 | v8::String::AsciiValue str(args[0]); |
| 116 | 135 | ||
| 117 | Manager::instance_->CallOutput(*str); |
136 | Manager::instance_->CallOutput(*str); |
| 118 | 137 | ||
| 119 | return handle_scope.Close(v8::Undefined()); |
138 | return handle_scope.Close(v8::Undefined()); |
| 120 | } |
139 | } |
| 121 | 140 | ||
| 122 | void Manager::Call(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
141 | void Manager::Call(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
| 123 | { |
142 | { |
| 124 | this->io_service_.post(boost::bind(&Manager::CallHandler, this, plugin_name, request_id, name, arguments)); |
143 | this->io_service_.post(boost::bind(&Manager::CallHandler, this, plugin_name, request_id, name, arguments)); |
| 125 | } |
144 | } |
| 126 | 145 | ||
| 127 | void Manager::Execute(std::string plugin_name, unsigned int request_id, std::string code) |
146 | void Manager::Execute(std::string plugin_name, unsigned int request_id, std::string code) |
| 128 | { |
147 | { |
| 129 | this->io_service_.post(boost::bind(&Manager::ExecuteHandler, this, plugin_name, request_id, code)); |
148 | this->io_service_.post(boost::bind(&Manager::ExecuteHandler, this, plugin_name, request_id, code)); |
| 130 | } |
149 | } |
| 131 | 150 | ||
| 132 | bool Manager::CallHandler(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
151 | bool Manager::CallHandler(std::string plugin_name, unsigned int request_id, std::string name, ArgumentListPointer arguments) |
| 133 | { |
152 | { |
| 134 | v8::Locker lock; |
153 | v8::Locker lock; |
| 135 | v8::Context::Scope context_scope(this->context_); |
154 | v8::Context::Scope context_scope(this->context_); |
| Line 139... | Line 158... | ||
| 139 | this->current_request_id_ = request_id; |
158 | this->current_request_id_ = request_id; |
| 140 | 159 | ||
| 141 | if (this->functions_.find(name) == this->functions_.end()) |
160 | if (this->functions_.find(name) == this->functions_.end()) |
| 142 | { |
161 | { |
| 143 | this->CallOutput("No such function found, " + name); |
162 | this->CallOutput("No such function found, " + name); |
| 144 | 163 | ||
| 145 | this->current_plugin_name_ = ""; |
164 | this->current_plugin_name_ = ""; |
| 146 | this->current_request_id_ = 0; |
165 | this->current_request_id_ = 0; |
| 147 | return false; |
166 | return false; |
| 148 | } |
167 | } |
| 149 | 168 | ||
| 150 | //LOG.Debug("Call: " + name); |
169 | //LOG.Debug("Call: " + name); |
| 151 | 170 | ||
| 152 | Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data()); |
171 | Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data()); |
| 153 | 172 | ||
| 154 | if (result.IsEmpty()) |
173 | if (result.IsEmpty()) |
| 155 | { |
174 | { |
| 156 | this->CallOutput(this->FormatException(try_catch)); |
175 | this->CallOutput(this->FormatException(try_catch)); |
| 157 | 176 | ||
| 158 | this->current_plugin_name_ = ""; |
177 | this->current_plugin_name_ = ""; |
| 159 | this->current_request_id_ = 0; |
178 | this->current_request_id_ = 0; |
| 160 | return false; |
179 | return false; |
| 161 | } |
180 | } |
| 162 | 181 | ||
| 163 | //v8::String::AsciiValue response(result->ToString()); |
182 | //v8::String::AsciiValue response(result->ToString()); |
| 164 | //this->CallOutput(plugin_name, request_id, std::string(*response)); |
183 | //this->CallOutput(plugin_name, request_id, std::string(*response)); |
| Line 173... | Line 192... | ||
| 173 | v8::Locker lock; |
192 | v8::Locker lock; |
| 174 | v8::HandleScope handle_scope; |
193 | v8::HandleScope handle_scope; |
| 175 | v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New(); |
194 | v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New(); |
| 176 | 195 | ||
| 177 | raw_template->Set(v8::String::New("Log"), v8::FunctionTemplate::New(Manager::Export_Log)); |
196 | raw_template->Set(v8::String::New("Log"), v8::FunctionTemplate::New(Manager::Export_Log)); |
| 178 | 197 | ||
| 179 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
198 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 180 | { |
199 | { |
| 181 | ExportFunctionList export_functions = this->plugins_[c]->GetExportFunctions(); |
200 | ExportFunctionList export_functions = this->plugins_[c]->GetExportFunctions(); |
| 182 | 201 | ||
| 183 | for (ExportFunctionList::iterator it = export_functions.begin(); it != export_functions.end(); it++) |
202 | for (ExportFunctionList::iterator it = export_functions.begin(); it != export_functions.end(); it++) |
| 184 | { |
203 | { |
| 185 | raw_template->Set(v8::String::New(it->first.data()), v8::FunctionTemplate::New(it->second)); |
204 | raw_template->Set(v8::String::New(it->first.data()), v8::FunctionTemplate::New(it->second)); |
| 186 | } |
205 | } |
| 187 | } |
206 | } |
| 188 | 207 | ||
| 189 | this->context_ = v8::Context::New(NULL, raw_template); |
208 | this->context_ = v8::Context::New(NULL, raw_template); |
| 190 | 209 | ||
| 191 | 210 | ||
| 192 | v8::Context::Scope context_scope(this->context_); |
211 | v8::Context::Scope context_scope(this->context_); |
| 193 | 212 | ||
| 194 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
213 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 195 | { |
214 | { |
| 196 | this->LoadScript("plugin/" + this->plugins_[c]->GetName() + ".js"); |
215 | this->LoadScript("plugin/" + this->plugins_[c]->GetName() + ".js"); |
| 197 | } |
216 | } |
| 198 | 217 | ||
| 199 | this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template); |
218 | this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template); |
| 200 | 219 | ||
| 201 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
220 | for (unsigned int c = 0; c < this->plugins_.size(); c++) |
| 202 | { |
221 | { |
| 203 | this->plugins_[c]->InitializeDone(); |
222 | this->plugins_[c]->InitializeDone(); |
| 204 | } |
223 | } |
| 205 | } |
224 | } |
| 206 | 225 | ||
| 207 | bool Manager::ImportFunction(std::string functionname) |
226 | bool Manager::ImportFunction(std::string functionname) |
| 208 | { |
227 | { |
| 209 | v8::Locker lock; |
228 | v8::Locker lock; |
| 210 | v8::Context::Scope context_scope(this->context_); |
229 | v8::Context::Scope context_scope(this->context_); |
| 211 | 230 | ||
| 212 | v8::TryCatch try_catch; |
231 | v8::TryCatch try_catch; |
| 213 | 232 | ||
| 214 | v8::Handle<v8::String> process_name = v8::String::New(functionname.data()); |
233 | v8::Handle<v8::String> process_name = v8::String::New(functionname.data()); |
| 215 | v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name); |
234 | v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name); |
| 216 | 235 | ||
| 217 | if (!process_val->IsFunction()) |
236 | if (!process_val->IsFunction()) |
| 218 | { |
237 | { |
| 219 | LOG.Error(functionname + " was not found to be a function!"); |
238 | LOG.Error(functionname + " was not found to be a function!"); |
| 220 | return false; |
239 | return false; |
| 221 | } |
240 | } |
| 222 | 241 | ||
| 223 | v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val); |
242 | v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val); |
| Line 271... | Line 290... | ||
| 271 | 290 | ||
| 272 | code += "\n"; |
291 | code += "\n"; |
| 273 | 292 | ||
| 274 | file.close(); |
293 | file.close(); |
| 275 | 294 | ||
| 276 | v8::Locker lock; |
295 | v8::Locker lock; |
| 277 | v8::Context::Scope context_scope(this->context_); |
296 | v8::Context::Scope context_scope(this->context_); |
| 278 | 297 | ||
| 279 | v8::TryCatch try_catch; |
298 | v8::TryCatch try_catch; |
| 280 | 299 | ||
| 281 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
300 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()), |
| 282 | v8::String::New(scriptname.data(), scriptname.length())); |
301 | v8::String::New(scriptname.data(), scriptname.length())); |
| 283 | 302 | ||
| Line 330... | Line 349... | ||
| 330 | { |
349 | { |
| 331 | this->CallOutput(this->FormatException(try_catch)); |
350 | this->CallOutput(this->FormatException(try_catch)); |
| 332 | } |
351 | } |
| 333 | else |
352 | else |
| 334 | { |
353 | { |
| 335 |
|
354 | v8::String::AsciiValue response(result->ToString()); |
| 336 |
|
355 | this->CallResult(std::string(*response)); |
| 337 | } |
356 | } |
| 338 | } |
357 | } |
| 339 | 358 | ||
| 340 | this->current_plugin_name_ = ""; |
359 | this->current_plugin_name_ = ""; |
| 341 | this->current_request_id_ = 0; |
360 | this->current_request_id_ = 0; |