fix(core): update

This commit is contained in:
Philipp Kunz 2019-04-11 17:52:01 +02:00
parent f512acdfaa
commit f728c56016
12 changed files with 74 additions and 33 deletions

View File

@ -7,4 +7,5 @@ export * from './smartuniverse.classes.universe';
export * from './smartuniverse.classes.universecache';
export * from './smartuniverse.classes.universechannel';
export * from './smartuniverse.classes.universemessage';
export * from './smartuniverse.interfaces';
export * from './interfaces';

View File

@ -0,0 +1,15 @@
export interface IServerGetMessagesRequestBody {
channel: string;
topic?: string;
youngerThan: number;
}
/**
* the interface for a standard request
*/
export interface IServerPutMessageRequestBody {
channel: string;
passphrase: string;
message: string;
payload: any;
}

3
ts/interfaces/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './http.interfaces';
export * from './universechannel.interfaces';
export * from './universemessage.interfaces';

View File

@ -0,0 +1 @@
export interface IUniverseChannel {}

View File

@ -0,0 +1 @@
export interface IUniverseMessage {}

View File

@ -5,10 +5,10 @@ import { Observable } from 'rxjs';
import { Smartsocket, SmartsocketClient } from '@pushrocks/smartsocket';
import * as url from 'url';
import * as interfaces from './interfaces';
import {
ClientUniverseChannel,
IServerGetMessagesRequestBody,
IServerPutMessageRequestBody,
UniverseMessage
} from './';

View File

@ -1,8 +1,9 @@
import * as plugins from './smartuniverse.plugins';
import * as interfaces from './interfaces';
import { ClientUniverse, IUniverseChannel } from './';
import { ClientUniverse } from './';
export class ClientUniverseChannel implements IUniverseChannel {
export class ClientUniverseChannel implements interfaces.IUniverseChannel {
// ======
// STATIC
// ======

View File

@ -1,15 +1,21 @@
import * as plugins from './smartuniverse.plugins';
import { IUniverseMessage } from './';
import * as interfaces from './interfaces';
export class ClientUniverseMessage implements IUniverseMessage {
export class ClientUniverseMessage implements interfaces.IUniverseMessage {
// ======
// STATIC
// ======
createMessage(messageArg: string, payloadArg: any) {}
public static createMessageFromPayload(messageArg: string, payloadArg: any) {
};
// ========
// INSTANCE
// ========
constructor(messageArg, payloadArg) {}
getAsJsonForPayload () {
}
}

View File

@ -5,25 +5,13 @@ import { UniverseCache, UniverseChannel, UniverseMessage } from './';
import * as paths from './smartuniverse.paths';
import * as interfaces from './interfaces';
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
}
export interface IServerGetMessagesRequestBody {
channel: string;
topic?: string;
youngerThan: number;
}
/**
* the interface for a standard request
*/
export interface IServerPutMessageRequestBody {
channel: string;
passphrase: string;
message: string;
payload: any;
}
/**
* main class that setsup a Universe
@ -35,9 +23,16 @@ export class Universe {
// options
private options: ISmartUniverseConstructorOptions;
// Store version handling
/**
* stores the version of the universe server running
* this is done since the version is exposed through the api and multiple fs actions are avoided this way.
*/
private universeVersionStore: string;
private get universeVersion() {
/**
* get the currently running version of smartuniverse
*/
public get universeVersion() {
if (this.universeVersionStore) {
return this.universeVersionStore;
} else {
@ -47,7 +42,14 @@ export class Universe {
}
}
/**
* the smartexpress server used
*/
private smartexpressServer: plugins.smartexpress.Server;
/**
* the smartsocket used
*/
private smartsocket: plugins.smartsocket.Smartsocket;
constructor(optionsArg: ISmartUniverseConstructorOptions) {
@ -59,14 +61,14 @@ export class Universe {
* adds a channel to the Universe
*/
public async addChannel(nameArg: string, passphraseArg: string) {
const newChannel = new UniverseChannel(this.universeCache, nameArg, passphraseArg);
this.universeCache.channelMap.add(newChannel);
const newChannel = UniverseChannel.createChannel(this.universeCache, nameArg, passphraseArg);
}
/**
* initiates a server
*/
public async initServer(portArg: number | string) {
// lets create the base smartexpress server
this.smartexpressServer = new plugins.smartexpress.Server({
cors: true,
defaultAnswer: async () => {
@ -76,31 +78,44 @@ export class Universe {
port: portArg
});
// lets create the http request route
this.smartexpressServer.addRoute('/sendmessage', new Handler('POST', async (req, res) => {
this.universeCache.addMessage(req.body);
}));
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({
port: 12345 // fix this within smartsocket
});
// add a role for the clients
const ClientRole = new plugins.smartsocket.SocketRole({
name: 'clientuniverse',
passwordHash: 'clientuniverse' // authentication happens on another level
});
// add the role to smartsocket
this.smartsocket.addSocketRoles([ClientRole]);
const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({
allowedRoles: [ClientRole],
funcName: 'channelSubscription',
funcDef: () => {}
funcDef: () => {} // TODO: implement an action upon connection of clients
});
// add smartsocket to the running smartexpress app
this.smartsocket.setExternalServer('express', this.smartexpressServer as any);
// should work with express as well
// start the socket
this.smartsocket.start();
// start the smartexpress instance
await this.smartexpressServer.start();
}
/**
* stop everything
*/
public async stopServer() {
await this.smartsocket.stop();
await this.smartexpressServer.stop();

View File

@ -39,7 +39,7 @@ export class UniverseCache {
}
/**
* add a message to the UniverseStore
* add a message to the UniverseCache
* @param messageArg
* @param attachedPayloadArg
*/
@ -50,7 +50,7 @@ export class UniverseCache {
}
/**
* Read a message from the UniverseStore
* Read a message from the UniverseCache
*/
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
const messageObservable = from(this.messageMap.getArray()).pipe(

View File

@ -23,6 +23,7 @@ export class UniverseChannel {
passphraseArg: string
) {
const newChannel = new UniverseChannel(universeCacheArg, channelNameArg, passphraseArg);
universeCacheArg.channelMap.add(newChannel);
return newChannel;
}

View File

@ -1,3 +0,0 @@
export interface IUniverseChannel {}
export interface IUniverseMessage {}