Skip to content

Commit

Permalink
Ready for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Samchon committed Oct 30, 2018
1 parent c6b3264 commit c640d94
Show file tree
Hide file tree
Showing 28 changed files with 320 additions and 60 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules/
build/
src/
test/
bundle/

*.log
*.*ignore
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ node_js:
- 'stable'
before_install:
- npm install -g typescript
- npm install -g browserify
install:
- npm install
script:
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ npm install --save tgrid
import { Vector } from "tstl/container";
import { WebServer } from "tgrid/protocol/web";

function main(): void
async function main(): Promise<void>
{
let server = new WebServer();
server.open(10101, async acceptor =>
await server.open(10101, async acceptor =>
{
// accept connection & provide Vector
await acceptor.accept();
await acceptor.listen(new Vector<number>());
});
Expand All @@ -52,18 +53,32 @@ import { WebConnector } from "tgrid/protocol/web";

async function main(): Promise<void>
{
//----
// PREPARE
//----
// do connect
let connector = 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 = 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));

// close connection
await connector.close();
}
main();
Expand Down
40 changes: 40 additions & 0 deletions build/bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require("fs");
const browserify = require('browserify');

function bundle(path, output)
{
return new Promise((resolve, reject) =>
{
let b = browserify([path]);
b.bundle((err, src) =>
{
if (err)
reject(err);
else
{
fs.writeFile(output, src, (err) =>
{
if (err)
reject(err);
else
resolve();
});
}
});
});
}

async function main()
{
const INSTANCES =
[
"worker-server",
"worker-client",
"shared-worker-server",
"shared-worker-client",
"web-client"
];
for (let instance of INSTANCES)
await bundle(`test/protocol/browser/${instance}.js`, `bundle/${instance}.js`);
}
main();
6 changes: 5 additions & 1 deletion build/clean
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ function iterate(path)
fs.rmdirSync(path);
}

iterate(__dirname + "/..");
function main()
{
iterate(__dirname + "/..");
}
main();
1 change: 1 addition & 0 deletions bundle/shared-worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="shared-worker-client.js"></script>
1 change: 1 addition & 0 deletions bundle/web.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="web-client.js"></script>
1 change: 1 addition & 0 deletions bundle/worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="worker-client.js"></script>
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
"email": "samchon@samchon.org",
"url": "http://samchon.org"
},
"version": "0.0.16",
"version": "0.0.17",
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"compile": "tsc && npm run bundle-worker",
"compile": "tsc && npm run bundle",
"build": "npm run compile && npm run test",
"bundle-worker": "browserify test/protocol/instances/worker-server.js -o test/protocol/instances/worker-server.bundle.js",
"bundle": "node build/bundle",
"clean": "node build/clean",
"test": "node test/main"
},
"dependencies": {
"tstl": "^2.0.8",
"tstl": "^2.0.9",
"websocket": "^1.0.28"
},
"devDependencies": {
"@types/browserify": "^12.0.36",
"@types/node": "^10.12.0",
"@types/sharedworker": "0.0.28",
"@types/websocket": "0.0.40"
"@types/websocket": "0.0.40",
"browserify": "^16.2.3"
},
"homepage": "https://github.com/samchon/tgrid#readme",
"repository": {
Expand Down
12 changes: 12 additions & 0 deletions src/protocol/internal/ICommunicator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @hidden
*/
export interface ICommunicator
{
/**
* Close handler.
*
* A function called when the connection has been closed. It doesn't matter who've closed the connection, whether user or remote system.
*/
handleClose: (code: number, reason: string) => void;
}
18 changes: 12 additions & 6 deletions src/protocol/internal/IConnector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommunicatorBase } from "../../base/CommunicatorBase";
import { ICommunicator } from "./ICommunicator";

export interface IConnector<State, Provider extends object>
extends CommunicatorBase<Provider>
export interface IConnector<State>
extends ICommunicator
{
/**
* Get state.
Expand All @@ -11,20 +11,26 @@ export interface IConnector<State, Provider extends object>
readonly state: State

/**
* Wait for server to provide.
* Wait server to provide.
*
* Wait server to specify its `Provider` permanently.
*/
wait(): Promise<void>;

/**
* Wait for server to provide or timeout.
* Wait server to provide or timeout.
*
* Wait server to specify its `Provider` for specified milliseconds.
*
* @param ms The maximum milliseconds for waiting.
* @return Whether awaken by completion or timeout.
*/
wait(ms: number): Promise<boolean>;

/**
* Wait for server to provide or time expiration.
* Wait server to provide or time expiration.
*
* Wait server to specify its `Provider` until time expiration.
*
* @param at The maximum time point to wait.
* @return Whether awaken by completion or time expiration.
Expand Down
29 changes: 26 additions & 3 deletions src/protocol/web/WebAcceptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as ws from "websocket";

import { CommunicatorBase } from "../../base/CommunicatorBase";
import { IWebCommunicator } from "./internal/IWebCommunicator";
import { Invoke } from "../../base/Invoke";

import { LogicError, RuntimeError } from "tstl/exception";

export class WebAcceptor extends CommunicatorBase
export class WebAcceptor extends CommunicatorBase implements IWebCommunicator
{
/**
* @hidden
Expand Down Expand Up @@ -52,6 +54,13 @@ export class WebAcceptor extends CommunicatorBase
/* ----------------------------------------------------------------
HANDSHAKES
---------------------------------------------------------------- */
/**
* Accept connection.
*
* @param protocol
* @param allowOrigin
* @param cookies
*/
public accept(
protocol?: string,
allowOrigin?: string,
Expand Down Expand Up @@ -83,6 +92,13 @@ export class WebAcceptor extends CommunicatorBase
});
}

/**
* Reject connection.
*
* @param status Status code.
* @param reason Detailed reason to reject.
* @param extraHeaders Extra headers if required.
*/
public reject(status?: number, reason?: string, extraHeaders?: object): Promise<void>
{
return new Promise(resolve =>
Expand All @@ -107,7 +123,7 @@ export class WebAcceptor extends CommunicatorBase
return;

this.listening_ = true;
this.connection_.sendUTF("LISTENING");
this.connection_.sendUTF("PROVIDE");
}

/* ----------------------------------------------------------------
Expand All @@ -131,7 +147,14 @@ export class WebAcceptor extends CommunicatorBase
.toString();
}

public handleClose: (code: number, reason: string)=>void;
/**
* @inheritDoc
*/
public handleClose: (code: number, reason: string) => void;

/**
* @inheritDoc
*/
public handleError: (error: Error)=>void;

/* ----------------------------------------------------------------
Expand Down
14 changes: 11 additions & 3 deletions src/protocol/web/WebConnector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CommunicatorBase } from "../../base/CommunicatorBase";
import { IWebCommunicator } from "./internal/IWebCommunicator";
import { IConnector } from "../internal/IConnector";
import { Invoke } from "../../base/Invoke";

Expand All @@ -14,11 +15,11 @@ import { is_node } from "tstl/utility/node";
*/
var g: IFeature = is_node()
? require("./internal/websocket-polyfill")
: <any>window;
: <any>self;

export class WebConnector<Provider extends object = {}>
extends CommunicatorBase<Provider>
implements IConnector<WebConnector.State, Provider>
implements IConnector<WebConnector.State>, IWebCommunicator
{
/**
* @hidden
Expand Down Expand Up @@ -166,7 +167,14 @@ export class WebConnector<Provider extends object = {}>
/* ----------------------------------------------------------------
EVENT HANDLERS
---------------------------------------------------------------- */
/**
* @inheritDoc
*/
public handleClose: (code: number, reason: string) => void;

/**
* @inheritDoc
*/
public handleError: (error: Error) => void;

/**
Expand Down Expand Up @@ -226,7 +234,7 @@ export class WebConnector<Provider extends object = {}>
*/
private _Handle_message(evt: MessageEvent): void
{
if (evt.data === "LISTENING")
if (evt.data === "PROVIDE")
{
this.server_is_listening_ = true;
this.cv_.notify_all();
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/web/WebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class WebServer
reject(error);
});

// DO OPEN - START LISTENING
// DO OPEN - START PROVIDE
this.server_.listen(port);
});
}
Expand Down
14 changes: 14 additions & 0 deletions src/protocol/web/internal/IWebCommunicator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ICommunicator } from "../../internal/ICommunicator";

/**
* @hidden
*/
export interface IWebCommunicator extends ICommunicator
{
/**
* Communication error handler.
*
* A function called when a communication (protocol) error has been occured. Note that, the error means the protocol level error, not means the logic level error occured by controller or provider.
*/
handleError: (error: Error) => void;
}
Loading

0 comments on commit c640d94

Please sign in to comment.