Files
smartuniverse/ts/smartuniverse.classes.clientuniverse.ts

155 lines
4.9 KiB
TypeScript
Raw Normal View History

2018-03-13 06:15:40 +01:00
import * as plugins from './smartuniverse.plugins';
2019-01-31 02:52:18 +01:00
import { Objectmap } from '@pushrocks/lik';
2018-03-13 06:15:40 +01:00
import { Observable } from 'rxjs';
2019-01-31 02:52:18 +01:00
import { Smartsocket, SmartsocketClient } from '@pushrocks/smartsocket';
2018-03-20 08:16:54 +01:00
import * as url from 'url';
2019-04-11 17:52:01 +02:00
import * as interfaces from './interfaces';
2019-08-13 18:06:13 +02:00
import { ClientUniverseChannel, ClientUniverseMessage } from './';
2019-06-06 23:23:37 +02:00
import { ClientUniverseCache } from './smartuniverse.classes.clientuniversecache';
2018-03-13 06:15:40 +01:00
2018-03-15 01:05:13 +01:00
export interface IClientOptions {
2018-03-20 08:16:54 +01:00
serverAddress: string;
2018-03-15 01:05:13 +01:00
}
2018-04-13 15:45:48 +02:00
/**
* this class is for client side only!!!
* allows connecting to a universe server
*/
export class ClientUniverse {
2018-03-15 01:05:13 +01:00
public options;
2019-04-24 23:27:57 +02:00
public smartsocketClient: plugins.smartsocket.SmartsocketClient;
2019-09-10 23:55:20 +02:00
public observableIntake: plugins.smartrx.ObservableIntake<ClientUniverseMessage<any>>;
2019-04-28 12:42:08 +02:00
public clientUniverseCache = new ClientUniverseCache();
2018-03-13 06:15:40 +01:00
2018-03-15 01:05:13 +01:00
constructor(optionsArg: IClientOptions) {
this.options = optionsArg;
}
2018-03-20 08:16:54 +01:00
2019-04-22 13:06:01 +02:00
/**
* adds a channel to the channelcache
* TODO: verify channel before adding it to the channel cache
*/
2019-08-13 18:41:27 +02:00
public addChannel(channelNameArg: string, passphraseArg: string) {
const existingChannel = this.getChannel(channelNameArg);
2019-04-22 22:04:52 +02:00
if (existingChannel) {
throw new Error('channel exists');
}
2019-04-22 23:11:51 +02:00
// lets create the channel
2019-09-01 17:04:25 +02:00
const clientUniverseChannel = ClientUniverseChannel.createClientUniverseChannel(
this,
channelNameArg,
passphraseArg
);
2019-08-13 18:41:27 +02:00
return clientUniverseChannel;
2019-04-22 13:06:01 +02:00
}
/**
* gets a channel from the channelcache
* @param channelName
2019-04-23 00:28:57 +02:00
* @param passphraseArg
2019-04-22 13:06:01 +02:00
*/
2019-08-13 18:41:27 +02:00
public getChannel(channelName: string): ClientUniverseChannel {
2019-08-13 15:48:20 +02:00
const clientUniverseChannel = this.clientUniverseCache.channelMap.find(channel => {
2019-04-22 22:04:52 +02:00
return channel.name === channelName;
2019-04-23 00:28:57 +02:00
});
2019-04-22 13:06:01 +02:00
return clientUniverseChannel;
}
2019-04-28 12:42:08 +02:00
/**
* remove a a achannel
* @param messageArg
*/
public removeChannel(channelNameArg, notifyServer = true) {
2019-09-01 17:04:25 +02:00
const clientUniverseChannel = this.clientUniverseCache.channelMap.findOneAndRemove(
channelItemArg => {
return channelItemArg.name === channelNameArg;
}
);
2019-04-28 12:42:08 +02:00
}
2019-08-13 15:48:20 +02:00
public async start() {
await this.checkConnection();
}
2019-09-10 00:39:18 +02:00
public async stop() {
await this.smartsocketClient.disconnect();
}
2019-04-22 13:06:01 +02:00
/**
* checks the connection towards a universe server
* since password validation is done through other means, a connection should always be possible
*/
2019-08-12 17:23:10 +02:00
public async checkConnection(): Promise<void> {
2019-04-24 23:27:57 +02:00
if (!this.smartsocketClient && !this.observableIntake) {
2018-03-20 08:16:54 +01:00
const parsedURL = url.parse(this.options.serverAddress);
2019-04-24 18:20:31 +02:00
const socketConfig: plugins.smartsocket.ISmartsocketClientOptions = {
2019-09-01 21:34:01 +02:00
alias: 'universeclient',
2018-03-20 08:16:54 +01:00
password: 'UniverseClient',
port: parseInt(parsedURL.port, 10),
role: 'UniverseClient',
2019-04-24 18:20:31 +02:00
url: parsedURL.protocol + '//' + parsedURL.hostname
};
2019-04-24 23:27:57 +02:00
this.smartsocketClient = new SmartsocketClient(socketConfig);
2018-03-20 08:16:54 +01:00
this.observableIntake = new plugins.smartrx.ObservableIntake();
2019-04-24 23:27:57 +02:00
// lets define some basic actions
/**
* should handle a forced unsubscription by the server
*/
2019-08-13 15:48:20 +02:00
const socketFunctionUnsubscribe = new plugins.smartsocket.SocketFunction({
2019-04-24 23:27:57 +02:00
funcName: 'unsubscribe',
allowedRoles: [],
2019-08-12 14:59:37 +02:00
funcDef: async (data: interfaces.IServerUnsubscribeActionPayload) => {
throw new Error('TODO');
}
2019-04-24 23:27:57 +02:00
});
/**
2019-08-13 15:48:20 +02:00
* handles message reception
2019-04-24 23:27:57 +02:00
*/
2019-09-10 01:19:10 +02:00
const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction<
interfaces.ISocketRequest_ProcessMessage
>({
2019-08-12 14:59:37 +02:00
funcName: 'processMessage',
allowedRoles: [],
2019-09-10 01:19:10 +02:00
funcDef: async messageDescriptorArg => {
2019-08-13 15:55:01 +02:00
plugins.smartlog.defaultLogger.log('info', 'Got message from server');
2019-09-10 01:19:10 +02:00
const clientUniverseMessage = ClientUniverseMessage.createMessageFromMessageDescriptor(
messageDescriptorArg
2019-09-01 17:04:25 +02:00
);
2019-09-10 01:19:10 +02:00
this.observableIntake.push(clientUniverseMessage);
// lets find the corresponding channel
const targetChannel = this.getChannel(clientUniverseMessage.targetChannelName);
if (targetChannel) {
await targetChannel.emitMessageLocally(clientUniverseMessage);
return {
messageStatus: 'ok'
};
} else {
return {
messageStatus: 'channel not found'
};
}
2019-08-12 14:59:37 +02:00
}
});
2019-04-24 23:27:57 +02:00
2019-08-13 15:48:20 +02:00
// add functions
this.smartsocketClient.addSocketFunction(socketFunctionUnsubscribe);
this.smartsocketClient.addSocketFunction(socketFunctionProcessMessage);
2019-04-24 23:27:57 +02:00
await this.smartsocketClient.connect();
2019-08-13 15:55:01 +02:00
plugins.smartlog.defaultLogger.log('info', 'universe client connected successfully');
2019-08-13 15:48:20 +02:00
await this.clientUniverseCache.channelMap.forEach(async clientUniverseChannelArg => {
2019-09-10 19:36:10 +02:00
await clientUniverseChannelArg.populateSubscriptionToServer();
2019-08-13 15:48:20 +02:00
});
2018-03-20 08:16:54 +01:00
}
}
2018-03-15 01:05:13 +01:00
}