-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherlnode.cc
173 lines (119 loc) · 4.83 KB
/
erlnode.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* erlnode.cc - ErlNode implementation
*
* Author: Sergey Chernov <[email protected]>
*/
#define BUILDING_NODE_EXTENSION
#include <iostream>
#include <sstream>
#include <cstdio>
/* Nodejs and V8 includes */
#include <v8.h>
#include <node.h>
#include <node_version.h>
/* Erlang includes */
#include <erl_interface.h>
#include <ei.h>
#include "erlnode.h"
using namespace v8;
using namespace node;
Persistent<Function> ErlNode::constructor;
const char* ErlNode::DEFAULT_NODE_NAME = "cnode@localhost";
const char* ErlNode::DEFAULT_SECRET = "secret";
ErlNode::ErlNode(const char* name, const char* secretCookie, int32_t instanceId) {
this->name = name;
this->instanceId = instanceId;
std::cout << "this node name: " << this->name << ", instanceId: " << this->instanceId << "\n";
erl_connect_init(1, (char*)secretCookie, this->instanceId);
}
ErlNode::~ErlNode() {
delete this->name;
}
Handle<Value> ErlNode::New(const Arguments& args) {
char* name = (char*)ErlNode::DEFAULT_NODE_NAME;
char* secretCookie = (char*)ErlNode::DEFAULT_SECRET;
int32_t instanceId = 0;
if (args[0]->IsObject()) {
Local<Object> obj = Local<Object>::Cast(args[0]);
Local<Value> value;
value = obj->Get(String::New("instanceId"));
if (!value->IsUndefined() && value->IsNumber())
instanceId = Local<Number>::Cast(value)->Value();
value = obj->Get(String::New("name"));
if (!value->IsUndefined() && value->IsString()) {
Local<String> string = Local<String>::Cast(value);
name = new char[string->Length() + 1];
string->WriteAscii(name);
}
value = obj->Get(String::New("secretCookie"));
if (!value->IsUndefined() && value->IsString()) {
Local<String> string = Local<String>::Cast(value);
secretCookie = new char[string->Length() + 1];
string->WriteAscii(secretCookie);
}
} else {
// TODO: throw exception or do something
}
ErlNode* erlNode = new ErlNode((const char*)name, (const char*)secretCookie, instanceId);
erlNode->Wrap(args.This());
return args.This();
}
void ErlNode::Init() {
Local<FunctionTemplate> tmpl = FunctionTemplate::New(New);
tmpl->SetClassName(String::NewSymbol(CLASS_NAME));
tmpl->InstanceTemplate()->SetInternalFieldCount(1);
// .protorype //
// getName()
Local<FunctionTemplate> getNameFunc = FunctionTemplate::New(getName);
tmpl->PrototypeTemplate()->Set(String::NewSymbol("getName"), getNameFunc->GetFunction());
// getInstanceId()
Local<FunctionTemplate> getInstanceIdFunc = FunctionTemplate::New(getInstanceId);
tmpl->PrototypeTemplate()->Set(String::NewSymbol("getInstanceId"), getInstanceIdFunc->GetFunction());
// connect()
Local<FunctionTemplate> connectFunc = FunctionTemplate::New(connect);
tmpl->PrototypeTemplate()->Set(String::NewSymbol("connect"), connectFunc->GetFunction());
constructor = Persistent<Function>::New(tmpl->GetFunction());
ErlNode::ErlNodeConnection::Init();
// erl_interface memory init function
erl_init(NULL, 0);
}
Handle<Value> ErlNode::NewInstance(const Arguments& args) {
HandleScope scope;
const unsigned argc = 1;
Handle<Value> argv[argc] = { args[0] };
Local<Object> instance = constructor->NewInstance(argc, argv);
return scope.Close(instance);
}
Handle<Value> ErlNode::getName(const Arguments& args){
HandleScope scope;
ErlNode* that = ObjectWrap::Unwrap<ErlNode>(args.This());
return scope.Close(String::New(that->name));
}
Handle<Value> ErlNode::getInstanceId(const Arguments& args) {
HandleScope scope;
ErlNode* that = ObjectWrap::Unwrap<ErlNode>(args.This());
return scope.Close(Integer::New(that->instanceId));
}
Handle<Value> ErlNode::connect(const Arguments& args) {
HandleScope scope;
if (!args[0]->IsString()) {
throw "Invalid nodeId value";
} else if (!args[1]->IsString()) {
throw "Invalid endpoint value";
}
Local<String> nodeIdArg = Local<String>::Cast(args[0]);
Local<String> endpointArg = Local<String>::Cast(args[1]);
char* nodeId = new char[nodeIdArg->Length() + 1];
char* endpoint = new char[endpointArg->Length() + 1];
nodeIdArg->WriteAscii(nodeId);
endpointArg->WriteAscii(endpoint);
ErlNode* erlNode = ObjectWrap::Unwrap<ErlNode>(args.This());
ErlNodeConnection* connection = erlNode->connect(nodeId, endpoint);
Local<Object> connectionObj = ErlNodeConnection::objTemplate->NewInstance();
connection->Wrap(connectionObj);
return scope.Close(connectionObj);
}
ErlNode::ErlNodeConnection* ErlNode::connect(const char* nodeId, const char* endpoint) {
// TODO: implement saving all child connections to linked list
return new ErlNodeConnection(nodeId, endpoint, *this);
}