Automatic and decentralized discovery and monitoring library in C. Built in support for a variable number of master processes, service advertising and channel messaging.
This repository is not a fork of discover ! It has the same behavior but it is a complete library written in C in order to be portable and used in various applications. The goal of this library is to be fully compatible with discover Node.js version and permit discovery, monitoring and communications between applications written in C and Javascript.
- automatic and decentralized discovery
- monitoring of the other processes
- service advertising
- channel messaging
Build libdiscover.so
with the following commands:
mkdir build
cd build
cmake ..
make
Install libdiscover.so
with the following commands:
make install
This library is compatible with discover release 1.2.0.
Build examples with the following commands:
mkdir build
cd build
cmake -DENABLE_DISCOVER_EXAMPLES=ON .
make
Show how to instanciate and configure a new discover instance in an application. Subscription to events (node added, removed, promotion, demotion...) and usage of channels to send messages.
Show how to use Multicast or Unicast instead of Broadcast (the default).
Performances have not been evaluated yet.
This goal of this library is to provide a C implementation of the original discover Node.js version. This allow a communication between processes written using different languages.
Create a new discover instance.
Set discover instance option
to the wanted value
which is passed by address. The following table shows the available options and their default value. Options must be set before starting the instance.
Option | Type | Default |
---|---|---|
helloInterval | int | 1000ms |
checkInterval | int | 2000ms |
nodeTimeout | int | 2000ms |
masterTimeout | int | 2000ms |
address | char * | "0.0.0.0" |
port | uint16_t | 12345 |
broadcast | char * | "255.255.255.255" |
multicast | char * | NULL |
multicastTTL | unsigned char | 1 |
unicast | char * | NULL |
key | char * | NULL |
mastersRequired | int | 1 |
weight | double | Computed on startup |
client | bool | false |
reuseAddr | bool | true |
ignoreProcess | bool | false |
ignoreInstance | bool | false |
advertisement | cJSON * | NULL |
hostname | char * | Retrieved on startup |
❗ The key can't be used today. Encryption of data is not available. First because I have not found any simple and satisfying library to do it, and then because the Cipher initialization used in discover Node.js version is currently deprecated. |
---|
Start the discover instance.
Register a callback fct
on the event topic
. An optionnal user
argument is available. The following table shows the available topics and their callback prototype.
Topic | Callback | Description |
---|---|---|
helloReceived | void *(*fct)(struct discover_s *, discover_node_t *, void *) | Called when hello message is received |
helloEmitted | void *(*fct)(struct discover_s *, void *) | Called when hello message is emitted |
promotion | void *(*fct)(struct discover_s *, void *) | Called when the instance is promoted |
demotion | void *(*fct)(struct discover_s *, void *) | Called when the instance is demoted |
check | void *(*fct)(struct discover_s *, void *) | Called when the check function is executed |
added | void *(*fct)(struct discover_s *, discover_node_t *, void *) | Called when a node is discovered |
master | void *(*fct)(struct discover_s *, discover_node_t *, void *) | Called when a node is promoted |
removed | void *(*fct)(struct discover_s *, discover_node_t *, void *) | Called when a node has disappeared |
error | void *(*fct)(struct discover_s *, char *, void *) | Called when an error occured |
Set advertisement
object. Can be used after starting the instance to update the advertisement content.
Promote my own instance.
Demote my own instance. If permanent
is true, prevent the instance to promote itself automatically.
Register a callback fct
on the channel event
. An optionnal user
argument is available.
Unregister to the event
.
Send data
to the channel event
.
Release internal memory and stop discover instance. All sockets are closed. Must be called to free ressources.
MIT