Skip to content
Snippets Groups Projects
Commit ce7c0a06 authored by Asad Salman's avatar Asad Salman Committed by Adrien Béraud
Browse files

nodejs: added data structure conversions and more callbacks

Added functions to convert C++ map<string, string> and vector<int> to native JS objects

Also added 5 more callbacks that depended on above mentioned data structures

Change-Id: I10715d69446c15906feed526fff178c27703af10
parent 0e2d40d7
Branches
Tags
No related merge requests found
......@@ -46,6 +46,16 @@ Persistent<Function>* getPresistentCb(const std::string &signal){
else return nullptr;
}
void intVectToJsArray(const std::vector<uint8_t>& intVect, const Local<Array>& jsArray){
for (unsigned int i = 0; i < intVect.size(); i++ )
jsArray->Set(SWIGV8_INTEGER_NEW_UNS(i), SWIGV8_INTEGER_NEW(intVect[i]));
}
void stringMapToJsMap( const std::map<std::string, std::string>& strmap, const Local<Object> &jsMap){
for (auto& kvpair: strmap)
jsMap->Set(V8_STRING_NEW(std::get<0>(kvpair)), V8_STRING_NEW(std::get<1>(kvpair)));
}
void setCallback(const std::string& signal, Local<Function>& func){
if (auto* presistentCb = getPresistentCb(signal)) {
if (func->IsObject() && func->IsFunction()) {
......@@ -146,53 +156,64 @@ void registeredNameFound(const std::string& account_id, int state, const std::st
}
}
/*
void volatileDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details ){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), volatileDetailsChangedCb);
if (!func.IsEmpty()) {
Local<Object> jsMap = SWIGV8_OBJECT_NEW();
stringMapToJsMap(details, jsMap);
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), jsMap};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 2, 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) };
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), SWIGV8_INTEGER_NEW_UNS(message_id),
V8_STRING_NEW(to), 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){
void incomingAccountMessage(const std::string& account_id, const std::string& from, const std::map<std::string, std::string>& payloads){
SWIGV8_HANDLESCOPE();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), incomingTrustRequestCb);
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);
Local<Object> jsMap = SWIGV8_OBJECT_NEW();
stringMapToJsMap(payloads, jsMap);
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), V8_STRING_NEW(from), jsMap};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 3, 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);
Local<Object> jsMap = SWIGV8_OBJECT_NEW();
stringMapToJsMap(devices, jsMap);
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), jsMap};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 2, callback_args);
}
}
}*/
/*void volatileDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details ){
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(), volatileDetailsChangedCb);
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), incomingTrustRequestCb);
if (!func.IsEmpty()) {
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();
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(), incomingAccountMessageCb);
if (!func.IsEmpty()) {
Local<Value> callback_args[] = { };
Local<Array> jsArray = SWIGV8_ARRAY_NEW();
intVectToJsArray(payload, jsArray);
Local<Value> callback_args[] = { V8_STRING_NEW(account_id), V8_STRING_NEW(from),
jsArray, SWIGV8_NUMBER_NEW(received)};
func->Call(SWIGV8_CURRENT_CONTEXT()->Global(), 4, callback_args);
}
}*/
\ No newline at end of file
}
......@@ -139,11 +139,11 @@ void init(const v8::Handle<v8::Value> &funcMap){
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 )),
exportable_callback<ConfigurationSignal::VolatileDetailsChanged>(bind(&volatileDetailsChanged, _1, _2)),
exportable_callback<ConfigurationSignal::KnownDevicesChanged>(bind(&knownDevicesChanged, _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 )),
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment