Class: MaxCube::Network::UDP::SampleSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/maxcube/network/udp/sample_socket.rb

Overview

Simple UDP ‘server’ for Cube protocol testing purposes.

Instance Method Summary collapse

Constructor Details

#initialize(port = PORT) ⇒ SampleSocket

Returns a new instance of SampleSocket.



8
9
10
11
12
13
14
15
# File 'lib/maxcube/network/udp/sample_socket.rb', line 8

def initialize(port = PORT)
  @port = port
  @socket = UDPSocket.new
  @socket.bind('0.0.0.0', port)

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

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/maxcube/network/udp/sample_socket.rb', line 17

def run
  puts "Starting socket on port #{@port} ...\n\n"
  loop do
    msg, addr = @socket.recvfrom(1024)
    port = addr[1]
    ipaddr = addr[3]
    puts "Income message from #{ipaddr}:#{port}: '#{msg}'"
    cmd(msg, ipaddr, port) if @serializer.valid_udp_msg(msg)
  end
rescue Interrupt
  close
end

#send_msg(msg, addr, port) ⇒ Object



30
31
32
# File 'lib/maxcube/network/udp/sample_socket.rb', line 30

def send_msg(msg, addr, port)
  @socket.send(msg, 0, addr, port)
end