Subversion Repositories HomeAutomation

Rev

Rev 1740 | Rev 1898 | 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
 
84
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
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
 
90
    this->Call( client_id, "Socket_OnNewData", arguments);
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
 
99
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
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
{
1740 runge 110
    v8::Locker lock;
1651 runge 111
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 112
    v8::HandleScope handle_scope;
1651 runge 113
 
114
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
115
 
116
    if (args.Length() < 2)
117
    {
118
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 119
        return handle_scope.Close(v8::Boolean::New(false));
1651 runge 120
    }
121
 
122
    v8::String::AsciiValue address(args[0]);
123
 
124
    net::ClientId client_id = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value());
125
 
1741 runge 126
    return handle_scope.Close(v8::Integer::New(client_id));
1651 runge 127
}
128
 
129
Value Socket::Export_Disconnect(const v8::Arguments& args)
130
{
1740 runge 131
    v8::Locker lock;
1651 runge 132
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 133
    v8::HandleScope handle_scope;
1651 runge 134
 
135
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
136
 
137
    if (args.Length() < 1)
138
    {
139
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 140
        return handle_scope.Close(v8::Boolean::New(false));
1651 runge 141
    }
142
 
143
    net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
144
 
1741 runge 145
    return handle_scope.Close(v8::Undefined());
1651 runge 146
}
147
 
148
Value Socket::Export_Send(const v8::Arguments& args)
149
{
1740 runge 150
    v8::Locker lock;
1651 runge 151
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 152
    v8::HandleScope handle_scope;
1651 runge 153
 
154
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
155
 
156
    if (args.Length() < 2)
157
    {
158
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 159
        return handle_scope.Close(v8::Boolean::New(false));
1651 runge 160
    }
161
 
162
    v8::String::AsciiValue data(args[1]);
163
 
164
    net::Manager::Instance()->SendTo(args[0]->Uint32Value(), std::string(*data));
165
 
1741 runge 166
    return handle_scope.Close(v8::Undefined());
1651 runge 167
}
168
 
169
}; // namespace plugin
170
}; // namespace vm
171
}; // namespace atom