nodecommander 0.1.0 copy "nodecommander: ^0.1.0" to clipboard
nodecommander: ^0.1.0 copied to clipboard

Network nodes communication. Send commands to nodes and get the responses

Node Commander #

Network nodes communication in Dart. Send commands to nodes and get the responses. Powered by Isohttpd

Usage #

Generate a key: dart bin/main.dart

Create some commands #

import 'package:nodecommander/nodecommander.dart';

const key = "7x70J-5n0AZptWlBLVMfknlRiOtALH-6dUT16tfCHCA=";

final List<NodeCommand> commands = <NodeCommand>[sayHello];

final NodeCommand sayHello = NodeCommand.define(
   name: "hello",
   key: key,
   /// The code executed on the soldier
   executor: (cmd) async {
     print("Saying hello to ${cmd.from}");
     return cmd.copyWithPayload(<String, dynamic>{"response": "hello"});
   },
   /// Optional: the code executed on the commander after
   /// the response is received from the soldier
   responseProcessor: (cmd) async =>
       print(cmd.payload["response"].toString()));

Run a soldier node #

import 'dart:async';
import 'package:nodecommander/nodecommander.dart';
import 'commands.dart' as cmds;

Future<void> main() async {
  final node = SoldierNode(
      name: "command_line_node_1", 
      key: key, 
      commands: cmds.commands, 
      verbose: true);
  // initialize the node
  await node.init();
  // print some info about the node
  node.info();
  // idle
  await Completer<void>().future;
}

Run a commander node #

Declare and initialize the node:

final node = CommanderNode(
    name: "commander",
    key: key, 
    commands: cmds.commands, 
    port: 8085, 
    verbose: true);
// initialize the node
await node.init();   
// print some info about the node
node.info();
// Wait for the node to be ready to operate
await node.onReady;

Discover soldier nodes on the network with udp broadcast:

unawaited(node.discoverNodes());
await Future<dynamic>.delayed(const Duration(seconds: 2));
for (final s in node.soldiers) {
  print("Soldier ${s.name} at ${s.address}");
}

Run commands on soldier nodes:

final String to = node.soldierUri("command_line_node_1");
// ping
await node.sendCommand(NodeCommand.ping(), to);
// send a command
await node.sendCommand(sayHello, to);
// or
await node.sendCommand(node.cmd("hello"), to);

Listen to command responses from soldiers:

node.commandsResponse.listen((NodeCommand cmd) {
   print("${cmd.from} has responded: ${cmd.payload}");
}

This is optional. The command responses are received after the response_processor is run if present

Create a cli with custom commands #

To run the cli:

import 'package:nodecommander/nodecommander.dart';

void main() {
  final node = CommanderNode(
      key: key,
      name: "cli",
      port: 8185,
      commands: <NodeCommand>[sayHello],
      verbose: true);
  NodeCommanderCli(node).run();
}
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Network nodes communication. Send commands to nodes and get the responses

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

corsac_jwt, dio, emodebug, err, extra_pedantic, isohttpd, meta, pedantic, prompts, uuid

More

Packages that depend on nodecommander