cameo-spy-addon.cc 2.93 KB
Newer Older
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
#include <node.h>
#include <iostream>
#include <unistd.h>
#include <uv.h>
#include <thread>
#include <sstream>
#include <functional>
#include <string>
#include <cameo/cameo.h>

using namespace std;
using namespace std::placeholders;

namespace nomad {

using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Context;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Number;
using v8::Integer;
using v8::Boolean;
using v8::Array;
using v8::Persistent;

unique_ptr<cameo::Server> server;
unique_ptr<cameo::Server> remoteServer;
unique_ptr<cameo::application::Instance> vexpSpy;
unique_ptr<cameo::application::Requester> requester;
Isolate * v8Isolate;

string nomadEndpoint;
std::string VEXPSPY = "vexpspy";

/**
 * Init function to initialise the Cameo Nomad addon.
 */
void Init(const FunctionCallbackInfo<Value>& args) {

	cout << "initialising vEXP spy" << endl;

	// Get the V8 isolate.
	v8Isolate = args.GetIsolate();

legoc's avatar
legoc committed
49
	// Local endpoint.
50
	v8::String::Utf8Value param1(args[0]->ToString());
legoc's avatar
legoc committed
51
	string localEndpoint(*param1);
52

legoc's avatar
legoc committed
53
54
55
    // Nomad endpoint.
	v8::String::Utf8Value param2(args[1]->ToString());
	string nomadEndpoint(*param2);
56
57
58
59

	// Init the app if it is not already done.
	if (cameo::application::This::getId() == -1) {

legoc's avatar
legoc committed
60
		cameo::application::This::init("vEXP", localEndpoint);
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

		cout << "initialised cameo" << endl;
	}

    // Init nomad3d positions.
    server.reset(new cameo::Server(cameo::application::This::getServer().getEndpoint()));

    cout << "cameo server " << *server << endl;

    vexpSpy = server->connect(VEXPSPY);
	if (vexpSpy->exists()) {
		// The application exists from a previous server session
		vexpSpy->kill();
		cameo::application::State state = vexpSpy->waitFor();
		cout << "terminated old vEXP spy with state " << cameo::application::toString(state) << endl;
	}

	vector<string> appArgs;
    appArgs.push_back(nomadEndpoint);

    vexpSpy = server->start(VEXPSPY, appArgs);

	if (!vexpSpy->exists()) {
		cout << "no vEXP spy" << endl;
	}
    else {
        cout << "vEXP spy " << *vexpSpy << endl;
    }

    // Create the requester
	requester = cameo::application::Requester::create(*vexpSpy, "get_data");

	if (requester.get() == 0) {
		cout << "cannot create requester" << endl;
		return;
	}

	// Create the remote server.
	remoteServer.reset(new cameo::Server(nomadEndpoint));

	args.GetReturnValue().Set(Undefined(v8Isolate));
}

void GetData(const FunctionCallbackInfo<Value>& args) {

	std::string reqData("DATA");

	// Send the request.
	requester->send(reqData);

	// Wait for the response.
	string response;
	requester->receive(response);
    
    args.GetReturnValue().Set(String::NewFromUtf8(args.GetIsolate(), response.c_str()));
}

/**
 * The init function declares what we will make visible to node.
 */
void init(Local<Object> exports) {

	// Register the functions.
	NODE_SET_METHOD(exports, "init", Init);
	NODE_SET_METHOD(exports, "getData", GetData);
}

NODE_MODULE(addonvexpspy, init)

}