#include #include #include #include #include #include #include #include #include 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 server; unique_ptr remoteServer; unique_ptr vexpSpy; unique_ptr requester; Isolate * v8Isolate; string nomadEndpoint; std::string VEXPSPY = "vexpspy"; /** * Init function to initialise the Cameo Nomad addon. */ void Init(const FunctionCallbackInfo& args) { cout << "initialising vEXP spy" << endl; // Get the V8 isolate. v8Isolate = args.GetIsolate(); // Local endpoint. v8::String::Utf8Value param1(args[0]->ToString()); string localEndpoint(*param1); // Nomad endpoint. v8::String::Utf8Value param2(args[1]->ToString()); string nomadEndpoint(*param2); // Init the app if it is not already done. if (cameo::application::This::getId() == -1) { cameo::application::This::init("vEXP", localEndpoint); 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 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& 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 exports) { // Register the functions. NODE_SET_METHOD(exports, "init", Init); NODE_SET_METHOD(exports, "getData", GetData); } NODE_MODULE(addonvexpspy, init) }