Skip to content

Commit f2fbbe9

Browse files
gurpreet319gsarng517_comcast
authored andcommitted
RDKEMW-9355: Add Support to run app widgets in different contexts within a single process
Reason for change: Changes related to client/server Test Procedure: build should be successful. Risks: low Priority: P2
1 parent a8b999f commit f2fbbe9

7 files changed

Lines changed: 467 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ set(JSRUNTIME_APP_FILES
118118

119119
if ( ENABLE_JSRUNTIME_SERVER )
120120
add_definitions("-DENABLE_JSRUNTIME_SERVER")
121-
add_definitions("-DWS_SERVER_PORT=9112")
122-
set (JSRUNTIME_APP_FILES ${JSRUNTIME_APP_FILES}
121+
add_definitions("-DWS_SERVER_PORT=5000")
122+
set (JSRUNTIME_COMMON_FILES ${JSRUNTIME_COMMON_FILES}
123123
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeServer.cpp
124124
)
125125
endif ( ENABLE_JSRUNTIME_SERVER )
@@ -134,6 +134,15 @@ add_library(${JSRUNTIME_LIBRARY_NAME} SHARED
134134
${JSRUNTIME_ENGINE_FILES}
135135
)
136136

137+
#JSRUNTIMECLIENTCONTAINER CHANGES
138+
option(BUILD_JSRUNTIME_CONTAINER "BUILD_JSRUNTIME_CONTAINER" ON)
139+
set(JSRUNTIME_CONTAINER_FILES
140+
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeClientContainer.cpp
141+
)
142+
set(JSRUNTIME_FILES
143+
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeContainer.cpp
144+
)
145+
137146
set(JSRUNTIME_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include/${JSRUNTIME_ENGINE_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/include/linux ${CMAKE_CURRENT_SOURCE_DIR}/src/jsc/jsc_lib ${JSRUNTIME_ENGINE_INCLUDE_DIRECTORIES})
138147

139148
set(JSRUNTIME_LIBRARY_LINK_DIRECTORIES ${JSRUNTIME_ENGINE_LIBRARY_LINK_DIRECTORIES})
@@ -192,6 +201,21 @@ if (BUILD_JSRUNTIME_CLIENT)
192201
target_link_libraries(jsruntime_client ${JSRUNTIME_LIBRARY_LINK_DIRECTORIES} ${JSRUNTIME_LINK_ETHANLIB} -lpthread)
193202
endif (BUILD_JSRUNTIME_CLIENT)
194203

204+
set(JSRUNTIMECONTAINER_LIBRARY_NAME "JSRuntimeContainer")
205+
206+
if (BUILD_JSRUNTIME_CONTAINER)
207+
add_library(${JSRUNTIMECONTAINER_LIBRARY_NAME} SHARED ${JSRUNTIME_FILES})
208+
target_include_directories(${JSRUNTIMECONTAINER_LIBRARY_NAME} PRIVATE ${JSRUNTIME_INCLUDE_DIRECTORIES})
209+
target_link_libraries(${JSRUNTIMECONTAINER_LIBRARY_NAME} ${JSRUNTIME_LIBRARY_LINK_DIRECTORIES} -lpthread)
210+
211+
add_executable(jsruntime_container ${JSRUNTIME_CONTAINER_FILES})
212+
add_dependencies(jsruntime_container ${JSRUNTIMECONTAINER_LIBRARY_NAME})
213+
target_include_directories(jsruntime_container PRIVATE ${JSRUNTIME_INCLUDE_DIRECTORIES})
214+
set_target_properties(jsruntime_container PROPERTIES OUTPUT_NAME "JSRuntimeContainer")
215+
target_link_libraries(jsruntime_container ${JSRUNTIME_LIBRARY_LINK_DIRECTORIES} -l${JSRUNTIMECONTAINER_LIBRARY_NAME} -lpthread)
216+
217+
endif (BUILD_JSRUNTIME_CONTAINER)
218+
195219
set(UWEBSOCKETS_TARGET "Linux")
196220
if (APPLE)
197221
set(UWEBSOCKETS_TARGET "Darwin")

include/JSRuntimeContainer.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef JSRUNTIMECONTAINER_H
2+
#define JSRUNTIMECONTAINER_H
3+
4+
#include <string>
5+
#include <functional>
6+
#include <map>
7+
8+
class JSRuntimeContainer
9+
{
10+
public:
11+
enum Namespace {
12+
NetworkNamespace = 0x01,
13+
MountNamespace = 0x02,
14+
IpcNamespace = 0x04,
15+
PidNamespace = 0x08,
16+
UserNamespace = 0x10,
17+
UtsNamespace = 0x20
18+
};
19+
20+
// Get container IP address
21+
static std::string getContainerIpAddress(const std::string& containerId);
22+
23+
// Check if container exists
24+
static bool isContainer(const std::string& containerId);
25+
26+
// Execute function in container namespace
27+
static bool nsEnter(const std::string& containerId, Namespace type, const std::function<void()>& func);
28+
29+
// WebSocket client functions
30+
static bool connectAndSend(const std::string& ip, const std::string& message);
31+
static std::string buildLaunchMessage(const std::string& url, const std::string& options);
32+
static std::string parseAppConfig(const std::string& configPath);
33+
34+
private:
35+
// Internal implementation functions
36+
static bool nsEnterImpl(const std::string& containerId, Namespace type, const std::function<void()>& func);
37+
static pid_t findContainerPid(const std::string& containerId);
38+
static bool nsEnterWithPid(pid_t pid, int nsType, const std::function<void()>& func);
39+
static void nsThread(int newNsFd, int nsType, bool* success, const std::function<void()>& func);
40+
};
41+
42+
#endif // JSRUNTIMECONTAINER_H
43+

include/JSRuntimeServer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#pragma once
2121
#include <NativeJSRenderer.h>
22+
#include <IExternalApplicationHandler.h>
2223

2324
#ifdef USE_WEBSOCKET_MOCK
2425
#include "websocketpp.hpp"
@@ -40,7 +41,7 @@ class JSRuntimeServer
4041
static JSRuntimeServer *getInstance();
4142
~JSRuntimeServer() = default;
4243

43-
void initialize(int serverport, std::shared_ptr<JsRuntime::NativeJSRenderer> renderer);
44+
void initialize(int serverport, std::shared_ptr<JsRuntime::NativeJSRenderer> renderer, std::shared_ptr<IExternalApplicationHandler> externalHandler = nullptr);
4445
bool start();
4546
bool stop();
4647

@@ -77,4 +78,5 @@ class JSRuntimeServer
7778
ConnectionSet mConnections;
7879
int mServerPort;
7980
std::shared_ptr<JsRuntime::NativeJSRenderer> mRenderer;
81+
std::shared_ptr<IExternalApplicationHandler> mExternalHandler;
8082
};

src/JSRuntimeClientContainer.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "JSRuntimeContainer.h"
2+
#include <iostream>
3+
#include <string>
4+
#include <vector>
5+
#include <unistd.h>
6+
#include "NativeJSLogger.h"
7+
int main()
8+
{
9+
std::string containerId = "com.sky.as.apps_TestApp";
10+
const std::string basePath = "/opt/twocontext"; // constant base path
11+
const std::vector<std::string> apps = {"app1", "app2"};
12+
13+
std::string ipAddress = JSRuntimeContainer::getContainerIpAddress(containerId);
14+
if (ipAddress.empty()) {
15+
NativeJSLogger::log(ERROR, "Failed to retrieve IP address for container");
16+
return 1;
17+
}
18+
19+
for (const auto &app : apps) {
20+
std::string url = basePath + std::string("/") + app + std::string("/index.html");
21+
if (access(url.c_str(), F_OK) == 0) {
22+
std::string pathAppConfig = basePath + std::string("/") + app + std::string("/app.config");
23+
std::string options = JSRuntimeContainer::parseAppConfig(pathAppConfig);
24+
std::string message = JSRuntimeContainer::buildLaunchMessage(url, options);
25+
JSRuntimeContainer::connectAndSend(ipAddress, message);
26+
}
27+
}
28+
29+
return 0;
30+
}
31+

0 commit comments

Comments
 (0)