2018-03-13 05:15:40 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
|
|
|
|
2019-01-31 01:52:18 +00:00
|
|
|
import { Handler, Route, Server } from '@pushrocks/smartexpress';
|
2018-05-28 10:07:25 +00:00
|
|
|
import { UniverseCache, UniverseChannel, UniverseMessage } from './';
|
2018-04-13 13:45:48 +00:00
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
import * as paths from './smartuniverse.paths';
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
import * as interfaces from './interfaces';
|
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
export interface ISmartUniverseConstructorOptions {
|
|
|
|
messageExpiryInMilliseconds: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2018-05-26 11:44:32 +00:00
|
|
|
public universeCache: UniverseCache;
|
2018-03-13 05:15:40 +00:00
|
|
|
|
|
|
|
// options
|
|
|
|
private options: ISmartUniverseConstructorOptions;
|
2018-03-20 07:16:54 +00:00
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
/**
|
|
|
|
* stores the version of the universe server running
|
|
|
|
* this is done since the version is exposed through the api and multiple fs actions are avoided this way.
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
private universeVersionStore: string;
|
2019-04-11 15:52:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the currently running version of smartuniverse
|
|
|
|
*/
|
|
|
|
public get universeVersion() {
|
2018-03-13 05:15:40 +00:00
|
|
|
if (this.universeVersionStore) {
|
|
|
|
return this.universeVersionStore;
|
|
|
|
} else {
|
|
|
|
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
|
|
|
|
this.universeVersionStore = packageJson.version;
|
|
|
|
return this.universeVersionStore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
/**
|
|
|
|
* the smartexpress server used
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
private smartexpressServer: plugins.smartexpress.Server;
|
2019-04-11 15:52:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the smartsocket used
|
|
|
|
*/
|
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;
|
2018-05-26 11:44:32 +00:00
|
|
|
this.universeCache = new UniverseCache(this.options.messageExpiryInMilliseconds);
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 10:07:25 +00:00
|
|
|
/**
|
|
|
|
* adds a channel to the Universe
|
|
|
|
*/
|
|
|
|
public async addChannel(nameArg: string, passphraseArg: string) {
|
2019-04-11 15:52:01 +00:00
|
|
|
const newChannel = UniverseChannel.createChannel(this.universeCache, nameArg, passphraseArg);
|
2018-05-28 10:07:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
/**
|
|
|
|
* initiates a server
|
|
|
|
*/
|
2019-04-11 16:50:43 +00:00
|
|
|
public async start(portArg: number | string) {
|
2019-04-11 15:52:01 +00:00
|
|
|
// lets create the base smartexpress server
|
2018-03-13 05:15:40 +00:00
|
|
|
this.smartexpressServer = new plugins.smartexpress.Server({
|
|
|
|
cors: true,
|
2019-01-31 01:52:18 +00:00
|
|
|
defaultAnswer: async () => {
|
|
|
|
return `smartuniverse server ${this.universeVersion}`;
|
|
|
|
},
|
2018-03-13 05:15:40 +00:00
|
|
|
forceSsl: false,
|
|
|
|
port: portArg
|
|
|
|
});
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// lets create the http request route
|
|
|
|
this.smartexpressServer.addRoute('/sendmessage', new Handler('POST', async (req, res) => {
|
|
|
|
this.universeCache.addMessage(req.body);
|
|
|
|
}));
|
|
|
|
|
2018-04-13 13:45:48 +00:00
|
|
|
// add websocket upgrade
|
|
|
|
this.smartsocket = new plugins.smartsocket.Smartsocket({
|
|
|
|
port: 12345 // fix this within smartsocket
|
|
|
|
});
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// add a role for the clients
|
2018-05-30 14:34:06 +00:00
|
|
|
const ClientRole = new plugins.smartsocket.SocketRole({
|
|
|
|
name: 'clientuniverse',
|
|
|
|
passwordHash: 'clientuniverse' // authentication happens on another level
|
|
|
|
});
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// add the role to smartsocket
|
2018-05-30 14:34:06 +00:00
|
|
|
this.smartsocket.addSocketRoles([ClientRole]);
|
|
|
|
|
|
|
|
const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({
|
|
|
|
allowedRoles: [ClientRole],
|
|
|
|
funcName: 'channelSubscription',
|
2019-04-11 15:52:01 +00:00
|
|
|
funcDef: () => {} // TODO: implement an action upon connection of clients
|
2018-05-30 14:34:06 +00:00
|
|
|
});
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// add smartsocket to the running smartexpress app
|
2018-05-28 10:07:25 +00:00
|
|
|
this.smartsocket.setExternalServer('express', this.smartexpressServer as any);
|
2019-04-11 15:52:01 +00:00
|
|
|
|
|
|
|
// start the socket
|
2018-04-13 13:45:48 +00:00
|
|
|
this.smartsocket.start();
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
// start the smartexpress instance
|
2018-03-13 05:15:40 +00:00
|
|
|
await this.smartexpressServer.start();
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:52:01 +00:00
|
|
|
/**
|
|
|
|
* stop everything
|
|
|
|
*/
|
2018-03-20 07:16:54 +00:00
|
|
|
public async stopServer() {
|
2018-03-27 22:38:32 +00:00
|
|
|
await this.smartsocket.stop();
|
2018-03-13 05:15:40 +00:00
|
|
|
await this.smartexpressServer.stop();
|
|
|
|
}
|
|
|
|
}
|