View Source NetworkMonitor (NetworkMonitor v1.1.4)

NetworkMonitor watches network interfaces and emits events to listeners when interfaces appear or vanish.

Example

{:ok, port} = :gen_tcp.connect('www.google.com', 80)
NetworkMonitor.close_on_down(port)
# now when the network cable is unplugged the port
# is automatically closed

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Wrapper around on_down_apply executes the :inet.close(socket) function call on the given socket when the sockets associated local interface goes down.

Returns a MapSet of the currently up interfaces

Execute the given {module, function, arguments} (mfa) tuple when the ip-address given in with addr is going down. addr should be a member of interfaces() or the mfa will be executed immediately.

Subscribe to interface events. The given pid or self() by default will receive these two signals on interface changes

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

close_on_down(socket, module \\ :inet)

View Source

Wrapper around on_down_apply executes the :inet.close(socket) function call on the given socket when the sockets associated local interface goes down.

The sockets associated local interface is retrieved using :inet.sockname(socket)

Instead of :inet another module name can be specified such as :ssl

Returns a MapSet of the currently up interfaces

Link to this function

on_down_apply(addr, m, f, a)

View Source

Execute the given {module, function, arguments} (mfa) tuple when the ip-address given in with addr is going down. addr should be a member of interfaces() or the mfa will be executed immediately.

Link to this function

subscribe(pid \\ self())

View Source

Subscribe to interface events. The given pid or self() by default will receive these two signals on interface changes:

  • {:interface_down, [ifs]}
  • {:interface_up, [ifs]}

Where ifs is a list of ip address tuples. So for example: {192, 168, 0, 1} for IPv4 or {0, 0, 0, 0, 0, 0, 0, 1} for IPv6