Skip to content

Commit

Permalink
feat(plugins): add initial plugin loader mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
wzr1337 committed Jan 10, 2017
1 parent 1ede568 commit 923b554
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
62 changes: 53 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,44 @@ import { Observable, Observer, BehaviorSubject} from '@reactivex/rxjs';
import * as express from 'express';
import { WebServer } from "./expressapp";
import * as uuid from "uuid";
import * as fs from "fs";
import * as path from "path";

declare function require(moduleName: string): any;

const PLUGINDIR = path.join(__dirname, "plugins");

const URIREGEX = /^\/(\w+)\/(\w+)\/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fAF]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})?#?\w*\??([\w$=&\(\)\:\,\;\-\+]*)?$/; //Group1: Servicename, Group2: Resourcename, Group3: element id, Group4: queryparameter list

// set up the server
var server = new WebServer();
server.init(); // need to init


/**
* Plugin loader
*
* browses the PLUGINDIR for available plugins and registers them with the viwi sevrer
*/
fs.readdir(path.join(__dirname, "plugins"), (err:NodeJS.ErrnoException, files: string[]) => {
if(err) {
console.error(err);
}
files.forEach(file => {
let module = path.join(PLUGINDIR, file);
if(fs.lstatSync(module).isDirectory()) {
let _module = require(module);
console.log("Loading Plugin:", file);
console.log(new _module().name());
}
});
});



const URIREGEX = /^\/(\w+)\/(\w+)\/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fAF]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})?#?\w*\??([\w$=&\(\)\:\,\;\-\+]*)?$/; //Group1: Servicename, Group2: Resourcename, Group3: element id, Group4: queryparameter list



const rendererId = "d6ebfd90-d2c1-11e6-9376-df943f51f0d8";//uuid.v1(); // FIXED for now

var renderers = [
Expand Down Expand Up @@ -97,6 +128,22 @@ server.app.get('/media/renderers/', (req: express.Request, res: express.Response
[ws: WebSocket]: BehaviorSubject<{}>
} = {};*/


class Media {
/**
* Renderer specific element retrieval
*
*/
static getElement(collection, elementId) {
return collection.find((element:BehaviorSubject<{}>) => {
return (<{id:string}>element.getValue()).id === elementId;
});
}
}

/**
* WebSocket stuff
*/
server.ws.on('connection', (ws) => {
ws.on("message", (message:string) => {
let msg = JSON.parse(message);
Expand All @@ -105,14 +152,12 @@ server.ws.on('connection', (ws) => {
let captureGroups = msg.event.match(URIREGEX);
if (captureGroups) {
if (captureGroups[3]) {
// element subscription

// find the element requested by the client
let element = renderers.find((element:BehaviorSubject<{}>) => {
return (<{id:string}>element.getValue()).id === rendererId;
});
// this is an element subscription
// === Service sepcific callback goes here ======
let element = Media.getElement(renderers, rendererId);
// ==============================================

element.subscribe( //@TODO keep per client referenc for unsubscription etc.
element.subscribe( //@TODO keep per client reference for unsubscription etc.
(data:any) => {
ws.send(JSON.stringify({type: "data", status: "ok", event: msg.event, data: data}));
},
Expand All @@ -138,7 +183,6 @@ server.ws.on('connection', (ws) => {
ws.send(JSON.stringify({type: "error", code: "501", data: "Not Implemented"}));
break;
}
msg.type;

})
});
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/media/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {viwiPlugin} from "../viwiPlugin";

class Renderer implements viwiPlugin {
private name_:string;

constructor() {
this.name_ = "abcdef";
}

name() {
return this.name_;
}
}

export = Renderer;
7 changes: 7 additions & 0 deletions src/plugins/viwiPlugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare namespace e {
interface viwiPlugin {
name();
}
}

export = e;

0 comments on commit 923b554

Please sign in to comment.