smartuniverse/ts/smartuniverse.classes.universechannel.ts

166 lines
5.3 KiB
TypeScript
Raw Permalink Normal View History

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
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';
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 {
// ======
// 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
*/
public static createChannel(
2019-08-13 13:48:20 +00:00
universeArg: Universe,
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-23 21:50:45 +00:00
/**
* returns boolean wether certain channel exists
*/
public static async doesChannelExists(universeCacheArg: UniverseCache, channelNameArg: string) {
const channel = universeCacheArg.channelMap.find(channelArg => {
return channelArg.name === channelNameArg;
});
if (channel) {
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
*/
public static authorizeAMessageForAChannel(
universeCacheArg: UniverseCache,
universeMessageArg: UniverseMessage
2019-08-12 13:10:40 +00:00
): UniverseChannel {
const foundChannel = universeCacheArg.channelMap.find(universeChannel => {
const result = universeChannel.authenticate(universeMessageArg);
return result;
2018-05-23 22:14:57 +00:00
});
if (foundChannel) {
universeMessageArg.authenticated = true;
universeMessageArg.universeChannelList.add(foundChannel);
2019-08-13 13:55:01 +00:00
plugins.smartlog.defaultLogger.log('ok', 'message authorized');
2018-05-23 22:14:57 +00:00
return foundChannel;
} else {
universeMessageArg.authenticated = false;
universeMessageArg.universeChannelList.add(universeCacheArg.blackListChannel);
2019-08-13 13:55:01 +00:00
plugins.smartlog.defaultLogger.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-23 22:14:57 +00:00
2019-08-13 13:48:20 +00:00
public static getUniverseChannelByName(universeRef: Universe, universeChannelName: string) {
return universeRef.universeCache.channelMap.find(channelArg => {
return channelArg.name === universeChannelName;
});
}
// ========
// 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 07:56:32 +00:00
private subject = new plugins.smartrx.rxjs.Subject<UniverseMessage>();
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
*/
public authenticate(universeMessageArg: UniverseMessage): boolean {
return (
2019-04-22 21:11:51 +00:00
this.name === universeMessageArg.targetChannelName &&
this.passphrase === universeMessageArg.passphrase
);
2018-05-19 22:41:59 +00:00
}
2019-08-13 13:48:20 +00:00
/**
* pushes a message to clients
* @param messageArg
*/
2019-09-10 07:56:32 +00:00
public async push(messageArg: UniverseMessage) {
this.subject.next(messageArg);
2019-08-13 13:48:20 +00:00
const universeConnectionsWithChannelAccess: UniverseConnection[] = [];
this.universeRef.universeCache.connectionMap.forEach(async socketConnection => {
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,
payload: messageArg.payload,
payloadStringType: messageArg.payloadStringType
};
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
public async subscribe(observer: plugins.smartrx.rxjs.Observer<any>) {
return this.subject.subscribe(observer);
}
/**
* sends a message to the channel
*/
public async sendMessage(messageDescriptor: interfaces.IMessageCreator) {
const messageToSend = new UniverseMessage({
id: plugins.smartunique.shortId(),
messageText: messageDescriptor.messageText,
payload: messageDescriptor.payload,
payloadStringType: messageDescriptor.payloadStringType,
targetChannelName: this.name,
passphrase: this.passphrase,
timestamp: Date.now()
});
this.push(messageToSend);
}
2018-04-13 13:45:48 +00:00
}