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';
|
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;
|
2019-09-01 14:54:36 +00:00
|
|
|
externalServer?: plugins.smartexpress.Server;
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
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-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;
|
2019-08-13 13:48:20 +00:00
|
|
|
this.universeCache = new UniverseCache(this, this.options.messageExpiryInMilliseconds);
|
2019-04-30 17:16:03 +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
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:07:25 +00:00
|
|
|
/**
|
|
|
|
* adds a channel to the Universe
|
|
|
|
*/
|
2019-08-13 16:43:33 +00:00
|
|
|
public addChannel(nameArg: string, passphraseArg: string) {
|
2019-08-13 13:48:20 +00:00
|
|
|
const newChannel = UniverseChannel.createChannel(this, nameArg, passphraseArg);
|
2019-09-10 08:50:55 +00:00
|
|
|
return newChannel;
|
2018-05-28 10:07:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 08:50:55 +00:00
|
|
|
/**
|
|
|
|
* returns a channel
|
|
|
|
*/
|
2019-09-17 12:01:24 +00:00
|
|
|
public getChannel(channelNameArg: string) {
|
2019-09-10 08:50:55 +00:00
|
|
|
return this.universeCache.channelMap.find(channelArg => {
|
|
|
|
return channelArg.name === channelNameArg;
|
|
|
|
});
|
2019-09-10 08:51:18 +00:00
|
|
|
}
|
2019-09-10 08:50:55 +00:00
|
|
|
|
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
|
2019-09-01 14:54:36 +00:00
|
|
|
if (!this.options.externalServer) {
|
|
|
|
this.smartexpressServer = new plugins.smartexpress.Server({
|
|
|
|
cors: true,
|
|
|
|
defaultAnswer: async () => {
|
|
|
|
return `smartuniverse server ${this.getUniverseVersion()}`;
|
|
|
|
},
|
|
|
|
forceSsl: false,
|
|
|
|
port: portArg
|
|
|
|
});
|
|
|
|
} else {
|
2019-09-01 16:22:44 +00:00
|
|
|
console.log('Universe is using externally supplied server');
|
2019-09-01 14:54:36 +00:00
|
|
|
this.smartexpressServer = this.options.externalServer;
|
|
|
|
}
|
2018-03-13 05:15:40 +00:00
|
|
|
|
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
|
2018-05-30 14:34:06 +00:00
|
|
|
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
|
2018-05-30 14:34:06 +00:00
|
|
|
});
|
|
|
|
|
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]);
|
|
|
|
|
2019-09-10 08:55:10 +00:00
|
|
|
const socketFunctionSubscription = new plugins.smartsocket.SocketFunction<
|
|
|
|
interfaces.ISocketRequest_SubscribeChannel
|
|
|
|
>({
|
2019-06-06 21:23:37 +00:00
|
|
|
allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level
|
2019-09-09 22:29:08 +00:00
|
|
|
funcName: 'subscribeChannel',
|
2019-09-10 08:55:10 +00:00
|
|
|
funcDef: async (dataArg, socketConnectionArg) => {
|
2019-09-09 22:29:08 +00:00
|
|
|
const universeConnection = new UniverseConnection({
|
|
|
|
socketConnection: socketConnectionArg,
|
|
|
|
authenticationRequests: [dataArg]
|
|
|
|
});
|
|
|
|
await UniverseConnection.addConnectionToCache(this, universeConnection);
|
|
|
|
return {
|
|
|
|
subscriptionStatus: 'subscribed'
|
|
|
|
};
|
2019-08-12 12:59:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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) => {
|
2019-09-09 22:29:08 +00:00
|
|
|
const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection(
|
|
|
|
this.universeCache,
|
|
|
|
socketConnectionArg
|
|
|
|
);
|
|
|
|
if (universeConnection) {
|
|
|
|
plugins.smartlog.defaultLogger.log(
|
|
|
|
'ok',
|
|
|
|
'found UniverseConnection for socket for incoming message'
|
2019-09-01 15:04:25 +00:00
|
|
|
);
|
2019-09-09 22:29:08 +00:00
|
|
|
} else {
|
|
|
|
plugins.smartlog.defaultLogger.log(
|
|
|
|
'warn',
|
|
|
|
'found no Authorized channel for incoming message'
|
2019-08-12 12:59:37 +00:00
|
|
|
);
|
2019-09-09 22:29:08 +00:00
|
|
|
return {
|
|
|
|
error: 'You need to authenticate for a channel'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(
|
|
|
|
socketConnectionArg,
|
|
|
|
dataArg
|
|
|
|
);
|
|
|
|
const foundChannel = await UniverseChannel.authorizeAMessageForAChannel(
|
|
|
|
this.universeCache,
|
|
|
|
unauthenticatedMessage
|
|
|
|
);
|
|
|
|
if (foundChannel && unauthenticatedMessage.authenticated) {
|
|
|
|
const authenticatedMessage = unauthenticatedMessage;
|
|
|
|
await this.universeCache.addMessage(authenticatedMessage);
|
|
|
|
}
|
2019-04-30 17:16:03 +00:00
|
|
|
}
|
2018-05-30 14:34:06 +00:00
|
|
|
});
|
|
|
|
|
2019-08-13 11:04:49 +00:00
|
|
|
// add socket functions
|
|
|
|
this.smartsocket.addSocketFunction(socketFunctionSubscription);
|
|
|
|
this.smartsocket.addSocketFunction(socketFunctionProcessMessage);
|
|
|
|
|
2019-09-01 19:27:45 +00:00
|
|
|
// start the server
|
2019-09-01 14:54:36 +00:00
|
|
|
if (!this.options.externalServer) {
|
|
|
|
await this.smartexpressServer.start();
|
|
|
|
}
|
2019-09-01 19:27:45 +00:00
|
|
|
|
|
|
|
// add smartsocket to the running smartexpress app
|
|
|
|
await this.smartsocket.setExternalServer('smartexpress', this.smartexpressServer);
|
2019-04-24 16:20:31 +00:00
|
|
|
await this.smartsocket.start();
|
2019-08-13 13:55:01 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('success', '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() {
|
2018-03-27 22:38:32 +00:00
|
|
|
await this.smartsocket.stop();
|
2019-09-01 14:54:36 +00:00
|
|
|
if (!this.options.externalServer) {
|
|
|
|
await this.smartexpressServer.stop();
|
|
|
|
}
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
}
|