This commit is contained in:
2018-03-13 06:15:40 +01:00
parent 44701c99fc
commit 76cb8a36fc
28 changed files with 464 additions and 91 deletions

View File

@ -1 +1,2 @@
export * from './smartuniverse.classes.smartuniverse';
export * from './smartuniverse.classes.universe';
export * from './smartuniverse.classes.universeclient';

View File

@ -0,0 +1,10 @@
import * as plugins from './smartuniverse.plugins';
import { Objectmap } from 'lik';
export class UniverseManager {
registerMember () {
}
}

View File

@ -1,42 +0,0 @@
import * as plugins from './smartuniverse.plugins';
import { Handler, Route, Server } from 'smartexpress';
import * as paths from './smartuniverse.paths';
export interface ISmartUniverseConstructorOptions {
port: number | string;
}
export class SmartUniverse {
private options: ISmartUniverseConstructorOptions;
private universeVersionStore: string;
private get universeVersion() {
if (this.universeVersionStore) {
return this.universeVersionStore;
} else {
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
this.universeVersionStore = packageJson.version;
return this.universeVersionStore;
}
}
private smartexpressServer: plugins.smartexpress.Server;
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
}
public async init() {
this.smartexpressServer = new plugins.smartexpress.Server({
cors: true,
defaultAnswer: `smartuniverse server ${this.universeVersion}`,
forceSsl: false,
port: this.options.port
});
const addRoute = new Route(this.smartexpressServer, 'addMessage');
const addHandler = new Handler('PUT', requestBody => {
return 'hi';
});
// await this.smartexpressServer.addRoute()
}
}

View File

@ -0,0 +1,84 @@
import * as plugins from './smartuniverse.plugins';
import { Handler, Route, Server } from 'smartexpress';
import { UniverseManager } from './smartuniverse.classes.manager';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { UniverseStore } from './smartuniverse.classes.universestore';
import * as paths from './smartuniverse.paths';
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
}
export interface IServerGetMessagesRequestBody {
youngerThan: number;
}
export interface IServerPutMessageRequestBody {
message: string,
payload: any
}
export class Universe {
// subinstances
public universeStore: UniverseStore;
public universeManager: UniverseManager;
// options
private options: ISmartUniverseConstructorOptions;
// Store version handling
private universeVersionStore: string;
private get universeVersion() {
if (this.universeVersionStore) {
return this.universeVersionStore;
} else {
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
this.universeVersionStore = packageJson.version;
return this.universeVersionStore;
}
}
private smartexpressServer: plugins.smartexpress.Server;
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
this.universeStore = new UniverseStore(this.options.messageExpiryInMilliseconds);
this.universeManager = new UniverseManager();
}
/**
* initiates a server
*/
public async initServer(portArg: number | string) {
this.smartexpressServer = new plugins.smartexpress.Server({
cors: true,
defaultAnswer: `smartuniverse server ${this.universeVersion}`,
forceSsl: false,
port: portArg
});
// route handling
const addMessageHandler = new Handler('PUT', request => {
const requestBody = request.body;
this.universeStore.addMessage(requestBody.message, requestBody.payload);
return true;
});
const readMessageHandler = new Handler('GET', request => {
const requestBody = request.body;
this.universeStore.readMessagesYoungerThan(requestBody.since);
})
const messageRoute = new Route(this.smartexpressServer, 'message');
messageRoute.addHandler(addMessageHandler);
messageRoute.addHandler(readMessageHandler);
await this.smartexpressServer.start();
}
public async stopServer () {
await this.smartexpressServer.stop();
}
}

View File

@ -0,0 +1,14 @@
import * as plugins from './smartuniverse.plugins';
import { Observable } from 'rxjs';
import { IServerGetMessagesRequestBody, IServerPutMessageRequestBody } from './smartuniverse.classes.universe'
export class UniverseClient {
public sendMessage(message, messagePayload) {
}
pulic getMessageObservable () {
}
}

View File

@ -0,0 +1,33 @@
import * as plugins from './smartuniverse.plugins';
import { Timer, TimeStamp } from 'smarttime';
import { UniverseStore } from './smartuniverse.classes.universestore';
export class UniverseMessage {
/**
* public and unique id
* numeric ascending
* adheres to time in milliseconds
* avoids duplications though
*/
public id: number;
public universeStore: UniverseStore;
public timestamp: TimeStamp;
public message: string;
public attachedPayload: any;
public destructionTimer: Timer;
constructor(parentUniverseStore: UniverseStore, messageArg: string, attachedPayloadArg: any, selfdestructAfterArg: number) {
this.universeStore = parentUniverseStore;
this.timestamp = new TimeStamp();
this.message = messageArg;
this.attachedPayload = attachedPayloadArg;
this.destructionTimer = new Timer(selfdestructAfterArg)
this.destructionTimer.start()
// set up self destruction by removing this from the parent messageStore
this.destructionTimer.completed.then(async () => {
this.universeStore.messageStore.remove(this);
})
}
}

View File

@ -0,0 +1,41 @@
import * as plugins from './smartuniverse.plugins';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { Objectmap } from 'lik'
import { Observable } from 'rxjs';
import { rxjs } from 'smartrx'
export class UniverseStore {
public standardMessageExpiry: number;
public destructionTime: number = 60000;
public messageStore = new Objectmap<UniverseMessage>();
private lastId: number = 0; // stores the last id
constructor(standardMessageExpiryArg: number) {
this.standardMessageExpiry = standardMessageExpiryArg;
}
/**
* add a message to the UniverseStore
* @param messageArg
* @param attachedPayloadArg
*/
public addMessage(messageArg, attachedPayloadArg) {
this.messageStore.add(new UniverseMessage(this, messageArg, attachedPayloadArg, this.destructionTime));
}
/**
* Read a message from the UniverseStore
*/
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
const messageObservable = rxjs.Observable
.from(this.messageStore.getArray())
.filter(messageArg => {
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
});
return messageObservable;
}
}

View File

@ -1,13 +1,15 @@
import * as plugins from './smartuniverse.plugins';
import { SmartUniverse } from './index';
import { Universe } from './index';
process.env.CLI = 'true';
const universeCli = new plugins.smartcli.Smartcli();
universeCli.standardTask().then(argvArg => {
const standardUniverse = new SmartUniverse({
port: 8765
universeCli.standardTask().then(async argvArg => {
const standardUniverse = new Universe({
messageExpiryInMilliseconds: 60000
});
await standardUniverse.initServer(8765);
});

View File

@ -1,6 +1,10 @@
import * as lik from 'lik';
import * as path from 'path';
import * as smartcli from 'smartcli';
import * as smartexpress from 'smartexpress';
import * as smartfile from 'smartfile';
import * as smartrequest from 'smartrequest';
import * as smartrx from 'smartrx';
import * as smarttime from 'smarttime';
export { path, smartcli, smartexpress, smartfile };
export { lik, path, smartcli, smartexpress, smartfile, smartrx, smartrequest, smarttime };