Class: MaxCube::Network::UDP::SampleSocket
- Inherits:
-
Object
- Object
- MaxCube::Network::UDP::SampleSocket
- Defined in:
- lib/maxcube/network/udp/sample_socket.rb
Overview
Simple UDP 'server' for Cube protocol testing purposes.
Instance Method Summary collapse
- #close ⇒ Object private
- #cmd(msg, addr, port) ⇒ Object private
-
#initialize(port = PORT) ⇒ SampleSocket
constructor
A new instance of SampleSocket.
- #msg_h ⇒ Object private
- #msg_i ⇒ Object private
- #msg_n ⇒ Object private
- #run ⇒ Object
- #send_msg(msg, addr, port) ⇒ Object
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
#close ⇒ Object (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_h ⇒ Object (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_i ⇒ Object (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_n ⇒ Object (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 |
#run ⇒ Object
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 |