2018-03-13 06:15:40 +01:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
2019-04-11 18:50:43 +02:00
|
|
|
import * as interfaces from './interfaces';
|
2018-03-13 06:15:40 +01:00
|
|
|
|
2019-01-31 02:52:18 +01:00
|
|
|
import { Objectmap } from '@pushrocks/lik';
|
2018-05-26 13:44:32 +02:00
|
|
|
|
2019-01-31 02:52:18 +01:00
|
|
|
import { Timer, TimeStamp } from '@pushrocks/smarttime';
|
2018-05-24 00:14:57 +02:00
|
|
|
import { Universe } from './smartuniverse.classes.universe';
|
2018-05-26 13:44:32 +02:00
|
|
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
|
|
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
2019-04-22 09:58:36 +02:00
|
|
|
import { IUniverseMessage } from './interfaces';
|
2018-03-13 06:15:40 +01:00
|
|
|
|
2018-04-13 15:45:48 +02:00
|
|
|
/**
|
|
|
|
* represents a message within a universe
|
2018-05-26 13:44:32 +02:00
|
|
|
* acts as a container to save message states like authentication status
|
2018-04-13 15:45:48 +02:00
|
|
|
*/
|
2019-04-11 18:50:43 +02:00
|
|
|
export class UniverseMessage implements interfaces.IUniverseMessage {
|
2019-08-12 14:59:37 +02:00
|
|
|
public static createMessageFromPayload(dataArg: interfaces.IUniverseMessage) {
|
|
|
|
return new UniverseMessage(dataArg);
|
|
|
|
}
|
|
|
|
|
2019-04-22 09:58:36 +02:00
|
|
|
public id: string;
|
|
|
|
|
|
|
|
public timestamp: number;
|
|
|
|
public smartTimestamp: TimeStamp;
|
|
|
|
|
|
|
|
public messageText: string;
|
|
|
|
public passphrase: string;
|
|
|
|
public payload: any;
|
|
|
|
public payloadStringType;
|
|
|
|
public targetChannelName: string;
|
2018-05-07 18:50:07 +02:00
|
|
|
|
|
|
|
/**
|
2018-05-26 13:44:32 +02:00
|
|
|
* the UniverseCache the message is attached to
|
2018-05-07 18:50:07 +02:00
|
|
|
*/
|
2018-05-26 13:44:32 +02:00
|
|
|
public universeCache: UniverseCache;
|
|
|
|
|
2018-05-20 00:41:59 +02:00
|
|
|
/**
|
|
|
|
* enables unprotected grouping of messages for efficiency purposes.
|
|
|
|
*/
|
2018-05-26 13:44:32 +02:00
|
|
|
public universeChannelList = new Objectmap<UniverseChannel>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wether the message is authenticated
|
|
|
|
*/
|
|
|
|
public authenticated: boolean = null;
|
2019-04-23 00:28:57 +02:00
|
|
|
|
2018-05-20 00:41:59 +02:00
|
|
|
/**
|
2019-04-22 09:58:36 +02:00
|
|
|
* a destruction timer for this message
|
2018-05-20 00:41:59 +02:00
|
|
|
*/
|
2018-04-29 14:17:26 +02:00
|
|
|
public destructionTimer: Timer; // a timer to take care of message destruction
|
2018-03-13 06:15:40 +01:00
|
|
|
|
2018-04-29 14:17:26 +02:00
|
|
|
/**
|
|
|
|
* the constructor to create a universe message
|
2018-05-07 18:50:07 +02:00
|
|
|
* @param messageArg
|
|
|
|
* @param attachedPayloadArg
|
2018-04-29 14:17:26 +02:00
|
|
|
*/
|
2019-04-22 09:58:36 +02:00
|
|
|
constructor(messageDescriptor: IUniverseMessage) {
|
|
|
|
this.smartTimestamp = new TimeStamp(this.timestamp);
|
|
|
|
this.messageText = messageDescriptor.messageText;
|
|
|
|
this.targetChannelName = messageDescriptor.targetChannelName;
|
|
|
|
this.passphrase = messageDescriptor.passphrase;
|
|
|
|
this.payload = messageDescriptor.payload;
|
2018-05-26 13:44:32 +02:00
|
|
|
// prevent memory issues
|
2018-05-20 00:41:59 +02:00
|
|
|
this.fallBackDestruction();
|
|
|
|
}
|
|
|
|
|
2018-05-26 13:44:32 +02:00
|
|
|
public setUniverseCache(universeCacheArg: UniverseCache) {
|
|
|
|
this.universeCache = universeCacheArg;
|
2018-05-20 00:41:59 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 23:50:45 +02:00
|
|
|
public setDestructionTimer(selfdestructAfterArg: number) {
|
2018-05-20 00:41:59 +02:00
|
|
|
if (selfdestructAfterArg) {
|
|
|
|
this.destructionTimer = new Timer(selfdestructAfterArg);
|
|
|
|
this.destructionTimer.start();
|
2018-03-13 06:15:40 +01:00
|
|
|
|
2018-05-26 13:44:32 +02:00
|
|
|
// set up self destruction by removing this from the parent messageCache
|
2018-05-20 00:41:59 +02:00
|
|
|
this.destructionTimer.completed.then(async () => {
|
2018-05-28 12:07:25 +02:00
|
|
|
this.universeCache.messageMap.remove(this);
|
2018-05-20 00:41:59 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.fallBackDestruction();
|
|
|
|
}
|
2018-05-23 23:50:45 +02:00
|
|
|
}
|
2018-05-20 00:41:59 +02:00
|
|
|
|
2018-05-26 13:44:32 +02:00
|
|
|
/**
|
|
|
|
* handles bad messages for further analysis
|
|
|
|
*/
|
2019-04-22 09:58:36 +02:00
|
|
|
public handleAsBadMessage() {
|
2018-05-26 13:44:32 +02:00
|
|
|
console.log('received a bad message');
|
|
|
|
}
|
|
|
|
|
2018-05-20 00:41:59 +02:00
|
|
|
/**
|
2018-05-23 23:50:45 +02:00
|
|
|
* prevents memory leaks if channels have no default
|
2018-05-20 00:41:59 +02:00
|
|
|
*/
|
2018-05-23 23:50:45 +02:00
|
|
|
private fallBackDestruction() {
|
2018-05-20 00:41:59 +02:00
|
|
|
plugins.smartdelay.delayFor(1000).then(() => {
|
|
|
|
if (!this.destructionTimer) {
|
|
|
|
this.setDestructionTimer(6000);
|
|
|
|
}
|
2018-03-20 08:16:54 +01:00
|
|
|
});
|
2018-03-13 06:15:40 +01:00
|
|
|
}
|
|
|
|
}
|