class MaxCube::Network::UDP::Client

Class that provides yet only basic UDP communication with Cube devices.

Public Class Methods

new(port = PORT) click to toggle source
# File lib/maxcube/network/udp/client.rb, line 9
def initialize(port = PORT)
  @socket = UDPSocket.new
  @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
  @port = port

  @parser = Messages::UDP::Parser.new
  @serializer = Messages::UDP::Serializer.new
end

Public Instance Methods

close() click to toggle source
# File lib/maxcube/network/udp/client.rb, line 47
def close
  puts "\nClosing client ..."
  @socket.close
end
discovery() click to toggle source
# File lib/maxcube/network/udp/client.rb, line 42
def discovery
  puts "Starting discovery ...\n\n"
  send_recv_hash(type: 'I')
end
recv_msg() click to toggle source
# File lib/maxcube/network/udp/client.rb, line 22
def recv_msg
  msg, addr = @socket.recvfrom(1024)
  port = addr[1]
  ipaddr = addr[3]
  [msg, ipaddr, port]
rescue Interrupt
  puts 'Aborted'
end
send_msg(msg, addr = BROADCAST) click to toggle source
# File lib/maxcube/network/udp/client.rb, line 18
def send_msg(msg, addr = BROADCAST)
  @socket.send(msg, 0, addr, @port)
end
send_recv_hash(hash, addr = BROADCAST) click to toggle source
# File lib/maxcube/network/udp/client.rb, line 31
def send_recv_hash(hash, addr = BROADCAST)
  msg = @serializer.serialize_udp_hash(hash)
  send_msg(msg, addr)
  msg, addr, port = recv_msg
  return nil unless msg
  hash = @parser.parse_udp_msg(msg)
  puts "'#{hash[:type]}' response from #{addr}:#{port}:\n" \
       "#{hash.to_yaml}\n"
  hash
end