123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- /*
- The MIT License (MIT)
- Copyright (c) 2014-2019 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
- */
- //import { Helpers } from '/helpers.js';
- class Luminary {
- constructor() {
- console.log("luminary constructor");
- this.helpers = _app.helpers; //new Helpers;
- this.info = {};
- this.pendingList = [];
- this.status = { pending: true, initialized: false, trials: 3 };
- this.clients = {};
- this.heartbeat = {}
- this.clientID = undefined;
- this.namespace = undefined;
- }
- unsubscribeFromHeartbeat() {
- //TODO
- }
- subscribeOnHeartbeat(heartbeat) {
- let self = this;
- heartbeat.on(resp => {
- var res = Gun.obj.copy(resp);
- if (res.tick) {
- if (self.start_time) {
- let currentTick = Gun.state.is(res, 'tick');
- self.heartbeat.lastTick = currentTick;
- let msg = self.stamp(res);
- if (!self.status.pending) {
- self.onMessage(msg)
- } else {
- self.pendingList.push(msg);
- }
- }
- }
- })
- }
- subscribeOnMessages() {
- let self = this;
- let instance = _LCSDB.get(this.namespace);
- instance.get('message').on(resp => {
- var res = Gun.obj.copy(resp);
- if (res.tick) {
- if (self.start_time) {
- let msg = self.stamp(res);
- if (msg.explicit) {
- if (msg.explicit == vwf.moniker_) {
- self.onMessage(msg);
- if (msg.action == 'setState') {
- if (self.status.pending) {
- self.distributePendingMessages();
- self.status.pending = false;
- }
- }
- console.log(res);
- }
- } else if (!self.status.pending) {
- self.onMessage(msg);
- } else {
- self.pendingList.push(msg);
- }
- }
- }
- })
- }
- stamp(source) {
- var message = source.tick
- if(typeof message == "string"){
- message = JSON.parse(source.tick);
- }
-
- // if(message.sender){
- // console.log("HEARTBEAT FROM: " + message.sender);
- // }
- message.state = Gun.state.is(source, 'tick');
- message.start_time = this.start_time; //Gun.state.is(source, 'start_time');
- message.rate = this.rate; //source.rate;
- var time = ((message.state - message.start_time) * message.rate) / 1000;
- if (message.action == 'getState') {
- console.log('GET STATE msg!!!');
- }
- if (message.action == 'setState') {
- time = ((this.setStateTime - message.start_time) * message.rate) / 1000;
- }
- message.time = Number(time);
- message.origin = "reflector";
- return message
- }
- stampExternalMessage(msg) {
- let message = Object.assign({}, msg);
- message.client = this.clientID;
- let instance = _LCSDB.get(this.namespace)//_LCSDB.get(meta.namespace);
- if (message.result === undefined) {
- instance.get('message').get('tick').put(JSON.stringify(message));
- } else if (message.action == "getState") {
- let state = message.result;//JSON.stringify(message.result);
- let toClient = message.parameters[0];
- let newMsg =
- JSON.stringify({
- action: "setState",
- parameters: [state],
- time: 'tick', //self.setStateTime,
- explicit: toClient
- })
- instance.get('message')
- .get('tick')
- .put(newMsg)
- } else if (message.action === "execute") {
- console.log("!!!! execute ", message)
- }
- }
- onMessage(message) {
- try {
- var fields = Object.assign({}, message);
- vwf.private.queue.insert(fields, !fields.action); // may invoke dispatch(), so call last before returning to the host
- } catch (e) {
- vwf.logger.warn(fields.action, fields.node, fields.member, fields.parameters,
- "exception performing action:", require("vwf/utility").exceptionMessage(e));
- }
- }
- async connect(path) {
- let self = this;
- let objForQuery = this.helpers.reduceSaveObject(path);
- this.clientID = Gun.text.random();
- this.namespace = this.helpers.GetNamespace(path.path);
- //vwf.moniker_ = clientID;
- this.info = {
- pathname: window.location.pathname.slice(1,
- window.location.pathname.lastIndexOf("/")),
- appRoot: "./public",
- path: JSON.stringify(objForQuery), //JSON.stringify(path)
- namespace: this.namespace,
- }
- //set an instance with namespace
- let luminaryPath = _app.luminaryPath;
- let lum = _LCSDB.get(luminaryPath);
- let instance = _LCSDB.get(this.namespace);
- instance.not(function (res) {
- instance
- .put(self.info)
- .put({
- 'start_time': 'start_time',
- 'rate': 1
- });
- lum.get('instances').set(instance);
- self.status.initialized = "first";
- });
- await instance.once(res => {
- self.start_time = Gun.state.is(res, 'start_time');
- self.rate = res.rate;
- }).promOnce();
- let client = _LCSDB.get(self.clientID).put({ id: self.clientID, instance: self.namespace, user: path.user }).once(res => {
- self.setStateTime = Gun.state.is(res, 'id');
- setInterval(function () {
- client.get('live').put('tick');
- }, 500);
- });
- instance.get('clients').set(client);
- lum.get('allclients').set(client);
- instance.get('clients').map().on(res => {
- if (res) {
- if (res.id && res.live) {
- let clientTime = Gun.state.is(res, 'live');
- //let now = Gun.time.is();
- //console.log("NEW CLIENT LIVE : " + res.id);
- if (!self.clients[res.id]) {
- self.clients[res.id] = {
- live: clientTime,
- old: clientTime
- }
- } else {
- self.clients[res.id].old = self.clients[res.id].live;
- self.clients[res.id].live = clientTime
- }
- if (self.status.initialized == "first" && self.setStateTime) {
- self.status.initialized = true;
- instance
- .put({
- 'start_time': 'start_time',
- 'rate': 1
- }).once(res => {
- self.start_time = Gun.state.is(res, 'start_time');
- self.rate = res.rate;
- if (!_app.isLuminaryGlobalHB) {
- let tickMsg = {
- parameters: "[]",
- time: 'tick', //hb
- sender: self.clientID
- };
- instance.get('heartbeat').get('tick').put(tickMsg);
- self.initHeartBeat();
- }
- self.initFirst(res);
- self.initDeleteClient();
- });
- let noty = new Noty({
- text: "FIRST CLIENT",
- timeout: 1000,
- theme: 'mint',
- layout: 'bottomRight',
- type: 'success'
- });
- noty.show();
- } else if (!self.status.initialized && self.setStateTime) {
- if (res.id == self.clientID && self.status.trials > 0) {
- self.status.trials = self.status.trials - 1;
- console.log("CONNECTION TRIALS FOR: " + res.id + ' - ' + self.status.trials);
- } else if (res.id !== self.clientID && self.clients[res.id].live - self.clients[res.id].old < 1000) {
- console.log("REQUEST STATE FROM: " + res.id);
- self.status.initialized = true;
- if (!_app.isLuminaryGlobalHB) {
- self.initHeartBeat();
- }
- self.initOtherClient(res);
- self.initDeleteClient();
- let noty = new Noty({
- text: "CONNECTING TO EXISTED CLIENT...",
- timeout: 1000,
- theme: 'mint',
- layout: 'bottomRight',
- type: 'success'
- });
- noty.show();
- } else if (res.id == self.clientID && self.status.trials == 0) {
- console.log("INITIALIZE WORLD FOR: " + res.id);
- //request for the new instance
- let path = JSON.parse(self.info.path);
- window.location.pathname = path.user + path.path["public_path"];
- }
- }
- }
- }
- })
- return path
- }
- distributePendingMessages() {
- let self = this;
- if (self.pendingList.length > 0) {
- console.log("!!!! getPendingMessages");
- let cloneList = [...self.pendingList];
- cloneList.forEach(el => {
- self.onMessage(el);
- })
- self.pendingList = [];
- //_app.status.pending = false;
- }
- }
- clientsMessage() {
- let self = this;
- let clientDescriptor = { extends: "http://vwf.example.com/client.vwf" };
- let clientNodeMessage =
- {
- action: "createChild",
- parameters: ["http://vwf.example.com/clients.vwf", self.clientID, clientDescriptor],
- time: 'tick'
- }
- return clientNodeMessage
- }
- initFirst(ack) {
- let self = this;
- let instance = _LCSDB.get(self.namespace);
- let clientMsg =
- JSON.stringify({
- action: "createNode",
- parameters: ["http://vwf.example.com/clients.vwf"],
- time: 'tick',
- explicit: self.clientID
- })
- let processedURL = JSON.parse(self.info.path).path;
- let appMsg =
- JSON.stringify({
- action: "createNode",
- parameters: [
- (processedURL.public_path === "/" ? "" : processedURL.public_path) + "/" + processedURL.application,
- "application"
- ],
- time: 'tick',
- explicit: self.clientID
- })
- instance.get('message')
- .get('tick')
- .put(clientMsg);
- instance.get('message')
- .get('tick')
- .put(appMsg, res => {
- self.status.pending = false;
- let clientsMessage = self.clientsMessage();
- instance.get('message')
- .get('tick')
- .put(JSON.stringify(clientsMessage), res => {
- console.log("CREATE CLIENT: - " + res);
- })
- });
- }
- initOtherClient(ack) {
- console.log('new other client');
- let self = this;
- let instance = _LCSDB.get(self.namespace);
- let masterID = ack.id;
- let msg =
- JSON.stringify({
- action: "getState",
- respond: true,
- time: 'tick',
- explicit: masterID,
- parameters: [self.clientID]
- })
- instance.get('message')
- .get('tick')
- .put(msg);
- let clientsMessage = self.clientsMessage();
- instance.get('message')
- .get('tick').put(JSON.stringify(clientsMessage), res=>{
-
- console.log("CREATE CLIENT: - " + res);
- });
- }
- initHeartBeat() {
- let self = this;
- let instance = _LCSDB.get(self.namespace);
- setInterval(function () {
- let message = {
- parameters: "[]",
- time: 'tick', //hb
- sender: self.clientID
- };
- instance.get('heartbeat').get('tick').once(data => {
- if (data) {
- //let res = JSON.parse(data);
- var res = data
- if(typeof res == "string"){
- res = JSON.parse(data);
- }
- if (res.sender) {
- let now = Gun.time.is();
- let diff = now - self.heartbeat.lastTick;
- if ((Object.keys(self.clients).length == 1)
- || (res.sender == self.clientID && diff < 1000)
- || (res.sender !== self.clientID && diff > 1000)) {
- //console.log("TICK FROM" + self.clientID);
- instance.get('heartbeat').get('tick').put(message, function (ack) {
- if (ack.err) {
- console.log('ERROR: ' + ack.err)
- }
- });
- }
- }
- }
- })
- }, 50);
- }
- initDeleteClient() {
- let self = this;
- let instance = _LCSDB.get(self.namespace);
- setInterval(function () {
- Object.keys(self.clients).forEach(el => {
- let current = Gun.time.is();
- if (el !== self.clientID) {
- if (current - self.clients[el].live > 10000) {
- console.log("CLIENT DISCONECTED : " + el);
- let clientDeleteMessage =
- {
- action: "deleteChild",
- parameters: ["http://vwf.example.com/clients.vwf", el],
- time: 'tick'
- };
- instance.get('message')
- .get('tick').once(res => {
- instance.get('message')
- .get('tick')
- .put(JSON.stringify(clientDeleteMessage), res => {
- instance.get('clients').get(el).put(null);
- delete self.clients[el];
- })
- })
- }
- }
- })
- }, 5000);
- }
- }
- export { Luminary }
|