@@ -73,6 +73,23 @@ class JsonWrap
7373 return res;
7474 }
7575
76+ uint32_t getUint32 (const char *name, bool &err)
77+ {
78+ uint32_t res;
79+ cJSON *itm = cJSON_GetObjectItem (mPtr , name);
80+ if (!itm || !cJSON_IsNumber (itm))
81+ {
82+ std::cerr << " Error: " << name << " is not a Uint32_t" << std::endl;
83+ err = true ;
84+ }
85+ else
86+ {
87+ res = (uint32_t ) itm->valuedouble ;
88+ err = false ;
89+ }
90+ return res;
91+ }
92+
7693 cJSON *get () { return mPtr ; }
7794
7895private:
@@ -82,7 +99,6 @@ class JsonWrap
8299 cJSON *mPtr ;
83100 bool mIsRoot ;
84101};
85-
86102JSRuntimeServer *JSRuntimeServer::getInstance ()
87103{
88104 static JSRuntimeServer instance;
@@ -181,7 +197,6 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg
181197 {
182198 break ;
183199 }
184-
185200 if (method == " launchApplication" )
186201 {
187202 JsonWrap jParams (jRoot, " params" );
@@ -194,40 +209,111 @@ void JSRuntimeServer::onMessage(websocketpp::connection_hdl hdl, message_ptr msg
194209 {
195210 break ;
196211 }
197- std::string options = jParams.getString (" options" , error);
198- ModuleSettings settings;
199- settings.fromString (options);
200- mRenderer ->launchApplication (url, settings);
201- result = " ok" ;
212+ std::string options = jParams.getString (" moduleSettings" , error);
213+ ModuleSettings moduleSettings;
214+ moduleSettings.fromString (options);
215+ uint32_t id = mRenderer ->createApplication (moduleSettings);
216+ mRenderer ->runApplication (id, url);
217+ std::ostringstream oss;
218+ oss<< " ID : " << id;
219+ result = oss.str ();
202220 }
203- else if (method == " terminateApplication " )
221+ if (method == " createApplication " )
204222 {
205223 JsonWrap jParams (jRoot, " params" );
206224 if (jParams.get () == nullptr )
207225 {
208226 break ;
209227 }
228+ std::string options = jParams.getString (" moduleSettings" , error);
229+ if (error)
230+ {
231+ break ;
232+ }
233+ ModuleSettings moduleSettings;
234+ moduleSettings.fromString (options);
235+ uint32_t id = mRenderer ->createApplication (moduleSettings);
236+ std::ostringstream oss;
237+ result = oss.str ();
238+ }
239+ if (method == " runApplication" )
240+ {
241+ JsonWrap jParams (jRoot, " params" );
242+ if (jParams.get () == nullptr )
243+ {
244+ break ;
245+ }
246+ uint32_t id = jParams.getUint32 (" id" , error);
247+ if (error)
248+ {
249+ break ;
250+ }
210251 std::string url = jParams.getString (" url" , error);
211252 if (error)
212253 {
213254 break ;
214255 }
215- mRenderer ->terminateApplication (url);
256+ mRenderer ->runApplication (id, url);
257+ std::ostringstream oss;
216258 result = " ok" ;
217259 }
218- else if (method == " getApplications" )
260+
261+ else if (method == " runJavaScript" )
262+ {
263+ JsonWrap jParams (jRoot, " params" );
264+ if ( jParams.get () == nullptr )
265+ {
266+ break ;
267+ }
268+ uint32_t id = jParams.getUint32 (" id" , error);
269+ if (error)
270+ {
271+ break ;
272+ }
273+ std::string code = jParams.getString (" code" , error);
274+ if (error)
275+ {
276+ break ;
277+ }
278+ mRenderer ->runJavaScript (id, code);
279+ result = " ok" ;
280+
281+ }
282+ else if (method == " destroyApplication" )
219283 {
220- std::vector<std::string> apps = mRenderer ->getApplications ();
221- std::ostringstream oss;
222- for (size_t i = 0 ; i < apps.size (); ++i)
284+ JsonWrap jParams (jRoot, " params" );
285+ if (jParams.get () == nullptr )
223286 {
224- if (i != 0 )
225- {
226- oss << ' ' ;
227- }
228- oss << apps[i];
287+ break ;
229288 }
230- result = oss.str ();
289+ uint32_t id = jParams.getUint32 (" id" , error);
290+ if (error)
291+ {
292+ break ;
293+ }
294+ mRenderer ->terminateApplication (id);
295+ result = " ok" ;
296+ }
297+
298+ else if (method == " getApplications" )
299+ {
300+ std::list<JsRuntime::ApplicationDetails> apps = mRenderer ->getApplications ();
301+ if (!apps.empty ())
302+ {
303+ std::ostringstream oss;
304+ for (const auto & app : apps)
305+ {
306+ if (!oss.str ().empty ())
307+ {
308+ oss << " " ;
309+ }
310+ oss << " ID: " << app.id << " , URL: " << app.url ;
311+ }
312+ result = oss.str ();
313+ }
314+ else
315+ result = " No ID found" ;
316+
231317 }
232318 else if (method == " ping" )
233319 {
0 commit comments