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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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
|
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]);
|
|
|
|
|
|
|
|
const SubscriptionSocketFunction = 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
|
2018-05-30 14:34:06 +00:00
|
|
|
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'
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const ProcessMessageSocketFunction = new plugins.smartsocket.SocketFunction({
|
|
|
|
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 {
|
|
|
|
console.log('universe client not yet present');
|
|
|
|
console.log('creating one now as send only');
|
|
|
|
const universeConnectionInstance = new UniverseConnection({
|
|
|
|
socketConnection: socketConnectionArg,
|
|
|
|
authenticationRequests: []
|
|
|
|
});
|
|
|
|
await UniverseConnection.addConnectionToCache(
|
|
|
|
this.universeCache,
|
|
|
|
universeConnectionInstance
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await UniverseChannel.authorizeAMessageForAChannel(
|
|
|
|
this.universeCache,
|
|
|
|
UniverseMessage.createMessageFromPayload(dataArg)
|
|
|
|
);
|
2019-06-06 20:22:45 +00:00
|
|
|
})();
|
2019-04-30 17:16:03 +00:00
|
|
|
}
|
2018-05-30 14:34:06 +00:00
|
|
|
});
|
|
|
|
|
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() {
|
2018-03-27 22:38:32 +00:00
|
|
|
await this.smartsocket.stop();
|
2018-03-13 05:15:40 +00:00
|
|
|
await this.smartexpressServer.stop();
|
|
|
|
}
|
|
|
|
}
|