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

#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

#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