Subversion Repositories HomeAutomation

Rev

Rev 1609 | Rev 1644 | 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 "System.h"
22
 
1604 runge 23
#include <v8-debug.h>
1601 runge 24
#include <stdio.h>
25
 
26
#include "vm/Manager.h"
27
 
28
namespace atom {
29
namespace vm {
30
namespace plugin {
31
 
32
logging::Logger System::LOG("vm::plugin::System");
33
 
1625 runge 34
System::System(boost::asio::io_service& io_service, bool legacy) : Plugin(io_service)
1601 runge 35
{
36
    this->name_ = "system";
1609 runge 37
 
38
    this->legacy_ = legacy;
1601 runge 39
 
40
    this->ExportFunction("Log",        System::Export_Log);
41
    this->ExportFunction("LoadScript", System::Export_LoadScript);
42
    this->ExportFunction("Execute",    System::Export_Execute);
43
}
44
 
45
System::~System()
46
{
47
 
48
}
49
 
50
void System::InitializeDone()
51
{
1625 runge 52
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
53
 
1601 runge 54
    Plugin::InitializeDone();
55
 
1608 runge 56
    this->ImportFunction("Start");
57
 
1609 runge 58
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
59
 
60
    arguments->push_back(v8::Boolean::New(this->legacy_));
61
 
62
    this->Call(0, "Start", arguments);
1601 runge 63
}
64
 
65
Value System::Export_Log(const v8::Arguments& args)
66
{
1625 runge 67
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
68
 
69
    LOG.Debug(std::string(__FUNCTION__) + " called!");
70
 
1604 runge 71
    if (args.Length() < 1)
72
    {
73
        LOG.Error("To few arguments to log.");
74
    }
75
 
1601 runge 76
    v8::String::AsciiValue str(args[0]);
77
 
78
    LOG.Info(*str);
79
 
80
    return v8::Undefined();
81
}
82
 
83
Value System::Export_LoadScript(const v8::Arguments& args)
84
{
1625 runge 85
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 86
 
1625 runge 87
    LOG.Debug(std::string(__FUNCTION__) + " called!");
88
 
1604 runge 89
    if (args.Length() < 1)
90
    {
91
        LOG.Error("To few arguments.");
92
    }
93
 
1601 runge 94
    v8::String::AsciiValue str(args[0]);
95
 
1608 runge 96
    return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
1601 runge 97
}
98
 
99
Value System::Export_Execute(const v8::Arguments& args)
100
{
1625 runge 101
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 102
 
1625 runge 103
    LOG.Debug(std::string(__FUNCTION__) + " called!");
104
 
1604 runge 105
    if (args.Length() < 1)
106
    {
107
        LOG.Error("To few arguments.");
108
    }
109
 
1601 runge 110
    v8::String::AsciiValue command(args[0]);
111
 
112
    std::string output_buffer;
113
    char output[128];
114
 
115
    FILE* pipe = popen(*command, "r" );
116
 
117
    if (pipe == NULL)
118
    {
119
        LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
120
        return v8::Boolean::New(false);
121
    }
122
 
123
    while (!feof(pipe))
124
    {
125
        if (fgets(output, 128, pipe) != NULL)
126
        {
127
            output_buffer += output;
128
        }
129
    }
130
 
131
    pclose(pipe);
132
 
133
    return v8::String::New(output_buffer.data());
134
}
135
 
136
}; // namespace plugin
137
}; // namespace vm
138
}; // namespace atom