Subversion Repositories HomeAutomation

Rev

Rev 1898 | Rev 1954 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1651 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 "Socket.h"
22
 
23
#include <v8-debug.h>
24
#include <stdio.h>
25
 
26
#include <boost/lexical_cast.hpp>
27
 
28
#include "vm/Manager.h"
29
#include "net/Manager.h"
30
 
31
namespace atom {
32
namespace vm {
33
namespace plugin {
34
 
35
logging::Logger Socket::LOG("vm::plugin::Socket");
36
 
37
Socket::Socket(boost::asio::io_service& io_service) : Plugin(io_service)
38
{
39
    this->name_ = "socket";
40
 
41
    this->ExportFunction("SocketExport_Connect",     Socket::Export_Connect);
42
    this->ExportFunction("SocketExport_Disconnect",  Socket::Export_Disconnect);
43
    this->ExportFunction("SocketExport_Send",        Socket::Export_Send);
44
}
45
 
46
Socket::~Socket()
47
{
48
 
49
}
50
 
51
void Socket::InitializeDone()
52
{
53
    Plugin::InitializeDone();
54
 
55
    this->ImportFunction("Socket_OnNewData");
56
    this->ImportFunction("Socket_OnNewState");
57
 
58
    net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Socket::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
59
                                           net::Manager::SignalOnNewData::slot_type(&Socket::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
60
}
61
 
62
void Socket::CallOutput(unsigned int request_id, std::string output)
63
{
64
    LOG.Info(output);
65
}
66
 
67
void Socket::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
68
{
69
    common::Byteset temp_buffer(data);
70
    this->io_service_.post(boost::bind(&Socket::SlotOnNewDataHandler, this, client_id, server_id, temp_buffer));
71
}
72
 
73
void Socket::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
74
{
75
    this->io_service_.post(boost::bind(&Socket::SlotOnNewStateHandler, this, client_id, server_id, client_state));
76
}
77
 
78
void Socket::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
79
{
1740 runge 80
    v8::Locker lock;
1651 runge 81
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 82
    v8::HandleScope handle_scope;
1651 runge 83
 
1898 runge 84
    LOG.Debug(std::string(__FUNCTION__) + " called!");
1651 runge 85
 
86
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
87
    arguments->push_back(v8::Integer::New(client_id));
88
    arguments->push_back(v8::String::New(data.ToCharString().data()));
89
 
1898 runge 90
    this->Call(client_id, "Socket_OnNewData", arguments);
1651 runge 91
}
92
 
93
void Socket::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
94
{
1740 runge 95
    v8::Locker lock;
1651 runge 96
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 97
    v8::HandleScope handle_scope;
1651 runge 98
 
1898 runge 99
    LOG.Debug(std::string(__FUNCTION__) + " called!");
1651 runge 100
 
101
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
102
    arguments->push_back(v8::Integer::New(client_id));
103
    arguments->push_back(v8::Uint32::New((unsigned int)client_state));
104
 
1741 runge 105
    this->Call(client_id, "Socket_OnNewState", arguments);
1651 runge 106
}
107
 
108
Value Socket::Export_Connect(const v8::Arguments& args)
109
{
1940 runge 110
    net::ClientId client_id = 0;
1740 runge 111
    v8::Locker lock;
1651 runge 112
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 113
    v8::HandleScope handle_scope;
1651 runge 114
 
1898 runge 115
    LOG.Debug(std::string(__FUNCTION__) + " called!");
1651 runge 116
 
117
    if (args.Length() < 2)
118
    {
119
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 120
        return handle_scope.Close(v8::Boolean::New(false));
1651 runge 121
    }
122
 
123
    v8::String::AsciiValue address(args[0]);
124
 
1940 runge 125
    try
126
    {
127
        client_id = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value());
128
    }
129
    catch (std::runtime_error &e)
130
    {
131
    }
132
 
1741 runge 133
    return handle_scope.Close(v8::Integer::New(client_id));
1651 runge 134
}
135
 
136
Value Socket::Export_Disconnect(const v8::Arguments& args)
137
{
1740 runge 138
    v8::Locker lock;
1651 runge 139
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 140
    v8::HandleScope handle_scope;
1651 runge 141
 
1898 runge 142
    LOG.Debug(std::string(__FUNCTION__) + " called!");
1651 runge 143
 
144
    if (args.Length() < 1)
145
    {
146
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 147
        return handle_scope.Close(v8::Boolean::New(false));
1651 runge 148
    }
149
 
150
    net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
151
 
1741 runge 152
    return handle_scope.Close(v8::Undefined());
1651 runge 153
}
154
 
155
Value Socket::Export_Send(const v8::Arguments& args)
156
{
1740 runge 157
    v8::Locker lock;
1651 runge 158
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 159
    v8::HandleScope handle_scope;
1651 runge 160
 
1898 runge 161
    LOG.Debug(std::string(__FUNCTION__) + " called!");
1651 runge 162
 
163
    if (args.Length() < 2)
164
    {
165
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 166
        return handle_scope.Close(v8::Boolean::New(false));
1651 runge 167
    }
168
 
169
    v8::String::AsciiValue data(args[1]);
170
 
171
    net::Manager::Instance()->SendTo(args[0]->Uint32Value(), std::string(*data));
172
 
1741 runge 173
    return handle_scope.Close(v8::Undefined());
1651 runge 174
}
175
 
176
}; // namespace plugin
177
}; // namespace vm
178
}; // namespace atom
1940 runge 179