Skip to content

Commit

Permalink
Upgrade TGrid version, and change interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Jun 25, 2024
1 parent 6f3a23e commit 76cd5be
Show file tree
Hide file tree
Showing 28 changed files with 239 additions and 235 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.0",
"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
73 changes: 36 additions & 37 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 Down Expand Up @@ -30,11 +29,11 @@ import { ProviderGroup } from "./server/ProviderGroup";
* @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 +47,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 +57,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 Down Expand Up @@ -159,32 +158,32 @@ 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();
}
// /**
// * 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 +195,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 +220,5 @@ export namespace MutexAcceptor {
/**
* Current state of the {@link MutexAcceptor}
*/
export import State = WebAcceptor.State;
export import State = WebSocketAcceptor.State;
}
55 changes: 22 additions & 33 deletions src/MutexConnector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Driver, Promisive, WebConnector } from "tgrid";
import { Driver, 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.
Expand Down Expand Up @@ -41,16 +41,16 @@ import { ProviderCapsule } from "./server/ProviderCapsule";
* @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> {
export class MutexConnector<Header> {
/**
* @hidden
*/
private base_: WebConnector<Header, Provider>;
private connector_: WebSocketConnector<Header, null, ProviderGroup>;

/**
* @hidden
*/
private controller_: Driver<ProviderCapsule<any>>;
private driver_: Driver<ProviderGroup>;

/* -----------------------------------------------------------
CONSTRUCTORS
Expand All @@ -61,9 +61,9 @@ export class MutexConnector<Header, Provider extends object | null> {
* @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);
this.driver_ = this.connector_.getDriver();
}

/**
Expand All @@ -84,7 +84,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 @@ -105,7 +105,7 @@ export class MutexConnector<Header, Provider extends object | null> {
* @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 +136,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 +146,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,14 +162,14 @@ 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;
return this.connector_.header;
}

/**
Expand All @@ -192,11 +192,8 @@ export class MutexConnector<Header, Provider extends object | null> {
* @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;
public getDriver(): Driver<ProviderGroup> {
return this.driver_;
}

/* -----------------------------------------------------------
Expand All @@ -216,7 +213,7 @@ export class MutexConnector<Header, Provider extends object | null> {
*/
public getConditionVariable(key: string): Promise<RemoteConditionVariable> {
return RemoteConditionVariable.create(
this.controller_.group.condition_variables,
this.driver_.condition_variables,
key,
);
}
Expand All @@ -234,7 +231,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.driver_.mutexes, key);
}

/**
Expand All @@ -254,11 +251,7 @@ export class MutexConnector<Header, Provider extends object | null> {
* @return A {@link RemoteSemaphore} object.
*/
public getSemaphore(key: string, count: number): Promise<RemoteSemaphore> {
return RemoteSemaphore.create(
this.controller_.group.semaphores,
key,
count,
);
return RemoteSemaphore.create(this.driver_.semaphores, key, count);
}

/**
Expand All @@ -278,7 +271,7 @@ 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.driver_.barriers, key, count);
}

/**
Expand All @@ -298,11 +291,7 @@ export class MutexConnector<Header, Provider extends object | null> {
* @return A {@link RemoteLatch} object.
*/
public getLatch(identifier: string, count: number): Promise<RemoteLatch> {
return RemoteLatch.create(
this.controller_.group.latches,
identifier,
count,
);
return RemoteLatch.create(this.driver_.latches, identifier, count);
}
}

Expand All @@ -313,5 +302,5 @@ export namespace MutexConnector {
/**
* Current state of the {@link MutexConnector}
*/
export import State = WebConnector.State;
export import State = WebSocketConnector.State;
}
19 changes: 7 additions & 12 deletions src/MutexServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
* @module msv
*/
//-----------------------------------------------------------
import { WebServer } from "tgrid";
import { WebSocketServer } from "tgrid";
import { MutexAcceptor } from "./MutexAcceptor";

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

Expand Down Expand Up @@ -40,7 +39,7 @@ export class MutexServer<Header, Provider extends object | null> {
/**
* @hidden
*/
private server_: WebServer<Header, ProviderCapsule<Provider>>;
private server_: WebSocketServer<Header, ProviderGroup, null>;

/**
* @hidden
Expand Down Expand Up @@ -68,7 +67,7 @@ export class MutexServer<Header, Provider extends object | null> {
public constructor(key: string, cert: string);

public constructor(key?: string, cert?: string) {
this.server_ = new WebServer(key!, cert!);
this.server_ = new WebSocketServer(key!, cert!);
this.components_ = new GlobalGroup();
}

Expand Down Expand Up @@ -97,15 +96,11 @@ export class MutexServer<Header, Provider extends object | null> {
*/
public open(
port: number,
handler: (acceptor: MutexAcceptor<Header, Provider>) => Promise<void>,
handler: (acceptor: MutexAcceptor<Header>) => Promise<void>,
): Promise<void> {
return this.server_.open(port, async (base) => {
let group: ProviderGroup = new ProviderGroup(this.components_, base);
let acceptor: MutexAcceptor<Header, Provider> = MutexAcceptor.create(
base,
group,
);

const group: ProviderGroup = new ProviderGroup(this.components_, base);
const acceptor: MutexAcceptor<Header> = MutexAcceptor.create(base, group);
await handler(acceptor);
});
}
Expand Down Expand Up @@ -153,5 +148,5 @@ export namespace MutexServer {
/**
* Current state of the {@link MutexServer}
*/
export import State = WebServer.State;
export import State = WebSocketServer.State;
}
2 changes: 1 addition & 1 deletion src/client/RemoteBarrier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class RemoteBarrier {
* @return Whether succeeded to waiting in the given time.
*/
public async wait_until(at: Date): Promise<boolean> {
let ms: number = at.getTime() - Date.now();
const ms: number = at.getTime() - Date.now();
return await this.wait_for(ms);
}

Expand Down
Loading

0 comments on commit 76cd5be

Please sign in to comment.