From f728c56016f75093482bf25a24fe480a73a19350 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Thu, 11 Apr 2019 17:52:01 +0200 Subject: [PATCH] fix(core): update --- ts/index.ts | 3 +- ts/interfaces/http.interfaces.ts | 15 +++++ ts/interfaces/index.ts | 3 + ts/interfaces/universechannel.interfaces.ts | 1 + ts/interfaces/universemessage.interfaces.ts | 1 + ts/smartuniverse.classes.clientuniverse.ts | 4 +- ...tuniverse.classes.clientuniversechannel.ts | 5 +- ...tuniverse.classes.clientuniversemessage.ts | 12 +++- ts/smartuniverse.classes.universe.ts | 55 ++++++++++++------- ts/smartuniverse.classes.universecache.ts | 4 +- ts/smartuniverse.classes.universechannel.ts | 1 + ts/smartuniverse.interfaces.ts | 3 - 12 files changed, 74 insertions(+), 33 deletions(-) create mode 100644 ts/interfaces/http.interfaces.ts create mode 100644 ts/interfaces/index.ts create mode 100644 ts/interfaces/universechannel.interfaces.ts create mode 100644 ts/interfaces/universemessage.interfaces.ts delete mode 100644 ts/smartuniverse.interfaces.ts diff --git a/ts/index.ts b/ts/index.ts index e934b6d..14822fa 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -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'; diff --git a/ts/interfaces/http.interfaces.ts b/ts/interfaces/http.interfaces.ts new file mode 100644 index 0000000..366f0cc --- /dev/null +++ b/ts/interfaces/http.interfaces.ts @@ -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; +} \ No newline at end of file diff --git a/ts/interfaces/index.ts b/ts/interfaces/index.ts new file mode 100644 index 0000000..345ff0d --- /dev/null +++ b/ts/interfaces/index.ts @@ -0,0 +1,3 @@ +export * from './http.interfaces'; +export * from './universechannel.interfaces'; +export * from './universemessage.interfaces'; diff --git a/ts/interfaces/universechannel.interfaces.ts b/ts/interfaces/universechannel.interfaces.ts new file mode 100644 index 0000000..ccde825 --- /dev/null +++ b/ts/interfaces/universechannel.interfaces.ts @@ -0,0 +1 @@ +export interface IUniverseChannel {} diff --git a/ts/interfaces/universemessage.interfaces.ts b/ts/interfaces/universemessage.interfaces.ts new file mode 100644 index 0000000..a65ee3d --- /dev/null +++ b/ts/interfaces/universemessage.interfaces.ts @@ -0,0 +1 @@ +export interface IUniverseMessage {} diff --git a/ts/smartuniverse.classes.clientuniverse.ts b/ts/smartuniverse.classes.clientuniverse.ts index 0e6cf01..ea0d1f4 100644 --- a/ts/smartuniverse.classes.clientuniverse.ts +++ b/ts/smartuniverse.classes.clientuniverse.ts @@ -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 './'; diff --git a/ts/smartuniverse.classes.clientuniversechannel.ts b/ts/smartuniverse.classes.clientuniversechannel.ts index 402a5ef..0979277 100644 --- a/ts/smartuniverse.classes.clientuniversechannel.ts +++ b/ts/smartuniverse.classes.clientuniversechannel.ts @@ -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 // ====== diff --git a/ts/smartuniverse.classes.clientuniversemessage.ts b/ts/smartuniverse.classes.clientuniversemessage.ts index f87e26c..882f01e 100644 --- a/ts/smartuniverse.classes.clientuniversemessage.ts +++ b/ts/smartuniverse.classes.clientuniversemessage.ts @@ -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 () { + + } } diff --git a/ts/smartuniverse.classes.universe.ts b/ts/smartuniverse.classes.universe.ts index a43dd6f..3dc0b2d 100644 --- a/ts/smartuniverse.classes.universe.ts +++ b/ts/smartuniverse.classes.universe.ts @@ -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(); diff --git a/ts/smartuniverse.classes.universecache.ts b/ts/smartuniverse.classes.universecache.ts index 5da3622..1bd6d29 100644 --- a/ts/smartuniverse.classes.universecache.ts +++ b/ts/smartuniverse.classes.universecache.ts @@ -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 { const messageObservable = from(this.messageMap.getArray()).pipe( diff --git a/ts/smartuniverse.classes.universechannel.ts b/ts/smartuniverse.classes.universechannel.ts index 04bb7e9..ca32997 100644 --- a/ts/smartuniverse.classes.universechannel.ts +++ b/ts/smartuniverse.classes.universechannel.ts @@ -23,6 +23,7 @@ export class UniverseChannel { passphraseArg: string ) { const newChannel = new UniverseChannel(universeCacheArg, channelNameArg, passphraseArg); + universeCacheArg.channelMap.add(newChannel); return newChannel; } diff --git a/ts/smartuniverse.interfaces.ts b/ts/smartuniverse.interfaces.ts deleted file mode 100644 index ff6060e..0000000 --- a/ts/smartuniverse.interfaces.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface IUniverseChannel {} - -export interface IUniverseMessage {}