Class: MaxCube::Network::UDP::Client
- Inherits:
-
Object
- Object
- MaxCube::Network::UDP::Client
- Defined in:
- lib/maxcube/network/udp/client.rb
Instance Method Summary collapse
- #close ⇒ Object
- #discovery ⇒ Object
-
#initialize(port = PORT) ⇒ Client
constructor
A new instance of Client.
- #recv_msg ⇒ Object
- #send_msg(msg, addr = BROADCAST) ⇒ Object
- #send_recv_hash(hash, addr = BROADCAST) ⇒ Object
Constructor Details
#initialize(port = PORT) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 |
# File 'lib/maxcube/network/udp/client.rb', line 7 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 |
Instance Method Details
#close ⇒ Object
45 46 47 48 |
# File 'lib/maxcube/network/udp/client.rb', line 45 def close puts "\nClosing client ..." @socket.close end |
#discovery ⇒ Object
40 41 42 43 |
# File 'lib/maxcube/network/udp/client.rb', line 40 def discovery puts "Starting discovery ...\n\n" send_recv_hash(type: 'I') end |
#recv_msg ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/maxcube/network/udp/client.rb', line 20 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) ⇒ Object
16 17 18 |
# File 'lib/maxcube/network/udp/client.rb', line 16 def send_msg(msg, addr = BROADCAST) @socket.send(msg, 0, addr, @port) end |
#send_recv_hash(hash, addr = BROADCAST) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/maxcube/network/udp/client.rb', line 29 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 |