velo-comm-package
v1.2.0
Published
A communication package for the velo autonomus project
Downloads
1
Readme
velo autonomus: node-comm-package
The NodeJS implementation of the internal module communication system of velo autonomus
Install
npm install velo-comm-package
Basics
The system is modular and decentralized. All the modules organize themselves using UDP-Multicasts. Direct communication is with TCP-Sockets (or Shared Memory (SHM), not implemented yet).
Every module uses the comm-package for the specific programming language. There you first create a new VeloModule object, which represents the module you're building. Here you set all the important information such as Module-ID, version and so on (more at Reference -> VeloModule -> constructor).
Using this VeloModule instance, you can create direct Connections to other modules over TCP (or SHM). Important note: To be able to handle incoming connections, you have to start a tcp server yourself.
Examples
Create a simple module:
const velo = require('velo-comm-package');
var testModule = new velo.VeloModule({
id: 'TEST',
name: 'Test module',
allowsTCP: true,
allowsSHM: false
});
Set the status:
testModule.setStatus(0); //OFFLINE
testModule.setStatus(1); //ONLINE
testModule.setStatus(2); //ERROR
Build a direct connection:
var testConnection = testModule.connectToModule('ANOTHER-MODULE');
testConnection.events.on('connect', () => {
console.log('Connected to ANOTHER-MODULE');
});
testConnection.events.on('message', (msg) => {
console.log('Message from ANOTHER-MODULE:', msg);
});
testConnection.events.on('error', (err) => {
console.error(err);
})
testConnection.events.on('close', () => {
console.log('Connection to ANOTHER-MODULE closed');
});
Create a TCP-Socket-Server:
var net = require('net');
var connectedSockets = []; //To send data to sockets from other places in the code
var server = net.createServer((socket) => {
connectedSockets.push(socket);
//Probably terrible error-handling here
socket.on('data', (data) => {
data = data.toString();
console.log(data, data.length);
});
socket.on('end', () => {
connectedSockets.splice(connectedSockets.indexOf(socket), 1);
});
socket.on('error', () => {
connectedSockets.splice(connectedSockets.indexOf(socket), 1);
});
});
server.listen(() => {
console.log(`TCP-Server listening on port: ${server.address().port}`));
testModule.setStatus(1);
testModule.tcpPort = server.address().port; //IMPORTANT! Needed for other modules to connect to yours
});
Reference
VeloModule
constructor(info?: ModuleInfo)
info (optional): The information about the module:
- id: string; The modules ID
- allowsSHM: boolean (default: false); Does the module accept incoming SHM-Connections?
- allowsTCP: boolean (default: true); Does the module accept incoming TCP-Connections?
- cluster: string; The modules cluster/machine name
- name: string; A well thats well readable
- version: string (default: '1.0'); The modules version
setStatus(status: number): void
Sets the status of the module.
status: The status you want to set as a number
| number | status | | -----: | ------ | | 0 | Offline | | 1 | Online | | 2 | Error |
connectToModule(id: string): DirectConnection
Builds a connection to a specified module and returns a DirectoConnection object. The kind of connection (TCP or SHM) is automatically chosen and it will chose SHM if possible, since it's faster.
id: The id of the module you want to connect to
DirectConnection
disconnect(): void
Ends the connection
send(message: any): void
Sends a message of any type.
on(event: string, eventHandler: Function)
Subscribes to an event like it's done with an event handler. You can also access the event handler directly using the event
property.
Supported events are:
connect
: Emitted, when the connection is built upmessage
: Emitted, when a new message is received; message is passed as stringclose
: Emitted, when the connection is closederror
: Emitted, when an error occurs; error is passed
Known issues
UDP Packets sent by Raspberry Pi aren't receiced on Windows
If the Raspberry Pi and the windows machine are on the same network, and Raspi receives packets from the windows machine but not the other way around, it's typically caused by the windows firewall. Deactivating the firewall should solve the problem.