Class: MaxCube::Network::TCP::SampleServer

Inherits:
Object
  • Object
show all
Defined in:
lib/maxcube/network/tcp/sample_server.rb

Overview

Simple TCP server for Cube protocol testing purposes.

Instance Method Summary collapse

Constructor Details

#initialize(port = PORT) ⇒ SampleServer

Returns a new instance of SampleServer



8
9
10
11
12
13
# 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

Instance Method Details

#closeObject (private)



90
91
92
93
# File 'lib/maxcube/network/tcp/sample_server.rb', line 90

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

#cmd(client, msg) ⇒ Object (private)



51
52
53
54
55
56
# 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) ⇒ Object (private) Also known as: msg_t, msg_z



58
59
60
# File 'lib/maxcube/network/tcp/sample_server.rb', line 58

def msg_a(_body = nil)
  'A:'
end

#msg_c(_body = nil) ⇒ Object (private)



65
66
67
# File 'lib/maxcube/network/tcp/sample_server.rb', line 65

def msg_c(_body = nil)
  'C:01b491,EQG0kQUAEg9KRVEwMzA1MjA1'
end

#msg_f(body = nil) ⇒ Object (private)



81
82
83
84
# 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) ⇒ Object (private)



69
70
71
# 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) ⇒ Object (private)



73
74
75
# 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) ⇒ Object (private)



77
78
79
# File 'lib/maxcube/network/tcp/sample_server.rb', line 77

def msg_n(_body = nil)
  'N:Aw4VzExFUTAwMTUzNDD/'
end

#msg_s(_body = nil) ⇒ Object (private)



86
87
88
# File 'lib/maxcube/network/tcp/sample_server.rb', line 86

def msg_s(_body = nil)
  'S:00,0,31'
end

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

#run_loop(client) ⇒ Object (private)



37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

#send_msg(client, msg) ⇒ Object



31
32
33
# File 'lib/maxcube/network/tcp/sample_server.rb', line 31

def send_msg(client, msg)
  client.puts(msg << "\r\n")
end