diff --git a/src/JSRuntimeServer.cpp b/src/JSRuntimeServer.cpp index 723e9e2..44438ad 100644 --- a/src/JSRuntimeServer.cpp +++ b/src/JSRuntimeServer.cpp @@ -73,6 +73,23 @@ class JsonWrap return res; } + uint32_t getUint32(const char *name, bool &err) + { + uint32_t res; + cJSON *itm = cJSON_GetObjectItem(mPtr, name); + if (!itm || !cJSON_IsNumber(itm)) + { + std::cerr << "Error: " << name << "is not a Uint32_t" << std::endl; + err = true; + } + else + { + res = (uint32_t) itm->valuedouble; + err = false; + } + return res; + } + cJSON *get() { return mPtr; } private: @@ -82,7 +99,6 @@ class JsonWrap cJSON *mPtr; bool mIsRoot; }; - JSRuntimeServer *JSRuntimeServer::getInstance() { static JSRuntimeServer instance; @@ -181,7 +197,6 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg { break; } - if (method == "launchApplication") { JsonWrap jParams(jRoot, "params"); @@ -194,40 +209,112 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg { break; } - std::string options = jParams.getString("options", error); - ModuleSettings settings; - settings.fromString(options); - mRenderer->launchApplication(url, settings); - result = "ok"; + std::string options = jParams.getString("moduleSettings", error); + ModuleSettings moduleSettings; + moduleSettings.fromString(options); + uint32_t id = mRenderer->createApplication(moduleSettings); + mRenderer->runApplication(id, url); + std::ostringstream oss; + oss<< "ID : " << id; + result = oss.str(); } - else if (method == "terminateApplication") + if (method == "createApplication") { JsonWrap jParams(jRoot, "params"); if (jParams.get() == nullptr) { break; } + std::string options = jParams.getString("moduleSettings", error); + if (error) + { + break; + } + ModuleSettings moduleSettings; + moduleSettings.fromString(options); + uint32_t id = mRenderer->createApplication(moduleSettings); + std::ostringstream oss; + oss<< "ID : " << id; + result = oss.str(); + } + if (method == "runApplication") + { + JsonWrap jParams(jRoot, "params"); + if (jParams.get() == nullptr) + { + break; + } + uint32_t id = jParams.getUint32("id", error); + if (error) + { + break; + } std::string url = jParams.getString("url", error); if (error) { break; } - mRenderer->terminateApplication(url); + mRenderer->runApplication(id, url); + std::ostringstream oss; result = "ok"; } - else if (method == "getApplications") + + else if (method == "runJavaScript") + { + JsonWrap jParams(jRoot, "params"); + if ( jParams.get() == nullptr) + { + break; + } + uint32_t id = jParams.getUint32("id", error); + if (error) + { + break; + } + std::string code = jParams.getString("code", error); + if (error) + { + break; + } + mRenderer->runJavaScript(id, code); + result = "ok"; + + } + else if (method == "destroyApplication") { - std::vector apps = mRenderer->getApplications(); - std::ostringstream oss; - for (size_t i = 0; i < apps.size(); ++i) + JsonWrap jParams(jRoot, "params"); + if (jParams.get() == nullptr) { - if (i != 0) - { - oss << ' '; - } - oss << apps[i]; + break; } - result = oss.str(); + uint32_t id = jParams.getUint32("id", error); + if (error) + { + break; + } + mRenderer->terminateApplication(id); + result = "ok"; + } + + else if (method == "getApplications") + { + std::list apps = mRenderer->getApplications(); + if(!apps.empty()) + { + std::ostringstream oss; + for (const auto& app : apps) + { + if (!oss.str().empty()) + { + oss << " "; + } + oss << "ID: " << app.id << ", URL: " << app.url; + } + result = oss.str(); + } + else + result = "No ID found"; + } else if (method == "ping") {