smartuniverse/ts/smartuniverse.classes.universe.ts

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-03-13 05:15:40 +00:00
import * as plugins from './smartuniverse.plugins';
import { Handler, Route, Server } from 'smartexpress';
2018-04-13 13:45:48 +00:00
import { UniverseChannel } from './smartuniverse.classes.universechannel';
2018-03-13 05:15:40 +00:00
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { UniverseStore } from './smartuniverse.classes.universestore';
2018-04-13 13:45:48 +00:00
2018-03-13 05:15:40 +00:00
import * as paths from './smartuniverse.paths';
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
}
export interface IServerGetMessagesRequestBody {
2018-04-29 12:17:26 +00:00
channel: string;
topic?: string;
2018-03-13 05:15:40 +00:00
youngerThan: number;
}
2018-05-23 22:14:57 +00:00
/**
* the interface for a standard request
*/
2018-03-13 05:15:40 +00:00
export interface IServerPutMessageRequestBody {
2018-05-19 22:41:59 +00:00
channel: string;
passphrase: string;
2018-03-20 07:16:54 +00:00
message: string;
payload: any;
2018-03-13 05:15:40 +00:00
}
2018-05-23 22:14:57 +00:00
/**
* main class that setsup a Universe
*/
2018-03-13 05:15:40 +00:00
export class Universe {
// subinstances
public universeStore: UniverseStore;
// options
private options: ISmartUniverseConstructorOptions;
2018-03-20 07:16:54 +00:00
2018-03-13 05:15:40 +00:00
// Store version handling
private universeVersionStore: string;
private get universeVersion() {
if (this.universeVersionStore) {
return this.universeVersionStore;
} else {
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
this.universeVersionStore = packageJson.version;
return this.universeVersionStore;
}
}
private smartexpressServer: plugins.smartexpress.Server;
2018-03-20 07:16:54 +00:00
private smartsocket: plugins.smartsocket.Smartsocket;
2018-03-13 05:15:40 +00:00
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
this.universeStore = new UniverseStore(this.options.messageExpiryInMilliseconds);
}
/**
* initiates a server
*/
public async initServer(portArg: number | string) {
this.smartexpressServer = new plugins.smartexpress.Server({
cors: true,
defaultAnswer: `smartuniverse server ${this.universeVersion}`,
forceSsl: false,
port: portArg
});
2018-04-13 13:45:48 +00:00
// message handling
2018-03-15 00:16:16 +00:00
// adds messages
2018-03-13 05:15:40 +00:00
const addMessageHandler = new Handler('PUT', request => {
2018-05-19 22:41:59 +00:00
const requestBody: IServerPutMessageRequestBody = request.body;
2018-05-23 22:14:57 +00:00
const message = new UniverseMessage(requestBody.message, requestBody.channel, requestBody.passphrase, requestBody.payload);
2018-05-19 22:41:59 +00:00
this.universeStore.addMessage(message);
2018-03-15 00:16:16 +00:00
console.log(requestBody);
2018-03-13 05:15:40 +00:00
return true;
});
2018-03-15 00:16:16 +00:00
// gets messages
2018-03-13 05:15:40 +00:00
const readMessageHandler = new Handler('GET', request => {
2018-04-13 13:45:48 +00:00
const done = plugins.smartq.defer<UniverseMessage[]>();
2018-03-13 05:15:40 +00:00
const requestBody = request.body;
2018-04-13 13:45:48 +00:00
const messageObservable = this.universeStore.readMessagesYoungerThan(requestBody.since);
messageObservable.toArray().subscribe(universeMessageArrayArg => {
done.resolve(universeMessageArrayArg);
});
return done.promise;
2018-03-20 07:16:54 +00:00
});
2018-03-13 05:15:40 +00:00
2018-04-13 13:45:48 +00:00
// create new Route for messages
2018-03-13 05:15:40 +00:00
const messageRoute = new Route(this.smartexpressServer, 'message');
messageRoute.addHandler(addMessageHandler);
messageRoute.addHandler(readMessageHandler);
2018-04-13 13:45:48 +00:00
const leaderElectionRoute = new Route(this.smartexpressServer, 'leadelection');
// TODO: implement Handlers for leader election
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({
port: 12345 // fix this within smartsocket
});
this.smartsocket.setExternalServer('express', this.smartexpressServer as any); // should work with express as well
this.smartsocket.start();
2018-03-13 05:15:40 +00:00
await this.smartexpressServer.start();
}
2018-03-20 07:16:54 +00:00
public async stopServer() {
await this.smartsocket.stop();
2018-03-13 05:15:40 +00:00
await this.smartexpressServer.stop();
}
}