This multifile hook is called if an UDP package is received on the
port of the unicast network identified by Scope. From is the origin
IP and port and Data is the message data that is deserialized as
defined for the scope (see udp_term_string/3).
This hook is intended to initiate a new node joining the network of
peers. We could in theory also omit the in-scope test and use a
normal broadcast to join. Using a different channal however provides
a basic level of security. A possibe implementation is below. The
first fragment is a hook added to the server, the second is a
predicate added to a client and the last initiates the request in
the client. The excanged term (join(X)
) can be used to exchange a
welcome handshake.
:- multifile udp_broadcast:udp_unicast_join_hook/3.
udp_broadcast:udp_unicast_join_hook(Scope, From, join(welcome)) :-
udp_peer_add(Scope, From),
join_request(Scope, Address, Reply) :-
udp_peer_add(Scope, Address),
broadcast_request(udp(Scope, join(X))).
?- join_request(myscope, "1.2.3.4":10001, Reply).
Reply = welcome.