-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
331 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,106 @@ | ||
'use strict' | ||
|
||
const { keyId } = require("../tools") | ||
|
||
class HistorySegment { | ||
constructor () { | ||
this.content = [] | ||
} | ||
|
||
addEvent (event) { | ||
this.content.push(event) | ||
} | ||
} | ||
|
||
class EntrySession { | ||
constructor (wampSession, syncMass) { | ||
constructor (wampSession, syncMass, history) { | ||
this.wampSession = wampSession | ||
this.stack = [] | ||
this.waitForValue = undefined | ||
this.stackApplicantId = [] | ||
this.curApplicantId = undefined | ||
this.syncMass = syncMass | ||
this.history = history | ||
this.segmentToWrite = new Map() | ||
|
||
wampSession.subscribe('mkId', (publishArgs, kwargs, opts) => { | ||
this.sync(kwargs.bundleId) | ||
console.log('mkId', kwargs) | ||
this.queueSync(kwargs.applicantId) | ||
}) | ||
|
||
wampSession.subscribe('ping', (publishArgs, kwargs, opts) => { | ||
console.log('ping', kwargs) | ||
wampSession.publish('pong', publishArgs, kwargs) | ||
}) | ||
|
||
wampSession.subscribe('mqlog', (publishArgs, kwargs, opts) => { | ||
this.delayEvent(kwargs) | ||
}) | ||
} | ||
|
||
mkDbId (segmentId, offset) { | ||
return segmentId.dt+keyId(segmentId.id)+keyId(offset) | ||
} | ||
|
||
sendToSync(bundleId) { | ||
console.log('mkId', bundleId) | ||
delayEvent (event) { | ||
let segment = this.segmentToWrite.get(event.applicantId) | ||
if (!segment) { | ||
console.error("applicantId not found to delay [", event.applicantId, "]", event) | ||
segment = new HistorySegment() | ||
this.segmentToWrite.set(event.applicantId, segment) | ||
} | ||
segment.addEvent(event) | ||
} | ||
|
||
sendToSync (applicantId) { | ||
console.log('sendToSync applicantId[', applicantId, "]") | ||
for (let [,ss] of this.syncMass) { | ||
ss.publish('mkId', [], {bundleId}) | ||
ss.publish('mkId', [], {applicantId}) | ||
} | ||
if (!this.segmentToWrite.has(applicantId)) { | ||
this.segmentToWrite.set(applicantId, new HistorySegment()) | ||
} | ||
} | ||
|
||
checkLine() { | ||
if (this.waitForValue) { | ||
if (this.curApplicantId) { | ||
return false | ||
} | ||
this.waitForValue = this.stack.shift() | ||
if (this.waitForValue) { | ||
this.sendToSync(this.waitForValue) | ||
this.curApplicantId = this.stackApplicantId.shift() | ||
if (this.curApplicantId) { | ||
this.sendToSync(this.curApplicantId) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
sync(bundleId) { | ||
this.stack.push(bundleId) | ||
queueSync (applicantId) { | ||
this.stackApplicantId.push(applicantId) | ||
return this.checkLine() | ||
} | ||
|
||
done(bundleId, syncId) { | ||
if (this.waitForValue === bundleId) { | ||
this.wampSession.publish('readyId', [], {bundleId, syncId}) | ||
commitSegment (applicantId, segmentId) { | ||
// check is applicant of mine session | ||
if (this.curApplicantId === applicantId) { | ||
this.dbSaveSegment(applicantId, segmentId) | ||
this.wampSession.publish('readyId', [], {applicantId, segmentId}) | ||
this.curApplicantId = undefined | ||
return this.checkLine() | ||
} | ||
return false | ||
} | ||
|
||
dbSaveSegment (applicantId, segmentId) { | ||
console.log("dbSaveSegment", applicantId, segmentId) | ||
let segment = this.segmentToWrite.get(applicantId) | ||
if (!segment) { | ||
console.error("applicantId not found in segments [", applicantId, "]") | ||
return | ||
} | ||
let offset = 1 | ||
for (let row of segment.content) { | ||
this.history.saveEventHistory(this.mkDbId(segmentId, offset), undefined, row.realm, row.uri, row.data) | ||
offset++ | ||
} | ||
} | ||
} | ||
|
||
exports.EntrySession = EntrySession |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.