2018-03-08 22:42:46 +00:00
|
|
|
"use strict";
|
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const plugins = require("./smartuniverse.plugins");
|
|
|
|
const smartexpress_1 = require("smartexpress");
|
2018-03-13 05:15:40 +00:00
|
|
|
const smartuniverse_classes_manager_1 = require("./smartuniverse.classes.manager");
|
|
|
|
const smartuniverse_classes_universestore_1 = require("./smartuniverse.classes.universestore");
|
2018-03-08 22:42:46 +00:00
|
|
|
const paths = require("./smartuniverse.paths");
|
2018-03-13 05:15:40 +00:00
|
|
|
class Universe {
|
2018-03-08 22:42:46 +00:00
|
|
|
get universeVersion() {
|
|
|
|
if (this.universeVersionStore) {
|
|
|
|
return this.universeVersionStore;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
|
|
|
|
this.universeVersionStore = packageJson.version;
|
|
|
|
return this.universeVersionStore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
constructor(optionsArg) {
|
|
|
|
this.options = optionsArg;
|
2018-03-13 05:15:40 +00:00
|
|
|
this.universeStore = new smartuniverse_classes_universestore_1.UniverseStore(this.options.messageExpiryInMilliseconds);
|
|
|
|
this.universeManager = new smartuniverse_classes_manager_1.UniverseManager();
|
2018-03-08 22:42:46 +00:00
|
|
|
}
|
2018-03-13 05:15:40 +00:00
|
|
|
/**
|
|
|
|
* initiates a server
|
|
|
|
*/
|
|
|
|
initServer(portArg) {
|
2018-03-08 22:42:46 +00:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
this.smartexpressServer = new plugins.smartexpress.Server({
|
|
|
|
cors: true,
|
|
|
|
defaultAnswer: `smartuniverse server ${this.universeVersion}`,
|
|
|
|
forceSsl: false,
|
2018-03-13 05:15:40 +00:00
|
|
|
port: portArg
|
2018-03-08 22:42:46 +00:00
|
|
|
});
|
2018-03-20 07:16:54 +00:00
|
|
|
this.smartsocket = new plugins.smartsocket.Smartsocket({
|
|
|
|
port: 12345 // fix this within smartsocket
|
|
|
|
});
|
2018-03-27 22:38:32 +00:00
|
|
|
this.smartsocket.setExternalServer('express', this.smartexpressServer); // should work with express as well
|
|
|
|
this.smartsocket.start();
|
2018-03-13 05:15:40 +00:00
|
|
|
// route handling
|
2018-03-15 00:16:16 +00:00
|
|
|
// adds messages
|
2018-03-13 05:15:40 +00:00
|
|
|
const addMessageHandler = new smartexpress_1.Handler('PUT', request => {
|
|
|
|
const requestBody = request.body;
|
|
|
|
this.universeStore.addMessage(requestBody.message, requestBody.payload);
|
2018-03-15 00:16:16 +00:00
|
|
|
console.log(requestBody);
|
2018-03-13 05:15:40 +00:00
|
|
|
return true;
|
2018-03-08 22:42:46 +00:00
|
|
|
});
|
2018-03-15 00:16:16 +00:00
|
|
|
// gets messages
|
2018-03-13 05:15:40 +00:00
|
|
|
const readMessageHandler = new smartexpress_1.Handler('GET', request => {
|
|
|
|
const requestBody = request.body;
|
|
|
|
this.universeStore.readMessagesYoungerThan(requestBody.since);
|
|
|
|
});
|
|
|
|
const messageRoute = new smartexpress_1.Route(this.smartexpressServer, 'message');
|
|
|
|
messageRoute.addHandler(addMessageHandler);
|
|
|
|
messageRoute.addHandler(readMessageHandler);
|
|
|
|
yield this.smartexpressServer.start();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
stopServer() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2018-03-27 22:38:32 +00:00
|
|
|
yield this.smartsocket.stop();
|
2018-03-13 05:15:40 +00:00
|
|
|
yield this.smartexpressServer.stop();
|
2018-03-08 22:42:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 05:15:40 +00:00
|
|
|
exports.Universe = Universe;
|
2018-03-27 22:38:32 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnR1bml2ZXJzZS5jbGFzc2VzLnVuaXZlcnNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnR1bml2ZXJzZS5jbGFzc2VzLnVuaXZlcnNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFBQSxtREFBbUQ7QUFFbkQsK0NBQXNEO0FBRXRELG1GQUFrRTtBQUVsRSwrRkFBc0U7QUFDdEUsK0NBQStDO0FBZS9DO0lBVUUsSUFBWSxlQUFlO1FBQ3pCLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxvQkFBb0IsQ0FBQyxDQUFDLENBQUM7WUFDOUIsTUFBTSxDQUFDLElBQUksQ0FBQyxvQkFBb0IsQ0FBQztRQUNuQyxDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDTixNQUFNLFdBQVcsR0FBRyxPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsQ0FBQyxZQUFZLENBQUMsS0FBSyxDQUFDLFdBQVcsQ0FBQyxDQUFDO1lBQ3pFLElBQUksQ0FBQyxvQkFBb0IsR0FBRyxXQUFXLENBQUMsT0FBTyxDQUFDO1lBQ2hELE1BQU0sQ0FBQyxJQUFJLENBQUMsb0JBQW9CLENBQUM7UUFDbkMsQ0FBQztJQUNILENBQUM7SUFLRCxZQUFZLFVBQTRDO1FBQ3RELElBQUksQ0FBQyxPQUFPLEdBQUcsVUFBVSxDQUFDO1FBQzFCLElBQUksQ0FBQyxhQUFhLEdBQUc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