class MaxCube::Network::UDP::SampleSocket

Simple UDP 'server' for Cube protocol testing purposes.

Public Class Methods

new(port = PORT) click to toggle source
# 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

Public Instance Methods

run() click to toggle source
# 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) click to toggle source
# File lib/maxcube/network/udp/sample_socket.rb, line 30
def send_msg(msg, addr, port)
  @socket.send(msg, 0, addr, port)
end

Private Instance Methods

close() click to toggle source
# File lib/maxcube/network/udp/sample_socket.rb, line 59
def close
  puts "\nClosing socket ..."
  @socket.close
end
cmd(msg, addr, port) click to toggle source
# File lib/maxcube/network/udp/sample_socket.rb, line 36
def cmd(msg, addr, port)
  type = @serializer.check_msg_msg_type(msg)
  puts "Message type: #{type}"

  method_str = "msg_#{type.downcase}"
  return unless respond_to?(method_str, true)
  send_msg(send(method_str), addr, port)
end
msg_h() click to toggle source
# File lib/maxcube/network/udp/sample_socket.rb, line 55
def msg_h
  "eQ3MaxApKEQ0565026>h\x00Pmax.eq-3.de,/cube"
end
msg_i() click to toggle source
# File lib/maxcube/network/udp/sample_socket.rb, line 45
def msg_i
  "eQ3MaxApKEQ0523864>I\x00\x09\x7f\x2c\x01\x13"
end
msg_n() click to toggle source
# File lib/maxcube/network/udp/sample_socket.rb, line 49
def msg_n
  'eQ3MaxApKEQ0565026>N' \
  "\xc0\xa8\x00\xde\xc0\xa8\x00\x01\xff\xff\x00\x00" \
  "\xc0\xa8\x00\x01\xc0\xa8\x00\x01"
end