Files
smartuniverse/ts/smartuniverse.classes.universe.ts
T

152 lines
4.6 KiB
TypeScript
Raw Permalink Normal View History

2023-07-25 11:33:13 +02:00
import * as plugins from './smartuniverse.plugins.js';
2018-03-13 06:15:40 +01:00
2023-07-25 11:33:13 +02:00
import { UniverseCache, UniverseChannel, UniverseMessage } from './index.js';
2018-04-13 15:45:48 +02:00
2023-07-25 11:33:13 +02:00
import * as interfaces from './interfaces/index.js';
import { UniverseConnection } from './smartuniverse.classes.universeconnection.js';
import { logger } from './smartuniverse.logging.js';
2019-04-11 17:52:01 +02:00
2018-03-13 06:15:40 +01:00
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
}
2018-05-24 00:14:57 +02:00
/**
2019-08-12 14:59:37 +02:00
* main class that setups a Universe
2018-05-24 00:14:57 +02:00
*/
2018-03-13 06:15:40 +01:00
export class Universe {
// subinstances
public universeCache: UniverseCache;
2018-03-13 06:15:40 +01:00
// options
private options: ISmartUniverseConstructorOptions;
2018-03-20 08:16:54 +01:00
2019-04-30 19:16:03 +02:00
/**
* the smartsocket used
*/
private smartsocket?: plugins.smartsocket.Smartsocket;
2019-04-30 19:16:03 +02:00
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
2019-08-13 15:48:20 +02:00
this.universeCache = new UniverseCache(this, this.options.messageExpiryInMilliseconds);
2019-04-30 19:16:03 +02:00
}
2019-04-11 17:52:01 +02: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.
*/
private universeVersionStore = '';
2019-04-11 17:52:01 +02:00
/**
* get the currently running version of smartuniverse
*/
2020-09-30 00:50:43 +00:00
/* public getUniverseVersion() {
2018-03-13 06:15:40 +01:00
if (this.universeVersionStore) {
return this.universeVersionStore;
} else {
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
this.universeVersionStore = packageJson.version;
return this.universeVersionStore;
}
2020-09-30 00:50:43 +00:00
} */
2018-03-13 06:15:40 +01:00
2018-05-28 12:07:25 +02:00
/**
* adds a channel to the Universe
*/
2019-08-13 18:43:33 +02:00
public addChannel(nameArg: string, passphraseArg: string) {
2019-08-13 15:48:20 +02:00
const newChannel = UniverseChannel.createChannel(this, nameArg, passphraseArg);
2019-09-10 10:50:55 +02:00
return newChannel;
2018-05-28 12:07:25 +02:00
}
2019-09-10 10:50:55 +02:00
/**
* returns a channel
*/
2019-09-17 14:01:24 +02:00
public getChannel(channelNameArg: string) {
2023-07-25 11:33:13 +02:00
return this.universeCache.channelMap.findSync((channelArg) => {
2019-09-10 10:50:55 +02:00
return channelArg.name === channelNameArg;
});
2019-09-10 10:51:18 +02:00
}
2019-09-10 10:50:55 +02:00
2018-03-13 06:15:40 +01:00
/**
* initiates a server
*/
2020-09-30 17:39:29 +00:00
public async start(portArg?: number) {
if (!portArg) {
throw new Error(`You need to specify a portArg to start on.`);
2020-09-30 17:39:29 +00:00
}
2020-09-30 00:13:14 +00:00
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({
2023-07-25 11:33:13 +02:00
alias: 'smartuniverse',
port: portArg,
2020-09-30 00:13:14 +00:00
});
2023-07-25 11:33:13 +02:00
const socketFunctionSubscription =
new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_SubscribeChannel>({
funcName: 'subscribeChannel',
funcDef: async (dataArg, socketConnectionArg) => {
const universeConnection = new UniverseConnection({
universe: this,
socketConnection: socketConnectionArg,
authenticationRequests: [dataArg],
});
await UniverseConnection.addConnectionToCache(this, universeConnection);
return {
subscriptionStatus: 'subscribed',
};
},
});
2019-08-12 14:59:37 +02:00
const socketFunctionProcessMessage =
new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_ProcessMessage>({
funcName: 'processMessage',
funcDef: async (messageDataArg: interfaces.IUniverseMessage, socketConnectionArg) => {
const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection(
this.universeCache,
socketConnectionArg
);
if (universeConnection) {
logger.log('ok', 'found UniverseConnection for socket for incoming message');
} else {
logger.log('warn', 'found no Authorized channel for incoming message');
return {
messageStatus: 'authentication required',
};
}
const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(
socketConnectionArg,
messageDataArg
);
const foundChannel = await UniverseChannel.authorizeAMessageForAChannel(
this.universeCache,
unauthenticatedMessage
);
if (foundChannel && unauthenticatedMessage.authenticated) {
const authenticatedMessage = unauthenticatedMessage;
await this.universeCache.addMessage(authenticatedMessage);
return {
messageStatus: 'ok',
};
}
2019-09-10 00:29:08 +02:00
return {
messageStatus: 'channel not found',
2019-09-10 00:29:08 +02:00
};
},
});
2019-08-13 13:04:49 +02:00
// add socket functions
this.smartsocket.addSocketFunction(socketFunctionSubscription);
this.smartsocket.addSocketFunction(socketFunctionProcessMessage);
2019-09-01 21:27:45 +02:00
// add smartsocket to the running smartexpress app
2019-04-24 18:20:31 +02:00
await this.smartsocket.start();
2020-09-24 18:13:48 +00:00
logger.log('success', 'started universe');
2018-03-13 06:15:40 +01:00
}
2019-04-11 17:52:01 +02:00
/**
* stop everything
*/
2018-03-20 08:16:54 +01:00
public async stopServer() {
await this.smartsocket?.stop();
2018-03-13 06:15:40 +01:00
}
}