Skip to content

Commit

Permalink
Broadcast Framework: Create Multi-client Broadcast Framework
Browse files Browse the repository at this point in the history
The broadcast framework is required to perform operation on collaborative editing. To create similar functionalities as VS Liveshare, all kind of events and event metadata need to be sent to every client connected to same server workspace.

Signed-off-by: Progyan Bhattacharya <bprogyan@gmail.com>
  • Loading branch information
0xTheProDev committed Apr 4, 2019
1 parent b1df8fc commit 9ea7e70
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cache:
- examples/browser/node_modules
- examples/electron/node_modules
- node_modules
- packages/broadcast/node_modules
- packages/bunyan/node_modules
- packages/callhierarchy/node_modules
- packages/console/node_modules
Expand Down
3 changes: 3 additions & 0 deletions packages/broadcast/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Broadcast Messaging

Work In Progress
11 changes: 11 additions & 0 deletions packages/broadcast/compile.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../configs/base.tsconfig",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"baseUrl": "."
},
"include": [
"src"
]
}
48 changes: 48 additions & 0 deletions packages/broadcast/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@theia/broadcast",
"version": "0.5.0",
"description": "Theia - Broadcast Messaging Extension",
"dependencies": {
"@theia/core": "^0.5.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"backend": "lib/node/broadcast-backend-module"
},
{
"frontend": "lib/browser/broadcast-frontend-module"
}
],
"keywords": [
"theia-extension"
],
"license": "EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0",
"repository": {
"type": "git",
"url": "https://github.com/theia-ide/theia.git"
},
"bugs": {
"url": "https://github.com/theia-ide/theia/issues"
},
"homepage": "https://github.com/theia-ide/theia",
"files": [
"lib",
"src"
],
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "theiaext clean",
"build": "theiaext build",
"watch": "theiaext watch",
"test": "theiaext test"
},
"devDependencies": {
"@theia/ext-scripts": "^0.5.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
29 changes: 29 additions & 0 deletions packages/broadcast/src/browser/broadcast-frontend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { ContainerModule } from 'inversify';
import { WebSocketConnectionProvider } from '@theia/core/lib/browser';
import { servicePath, IBroadcastClientDispatch, BroadcastClientDispatch, IBroadcastServer, BroadcastWatcher } from '../common';

export default new ContainerModule(bind => {
bind(IBroadcastClientDispatch).to(BroadcastClientDispatch).inSingletonScope();
bind(BroadcastWatcher).toSelf().inSingletonScope();
bind(IBroadcastServer).toDynamicValue(({ container }) => {
const watcher = container.get(BroadcastWatcher);
const connection = container.get(WebSocketConnectionProvider);
return connection.createProxy<IBroadcastServer>(servicePath, watcher.getConnectedClient());
}).inSingletonScope();
});
51 changes: 51 additions & 0 deletions packages/broadcast/src/common/broadcast-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { inject, injectable } from 'inversify';
import { IBroadcastProtocol, IBroadcastServer, IBroadcastState } from './broadcast-protocol';
import { BroadcastWatcher } from './broadcast-watcher';

export const IBroadcastClientDispatch = Symbol('IBroadcastClientDispatch');

type onStateUpdateHandler = (event: IBroadcastProtocol) => void;

export interface IBroadcastClientDispatch {
onStateUpdate(callback: onStateUpdateHandler): void;
getState(): Promise<IBroadcastState>;
setState(state: IBroadcastState): Promise<IBroadcastProtocol>;
}

@injectable()
export class BroadcastClientDispatch implements IBroadcastClientDispatch {

constructor(
@inject(BroadcastWatcher) private readonly watcher: BroadcastWatcher,
@inject(IBroadcastServer) private readonly server: IBroadcastServer
) { }

onStateUpdate(callback: onStateUpdateHandler) {
this.watcher.onStateUpdate(callback);
}

getState(): Promise<IBroadcastState> {
return this.server.getState();
}

async setState(state: IBroadcastState): Promise<IBroadcastProtocol> {
await this.server.getState();
return this.server.setState(state);
}
}
48 changes: 48 additions & 0 deletions packages/broadcast/src/common/broadcast-protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

/** Interface for Broadcast Message State */
export const IBroadcastState = Symbol('IBroadcastState');

export type IBroadcastState = string | Object;

/** Interface for Broadcast Message Protocol */
export const IBroadcastProtocol = Symbol('IBroadcastProtocol');

export interface IBroadcastProtocol {
prevState: IBroadcastState;
state: IBroadcastState;
}

/** Interface for Broadcast Client */
export const IBroadcastClient = Symbol('IBroadcastClient');

export interface IBroadcastClient {
onStateUpdate(event: IBroadcastProtocol): void;
}

/** Interface for Broadcast Server */
export const IBroadcastServer = Symbol('IBroadcastServer');

export interface IBroadcastServer {
addClient(client: IBroadcastClient): void;
removeClient(client: IBroadcastClient): void;
setState(state: IBroadcastState): Promise<IBroadcastProtocol>;
getState(): Promise<IBroadcastState>;
}

/** API Endpoint for the Broadcast Service */
export const servicePath = '/services/broadcast';
50 changes: 50 additions & 0 deletions packages/broadcast/src/common/broadcast-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable } from 'inversify';
import { IBroadcastServer, IBroadcastClient, IBroadcastState, IBroadcastProtocol } from './broadcast-protocol';

@injectable()
export class BroadcastServer implements IBroadcastServer {

private readonly clients: Set<IBroadcastClient>;
private state: string | Object;

constructor() {
this.clients = new Set<IBroadcastClient>();
this.state = {};
}

addClient(client: IBroadcastClient) {
this.clients.add(client);
}

removeClient(client: IBroadcastClient) {
this.clients.delete(client);
}

setState(state: IBroadcastState): Promise<IBroadcastProtocol> {
const prevState = this.state;
this.state = state;
const stateUpdateMetadata: IBroadcastProtocol = { prevState, state };
this.clients.forEach(client => client.onStateUpdate(stateUpdateMetadata));
return Promise.resolve(stateUpdateMetadata);
}

getState(): Promise<IBroadcastState> {
return Promise.resolve(this.state);
}
}
42 changes: 42 additions & 0 deletions packages/broadcast/src/common/broadcast-watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable } from 'inversify';
import { Emitter, Event } from '@theia/core/lib/common';
import { IBroadcastClient, IBroadcastProtocol } from './broadcast-protocol';

@injectable()
export class BroadcastWatcher {

private readonly emitter: Emitter<IBroadcastProtocol>;

constructor() {
this.emitter = new Emitter<IBroadcastProtocol>();
}

getConnectedClient(): IBroadcastClient {
const { emitter } = this;
return {
onStateUpdate(event: IBroadcastProtocol) {
emitter.fire(event);
}
};
}

get onStateUpdate(): Event<IBroadcastProtocol> {
return this.emitter.event;
}
}
20 changes: 20 additions & 0 deletions packages/broadcast/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

export * from './broadcast-client';
export * from './broadcast-protocol';
export * from './broadcast-server';
export * from './broadcast-watcher';
34 changes: 34 additions & 0 deletions packages/broadcast/src/node/broadcast-backend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { ContainerModule } from 'inversify';
import { ConnectionHandler, JsonRpcConnectionHandler } from '@theia/core/lib/common';
import { servicePath, IBroadcastClientDispatch, BroadcastClientDispatch, IBroadcastClient, IBroadcastServer, BroadcastServer, BroadcastWatcher } from '../common';

export default new ContainerModule(bind => {
bind(IBroadcastClientDispatch).to(BroadcastClientDispatch).inSingletonScope();
bind(BroadcastWatcher).toSelf().inSingletonScope();
bind(BroadcastServer).toSelf().inSingletonScope();
bind(IBroadcastServer).toService(BroadcastServer);
bind(ConnectionHandler).toDynamicValue(({ container }) =>
new JsonRpcConnectionHandler<IBroadcastClient>(servicePath, client => {
const server = container.get<IBroadcastServer>(IBroadcastServer);
client.onDidOpenConnection(() => server.addClient(client));
client.onDidCloseConnection(() => server.removeClient(client));
return container.get<IBroadcastServer>(IBroadcastServer);
})
).inSingletonScope();
});

0 comments on commit 9ea7e70

Please sign in to comment.