Skip to content

Commit

Permalink
Merge pull request #16 from samchon/features/tgrid
Browse files Browse the repository at this point in the history
Upgrade TGrid version, and change interfaces
  • Loading branch information
samchon authored Jun 25, 2024
2 parents 6f3a23e + d027b4a commit 10ac1c7
Show file tree
Hide file tree
Showing 28 changed files with 244 additions and 310 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mutex-server",
"version": "0.5.0",
"version": "0.6.1",
"description": "Mutex Server using WebSocket",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -70,11 +70,11 @@
"ts-patch": "^3.1.2",
"tslib": "^2.6.2",
"typedoc": "^0.25.12",
"typescript": "^5.4.2",
"typescript": "5.4.5",
"typescript-transform-paths": "^3.4.7"
},
"dependencies": {
"tgrid": "^0.10.0",
"tgrid": "^1.0.1",
"tstl": "^3.0.0"
},
"files": [
Expand Down
68 changes: 15 additions & 53 deletions src/MutexAcceptor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Driver, WebAcceptor } from "tgrid";
import { WebSocketAcceptor } from "tgrid";

import { ProviderCapsule } from "./server/ProviderCapsule";
import { ProviderGroup } from "./server/ProviderGroup";

/**
Expand All @@ -9,32 +8,27 @@ import { ProviderGroup } from "./server/ProviderGroup";
* - available only in NodeJS.
*
* The {@link MutexAcceptor} is a communicator class interacting with the remote client, through
* websocket and [RFC](https://github.com/samchon/tgrid#13-remote-function-call) protocol, in the
* websocket and [RPC](https://tgrid.com/docs/remote-procedure-call/) protocol, in the
* `mutex-server`. The {@link MutexAcceptor} objects are always created by the {@link MutexServer}
* class, whenever a remote client connects to the `mutex-server`.
*
* You can accept or reject the client's connection by calling {@link accept} or {@link reject}
* method. If you {@link accept} the connection, the interaction would be started and the client
* can access to critical component sections of this `mutex-server`.
*
* Also, when declaring this {@link MutexAcceptor} type, you've to define two template arguments,
* *Header* and *Provider*. The *Header* type repersents an initial data gotten from the remote
* client after the connection. I hope you and client not to omit it and utilize it as an
* activation tool to enhance security.
*
* The second template argument *Provider* represents the additional features provided for the
* remote client. If you don't have any plan to provide additional feature to the remote client,
* just declare it as `null`
* Also, when declaring this {@link MutexAcceptor} type, you've to define one template argument,
* *Header*. The *Header* type repersents an initial data gotten from the remote client after
* the connection. I hope you and client not to omit it and utilize it as an activation tool
* to enhance security.
*
* @template Header Type of the header containing initial data.
* @template Provider Type of additional features provided for the remote client.
* @author Jeongho Nam - https://github.com/samchon
*/
export class MutexAcceptor<Header, Provider extends object | null> {
export class MutexAcceptor<Header> {
/**
* @hidden
*/
private base_: WebAcceptor<Header, ProviderCapsule<Provider>>;
private base_: WebSocketAcceptor<Header, ProviderGroup, null>;

/**
* @hidden
Expand All @@ -48,7 +42,7 @@ export class MutexAcceptor<Header, Provider extends object | null> {
* @hidden
*/
private constructor(
base: WebAcceptor<Header, ProviderCapsule<Provider>>,
base: WebSocketAcceptor<Header, ProviderGroup, null>,
group: ProviderGroup,
) {
this.base_ = base;
Expand All @@ -58,10 +52,10 @@ export class MutexAcceptor<Header, Provider extends object | null> {
/**
* @internal
*/
public static create<Header, Provider extends object | null>(
base: WebAcceptor<Header, ProviderCapsule<Provider>>,
public static create<Header>(
base: WebSocketAcceptor<Header, ProviderGroup, null>,
group: ProviderGroup,
) {
): MutexAcceptor<Header> {
return new MutexAcceptor(base, group);
}

Expand All @@ -74,11 +68,6 @@ export class MutexAcceptor<Header, Provider extends object | null> {
* acquired and tried would be automatically unlocked and cancelled by this `mutex-server`.
* Also, remote critical section components had assigned to the client would be returned, too.
*
* In addition, the disconnection destories all [RFC](https://github.com/samchon/tgrid#13-remote-function-call)s
* between the remote client. Therefore, if the remote client is providing custom features to
* your `mutex-server` and there're uncompleted method calls to the `Provider` through
* `Driver<Controller>`, {@link RuntimeError} exceptions would be thrown.
*
* @param code Closing code.
* @param reason Reason why.
* @throw DomainError when the connection is not online.
Expand Down Expand Up @@ -159,33 +148,6 @@ export class MutexAcceptor<Header, Provider extends object | null> {
return this.base_.state;
}

/**
* Get Driver for [RFC](https://github.com/samchon/tgrid#13-remote-function-call).
*
* If remote client provides custom features for this `mutex-server`, you can utilize
* those features thorugh returned `Driver` object by this method. To use this method, you
* should define the `Controller` template argument, a type of interface who is defining
* additionla features provided from the remote client.
*
* The returned object `Driver` makes to call remote functions, defined in the `Controller`
* and provided by `Provider` in the remote client, possible. In other words, callling a
* function in the `Driver<Controller>`, it means to call a matched function in the remote
* client's `Provider` object.
*
* - `Controller`: Definition only
* - `Driver`: [Remote Function Call](https://github.com/samchon/tgrid#13-remote-function-call)
*
* @template Controller An interface for provided features (functions & objects) from the remote client (`Provider`).
* @template UseParametric Whether to convert type of function parameters to be compatible with their pritimive.
* @return A `Driver` for the [RFC](https://github.com/samchon/tgrid#13-remote-function-call).
*/
public getDriver<
Controller extends object,
UseParametric extends boolean = false,
>(): Driver<Controller, UseParametric> {
return this.base_.getDriver();
}

/* ----------------------------------------------------------------
HANDSHAKES
---------------------------------------------------------------- */
Expand All @@ -196,8 +158,8 @@ export class MutexAcceptor<Header, Provider extends object | null> {
*
* @param provider An object providing additional features for the remote client. If you don't have plan to proivde any additional feature, assign `null`.
*/
public async accept(provider: Provider): Promise<void> {
await this.base_.accept({ group: this.group_, provider: provider });
public async accept(): Promise<void> {
await this.base_.accept(this.group_);
this.base_.join().then(() => this.group_.destructor());
}

Expand All @@ -221,5 +183,5 @@ export namespace MutexAcceptor {
/**
* Current state of the {@link MutexAcceptor}
*/
export import State = WebAcceptor.State;
export import State = WebSocketAcceptor.State;
}
98 changes: 29 additions & 69 deletions src/MutexConnector.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Driver, Promisive, WebConnector } from "tgrid";
import { WebSocketConnector } from "tgrid";

import { RemoteBarrier } from "./client/RemoteBarrier";
import { RemoteConditionVariable } from "./client/RemoteConditionVariable";
import { RemoteLatch } from "./client/RemoteLatch";
import { RemoteMutex } from "./client/RemoteMutex";
import { RemoteSemaphore } from "./client/RemoteSemaphore";
import { ProviderCapsule } from "./server/ProviderCapsule";
import { ProviderGroup } from "./server/ProviderGroup";

/**
* Mutex server connector for client.
*
* The {@link MutexConnector} is a communicator class who can connect to a remote `mutex-server`
* and interact with it through websocket and [RFC](https://github.com/samchon/tgrid#13-remote-function-call)
* and interact with it through websocket and [RPC](https://tgrid.com/docs/remote-procedure-call/)
* protocol.
*
* You can connect to the websocket `mutex-server` using {@link connect} method. After the
Expand All @@ -28,29 +28,19 @@ import { ProviderCapsule } from "./server/ProviderCapsule";
* - {@link getBarrier}
* - {@link getLatch}
*
* Also, when declaring this {@link MutexConnector} type, you've to define two template arguments,
* *Header* and *Provider*. The *Header* type repersents an initial data sending to the remote
* `mutex-server` after connection. I hope you not to omit it and utilize it as an activation tool
* to enhance security.
*
* The second template argument *Provider* represents the custom features provided for the remote
* `mutex-server`. If you don't have any plan to provide any feature to the `mutex-server`, just
* declare it as `null`.
* Also, when declaring this {@link MutexConnector} type, you've to define one template argument,
* *Header*. The *Header* type repersents an initial data sending to the remote `mutex-server`
* after connection. I hope you not to omit it and utilize it as an activation tool to
* enhance security.
*
* @template Header Type of the *header* containing initial data.
* @template Provider Type of additional features provided for the remote server. If no plan to provide anything, declare `null`.
* @author Jeongho Nam - https://github.com/samchon
*/
export class MutexConnector<Header, Provider extends object | null> {
/**
* @hidden
*/
private base_: WebConnector<Header, Provider>;

export class MutexConnector<Header> {
/**
* @hidden
*/
private controller_: Driver<ProviderCapsule<any>>;
private connector_: WebSocketConnector<Header, null, ProviderGroup>;

/* -----------------------------------------------------------
CONSTRUCTORS
Expand All @@ -59,11 +49,9 @@ export class MutexConnector<Header, Provider extends object | null> {
* Initializer Constructor.
*
* @param header Initial data sending to the remote server after connection. Hope to use it as an activation tool.
* @param provider Additional feature provided for the remote server. If don't plan to provide anything to the remote server, assign `null`.
*/
public constructor(header: Header, provider: Provider) {
this.base_ = new WebConnector(header, provider);
this.controller_ = this.base_.getDriver();
public constructor(header: Header) {
this.connector_ = new WebSocketConnector(header, null);
}

/**
Expand All @@ -84,7 +72,7 @@ export class MutexConnector<Header, Provider extends object | null> {
* @throw WebError when server does not accept your connection until timeout.
*/
public connect(url: string, timeout?: number): Promise<void> {
return this.base_.connect(url, { timeout });
return this.connector_.connect(url, { timeout });
}

/**
Expand All @@ -97,15 +85,10 @@ export class MutexConnector<Header, Provider extends object | null> {
* tried to get locks through the remote critical section components by calling their methods,
* those methods would throw {@link RuntimeError} exceptions.
*
* Also, the disconnection destories all [RFC](https://github.com/samchon/tgrid#13-remote-function-call)s
* between the remote server. Therefore, if you or server has an additional `Provider` and
* there're uncompleted method calls to the `Provideer` through the `Driver<Controller>`,
* {@link RuntimeError} exceptions would be thrown.
*
* @throw DomainError when the connection is not online.
*/
public async close(): Promise<void> {
await this.base_.close();
await this.connector_.close();
}

/**
Expand Down Expand Up @@ -136,7 +119,7 @@ export class MutexConnector<Header, Provider extends object | null> {
public join(at: Date): Promise<boolean>;

public join(param?: number | Date): Promise<void | boolean> {
return this.base_.join(param! as Date);
return this.connector_.join(param! as Date);
}

/* -----------------------------------------------------------
Expand All @@ -146,7 +129,7 @@ export class MutexConnector<Header, Provider extends object | null> {
* Get connection url.
*/
public get url(): string | undefined {
return this.base_.url;
return this.connector_.url;
}

/**
Expand All @@ -162,46 +145,19 @@ export class MutexConnector<Header, Provider extends object | null> {
* - `CLOSED`: The connection is offline.
*/
public get state(): MutexConnector.State {
return this.base_.state;
return this.connector_.state;
}

/**
* Get header.
*/
public get header(): Header {
return this.base_.header;
}

/**
* Get Driver for [RFC](https://github.com/samchon/tgrid#13-remote-function-call).
*
* If your target {@link MutexServer} provides additional features, you can utilize those
* features through returned `Driver` object by this method. To use this method, you should
* define the `Controller` template argument, a type of interface who is defining additional
* features provided from the remote server.
*
* The returned object `Driver` makes to call remote functions, defined in the `Controller`
* and provided by `Provider` in the remote server, possible. In other words, callling a
* function in the `Driver<Controller>`, it means to call a matched function in the remote
* server's `Provider` object.
*
* - `Controller`: Definition only
* - `Driver`: [Remote Function Call](https://github.com/samchon/tgrid#13-remote-function-call)
*
* @template Controller An interface for provided features (functions & objects) from the remote system (`Provider`).
* @template UseParametric Whether to convert type of function parameters to be compatible with their pritimive.
* @return A `Driver` for the [RFC](https://github.com/samchon/tgrid#13-remote-function-call).
*/
public getDriver<
Controller extends object,
UseParametric extends boolean = false,
>(): Promisive<Controller, UseParametric> {
return this.controller_.provider as any;
return this.connector_.header;
}

/* -----------------------------------------------------------
THREAD COMPONENTS
----------------------------------------------------------- */
THREAD COMPONENTS
----------------------------------------------------------- */
/**
* Get remote condition variable.
*
Expand All @@ -216,7 +172,7 @@ export class MutexConnector<Header, Provider extends object | null> {
*/
public getConditionVariable(key: string): Promise<RemoteConditionVariable> {
return RemoteConditionVariable.create(
this.controller_.group.condition_variables,
this.connector_.getDriver().condition_variables,
key,
);
}
Expand All @@ -234,7 +190,7 @@ export class MutexConnector<Header, Provider extends object | null> {
* @return A {@link RemoteMutex} object.
*/
public getMutex(key: string): Promise<RemoteMutex> {
return RemoteMutex.create(this.controller_.group.mutexes, key);
return RemoteMutex.create(this.connector_.getDriver().mutexes, key);
}

/**
Expand All @@ -255,7 +211,7 @@ export class MutexConnector<Header, Provider extends object | null> {
*/
public getSemaphore(key: string, count: number): Promise<RemoteSemaphore> {
return RemoteSemaphore.create(
this.controller_.group.semaphores,
this.connector_.getDriver().semaphores,
key,
count,
);
Expand All @@ -278,7 +234,11 @@ export class MutexConnector<Header, Provider extends object | null> {
* @return A {@link RemoteBarrier} object.
*/
public getBarrier(key: string, count: number): Promise<RemoteBarrier> {
return RemoteBarrier.create(this.controller_.group.barriers, key, count);
return RemoteBarrier.create(
this.connector_.getDriver().barriers,
key,
count,
);
}

/**
Expand All @@ -299,7 +259,7 @@ export class MutexConnector<Header, Provider extends object | null> {
*/
public getLatch(identifier: string, count: number): Promise<RemoteLatch> {
return RemoteLatch.create(
this.controller_.group.latches,
this.connector_.getDriver().latches,
identifier,
count,
);
Expand All @@ -313,5 +273,5 @@ export namespace MutexConnector {
/**
* Current state of the {@link MutexConnector}
*/
export import State = WebConnector.State;
export import State = WebSocketConnector.State;
}
Loading

0 comments on commit 10ac1c7

Please sign in to comment.