fix(core): improve channel handling

This commit is contained in:
Philipp Kunz 2018-05-24 00:14:57 +02:00
parent d033780015
commit 7991baf2bf
5 changed files with 2527 additions and 8 deletions

2491
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,9 @@
},
"devDependencies": {
"@types/node": "^9.6.0",
"tapbundle": "^2.0.0"
"tapbundle": "^2.0.0",
"ts-node": "^6.0.3",
"typescript": "^2.8.3"
},
"dependencies": {
"lik": "^2.0.5",

View File

@ -17,6 +17,9 @@ export interface IServerGetMessagesRequestBody {
youngerThan: number;
}
/**
* the interface for a standard request
*/
export interface IServerPutMessageRequestBody {
channel: string;
passphrase: string;
@ -24,6 +27,9 @@ export interface IServerPutMessageRequestBody {
payload: any;
}
/**
* main class that setsup a Universe
*/
export class Universe {
// subinstances
public universeStore: UniverseStore;
@ -66,7 +72,7 @@ export class Universe {
// adds messages
const addMessageHandler = new Handler('PUT', request => {
const requestBody: IServerPutMessageRequestBody = request.body;
const message = new UniverseMessage(requestBody.message, requestBody.payload);
const message = new UniverseMessage(requestBody.message, requestBody.channel, requestBody.passphrase, requestBody.payload);
this.universeStore.addMessage(message);
console.log(requestBody);
return true;

View File

@ -9,18 +9,35 @@ export class UniverseChannel {
/**
* stores the channels that are available within the universe
*/
public static channelStore = new Objectmap();
public static channelStore = new Objectmap<UniverseChannel>();
/**
* allows messages to be processed in a blacklist mode for further analysis
*/
public static blackListChannel = new UniverseChannel('blacklist', 'nada');
/**
* creates new channels
* @param channelArg the name of the topic
* @param passphraseArg the secret thats used for a certain topic.
*/
public static createChannel = (channelNameArg: string, passphraseArg: string) => {
public static createChannel (channelNameArg: string, passphraseArg: string) {
const newChannel = new UniverseChannel(channelNameArg, passphraseArg);
return newChannel;
};
public static authorizeForChannel (channelNameArg: string, passphraseArg: string) {
const foundChannel = this.channelStore.find(universeChannel => {
const result = universeChannel.authenticate(channelNameArg, passphraseArg);
return result;
});
if(foundChannel) {
return foundChannel;
} else {
return this.blackListChannel;
}
};
/**
* the name of the channel
*/
@ -40,7 +57,7 @@ export class UniverseChannel {
/**
* authenticates a client on the server side
*/
public async authenticateClient(passphraseArg: string): boolean {
return passphraseArg === this.passphrase;
public authenticate(channelNameArg: string, passphraseArg: string): boolean {
return (this.name === channelNameArg && this.passphrase === passphraseArg);
}
}

View File

@ -1,7 +1,9 @@
import * as plugins from './smartuniverse.plugins';
import { Timer, TimeStamp } from 'smarttime';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { UniverseStore } from './smartuniverse.classes.universestore';
import { Universe } from './smartuniverse.classes.universe';
/**
* represents a message within a universe
@ -22,7 +24,7 @@ export class UniverseMessage {
/**
* enables unprotected grouping of messages for efficiency purposes.
*/
public universeChannel: string;
public universeChannel: UniverseChannel;
/**
* time of creation
@ -45,9 +47,10 @@ export class UniverseMessage {
* @param messageArg
* @param attachedPayloadArg
*/
constructor(messageArg: string, attachedPayloadArg: any) {
constructor(messageArg: string, channelNameArg: string, passphraseArg: string, attachedPayloadArg: any) {
this.timestamp = new TimeStamp();
this.message = messageArg;
this.universeChannel = UniverseChannel.authorizeForChannel(channelNameArg, passphraseArg);
this.attachedPayload = attachedPayloadArg;
this.fallBackDestruction();
}