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

#closeObject (private)



59
60
61
62
# File 'lib/maxcube/network/udp/sample_socket.rb', line 59

def close
  puts "\nClosing socket ..."
  @socket.close
end

#cmd(msg, addr, port) ⇒ Object (private)



36
37
38
39
40
41
42
43
# 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_hObject (private)



55
56
57
# File 'lib/maxcube/network/udp/sample_socket.rb', line 55

def msg_h
  "eQ3MaxApKEQ0565026>h\x00Pmax.eq-3.de,/cube"
end

#msg_iObject (private)



45
46
47
# File 'lib/maxcube/network/udp/sample_socket.rb', line 45

def msg_i
  "eQ3MaxApKEQ0523864>I\x00\x09\x7f\x2c\x01\x13"
end

#msg_nObject (private)



49
50
51
52
53
# 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

#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