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