-
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
5,844 additions
and
556 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const sqlite3 = require('sqlite3') | ||
const sqlite = require('sqlite') | ||
const { DbBinder } = require('../lib/sqlite/dbbinder') | ||
const Router = require('../index') | ||
const { BaseRealm } = require('../lib/realm') | ||
const { ReactEngine } = require('../lib/binder') | ||
|
||
async function main () { | ||
const db = await sqlite.open({ | ||
filename: '../dbfiles/msgdb.sqlite', | ||
driver: sqlite3.Database | ||
}) | ||
|
||
const binder = new DbBinder(db) | ||
const maxId = await binder.init() | ||
binder.startIntervalTimer() | ||
console.log('loaded max id:', maxId) | ||
|
||
const router = new Router() | ||
router.createRealm = () => new BaseRealm(router, new ReactEngine(binder)) | ||
router.setLogTrace(true) | ||
router.listenWAMP({ port: 9000 }) | ||
router.listenMQTT({ port: 1883 }) | ||
} | ||
|
||
main().then((value) => { | ||
console.log('DONE.') | ||
}, (err) => { | ||
console.error('ERROR:', err, err.stack) | ||
}) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict' | ||
|
||
const tools = require('../tools') | ||
const { makeDataSerializable, unSerializeData } = require('../realm') | ||
const { ReactBinder } = require('../binder') | ||
const Msg = require('./msg') | ||
|
||
class DbBinder extends ReactBinder { | ||
constructor (db) { | ||
super() | ||
this.msg = new Msg(db) | ||
this._messageGen = 0 | ||
this.dateStr = tools.keyDate(new Date()) | ||
} | ||
|
||
startIntervalTimer () { | ||
setInterval(() => { | ||
let dateStr = tools.keyDate(new Date()) | ||
if (dateStr > this.dateStr) { | ||
this._messageGen = 0 | ||
} | ||
}, 60000) | ||
} | ||
|
||
async init () { | ||
await this.msg.createTables() | ||
let curId = await this.msg.getMaxId() | ||
if (curId) { | ||
this.parseId(curId) | ||
} | ||
return curId | ||
} | ||
|
||
parseId (encodedId) { | ||
let newDateStr = encodedId.substr(0, 10) | ||
let intLen = encodedId.charCodeAt(10) - 96 | ||
let newId = parseInt(encodedId.substr(11, intLen), 36) | ||
if (newDateStr > this.dateStr) { | ||
this._messageGen = newId | ||
} else if (newDateStr == this.dateStr && newId > this._messageGen) { | ||
this._messageGen = newId | ||
} | ||
} | ||
|
||
getNewId () { | ||
return this.dateStr + tools.keyId(++this._messageGen) | ||
} | ||
|
||
keepHistory (engine, actor) { | ||
const id = this.getNewId() | ||
actor.setEventId(id) | ||
// console.log("getNewId", id, actor.getEvent()) | ||
|
||
let result | ||
if (actor.getOpt().trace) { | ||
result = this.msg.saveMsg( | ||
id, | ||
engine.getRealmName(), | ||
actor.getUri(), | ||
makeDataSerializable(actor.getData()) | ||
) | ||
} else { | ||
result = Promise.resolve() | ||
} | ||
result.then(() => { | ||
actor.destSID = engine.dispatch(actor.getEvent()) | ||
actor.confirm(actor.msg) | ||
}) | ||
} | ||
|
||
getHistoryAfter (engine, after, uri, cbRow) { | ||
return this.msg.getHistory( | ||
engine.getRealmName(), | ||
{ fromId: after, uri }, | ||
(event) => { | ||
cbRow({ | ||
qid: event.id, | ||
uri: event.uri, | ||
data: unSerializeData(event.body) | ||
}) | ||
} | ||
) | ||
} | ||
|
||
cleanupSession(engine, sessionId) { | ||
} | ||
} | ||
|
||
exports.DbBinder = DbBinder |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict' | ||
|
||
const { defaultParse, restoreUri } = require('../topic_pattern') | ||
|
||
function Msg (database) { | ||
this.createTables = function () { | ||
return database.run( | ||
'CREATE TABLE IF NOT EXISTS msg (' + | ||
'msg_id TEXT not null,' + | ||
'msg_realm TEXT not null,' + | ||
'msg_uri TEXT not null,' + | ||
'msg_body TEXT,' + | ||
'PRIMARY KEY (msg_id));' | ||
) | ||
} | ||
|
||
this.getMaxId = function () { | ||
return database.all( | ||
'SELECT MAX(msg_id) max_id FROM msg', [] | ||
).then((result) => { | ||
if (!Array.isArray(result)) { | ||
return undefined | ||
} | ||
if (result.length === 0) { | ||
return undefined | ||
} | ||
if (result[0].max_id) { | ||
return result[0].max_id | ||
} else { | ||
return undefined | ||
} | ||
}) | ||
} | ||
|
||
this.saveMsg = function (id, realm, uri, body) { | ||
return database.run( | ||
'insert into msg values (?,?,?,?);', | ||
[id, realm, restoreUri(uri), JSON.stringify(body)] | ||
) | ||
} | ||
|
||
this.getHistory = function (realm, range, rowcb) { | ||
let sql = 'SELECT msg_id, msg_uri, msg_body FROM msg' | ||
let where = [] | ||
if (range.fromId) { | ||
where.push('msg_id > "' + range.fromId + '"') | ||
} | ||
if (range.toId) { | ||
where.push('msg_id <= "' + range.toId + '"') | ||
} | ||
|
||
where.push('msg_realm = ?') | ||
where.push('msg_uri = ?') | ||
sql += ' WHERE ' + where.join(' AND ') + ' ORDER BY msg_id' | ||
|
||
// console.log('range', [realm, restoreUri(range.uri)], sql) | ||
return database.each( | ||
sql, | ||
[realm, restoreUri(range.uri)], | ||
(err, row) => { | ||
rowcb({ | ||
id: row.msg_id, | ||
uri: defaultParse(row.msg_uri), | ||
body: JSON.parse(row.msg_body) | ||
}) | ||
} | ||
) | ||
} | ||
} | ||
|
||
module.exports = Msg |
Oops, something went wrong.