fix(core): update
This commit is contained in:
		| @@ -1,7 +1,15 @@ | ||||
| export interface IUniverseMessage { | ||||
| export interface IMessageCreator { | ||||
|   messageText: string; | ||||
|   targetChannelName: string; | ||||
|   passphrase: string; | ||||
|   payload?: string | number | any; | ||||
|   payloadStringType?: 'Buffer' | 'string' | 'object'; | ||||
|   targetChannelName: string; | ||||
| } | ||||
|  | ||||
| export interface IUniverseMessage extends IMessageCreator { | ||||
|   id: string; | ||||
|   /** | ||||
|    * time of creation | ||||
|    */ | ||||
|   timestamp: number; | ||||
|   passphrase: string; | ||||
| } | ||||
|   | ||||
| @@ -31,8 +31,14 @@ export class ClientUniverse { | ||||
|     this.options = optionsArg; | ||||
|   } | ||||
|  | ||||
|   public async sendMessage(messageArg: interfaces.IUniverseMessage) { | ||||
|     const requestBody: interfaces.IUniverseMessage = messageArg; | ||||
|   public async sendMessage(messageArg: interfaces.IMessageCreator) { | ||||
|     const requestBody: interfaces.IUniverseMessage = { | ||||
|       id: plugins.smartunique.shortId(), | ||||
|       timestamp: Date.now(), | ||||
|       passphrase: (await this.getChannel(messageArg.targetChannelName))., | ||||
|       ...messageArg, | ||||
|  | ||||
|     }; | ||||
|     const requestBodyString = JSON.stringify(requestBody); | ||||
|     // TODO: User websocket connection if available | ||||
|     const response = await plugins.smartrequest.postJson(`${this.options.serverAddress}/sendmessage` , { | ||||
| @@ -40,7 +46,7 @@ export class ClientUniverse { | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   public async getChannel(channelName: string, passphrase): Promise<ClientUniverseChannel> { | ||||
|   public async getChannel(channelName: string, passphraseArg?: string): Promise<ClientUniverseChannel> { | ||||
|     await this.checkConnection(); | ||||
|     const clientUniverseChannel = await ClientUniverseChannel.createClientUniverseChannel( | ||||
|       this, | ||||
|   | ||||
| @@ -9,9 +9,10 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { | ||||
|   // ====== | ||||
|   public static async createClientUniverseChannel( | ||||
|     clientUniverseArg: ClientUniverse, | ||||
|     channelName: string | ||||
|     channelName: string, | ||||
|     passphraseArg: string | ||||
|   ): Promise<ClientUniverseChannel> { | ||||
|     const clientChannel = new ClientUniverseChannel(clientUniverseArg); | ||||
|     const clientChannel = new ClientUniverseChannel(clientUniverseArg, passphraseArg); | ||||
|     await clientChannel.transmitSubscription(); | ||||
|     return clientChannel; | ||||
|   } | ||||
| @@ -21,9 +22,11 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { | ||||
|   // ======== | ||||
|  | ||||
|   public clientUniverse: ClientUniverse; | ||||
|   public passphrase: string; | ||||
|  | ||||
|   constructor(clientUniverseArg: ClientUniverse) { | ||||
|   constructor(clientUniverseArg: ClientUniverse, passphraseArg: string) { | ||||
|     this.clientUniverse = clientUniverseArg; | ||||
|     this.passphrase = passphraseArg | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|   | ||||
| @@ -6,13 +6,24 @@ export class ClientUniverseMessage implements interfaces.IUniverseMessage { | ||||
|   // ====== | ||||
|   // STATIC | ||||
|   // ====== | ||||
|   public static createMessageFromPayload(messageArg: string, payloadArg: any) { | ||||
|   public static createMessageFromPayload(messageDescriptor: interfaces.IUniverseMessage) { | ||||
|  | ||||
|   }; | ||||
|  | ||||
|   // ======== | ||||
|   // INSTANCE | ||||
|   // ======== | ||||
|  | ||||
|   public id: string; | ||||
|  | ||||
|   public timestamp: number; | ||||
|   public smartTimestamp: plugins.smarttime.TimeStamp; | ||||
|  | ||||
|   public messageText: string; | ||||
|   public passphrase: string; | ||||
|   public payload: any; | ||||
|   public payloadStringType; | ||||
|   public targetChannelName: string; | ||||
|   constructor(messageArg, payloadArg) {} | ||||
|  | ||||
|   getAsJsonForPayload () { | ||||
|   | ||||
| @@ -80,7 +80,8 @@ export class Universe { | ||||
|  | ||||
|     // lets create the http request route | ||||
|     this.smartexpressServer.addRoute('/sendmessage', new Handler('POST', async (req, res) => { | ||||
|       this.universeCache.addMessage(req.body); | ||||
|       const universeMessageInstance = new UniverseMessage(req.body); | ||||
|       this.universeCache.addMessage(universeMessageInstance); | ||||
|     })); | ||||
|  | ||||
|     // add websocket upgrade | ||||
|   | ||||
| @@ -55,7 +55,7 @@ export class UniverseCache { | ||||
|   public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> { | ||||
|     const messageObservable = from(this.messageMap.getArray()).pipe( | ||||
|       filter(messageArg => { | ||||
|         return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime); | ||||
|         return messageArg.smartTimestamp.isYoungerThanMilliSeconds(this.destructionTime); | ||||
|       }) | ||||
|     ); | ||||
|     return messageObservable; | ||||
|   | ||||
| @@ -7,34 +7,30 @@ import { Timer, TimeStamp } from '@pushrocks/smarttime'; | ||||
| import { Universe } from './smartuniverse.classes.universe'; | ||||
| import { UniverseChannel } from './smartuniverse.classes.universechannel'; | ||||
| import { UniverseCache } from './smartuniverse.classes.universecache'; | ||||
| import { IUniverseMessage } from './interfaces'; | ||||
|  | ||||
| /** | ||||
|  * represents a message within a universe | ||||
|  * acts as a container to save message states like authentication status | ||||
|  */ | ||||
| export class UniverseMessage implements interfaces.IUniverseMessage { | ||||
|   /** | ||||
|    * public and unique id | ||||
|    * numeric ascending | ||||
|    * adheres to time in milliseconds | ||||
|    *   -> meaning it describes the time of arrival | ||||
|    *   -> two messages received at the same time will count up the second one | ||||
|    *   -> avoids duplications of messages | ||||
|    *   -> may be changed to nanoseconds to ensure higher throughput | ||||
|    */ | ||||
|   public id: number; | ||||
|    | ||||
|   public id: string; | ||||
|  | ||||
|   public timestamp: number; | ||||
|   public smartTimestamp: TimeStamp; | ||||
|  | ||||
|   public messageText: string; | ||||
|   public passphrase: string; | ||||
|   public payload: any; | ||||
|   public payloadStringType; | ||||
|   public targetChannelName: string; | ||||
|  | ||||
|   /** | ||||
|    * the UniverseCache the message is attached to | ||||
|    */ | ||||
|   public universeCache: UniverseCache; | ||||
|  | ||||
|   /** | ||||
|    * requestedChannelName | ||||
|    */ | ||||
|   public requestedChannelName: string; | ||||
|   public requestedChannelPassphrase: string; | ||||
|  | ||||
|   /** | ||||
|    * enables unprotected grouping of messages for efficiency purposes. | ||||
|    */ | ||||
| @@ -44,21 +40,10 @@ export class UniverseMessage implements interfaces.IUniverseMessage { | ||||
|    * wether the message is authenticated | ||||
|    */ | ||||
|   public authenticated: boolean = null; | ||||
|  | ||||
|    | ||||
|   /** | ||||
|    * time of creation | ||||
|    * a destruction timer for this message | ||||
|    */ | ||||
|   public timestamp: TimeStamp; | ||||
|  | ||||
|   /** | ||||
|    * the actual message | ||||
|    */ | ||||
|   public message: string; | ||||
|  | ||||
|   /** | ||||
|    * any attached payloads. Can be of binary format. | ||||
|    */ | ||||
|   public attachedPayload: any; | ||||
|   public destructionTimer: Timer; // a timer to take care of message destruction | ||||
|  | ||||
|   /** | ||||
| @@ -66,17 +51,12 @@ export class UniverseMessage implements interfaces.IUniverseMessage { | ||||
|    * @param messageArg | ||||
|    * @param attachedPayloadArg | ||||
|    */ | ||||
|   constructor( | ||||
|     messageArg: string, | ||||
|     requestedChannelNameArg: string, | ||||
|     passphraseArg: string, | ||||
|     attachedPayloadArg: any | ||||
|   ) { | ||||
|     this.timestamp = new TimeStamp(); | ||||
|     this.message = messageArg; | ||||
|     this.requestedChannelName = requestedChannelNameArg; | ||||
|     this.requestedChannelPassphrase = passphraseArg; | ||||
|     this.attachedPayload = attachedPayloadArg; | ||||
|   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; | ||||
|     // prevent memory issues | ||||
|     this.fallBackDestruction(); | ||||
|   } | ||||
| @@ -102,7 +82,7 @@ export class UniverseMessage implements interfaces.IUniverseMessage { | ||||
|   /** | ||||
|    * handles bad messages for further analysis | ||||
|    */ | ||||
|   handleAsBadMessage() { | ||||
|   public handleAsBadMessage() { | ||||
|     console.log('received a bad message'); | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -14,6 +14,7 @@ import * as smartrequest from '@pushrocks/smartrequest'; | ||||
| import * as smartrx from '@pushrocks/smartrx'; | ||||
| import * as smartsocket from '@pushrocks/smartsocket'; | ||||
| import * as smarttime from '@pushrocks/smarttime'; | ||||
| import * as smartunique from '@pushrocks/smartunique'; | ||||
|  | ||||
| export { | ||||
|   lik, | ||||
| @@ -25,5 +26,6 @@ export { | ||||
|   smartrx, | ||||
|   smartrequest, | ||||
|   smartsocket, | ||||
|   smarttime | ||||
|   smarttime, | ||||
|   smartunique | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user