Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 106 additions & 19 deletions src/JSRuntimeServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we have value number, something like that?

Copy link
Copy Markdown
Contributor Author

@gurpreet319 gurpreet319 May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have valueint, but its DEPRECATED, mentioned in the documentation, and this valuedouble is used for number
https://github.com/DaveGamble/cJSON/blob/8f2beb57ddad1f94bed899790b00f46df893ccac/cJSON.h#L116C31-L116C41

err = false;
}
return res;
}

cJSON *get() { return mPtr; }

private:
Expand All @@ -82,7 +99,6 @@ class JsonWrap
cJSON *mPtr;
bool mIsRoot;
};

JSRuntimeServer *JSRuntimeServer::getInstance()
{
static JSRuntimeServer instance;
Expand Down Expand Up @@ -181,7 +197,6 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg
{
break;
}

if (method == "launchApplication")
{
JsonWrap jParams(jRoot, "params");
Expand All @@ -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<std::string> 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<JsRuntime::ApplicationDetails> 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")
{
Expand Down