smartuniverse/ts/smartuniverse.classes.universe.ts

130 lines
3.6 KiB
TypeScript
Raw Normal View History

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';
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
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;
this.universeCache = new UniverseCache(this.options.messageExpiryInMilliseconds);
2018-03-13 05:15:40 +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-03-13 05:15:40 +00:00
/**
* initiates a server
*/
2019-04-22 22:28:57 +00:00
public async start(portArg: number) {
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
2019-04-22 22:28:57 +00:00
this.smartexpressServer.addRoute(
'/sendmessage',
new Handler('POST', async (req, res) => {
const universeMessageInstance = new UniverseMessage(req.body);
this.universeCache.addMessage(universeMessageInstance);
res.status(200);
res.end();
})
);
2019-04-11 15:52:01 +00:00
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
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
this.smartsocket.addSocketRoles([ClientRole]);
const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({
allowedRoles: [ClientRole],
funcName: 'channelSubscription',
2019-04-22 11:06:01 +00:00
funcDef: () => {
console.log('a client connected');
} // TODO: implement an action upon connection of clients
});
2019-04-11 15:52:01 +00:00
// add smartsocket to the running smartexpress app
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() {
await this.smartsocket.stop();
2018-03-13 05:15:40 +00:00
await this.smartexpressServer.stop();
}
}