Commit 5539b1ff authored by Asad Salman's avatar Asad Salman Committed by Adrien Béraud

nodejs: added more callbacks

Also made the registration of callbacks and init() an atomic function call.

Change-Id: Ifdc8e0dc20c8d617e9c61320634e257e7f264277
parent e67f2c21
......@@ -7,14 +7,12 @@ BUILT_SOURCES= \
ring_wrapper.cpp: nodejs_interface.i configurationmanager.i managerimpl.i
$(SWIG) -v -c++ -javascript -node -o ring_wrapper.cpp nodejs_interface.i
sed -i 's/_wrap_setAccountsChangedCb)/setAccountsChangedCb)/g' ring_wrapper.cpp
sed -i 's/_wrap_setRegistrationStateChangedCb)/setRegistrationStateChangedCb)/g' ring_wrapper.cpp
sed -i 's/_wrap_init)/init)/g' ring_wrapper.cpp
build/Makefile: ring_wrapper.cpp binding.gyp
node-gyp configure
build/Release/obj.target/dring.node: build/Makefile ring_wrapper.cpp
build/Release/obj.target/dring.node: build/Makefile ring_wrapper.cpp callback.h
node-gyp build
CLEANFILES= \
......
v8::Persistent<v8::Function> accountsChangedCb;
v8::Persistent<v8::Function> registrationStateChangedCb;
#pragma once
static void setAccountsChangedCb(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE();
if (args[0]->IsObject()) {
if (args[0]->IsFunction()){
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(args[0]);
accountsChangedCb.Reset(v8::Isolate::GetCurrent(), func);
#define V8_STRING_NEW(str) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), str.data(), v8::String::kNormalString, str.size())
using namespace v8;
Persistent<Function> accountsChangedCb;
Persistent<Function> registrationStateChangedCb;
Persistent<Function> volatileDetailsChangedCb;
Persistent<Function> incomingAccountMessageCb;
Persistent<Function> accountMessageStatusChangedCb;
Persistent<Function> incomingTrustRequestCb;
Persistent<Function> contactAddedCb;
Persistent<Function> contactRemovedCb;
Persistent<Function> exportOnRingEndedCb;
Persistent<Function> nameRegistrationEndedCb;
Persistent<Function> knownDevicesChangedCb;
Persistent<Function> registeredNameFoundCb;
Persistent<Function>* getPresistentCb(const std::string &signal){
if (signal == "AccountsChanged")
return &accountsChangedCb;
else if (signal == "RegistrationStateChanged")
return &registrationStateChangedCb;
else if (signal == "VolatileDetailsChanged")
return &volatileDetailsChangedCb;
else if (signal == "IncomingAccountMessage")
return &incomingAccountMessageCb;
else if (signal == "AccountMessageStatusChanged")
return &accountMessageStatusChangedCb;
else if (signal == "IncomingTrustRequest")
return &incomingTrustRequestCb;
else if (signal == "ContactAdded")
return &contactAddedCb;
else if (signal == "ContactRemoved")
return &contactRemovedCb;
else if (signal == "ExportOnRingEnded")
return &exportOnRingEndedCb;
else if (signal == "NameRegistrationEnded")
return &nameRegistrationEndedCb;
else if (signal == "KnownDevicesChanged")
return &knownDevicesChangedCb;
else if (signal == "RegisteredNameFound")
return &registeredNameFoundCb;
else return nullptr;
}
void setCallback(const std::string& signal, Local<Function>& func){
if (auto* presistentCb = getPresistentCb(signal)) {
if (func->IsObject() && func->IsFunction()) {
presistentCb->Reset(Isolate::GetCurrent(), func);
} else {
accountsChangedCb.Reset();
presistentCb->Reset();
}
}
else {
printf("No Signal Associated with Event \'%s\'\n", signal.c_str() );
}
}
void parseCbMap(const Local<Value>& arg){
Local<Object> array = arg->ToObject();
Local<Array> props = array->GetOwnPropertyNames();
for (uint32_t i = 0; i < props->Length(); ++i) {
const Local<Value> key_local = props->Get(i);
std::string key = *String::Utf8Value(key_local);
Handle<Object> buffer = array->Get(V8_STRING_NEW(key))->ToObject();
Local<Function> func = Local<Function>::Cast(buffer);
setCallback(key,func);
}
}
void registrationStateChanged(const std::string& account_id,const std::string& state,int code,const std::string& detail_str){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), registrationStateChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), V8_STRING_NEW(state),
SWIGV8_INTEGER_NEW(code),V8_STRING_NEW(detail_str) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
void accountsChanged(){
SWIGV8_HANDLESCOPE();
v8::Local<v8::Function> func = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), accountsChangedCb);
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), accountsChangedCb);
if (!func.IsEmpty()) {
printf("accountsChanged Called | C++\n" );
v8::Local<v8::Value> callback_args[] = { };
v8::Handle<v8::Value> js_result = func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 0, callback_args);
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 0, callback_args);
}
}
void contactAdded(const std::string& account_id, const std::string& uri, bool confirmed){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), contactAddedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id),V8_STRING_NEW(uri),
SWIGV8_BOOLEAN_NEW(confirmed)};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
static void setRegistrationStateChangedCb(const SwigV8Arguments &args) {
}
void contactRemoved(const std::string& account_id, const std::string& uri, bool banned){
SWIGV8_HANDLESCOPE();
if (args[0]->IsObject()) {
if (args[0]->IsFunction()){
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(args[0]);
registrationStateChangedCb.Reset(v8::Isolate::GetCurrent(), func);
} else {
registrationStateChangedCb.Reset();
}
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), contactRemovedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id),V8_STRING_NEW(uri),
SWIGV8_BOOLEAN_NEW(banned) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void exportOnRingEnded(const std::string& account_id, int state, const std::string& pin){
SWIGV8_HANDLESCOPE();
void registrationStateChanged(const std::string& account_id,const std::string& state,int code,const std::string& detail_str){
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), exportOnRingEndedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id), SWIGV8_INTEGER_NEW(state),
V8_STRING_NEW(pin) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void nameRegistrationEnded(const std::string& account_id, int state, const std::string& name){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), nameRegistrationEndedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), SWIGV8_INTEGER_NEW(state),
V8_STRING_NEW(name)};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void registeredNameFound(const std::string& account_id, int state, const std::string& address, const std::string& name){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), registeredNameFoundCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = {V8_STRING_NEW(account_id),SWIGV8_INTEGER_NEW(state),
V8_STRING_NEW(address),V8_STRING_NEW(name) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
/*
void accountMessageStatusChanged(const std::string& account_id, uint64_t message_id, const std::string& to, int state){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), accountMessageStatusChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { SWIGV8_STRING_NEW(account_id.c_str()), SWIGV8_INTEGER_NEW(message_id)
SWIGV8_STRING_NEW(to.c_str()),SWIGV8_INTEGER_NEW(state) };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
void incomingTrustRequest(const std::string& account_id, const std::string& from, const std::vector<uint8_t>& payload, time_t received){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), incomingTrustRequestCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
void knownDevicesChanged(const std::string& account_id, const std::map<std::string, std::string>& devices){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), knownDevicesChangedCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}*/
/*void volatileDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details ){
SWIGV8_HANDLESCOPE();
v8::Local<v8::Function> func = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), registrationStateChangedCb);
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), volatileDetailsChangedCb);
if (!func.IsEmpty()) {
printf("registrationStateChanged Called | C++\n" );
v8::Local<v8::Value> callback_args[] = { SWIGV8_STRING_NEW(account_id.c_str()),SWIGV8_STRING_NEW(state.c_str()),
SWIGV8_INTEGER_NEW(code),SWIGV8_STRING_NEW(detail_str.c_str()) };
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, callback_args);
}
}
void incomingAccountMessage(const std::string& account_id, const std::string& from, const std::map<std::string, std::string>& payloads){
SWIGV8_HANDLESCOPE();
v8::Handle<v8::Value> js_result = func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), incomingAccountMessageCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}
\ No newline at end of file
}*/
\ No newline at end of file
static void setAccountsChangedCb(const SwigV8Arguments &args);
static void setRegistrationStateChangedCb(const SwigV8Arguments &args);
\ No newline at end of file
......@@ -20,21 +20,19 @@
"use strict";
const dring = require("./build/Release/dring");
dring.setAccountsChangedCb(function () {
console.log("accountsChanged Called | JavaScript");
dring.init({
"AccountsChanged": function(){
console.log("AccountsChanged JS");
},
"RegistrationStateChanged": function(account_id, state, code, detail_str){
console.log("RegistrationStateChanged JS " + account_id + "|" + state + "|" + code + "|" + detail_str);
}
});
dring.setRegistrationStateChangedCb(function (account_id, state, code, detail_str) {
console.log("registrationStateChanged Called | JavaScript");
console.log(account_id + "|" + state + "|" + code + "|" + detail_str);
});
dring.init();
var params = new dring.StringMap();
/*var params = new dring.StringMap();
params.set("Account.type", "RING");
params.set("Account.alias", "RingAccount");
dring.addAccount(params);
dring.addAccount(params);*/
setInterval(function () {
dring.pollEvents();
......
......@@ -98,7 +98,6 @@ namespace std {
%include "configurationmanager.i"
%include "presencemanager.i"
%include "videomanager.i"
%include "callback.i"
#include "dring/callmanager_interface.h"
......@@ -112,9 +111,10 @@ namespace std {
* that are not declared elsewhere in the c++ code
*/
void init(){
using namespace std::placeholders;
void init(const SwigV8Arguments& args){
parseCbMap(args[0]);
using namespace std::placeholders;
using std::bind;
using DRing::exportable_callback;
using DRing::ConfigurationSignal;
......@@ -122,7 +122,18 @@ void init(){
const std::map<std::string, SharedCallback> configEvHandlers = {
exportable_callback<ConfigurationSignal::AccountsChanged>(bind(&accountsChanged)),
exportable_callback<ConfigurationSignal::RegistrationStateChanged>(bind(&registrationStateChanged, _1, _2, _3, _4))
exportable_callback<ConfigurationSignal::RegistrationStateChanged>(bind(&registrationStateChanged, _1, _2, _3, _4)),
exportable_callback<ConfigurationSignal::ContactAdded>(bind(&contactAdded, _1, _2, _3 )),
exportable_callback<ConfigurationSignal::ContactRemoved>(bind(&contactRemoved, _1, _2, _3 )),
exportable_callback<ConfigurationSignal::ExportOnRingEnded>(bind(&exportOnRingEnded, _1, _2, _3 )),
exportable_callback<ConfigurationSignal::NameRegistrationEnded>(bind(&nameRegistrationEnded, _1, _2, _3 )),
exportable_callback<ConfigurationSignal::RegisteredNameFound>(bind(&registeredNameFound, _1, _2, _3, _4 )),
//exportable_callback<ConfigurationSignal::KnownDevicesChanged>(bind(&knownDevicesChanged, _1, _2 )),
//exportable_callback<ConfigurationSignal::VolatileDetailsChanged>(bind(&volatileDetailsChanged, _1, _2)),
//exportable_callback<ConfigurationSignal::IncomingAccountMessage>(bind(&incomingAccountMessage, _1, _2, _3 )),
//exportable_callback<ConfigurationSignal::AccountMessageStatusChanged>(bind(&accountMessageStatusChanged, _1, _2, _3, _4 )),
//exportable_callback<ConfigurationSignal::IncomingTrustRequest>(bind(&incomingTrustRequest, _1, _2, _3, _4 )),
};
if (!DRing::init(static_cast<DRing::InitFlag>(DRing::DRING_FLAG_DEBUG)))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment