Class: Evesync::Discover

Inherits:
Object
  • Object
show all
Defined in:
lib/evesync/discover.rb

Overview

Discover other nodes Handles discovering messages sending and receiving

Example

disc = Discover.new
disc.send_discovery_message
...
disc.stop

Constant Summary collapse

DISCOVERY_REQ =
'EVESYNC'.freeze
DISCOVERY_ANS =
'DISCOVERED'.freeze

Instance Method Summary collapse

Constructor Details

#initializeDiscover

Returns a new instance of Discover.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/evesync/discover.rb', line 25

def initialize
  # Starting thread that sends and accepts UDP-packages.
  # This is how a node can say that it's online
  @evesync = IPC::Client.new(
    port: :evemond
  )
  @port = Config[:evesyncd]['broadcast_port']
  @listen_sock = UDPSocket.new
  @listen_sock.bind('0.0.0.0', @port)
  @listen_thread = Thread.new { listen_discovery }
end

Instance Method Details

#send_discovery_message(ip = '<broadcast>', message = DISCOVERY_REQ) ⇒ Object

Sending UDP message on broadcast Discovering our nodes



41
42
43
44
45
46
47
48
49
50
# File 'lib/evesync/discover.rb', line 41

def send_discovery_message(ip='<broadcast>', message=DISCOVERY_REQ)
  udp_sock = UDPSocket.new
  if is_broadcast(ip)
    udp_sock.setsockopt(
      Socket::SOL_SOCKET, Socket::SO_BROADCAST, true
    )
  end
  udp_sock.send(to_discover_msg(message: message), 0, ip, @port)
  udp_sock.close
end

#stopObject



52
53
54
# File 'lib/evesync/discover.rb', line 52

def stop
  @listen_thread.exit
end