class MaxCube::Network::TCP::SampleServer

Simple TCP server for Cube protocol testing purposes.

Public Class Methods

new(port = PORT) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 8
def initialize(port = PORT)
  @port = port
  @server = TCPServer.new(port)

  @ntp_servers = %w[nl.pool.ntp.org ntp.homematic.com]
end

Public Instance Methods

run() click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 15
def run
  puts "Starting server on port #{@port} ...\n\n"
  loop do
    Thread.start(@server.accept) do |client|
      puts "Accepting #{client.addr[3]}:#{client.addr[1]} ..."
      send_msg(client, msg_h)
      send_msg(client, msg_l)
      loop do
        run_loop(client)
      end
    end
  end
rescue Interrupt
  close
end
send_msg(client, msg) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 31
def send_msg(client, msg)
  client.puts(msg << "\r\n")
end

Private Instance Methods

close() click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 90
def close
  puts "\nClosing server ..."
  @server.close
end
cmd(client, msg) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 51
def cmd(client, msg)
  type, body = msg.split(':')
  method_str = "msg_#{type}"
  return unless respond_to?(method_str, true)
  send_msg(client, method(method_str).call(body))
end
msg_a(_body = nil) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 58
def msg_a(_body = nil)
  'A:'
end
Also aliased as: msg_t, msg_z
msg_c(_body = nil) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 65
def msg_c(_body = nil)
  'C:01b491,EQG0kQUAEg9KRVEwMzA1MjA1'
end
msg_f(body = nil) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 81
def msg_f(body = nil)
  @ntp_servers = body.split(',') if body
  'F:' + @ntp_servers.join(',')
end
msg_h(_body = nil) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 69
def msg_h(_body = nil)
  'H:KEQ0523864,097f2c,0113,00000000,477719c0,00,32,0d0c09,1404,03,0000'
end
msg_l(_body = nil) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 73
def msg_l(_body = nil)
  'L:Cw/a7QkSGBgoAMwACw/DcwkSGBgoAM8ACw/DgAkSGBgoAM4A'
end
msg_n(_body = nil) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 77
def msg_n(_body = nil)
  'N:Aw4VzExFUTAwMTUzNDD/'
end
msg_s(_body = nil) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 86
def msg_s(_body = nil)
  'S:00,0,31'
end
msg_t(_body = nil)
Alias for: msg_a
msg_z(_body = nil)
Alias for: msg_a
run_loop(client) click to toggle source
# File lib/maxcube/network/tcp/sample_server.rb, line 37
def run_loop(client)
  msgs = client.gets
  raise IOError unless msgs
  msgs.split("\r\n").each do |msg|
    raise IOError if msg == 'q:'
    puts "Income message: '#{msg}'"
    cmd(client, msg)
  end
rescue IOError
  puts "Closing #{client.addr[3]}:#{client.addr[1]} ..."
  client.close
  Thread.stop
end