2018-04-13 13:45:48 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
2019-08-13 13:48:20 +00:00
|
|
|
import * as interfaces from './interfaces';
|
2018-04-13 13:45:48 +00:00
|
|
|
|
2018-05-26 11:44:32 +00:00
|
|
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
|
|
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
2019-08-13 13:48:20 +00:00
|
|
|
import { UniverseConnection } from './smartuniverse.classes.universeconnection';
|
|
|
|
import { Universe } from './smartuniverse.classes.universe';
|
2020-09-24 18:13:48 +00:00
|
|
|
import { logger } from './smartuniverse.logging';
|
2018-04-29 12:17:26 +00:00
|
|
|
|
2018-04-13 13:45:48 +00:00
|
|
|
/**
|
2018-04-29 12:17:26 +00:00
|
|
|
* enables messages to stay within a certain scope.
|
2018-04-13 13:45:48 +00:00
|
|
|
*/
|
|
|
|
export class UniverseChannel {
|
2018-05-24 14:55:24 +00:00
|
|
|
// ======
|
|
|
|
// STATIC
|
|
|
|
// ======
|
2018-05-23 21:50:45 +00:00
|
|
|
|
2018-05-07 16:50:07 +00:00
|
|
|
/**
|
2018-05-19 22:41:59 +00:00
|
|
|
* creates new channels
|
|
|
|
* @param channelArg the name of the topic
|
|
|
|
* @param passphraseArg the secret thats used for a certain topic.
|
2018-05-07 16:50:07 +00:00
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public static createChannel(
|
2019-08-13 13:48:20 +00:00
|
|
|
universeArg: Universe,
|
2018-05-26 11:44:32 +00:00
|
|
|
channelNameArg: string,
|
|
|
|
passphraseArg: string
|
|
|
|
) {
|
2019-08-13 13:48:20 +00:00
|
|
|
const newChannel = new UniverseChannel(universeArg, channelNameArg, passphraseArg);
|
|
|
|
universeArg.universeCache.channelMap.add(newChannel);
|
2018-05-19 22:41:59 +00:00
|
|
|
return newChannel;
|
2018-05-26 11:44:32 +00:00
|
|
|
}
|
2018-05-23 21:50:45 +00:00
|
|
|
|
2018-05-24 14:55:24 +00:00
|
|
|
/**
|
|
|
|
* returns boolean wether certain channel exists
|
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public static async doesChannelExists(universeCacheArg: UniverseCache, channelNameArg: string) {
|
2020-09-24 18:17:52 +00:00
|
|
|
const channel = universeCacheArg.channelMap.find((channelArg) => {
|
2018-05-24 14:55:24 +00:00
|
|
|
return channelArg.name === channelNameArg;
|
|
|
|
});
|
2018-05-26 11:44:32 +00:00
|
|
|
if (channel) {
|
2018-05-24 14:55:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 12:59:37 +00:00
|
|
|
/**
|
|
|
|
* a static message authorization function that takes the UniverseCache
|
|
|
|
* (where messages and channels are stored and their lifetime is managed)
|
|
|
|
* and the universemessage to find a fitting channel for the message
|
|
|
|
* @param universeCacheArg
|
|
|
|
* @param universeMessageArg
|
|
|
|
*/
|
2018-05-26 11:44:32 +00:00
|
|
|
public static authorizeAMessageForAChannel(
|
|
|
|
universeCacheArg: UniverseCache,
|
2019-09-10 21:55:20 +00:00
|
|
|
universeMessageArg: UniverseMessage<any>
|
2019-08-12 13:10:40 +00:00
|
|
|
): UniverseChannel {
|
2020-09-24 18:17:52 +00:00
|
|
|
const foundChannel = universeCacheArg.channelMap.find((universeChannel) => {
|
2018-05-26 11:44:32 +00:00
|
|
|
const result = universeChannel.authenticate(universeMessageArg);
|
|
|
|
return result;
|
2018-05-23 22:14:57 +00:00
|
|
|
});
|
2018-05-26 11:44:32 +00:00
|
|
|
if (foundChannel) {
|
|
|
|
universeMessageArg.authenticated = true;
|
|
|
|
universeMessageArg.universeChannelList.add(foundChannel);
|
2020-09-24 18:13:48 +00:00
|
|
|
logger.log('ok', 'message authorized');
|
2018-05-23 22:14:57 +00:00
|
|
|
return foundChannel;
|
|
|
|
} else {
|
2018-05-26 11:44:32 +00:00
|
|
|
universeMessageArg.authenticated = false;
|
|
|
|
universeMessageArg.universeChannelList.add(universeCacheArg.blackListChannel);
|
2020-09-24 18:13:48 +00:00
|
|
|
logger.log('warn', 'message not valid');
|
2019-08-12 13:10:40 +00:00
|
|
|
return null;
|
2018-05-23 22:14:57 +00:00
|
|
|
}
|
2018-05-26 11:44:32 +00:00
|
|
|
}
|
2018-05-23 22:14:57 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
public static getUniverseChannelByName(universeRef: Universe, universeChannelName: string) {
|
2020-09-24 18:17:52 +00:00
|
|
|
return universeRef.universeCache.channelMap.find((channelArg) => {
|
2019-08-13 13:48:20 +00:00
|
|
|
return channelArg.name === universeChannelName;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-24 14:55:24 +00:00
|
|
|
// ========
|
|
|
|
// INSTANCE
|
|
|
|
// ========
|
2018-05-19 22:41:59 +00:00
|
|
|
/**
|
|
|
|
* the name of the channel
|
|
|
|
*/
|
|
|
|
public name: string;
|
2019-08-13 13:48:20 +00:00
|
|
|
public universeRef: Universe;
|
2019-09-10 21:55:20 +00:00
|
|
|
private subject = new plugins.smartrx.rxjs.Subject<UniverseMessage<any>>();
|
2018-04-29 12:17:26 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-19 22:41:59 +00:00
|
|
|
* the passphrase for the channel
|
2018-04-29 12:17:26 +00:00
|
|
|
*/
|
2018-05-19 22:41:59 +00:00
|
|
|
public passphrase: string;
|
2018-04-29 12:17:26 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
constructor(universeArg: Universe, channelNameArg: string, passphraseArg: string) {
|
|
|
|
this.universeRef = universeArg;
|
2018-05-19 22:41:59 +00:00
|
|
|
this.name = channelNameArg;
|
|
|
|
this.passphrase = passphraseArg;
|
|
|
|
}
|
2018-04-13 13:45:48 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-12 12:59:37 +00:00
|
|
|
* authenticates a client on the server side by matching
|
|
|
|
* # the messages channelName against the unverseChannel's name
|
|
|
|
* # the messages password against the universeChannel's password
|
2018-04-13 13:45:48 +00:00
|
|
|
*/
|
2019-09-10 21:55:20 +00:00
|
|
|
public authenticate(universeMessageArg: UniverseMessage<any>): boolean {
|
2018-05-26 11:44:32 +00:00
|
|
|
return (
|
2019-04-22 21:11:51 +00:00
|
|
|
this.name === universeMessageArg.targetChannelName &&
|
|
|
|
this.passphrase === universeMessageArg.passphrase
|
2018-05-26 11:44:32 +00:00
|
|
|
);
|
2018-05-19 22:41:59 +00:00
|
|
|
}
|
2018-05-28 10:07:25 +00:00
|
|
|
|
2019-08-13 13:48:20 +00:00
|
|
|
/**
|
|
|
|
* pushes a message to clients
|
|
|
|
* @param messageArg
|
|
|
|
*/
|
2019-09-10 21:55:20 +00:00
|
|
|
public async push(messageArg: UniverseMessage<any>) {
|
2019-09-10 07:56:32 +00:00
|
|
|
this.subject.next(messageArg);
|
2019-08-13 13:48:20 +00:00
|
|
|
const universeConnectionsWithChannelAccess: UniverseConnection[] = [];
|
2020-09-24 18:17:52 +00:00
|
|
|
await this.universeRef.universeCache.connectionMap.forEach(async (socketConnection) => {
|
2019-08-13 13:48:20 +00:00
|
|
|
if (socketConnection.authenticatedChannels.includes(this)) {
|
|
|
|
universeConnectionsWithChannelAccess.push(socketConnection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (const universeConnection of universeConnectionsWithChannelAccess) {
|
2019-09-01 15:04:25 +00:00
|
|
|
const smartsocket = universeConnection.socketConnection
|
|
|
|
.smartsocketRef as plugins.smartsocket.Smartsocket;
|
2019-08-13 13:48:20 +00:00
|
|
|
const universeMessageToSend: interfaces.IUniverseMessage = {
|
|
|
|
id: messageArg.id,
|
|
|
|
timestamp: messageArg.timestamp,
|
|
|
|
passphrase: messageArg.passphrase,
|
|
|
|
targetChannelName: this.name,
|
|
|
|
messageText: messageArg.messageText,
|
2020-09-24 18:17:52 +00:00
|
|
|
payload: messageArg.payload,
|
2019-08-13 13:48:20 +00:00
|
|
|
};
|
2019-09-01 15:04:25 +00:00
|
|
|
smartsocket.clientCall(
|
|
|
|
'processMessage',
|
|
|
|
universeMessageToSend,
|
|
|
|
universeConnection.socketConnection
|
|
|
|
);
|
2019-08-13 13:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 07:56:32 +00:00
|
|
|
|
|
|
|
// functions to interact with a channel locally
|
2019-09-10 21:55:20 +00:00
|
|
|
public subscribe(observingFunctionArg: (messageArg: UniverseMessage<any>) => void) {
|
2019-09-10 17:36:10 +00:00
|
|
|
return this.subject.subscribe(
|
2020-09-24 18:17:52 +00:00
|
|
|
(messageArg) => {
|
2019-09-10 17:36:10 +00:00
|
|
|
observingFunctionArg(messageArg);
|
|
|
|
},
|
2020-09-24 18:17:52 +00:00
|
|
|
(error) => console.log(error)
|
2019-09-10 17:36:10 +00:00
|
|
|
);
|
2019-09-10 07:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sends a message to the channel
|
|
|
|
*/
|
2020-09-29 19:39:13 +00:00
|
|
|
public async postMessage(messageDescriptor: interfaces.IMessageCreator) {
|
2019-09-10 07:56:32 +00:00
|
|
|
const messageToSend = new UniverseMessage({
|
2020-09-30 00:50:43 +00:00
|
|
|
id: plugins.isounique.uni(),
|
2019-09-10 07:56:32 +00:00
|
|
|
messageText: messageDescriptor.messageText,
|
|
|
|
payload: messageDescriptor.payload,
|
|
|
|
targetChannelName: this.name,
|
|
|
|
passphrase: this.passphrase,
|
2020-09-24 18:17:52 +00:00
|
|
|
timestamp: Date.now(),
|
2019-09-10 07:56:32 +00:00
|
|
|
});
|
2019-09-17 13:40:54 +00:00
|
|
|
this.universeRef.universeCache.addMessage(messageToSend);
|
2019-09-10 07:56:32 +00:00
|
|
|
}
|
2018-04-13 13:45:48 +00:00
|
|
|
}
|