Skip to content

Latest commit

 

History

History
283 lines (154 loc) · 6.99 KB

File metadata and controls

283 lines (154 loc) · 6.99 KB

Module rbeacon

The rbeacon module implements a peer-to-peer discovery service for local networks. Behaviours: gen_server.

Description

A beacon can broadcast and/or capture service announcements using UDP messages on the local area network. This implementation uses IPv4 UDP broadcasts. You can define the format of your outgoing beacons, and set a filter that validates incoming beacons. Beacons are sent and received asynchronously in the background.

Data Types


beacon() = pid()

beacon_opts() = [active | {active, true | false | once | integer()} | {interval, integer()} | noecho | {noecho, true | false}]

Function Index

broadcast_ip/1Return our own Broadcast IP address as printable string.
close/1close a beacon Close a beacon.
control/2Assigns a new controlling process Pid to beacon The controlling process is the process which receives messages from the beacon.
hostname/1Return our own IP address as printable string.
new/1Create a new beacon on a certain UDP port.
new/2Create a new beacon on a certain UDP port with options.
noecho/1 Filter out any beacon that looks exactly like ours.
publish/2Start broadcasting beacon to peers at the specified interval.
recv/1wait to receive a beacon.
recv/2wait to receive a beacon with timeout.
set_interval/2Set broadcast interval in milliseconds (default is 1000 msec).
setopts/2set beacon options.
silence/1Stop broadcasting beacons.
subscribe/2Start listening to other peers; zero-sized filter means get everything All messages received by the peer will be then sent to the process owner.
unsubscribe/1Stop listening to other peers.

Function Details

broadcast_ip/1


broadcast_ip(Ref::beacon()) -> string()



Return our own Broadcast IP address as printable string

close/1


close(Ref::beacon()) -> ok



close a beacon Close a beacon.

control/2


control(Ref::beacon(), Pid::pid()) -> ok | {error, not_owner}



Assigns a new controlling process Pid to beacon The controlling process is the process which receives messages from the beacon. If called by any other process than the current controlling process, {error, not_owner} is returned.

hostname/1


hostname(Ref::beacon()) -> string()



Return our own IP address as printable string

new/1


new(Port::integer()) -> {ok, beacon()} | {error, term()}



Create a new beacon on a certain UDP port.

new/2


new(Port::integer(), Options::beacon_opts()) -> {ok, beacon()} | {error, term()}



Create a new beacon on a certain UDP port with options.

noecho/1


noecho(Ref::beacon()) -> ok



Filter out any beacon that looks exactly like ours

publish/2


publish(Ref::beacon(), Msg::binary()) -> ok | {error, term()}



Start broadcasting beacon to peers at the specified interval

recv/1


recv(Ref::beacon()) -> {ok, Addr::string(), Msg::binary()} | {error, term()}



wait to receive a beacon

recv/2


recv(Ref::beacon(), Timeout::integer() | infinity) -> {ok, Addr::string(), Msg::binary()} | {error, term()}



wait to receive a beacon with timeout. Timeout can be an integer in milliseconds or infinity.

set_interval/2


set_interval(Ref::beacon(), Interval::integer()) -> ok | {error, term()}



Set broadcast interval in milliseconds (default is 1000 msec)

setopts/2


setopts(Ref::beacon(), Options::beacon_opts()) -> ok



set beacon options. Valid options are:

  • active, {active, true | false | once| N}: like inet:setopts/1

  • {interval, N}: Set broadcast interval in milliseconds

  • noecho | {noecho, true | false}: Filter out any beacon that looks exactly like ours.

silence/1


silence(Ref::beacon()) -> ok



Stop broadcasting beacons

subscribe/2


subscribe(Ref::beacon(), Filter::binary() | string()) -> ok



Start listening to other peers; zero-sized filter means get everything All messages received by the peer will be then sent to the process owner. Incoming messages are:

  • {rbeacon, Beacon, Message, SenderIp} : when a subscription is received
  • {rbeacon, Beacon, closed} : when a beacon is closed
  • {'EXIT', rbeacon, Beacon, Reason}: when a beacon exit anormaly

unsubscribe/1


unsubscribe(Ref::beacon()) -> ok



Stop listening to other peers