Simple 2nd layer Byteball chat bot communication protocol.
There are two roles: a coordinator and a worker.
Coordinator
The coordinator is a byteball chatbot that manages the group of worker bots and provides an interface to the user to communicate with the cluster.
const eventBus = require('byteballcore/event_bus.js');
const headlessWallet = require('headless-byteball');
const cluster = require("byteball-cluster");
require('byteballcore/wallet.js');
const coordinator = cluster.Coordinator;
function sayHello() {
coordinator.sendAll({
method: "hello",
name: "Coordinator"
}, (err, response) => {
if (err) return console.error(err.message);
console.log(response.result);
});
}
eventBus.on('text', coordinator.listen);
eventBus.once("headless_wallet_ready", function() {
setTimeout(sayHello, 10000); // let workers join, then say hello
}
Worker
The worker is a byteball chat bot that performs a task instructed by the coordinator. The worker uses the pairing code of the coordinator to join the cluster.
const eventBus = require("byteballcore/event_bus.js");
const headlessWallet = require("headless-byteball");
const config = require("byteballcore/conf.js");
const cluster = require("byteball-cluster");
require("byteballcore/wallet.js");
const worker = cluster.Worker;
eventBus.once("headless_wallet_ready", function() {
worker.join(config.coordinatorPairingCode, (err, coordinator) => {
if (err) return Error(err);
console.error("Joined computing cluster " + coordinator);
});
});
eventBus.on("text", worker.listen);
worker.on("hello", (coordinator, message, callback) => {
callback(null, {
result: "Hello " + message.name
});
});