You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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";asyncfunctionmain(): Promise<void>{letserver: WebServer=newWebServer();awaitserver.open(10101,asyncacceptor=>{// accept connection & provide Vectorawaitacceptor.accept();awaitacceptor.listen(newVector<number>());});}main();
client.ts
import{WebConnector}from"tgrid/protocols/web/WebConnector";import{Driver}from"tgrid/components/Driver";import{Vector}from"tstl/container/Vector";asyncfunctionmain(): Promise<void>{//----// PREPARE//----// do connectletconnector: WebConnector=newWebConnector();awaitconnector.connect("ws://127.0.0.1:10101");// wait server to provide the Vectorawaitconnector.wait();// get Driver<Controller>letv: Driver<Vector<number>>=connector.getDriver<Vector<number>>();//----// CALL FUNCTIONS IN THE REMOTE SYSTEM//----// insert elementsfor(leti: number=0;i<5;++i)awaitv.push_back(i);// access elementsconsole.log("size:",awaitv.size());for(leti: number=0;i<awaitv.size();++i)console.log(" element:",awaitv.at(i));// catching exception is also possibletry{awaitv.at(9999);}catch(exp){console.log(`${exp.name}: "${exp.message}"`);}// close the connectionawaitconnector.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";interfaceIController{mutex: Mutex;print(str: string): void;}asyncfunctionmain(str: string): Promise<void>{// PREPARE SERVERletserver: WorkerServer=newWorkerServer();awaitserver.open();// REMOTE FUNCTION CALLSletdriver: Driver<IController>=server.getDriver<IController>();awaitdriver.mutex.lock();{for(leti: number=0;i<20;++i)awaitdriver.print(str);awaitdriver.print("\n");}awaitdriver.mutex.unlock();// CLOSE THE SERVER (WORKER)awaitserver.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 PROVIDEnamespaceprovider{exportvarmutex=newMutex();exportfunctionprint(str: string): void{process.stdout.write(str);}}asyncfunctionmain(): Promise<void>{letworkers: Vector<WorkerConnector<typeofprovider>>=newVector();// CREATE WORKERSfor(leti: number=0;i<4;++i){workers.push_back(newWorkerConnector(provider));awaitworkers.back().connect("./child.js");}// WAIT THEM TO BE CLOSEDfor(letwofworkers)awaitw.join();}main();
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.
server.ts
client.ts
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:
child.ts
parent.ts
The text was updated successfully, but these errors were encountered: