-
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
21 changed files
with
672 additions
and
283 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const QueueClient = require('../lib/hyper/queueClient').QueueClient | ||
const NetTransport = require('../lib/hyper/net_transport') | ||
|
||
const conf_realm_host = process.env.FC_REALM_HOST || '127.0.0.1' | ||
const conf_realm_port = process.env.FC_REALM_PORT || 9300 | ||
|
||
let client = new QueueClient() | ||
|
||
let socket = NetTransport.createClientSocket(client) | ||
let maxPrefix = undefined | ||
let maxDate = undefined | ||
let maxSegment = undefined | ||
|
||
async function worker () { | ||
await new Promise((resolve, reject) => { | ||
socket.connect(conf_realm_port, conf_realm_host, function () { | ||
resolve() | ||
}) | ||
}) | ||
|
||
let loginDetails = await client.login({}) | ||
console.log('loginDetails', loginDetails) | ||
|
||
await client.trace('getNewSegment', (data, task) => { | ||
let newPrefix = data.date + data.segment | ||
if (!maxPrefix || maxPrefix < newPrefix) { | ||
maxPrefix = newPrefix | ||
maxDate = data.date | ||
maxSegment = data.segment | ||
} | ||
|
||
let result = { date: maxDate, segment: maxSegment } | ||
console.log('segment', maxPrefix, data, result) | ||
|
||
client.push('takeNewSegment', result) | ||
task.resolve({}) | ||
}) | ||
} | ||
|
||
worker().then(function (value) { | ||
console.log('worker OK:', value) | ||
}, function (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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict' | ||
|
||
const net = require('net') | ||
const msgpack = require('msgpack-lite') | ||
const { SESSION_TX, SESSION_RX, SESSION_WARNING } = require('../messages') | ||
|
||
function ServerNetSender (socket, session, router) { | ||
// var encodeStream = msgpack.createEncodeStream() | ||
// encodeStream.pipe(socket) | ||
this.send = function (msg, callback) { | ||
router.emit(SESSION_TX, session, JSON.stringify(msg)) | ||
socket.write(msgpack.encode(msg)) | ||
// encodeStream.write(msg); | ||
// encodeStream.end(); does not sending without end, but disconnections | ||
} | ||
|
||
this.close = function (code, reason) { | ||
socket.end() | ||
} | ||
} | ||
|
||
function NetServer (gate, options) { | ||
let router = gate.getRouter() | ||
let _server = net.Server(function (socket) { | ||
let session = gate.createSession() | ||
let sender = new ServerNetSender(socket, session, router) | ||
let decodeStream = msgpack.createDecodeStream() | ||
|
||
socket.pipe(decodeStream).on('data', function (msg) { | ||
let ctx = gate.createContext(session, sender) | ||
try { | ||
router.emit(SESSION_RX, session, JSON.stringify(msg)) | ||
session.handle(ctx, msg) | ||
} catch (e) { | ||
router.emit(SESSION_WARNING, session, 'invalid message', msg) | ||
session.close(1003, 'protocol violation') | ||
console.log(e) | ||
} | ||
}) | ||
|
||
socket.on('end', function () { | ||
}) | ||
|
||
socket.on('close', function () { | ||
session.cleanup() | ||
}) | ||
}) | ||
_server.listen(options) | ||
|
||
return _server | ||
} | ||
|
||
function ClientNetSender (socket) { | ||
this.send = function (msg, callback) { | ||
socket.write(msgpack.encode(msg)) | ||
} | ||
|
||
this.close = function (code, reason) { | ||
socket.end() | ||
} | ||
} | ||
|
||
function createClientSocket (client) { | ||
var socket = new net.Socket() | ||
var sender = new ClientNetSender(socket) | ||
client.sender = sender | ||
|
||
let decodeStream = msgpack.createDecodeStream() | ||
|
||
socket.pipe(decodeStream).on('data', function (msg) { | ||
client.handle(msg) | ||
}) | ||
return socket | ||
} | ||
|
||
exports.NetServer = NetServer | ||
exports.createClientSocket = createClientSocket |
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,88 @@ | ||
'use strict' | ||
|
||
const { match, extract, defaultParse, restoreUri } = require('./topic_pattern') | ||
const errorCodes = require('./realm_error').errorCodes | ||
const KeyValueStorageAbstract = require('./realm').KeyValueStorageAbstract | ||
|
||
class MemKeyValueStorage extends KeyValueStorageAbstract { | ||
constructor () { | ||
super() | ||
this._keyDb = new Map() | ||
} | ||
|
||
getKey (uri, cbRow) { | ||
return new Promise((resolve, reject) => { | ||
for (let [key, item] of this._keyDb) { | ||
const aKey = defaultParse(key) | ||
if (match(aKey, uri)) { | ||
cbRow(aKey, item[1] /* data */) | ||
} | ||
} | ||
resolve() | ||
}) | ||
} | ||
|
||
setKeyActor (actor) { | ||
const opt = actor.getOpt() | ||
const suri = restoreUri(extract(actor.getUri(), this.getUriPattern())) | ||
const data = actor.getData() | ||
|
||
const pub = () => { | ||
if (actor) { | ||
this.pubActor(actor) | ||
} | ||
if (data === null) { | ||
this._keyDb.delete(suri) | ||
} else { | ||
this._keyDb.set(suri, [actor.getSid(), data]) | ||
} | ||
} | ||
const row = this._keyDb.get(suri) | ||
|
||
if ('when' in opt) { | ||
if (opt.when === null) { | ||
|
||
if (undefined === row) { | ||
pub() | ||
return | ||
} | ||
const [sid, val, when] = row | ||
|
||
if (val === null) { | ||
pub() | ||
return | ||
} | ||
|
||
actor.reject(errorCodes.ERROR_INVALID_PAYLOAD, 'Found value is not empty') | ||
return | ||
} | ||
|
||
if (undefined !== row) { | ||
const [sid, val, when] = row | ||
if (val !== null) { | ||
pub() | ||
return | ||
} | ||
} | ||
actor.reject(errorCodes.ERROR_INVALID_PAYLOAD, 'Value is empty') | ||
return | ||
} | ||
|
||
pub() | ||
} | ||
|
||
removeSession (sessionId) { | ||
let toRemove = [] | ||
for (let key in this._keyDb) { | ||
const keySessionId = this._keyDb.get(key)[0] | ||
if (keySessionId === sessionId) { | ||
toRemove.push(key) | ||
} | ||
} | ||
for (let i = 0; i < toRemove.length; i++) { | ||
this.setKeyData(toRemove[i], null) | ||
} | ||
} | ||
} | ||
|
||
exports.MemKeyValueStorage = MemKeyValueStorage |
Oops, something went wrong.