Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TGrid - Network & Thread Extension #22

Closed
samchon opened this issue Nov 19, 2018 · 0 comments
Closed

TGrid - Network & Thread Extension #22

samchon opened this issue Nov 19, 2018 · 0 comments
Assignees
Labels
C++11 C++23 enhancement New feature or request
Projects

Comments

@samchon
Copy link
Owner

samchon commented Nov 19, 2018

It's not possible to implement thread and network modules in TypeScript following the C++ standard. However, they're very important features cannot skip implementation on the excuse of domain problem in the JavaScript.

Thus, I decided to provide those network & thread features as a separate module, an independent extension. The new extension would not follow the regular spec of C++ standard, however it should be familiar for the STL users.

Network Extension

In JavaScript (Web enviroment), only HTTP & Web Socket Protocols are enable to use. Any other protocols are not allowed.

In such restriction, TGrid would support only Web Socket Protocol with special concepts. With those concepts, STL lovers would not be confused when using TGrid; which is not following the C++ standard, but familiar with STL's principle paradigm.

  • RFC (Remote Function Call)
  • ROC (Remote Object Call)
  • OON (Object Oriented Network)

HTTP Protocol? Use fetch instead.

server.ts

import { WebServer } from "tgrid/protocols/web/WebServer";
import { Vector } from "tstl/container/Vector";

async function main(): Promise<void>
{
    let server: WebServer = new WebServer();
    await server.open(10101, async acceptor =>
    {
        // accept connection & provide Vector
        await acceptor.accept();
        await acceptor.listen(new Vector<number>());
    });
}
main();

client.ts

import { WebConnector } from "tgrid/protocols/web/WebConnector";
import { Driver } from "tgrid/components/Driver";
import { Vector } from "tstl/container/Vector";

async function main(): Promise<void>
{
    //----
    // PREPARE
    //----
    // do connect
    let connector: WebConnector = new WebConnector();
    await connector.connect("ws://127.0.0.1:10101");

    // wait server to provide the Vector
    await connector.wait();

    // get Driver<Controller>
    let v: Driver<Vector<number>> = connector.getDriver<Vector<number>>();

    //----
    // CALL FUNCTIONS IN THE REMOTE SYSTEM
    //----
    // insert elements
    for (let i: number = 0; i < 5; ++i)
        await v.push_back(i);

    // access elements
    console.log("size:", await v.size());
    for (let i: number = 0; i < await v.size(); ++i)
        console.log("  element:", await v.at(i));

    // catching exception is also possible
    try 
    {
        await v.at(9999);
    } 
    catch (exp) 
    {
        console.log(`${exp.name}: "${exp.message}"`);
    }

    // close the connection
    await connector.close();
}
main();
size: 5
  element: 0
  element: 1
  element: 2
  element: 3
  element: 4
out_of_range: "Target index is greater than Vector's size."

Thread Extension

JavaScript does not support the thread. JavaScript only provides worker, which is a same concept with process. In the worker (process) environment, sharing memory is not possible; only IPC (Inter-Process Communication) is allowed.

In such restriction, I suggest an alternative solution. It's not possible to sharing memory, but sharing objects and functions are possible. It's not a regular spec of C++ STL, however, those concepts in the TGrid will make them (sharing functions and objects between workers) possible:

  • RFC (Remote Function Call)
  • ROC (Remote Object Call)
  • OON (Object Oriented Network)

child.ts

import { WorkerServer } from "tgrid/protocol/workers/WorkerServer";

import { Mutex } from "tstl/thread/Mutex";
import { randint } from "tstl/algorithm";

interface IController
{
    mutex: Mutex;
    print(str: string): void;
}

async function main(str: string): Promise<void>
{
    // PREPARE SERVER
    let server: WorkerServer = new WorkerServer();
    await server.open();

    // REMOTE FUNCTION CALLS
    let driver: Driver<IController> = server.getDriver<IController>();
    await driver.mutex.lock();
    {
        for (let i: number = 0; i < 20; ++i)
            await driver.print(str);
        await driver.print("\n");
    }
    await driver.mutex.unlock();

    // CLOSE THE SERVER (WORKER)
    await server.close();
}
main(randint(0, 9) + "");

parent.ts

import { WorkerConnector } from "tgrid/protocols/workers/WorkerConnector";

import { Vector } from "tstl/container";
import { Mutex } from "tstl/thread";

// FEATURES TO PROVIDE
namespace provider
{
    export var mutex = new Mutex();
    export function print(str: string): void
    {
        process.stdout.write(str);
    }
}

async function main(): Promise<void>
{
    let workers: Vector<WorkerConnector<typeof provider>> = new Vector();

    // CREATE WORKERS
    for (let i: number = 0; i < 4; ++i)
    {
        workers.push_back(new WorkerConnector(provider));
        await workers.back().connect("./child.js");
    }

    // WAIT THEM TO BE CLOSED
    for (let w of workers)
        await w.join();
}
main();
11111111111111111111
88888888888888888888
44444444444444444444
33333333333333333333
@samchon samchon added this to To do in v2.1 Update Nov 20, 2018
@samchon samchon moved this from To do to In progress in v2.1 Update Nov 20, 2018
@samchon samchon added enhancement New feature or request C++20 C++11 help wanted Extra attention is needed C++23 and removed help wanted Extra attention is needed C++20 labels Nov 20, 2018
@samchon samchon moved this from In progress to On review in v2.1 Update Dec 15, 2018
@samchon samchon closed this as completed Dec 22, 2018
v2.1 Update automation moved this from On review to Done Dec 22, 2018
@samchon samchon self-assigned this Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++11 C++23 enhancement New feature or request
Projects
No open projects
v2.1 Update
  
Done
Development

No branches or pull requests

1 participant