Subversion Repositories HomeAutomation

Rev

Rev 1619 | Rev 1642 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1619 Rev 1625
Line 108... Line 108...
108
    if (this->functions_.find(name) == this->functions_.end())
108
    if (this->functions_.find(name) == this->functions_.end())
109
    {
109
    {
110
        this->SendResponse(plugin_name, request_id, "No such function found, " + name);
110
        this->SendResponse(plugin_name, request_id, "No such function found, " + name);
111
        return;
111
        return;
112
    }
112
    }
-
 
113
 
-
 
114
    LOG.Debug("Call: " + name);
113
 
115
 
114
    Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data());
116
    Value result = this->functions_[name]->Call(this->context_->Global(), arguments->size(), arguments->data());
115
   
117
   
116
    if (result.IsEmpty())
118
    if (result.IsEmpty())
117
    {
119
    {
118
        this->SendResponse(plugin_name, request_id, this->FormatException(try_catch));
120
        this->SendResponse(plugin_name, request_id, this->FormatException(try_catch));
119
        return;
121
        return;
120
    }
122
    }
121
   
123
   
122
    v8::String::AsciiValue response(result->ToString());
124
    v8::String::AsciiValue response(result->ToString());
123
   
125
   
124
    this->SendResponse(plugin_name, request_id, std::string(*response));
126
    this->SendResponse(plugin_name, request_id, std::string(*response));
125
}
127
}
126
 
128
 
127
void Manager::StartHandler()
129
void Manager::StartHandler()
128
{
130
{
129
    v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New();
131
    v8::Handle<v8::ObjectTemplate> raw_template = v8::ObjectTemplate::New();
130
   
132
   
131
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
133
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
132
    {
134
    {
133
        ExportFunctionList export_functions = this->plugins_[c]->GetExportFunctions();
135
        ExportFunctionList export_functions = this->plugins_[c]->GetExportFunctions();
134
       
136
       
135
        for (ExportFunctionList::iterator it = export_functions.begin(); it != export_functions.end(); it++)
137
        for (ExportFunctionList::iterator it = export_functions.begin(); it != export_functions.end(); it++)
136
        {
138
        {
137
            raw_template->Set(v8::String::New(it->first.data()), v8::FunctionTemplate::New(it->second));
139
            raw_template->Set(v8::String::New(it->first.data()), v8::FunctionTemplate::New(it->second));
138
        }
140
        }
139
    }
141
    }
140
   
142
   
141
    this->context_ = v8::Context::New(NULL, raw_template);
143
    this->context_ = v8::Context::New(NULL, raw_template);
142
   
144
   
143
    v8::Context::Scope context_scope(this->context_);
145
    v8::Context::Scope context_scope(this->context_);
144
   
146
   
145
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
147
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
146
    {
148
    {
147
        this->LoadScript("plugin/" + this->plugins_[c]->GetName() + ".js");
149
        this->LoadScript("plugin/" + this->plugins_[c]->GetName() + ".js");
148
    }
150
    }
149
   
151
   
150
    this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template);
152
    this->object_template_ = v8::Persistent<v8::ObjectTemplate>::New(raw_template);
151
   
153
   
152
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
154
    for (unsigned int c = 0; c < this->plugins_.size(); c++)
153
    {
155
    {
154
        this->plugins_[c]->InitializeDone();
156
        this->plugins_[c]->InitializeDone();
155
    }
157
    }
156
   
158
   
157
    LOG.Info("VM initialized.");
159
    LOG.Info("VM initialized.");
158
}
160
}
159
 
161
 
160
bool Manager::ImportFunction(std::string functionname)
162
bool Manager::ImportFunction(std::string functionname)
Line 167... Line 169...
167
    v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name);
169
    v8::Handle<v8::Value> process_val = this->context_->Global()->Get(process_name);
168
   
170
   
169
    if (!process_val->IsFunction())
171
    if (!process_val->IsFunction())
170
    {
172
    {
171
        LOG.Error(functionname + " was not found to be a function!");
173
        LOG.Error(functionname + " was not found to be a function!");
172
        return false;
174
        return false;
173
    }
175
    }
174
   
176
   
175
    v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val);
177
    v8::Handle<v8::Function> process_fun = v8::Handle<v8::Function>::Cast(process_val);
176
   
178
   
177
    this->functions_[functionname] = v8::Persistent<v8::Function>::New(process_fun);
179
    this->functions_[functionname] = v8::Persistent<v8::Function>::New(process_fun);
178
   
180
   
179
    return true;
181
    return true;
180
}
182
}
181
 
183
 
182
bool Manager::LoadScript(std::string scriptname)
184
bool Manager::LoadScript(std::string scriptname)
183
{
185
{
184
    std::string path = this->script_path_ + scriptname;
186
    std::string path = this->script_path_ + scriptname;
185
    std::ifstream file(path.data());
187
    std::ifstream file(path.data());
186
   
188
   
187
    if (!file.is_open())
189
    if (!file.is_open())
188
    {
190
    {
189
        file.close();
191
        file.close();
190
        LOG.Warning("Could not load " + scriptname + ".");
192
        LOG.Warning("Could not load " + scriptname + ".");
191
        return false;
193
        return false;
192
    }
194
    }
193
   
195
   
194
    std::string code = "";
196
    std::string code = "";
195
    std::string line;
197
    std::string line;
196
   
198
   
197
    while (!file.eof())
199
    while (!file.eof())
198
    {
200
    {
199
        std::getline(file, line);
201
        std::getline(file, line);
200
       
202
       
201
        if (file.eof())
203
        if (file.eof())
202
        {
204
        {
203
            break;
205
            break;
204
        }
206
        }
205
       
207
       
206
        code += line + "\n";
208
        code += line + "\n";
207
    }
209
    }
208
   
210
   
209
    code += "\n";
211
    code += "\n";
210
   
212
   
211
    file.close();
213
    file.close();
212
   
214
   
213
    v8::Context::Scope context_scope(this->context_);
215
    v8::Context::Scope context_scope(this->context_);
214
   
216
   
215
    v8::TryCatch try_catch;
217
    v8::TryCatch try_catch;
216
   
218
   
217
    v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()),
219
    v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()),
218
                                                        v8::String::New(scriptname.data(), scriptname.length()));
220
                                                        v8::String::New(scriptname.data(), scriptname.length()));
219
   
221
   
220
    if (script.IsEmpty())
222
    if (script.IsEmpty())
221
    {
223
    {
222
        LOG.Error(this->FormatException(try_catch));
224
        LOG.Error(this->FormatException(try_catch));
223
        return false;
225
        return false;
224
    }
226
    }
225
    else
227
    else
226
    {
228
    {
227
        Value result = script->Run();
229
        Value result = script->Run();
228
       
230
       
229
        if (result.IsEmpty())
231
        if (result.IsEmpty())
230
        {
232
        {
231
            LOG.Error(this->FormatException(try_catch));
233
            LOG.Error(this->FormatException(try_catch));
232
            return false;
234
            return false;
233
        }
235
        }
234
    }
236
    }
235
   
237
   
236
    LOG.Info("Loaded " + scriptname + " successfully.");
238
    LOG.Info("Loaded " + scriptname + " successfully.");
237
    return true;
239
    return true;
238
}
240
}
239
 
241
 
240
void Manager::ExecuteHandler(std::string plugin_name, unsigned int request_id, std::string code)
242
void Manager::ExecuteHandler(std::string plugin_name, unsigned int request_id, std::string code)
241
{
243
{
242
    std::string scriptname = "execute_" + plugin_name + ".js";
244
    std::string scriptname = "execute_" + plugin_name + ".js";
243
   
245
   
244
    v8::Context::Scope context_scope(this->context_);
246
    v8::Context::Scope context_scope(this->context_);
245
   
247
   
246
    v8::TryCatch try_catch;
248
    v8::TryCatch try_catch;
247
   
249
   
248
    v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()),
250
    v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.data(), code.length()),
249
                                                        v8::String::New(scriptname.data(), scriptname.length()));
251
                                                        v8::String::New(scriptname.data(), scriptname.length()));
250
   
252
   
251
    if (script.IsEmpty())
253
    if (script.IsEmpty())
252
    {
254
    {
253
        this->SendResponse(plugin_name, request_id, this->FormatException(try_catch));
255
        this->SendResponse(plugin_name, request_id, this->FormatException(try_catch));
254
    }
256
    }
255
    else
257
    else
256
    {
258
    {
257
        Value result = script->Run();
259
        Value result = script->Run();
258
       
260
       
259
        if (result.IsEmpty())
261
        if (result.IsEmpty())
260
        {
262
        {
261
            this->SendResponse(plugin_name, request_id, this->FormatException(try_catch));
263
            this->SendResponse(plugin_name, request_id, this->FormatException(try_catch));
262
        }
264
        }
263
        else
265
        else
264
        {
266
        {
265
            v8::String::AsciiValue response(result->ToString());
267
            v8::String::AsciiValue response(result->ToString());
266
           
268
           
267
            this->SendResponse(plugin_name, request_id, std::string(*response));
269
            this->SendResponse(plugin_name, request_id, std::string(*response));
268
        }
270
        }
269
    }
271
    }
270
}
272
}
271
 
273
 
272
std::string Manager::FormatException(v8::TryCatch& try_catch)
274
std::string Manager::FormatException(v8::TryCatch& try_catch)
273
{
275
{
-
 
276
    v8::Context::Scope context_scope(this->context_);
-
 
277
   
274
    std::string result;
278
    std::string result;
275
   
279
   
276
    v8::HandleScope handle_scope;
280
    v8::HandleScope handle_scope;
277
    v8::String::Utf8Value exception(try_catch.Exception());
281
    v8::String::Utf8Value exception(try_catch.Exception());
278
    v8::Handle<v8::Message> message = try_catch.Message();
282
    v8::Handle<v8::Message> message = try_catch.Message();