Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 26 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ set(JSRUNTIME_APP_FILES

if ( ENABLE_JSRUNTIME_SERVER )
add_definitions("-DENABLE_JSRUNTIME_SERVER")
Comment thread
gurpreet319 marked this conversation as resolved.
add_definitions("-DWS_SERVER_PORT=9112")
set (JSRUNTIME_APP_FILES ${JSRUNTIME_APP_FILES}
add_definitions("-DWS_SERVER_PORT=5000")
Comment thread
vjain008 marked this conversation as resolved.
set (JSRUNTIME_COMMON_FILES ${JSRUNTIME_COMMON_FILES}
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeServer.cpp
Comment thread
gurpreet319 marked this conversation as resolved.
)
endif ( ENABLE_JSRUNTIME_SERVER )
Expand All @@ -134,6 +134,15 @@ add_library(${JSRUNTIME_LIBRARY_NAME} SHARED
${JSRUNTIME_ENGINE_FILES}
)

#JSRUNTIMECLIENTCONTAINER CHANGES
option(BUILD_JSRUNTIME_CONTAINER "BUILD_JSRUNTIME_CONTAINER" ON)
set(JSRUNTIME_CONTAINER_FILES
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeClientContainer.cpp
)
set(JSRUNTIME_FILES
${JSRUNTIME_COMMON_SOURCE_DIRECTORY}/JSRuntimeContainer.cpp
)

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})

set(JSRUNTIME_LIBRARY_LINK_DIRECTORIES ${JSRUNTIME_ENGINE_LIBRARY_LINK_DIRECTORIES})
Expand Down Expand Up @@ -192,6 +201,21 @@ if (BUILD_JSRUNTIME_CLIENT)
target_link_libraries(jsruntime_client ${JSRUNTIME_LIBRARY_LINK_DIRECTORIES} ${JSRUNTIME_LINK_ETHANLIB} -lpthread)
endif (BUILD_JSRUNTIME_CLIENT)

set(JSRUNTIMECONTAINER_LIBRARY_NAME "JSRuntimeContainer")

if (BUILD_JSRUNTIME_CONTAINER)
add_library(${JSRUNTIMECONTAINER_LIBRARY_NAME} SHARED ${JSRUNTIME_FILES})
target_include_directories(${JSRUNTIMECONTAINER_LIBRARY_NAME} PRIVATE ${JSRUNTIME_INCLUDE_DIRECTORIES})
target_link_libraries(${JSRUNTIMECONTAINER_LIBRARY_NAME} ${JSRUNTIME_LIBRARY_LINK_DIRECTORIES} -lpthread)

add_executable(jsruntime_container ${JSRUNTIME_CONTAINER_FILES})
add_dependencies(jsruntime_container ${JSRUNTIMECONTAINER_LIBRARY_NAME})
target_include_directories(jsruntime_container PRIVATE ${JSRUNTIME_INCLUDE_DIRECTORIES})
set_target_properties(jsruntime_container PROPERTIES OUTPUT_NAME "JSRuntimeContainer")
target_link_libraries(jsruntime_container ${JSRUNTIME_LIBRARY_LINK_DIRECTORIES} -l${JSRUNTIMECONTAINER_LIBRARY_NAME} -lpthread)

endif (BUILD_JSRUNTIME_CONTAINER)

set(UWEBSOCKETS_TARGET "Linux")
if (APPLE)
set(UWEBSOCKETS_TARGET "Darwin")
Expand Down
43 changes: 43 additions & 0 deletions include/JSRuntimeContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef JSRUNTIMECONTAINER_H
#define JSRUNTIMECONTAINER_H

#include <string>
#include <functional>
#include <map>

class JSRuntimeContainer
{
public:
enum Namespace {
NetworkNamespace = 0x01,
MountNamespace = 0x02,
IpcNamespace = 0x04,
PidNamespace = 0x08,
UserNamespace = 0x10,
UtsNamespace = 0x20
};

// Get container IP address
static std::string getContainerIpAddress(const std::string& containerId);

// Check if container exists
static bool isContainer(const std::string& containerId);

// Execute function in container namespace
static bool nsEnter(const std::string& containerId, Namespace type, const std::function<void()>& func);

// WebSocket client functions
static bool connectAndSend(const std::string& ip, const std::string& message);
static std::string buildLaunchMessage(const std::string& url, const std::string& options);
static std::string parseAppConfig(const std::string& configPath);

private:
// Internal implementation functions
static bool nsEnterImpl(const std::string& containerId, Namespace type, const std::function<void()>& func);
static pid_t findContainerPid(const std::string& containerId);
static bool nsEnterWithPid(pid_t pid, int nsType, const std::function<void()>& func);
static void nsThread(int newNsFd, int nsType, bool* success, const std::function<void()>& func);
};

#endif // JSRUNTIMECONTAINER_H

4 changes: 3 additions & 1 deletion include/JSRuntimeServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#pragma once
#include <NativeJSRenderer.h>
#include <IExternalApplicationHandler.h>

#ifdef USE_WEBSOCKET_MOCK
#include "websocketpp.hpp"
Expand All @@ -40,7 +41,7 @@ class JSRuntimeServer
static JSRuntimeServer *getInstance();
~JSRuntimeServer() = default;

void initialize(int serverport, std::shared_ptr<JsRuntime::NativeJSRenderer> renderer);
void initialize(int serverport, std::shared_ptr<JsRuntime::NativeJSRenderer> renderer, std::shared_ptr<IExternalApplicationHandler> externalHandler = nullptr);
bool start();
bool stop();

Expand Down Expand Up @@ -77,4 +78,5 @@ class JSRuntimeServer
ConnectionSet mConnections;
int mServerPort;
std::shared_ptr<JsRuntime::NativeJSRenderer> mRenderer;
std::shared_ptr<IExternalApplicationHandler> mExternalHandler;
};
31 changes: 31 additions & 0 deletions src/JSRuntimeClientContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "JSRuntimeContainer.h"
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include "NativeJSLogger.h"
int main()
{
std::string containerId = "com.sky.as.apps_TestApp";
const std::string basePath = "/opt/twocontext"; // constant base path
const std::vector<std::string> apps = {"app1", "app2"};
Comment thread
gurpreet319 marked this conversation as resolved.

std::string ipAddress = JSRuntimeContainer::getContainerIpAddress(containerId);
if (ipAddress.empty()) {
Comment thread
gurpreet319 marked this conversation as resolved.
NativeJSLogger::log(ERROR, "Failed to retrieve IP address for container");
return 1;
}

for (const auto &app : apps) {
std::string url = basePath + std::string("/") + app + std::string("/index.html");
if (access(url.c_str(), F_OK) == 0) {
Comment thread
vjain008 marked this conversation as resolved.
std::string pathAppConfig = basePath + std::string("/") + app + std::string("/app.config");
Comment thread
vjain008 marked this conversation as resolved.
Comment thread
vjain008 marked this conversation as resolved.
std::string options = JSRuntimeContainer::parseAppConfig(pathAppConfig);
std::string message = JSRuntimeContainer::buildLaunchMessage(url, options);
JSRuntimeContainer::connectAndSend(ipAddress, message);
}
}

return 0;
}

Loading