Subversion Repositories HomeAutomation

Rev

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