Subversion Repositories HomeAutomation

Rev

Rev 1651 | Rev 1741 | 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());
82
 
83
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
84
 
85
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
86
    arguments->push_back(v8::Integer::New(client_id));
87
    arguments->push_back(v8::String::New(data.ToCharString().data()));
88
 
89
    this->Call( client_id, "Socket_OnNewData", arguments);
90
}
91
 
92
void Socket::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
93
{
1740 runge 94
    v8::Locker lock;
1651 runge 95
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
96
 
97
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
98
 
99
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
100
    arguments->push_back(v8::Integer::New(client_id));
101
    arguments->push_back(v8::Uint32::New((unsigned int)client_state));
102
 
103
    this->Call( client_id, "Socket_OnNewState", arguments);
104
}
105
 
106
Value Socket::Export_Connect(const v8::Arguments& args)
107
{
1740 runge 108
    v8::Locker lock;
1651 runge 109
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
110
 
111
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
112
 
113
    if (args.Length() < 2)
114
    {
115
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
116
        return v8::Boolean::New(false);
117
    }
118
 
119
    v8::String::AsciiValue address(args[0]);
120
 
121
    net::ClientId client_id = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value());
122
 
123
    return v8::Integer::New(client_id);
124
}
125
 
126
Value Socket::Export_Disconnect(const v8::Arguments& args)
127
{
1740 runge 128
    v8::Locker lock;
1651 runge 129
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
130
 
131
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
132
 
133
    if (args.Length() < 1)
134
    {
135
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
136
        return v8::Boolean::New(false);
137
    }
138
 
139
    net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
140
 
141
    return v8::Undefined();
142
}
143
 
144
Value Socket::Export_Send(const v8::Arguments& args)
145
{
1740 runge 146
    v8::Locker lock;
1651 runge 147
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
148
 
149
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
150
 
151
    if (args.Length() < 2)
152
    {
153
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
154
        return v8::Boolean::New(false);
155
    }
156
 
157
    v8::String::AsciiValue data(args[1]);
158
 
159
    net::Manager::Instance()->SendTo(args[0]->Uint32Value(), std::string(*data));
160
 
161
    return v8::Undefined();
162
}
163
 
164
}; // namespace plugin
165
}; // namespace vm
166
}; // namespace atom