mokka
Version:
Mokka Consensus Algorithm implementation in Javascript
73 lines • 2.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const zmq = __importStar(require("zeromq"));
const main_1 = require("../consensus/main");
class ZMQMokka extends main_1.Mokka {
constructor() {
super(...arguments);
this.sockets = new Map();
}
async initialize() {
this.logger.info(`initializing reply socket on port ${this.address}`);
const socket = new zmq.Pair({ receiveHighWaterMark: 10 });
await socket.bind(this.address);
this.sockets.set(this.address, socket);
for await (const [msg] of this.sockets.get(this.address)) {
await this.emitPacket(msg);
}
}
/**
* The message to write.
*
* @param {string} address The peer address
* @param {Object} packet The packet to write to the connection.
* @api private
*/
async write(address, packet) {
if (!this.sockets.has(address)) {
const socket = new zmq.Pair({ receiveHighWaterMark: 10 });
socket.connect(address);
this.sockets.set(address, socket);
}
try {
await this.sockets.get(address).send(packet);
}
catch (e) {
this.sockets.get(address).close();
const socket = new zmq.Pair({ receiveHighWaterMark: 10 });
socket.connect(address);
this.sockets.set(address, socket);
}
}
async disconnect() {
await super.disconnect();
for (const socket of this.sockets.values()) {
socket.close();
}
}
async connect() {
this.initialize();
await super.connect();
}
}
exports.default = ZMQMokka;
//# sourceMappingURL=ZMQ.js.map