smartuniverse/ts/smartuniverse.classes.universe.ts

164 lines
5.3 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';
2019-04-30 17:16:03 +00:00
import { UniverseConnection } from './smartuniverse.classes.universeconnection';
2019-04-11 15:52:01 +00:00
2018-03-13 05:15:40 +00:00
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
}
2018-05-23 22:14:57 +00:00
/**
2019-08-12 12:59:37 +00:00
* main class that setups a Universe
2018-05-23 22:14:57 +00:00
*/
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-30 17:16:03 +00:00
/**
* the smartexpress server used
*/
private smartexpressServer: plugins.smartexpress.Server;
/**
* the smartsocket used
*/
private smartsocket: plugins.smartsocket.Smartsocket;
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
this.universeCache = new UniverseCache(this.options.messageExpiryInMilliseconds);
}
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
*/
2019-04-30 17:16:03 +00:00
public getUniverseVersion() {
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;
}
}
/**
* 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 () => {
2019-06-06 20:22:45 +00:00
return `smartuniverse server ${this.getUniverseVersion()}`;
2019-01-31 01:52:18 +00:00
},
2018-03-13 05:15:40 +00:00
forceSsl: false,
port: portArg
});
2018-04-13 13:45:48 +00:00
// add websocket upgrade
2019-04-24 16:20:31 +00:00
this.smartsocket = new plugins.smartsocket.Smartsocket({});
2018-04-13 13:45:48 +00:00
2019-04-11 15:52:01 +00:00
// add a role for the clients
const ClientRole = new plugins.smartsocket.SocketRole({
2019-04-24 16:20:31 +00:00
name: 'UniverseClient',
passwordHash: plugins.smarthash.sha256FromStringSync('UniverseClient') // authentication happens on another level
});
2019-04-11 15:52:01 +00:00
// add the role to smartsocket
this.smartsocket.addSocketRoles([ClientRole]);
2019-08-13 11:04:49 +00:00
const socketFunctionSubscription = new plugins.smartsocket.SocketFunction({
2019-06-06 21:23:37 +00:00
allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level
funcName: 'channelSubscription',
2019-08-12 12:59:37 +00:00
funcDef: async (
dataArg: interfaces.IServerCallSubscribeActionPayload,
socketConnectionArg
) => {
2019-06-06 21:23:37 +00:00
// run in "this context" of this class
2019-08-12 12:59:37 +00:00
await (async () => {
2019-06-06 21:23:37 +00:00
// TODO: properly add the connection
const universeConnection = new UniverseConnection({
2019-06-10 15:46:06 +00:00
socketConnection: socketConnectionArg,
authenticationRequests: []
2019-08-12 12:59:37 +00:00
});
await UniverseConnection.addConnectionToCache(this.universeCache, universeConnection);
return {
'subscription status': 'success'
};
})();
}
});
2019-08-13 11:04:49 +00:00
const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction({
2019-08-12 12:59:37 +00:00
allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level
funcName: 'processMessage',
funcDef: async (dataArg: interfaces.IUniverseMessage, socketConnectionArg) => {
// run in "this" context of this class
await (async () => {
const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection(
this.universeCache,
socketConnectionArg
);
if (universeConnection) {
console.log('found UniverseConnection for socket');
} else {
2019-08-12 15:23:10 +00:00
return {
error: 'You need to authenticate for a channel'
};
2019-08-12 12:59:37 +00:00
}
2019-08-12 13:10:40 +00:00
const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(dataArg);
const foundChannel = await UniverseChannel.authorizeAMessageForAChannel(
2019-08-12 12:59:37 +00:00
this.universeCache,
2019-08-12 13:10:40 +00:00
unauthenticatedMessage
2019-08-12 12:59:37 +00:00
);
2019-08-12 13:12:31 +00:00
if (foundChannel && unauthenticatedMessage.authenticated) {
2019-08-12 13:10:40 +00:00
const authenticatedMessage = unauthenticatedMessage;
await this.universeCache.addMessage(authenticatedMessage);
}
2019-06-06 20:22:45 +00:00
})();
2019-04-30 17:16:03 +00:00
}
});
2019-08-13 11:04:49 +00:00
// add socket functions
this.smartsocket.addSocketFunction(socketFunctionSubscription);
this.smartsocket.addSocketFunction(socketFunctionProcessMessage);
2019-04-11 15:52:01 +00:00
// add smartsocket to the running smartexpress app
2019-04-24 16:20:31 +00:00
this.smartsocket.setExternalServer('smartexpress', this.smartexpressServer as any);
// start everything
2018-03-13 05:15:40 +00:00
await this.smartexpressServer.start();
2019-04-24 16:20:31 +00:00
await this.smartsocket.start();
console.log('started universe');
2018-03-13 05:15:40 +00:00
}
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();
}
}