How to block an erlang process until a network connection becomes available
I am setting up a sort of syncing mechanism between two different erlang nodes (on 2 different machines or devices). One of these devices will be disconnected from the network to accomplish another task.
When the devi开发者_JAVA技巧ce is reconnected to the network, it needs to contact the other erlang node to initiate data syncing.
Is there a way in erlang to receive a message when the network connection is available? Or would I have to do some sort of polling.
Thanks, Brad
You can subscribe to node connection changes through net_kernel:monitor_nodes/1
. This will send you {nodeup, Node}
and {nodedown, Node}
messages you can use to drive your own sync logic.
You would probably want a process that regularly runs net_adm:ping(TargetNode)
to try to connect to the other node so that would would then get the {nodeup, TargetNode}
messages. This would avoid needing to explicitly check OS network availability via polling (the undocumented) inet:getiflist/0
for changes.
精彩评论