Class: HomeQ::CP::Server

Inherits:
Object
  • Object
show all
Includes:
Base::Configuration, Base::Logging
Defined in:
lib/homeq/cp/server.rb

Constant Summary collapse

DEFAULT_PORT =
56001
DEFAULT_HOST =
'localhost'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::Configuration

#config

Methods included from Base::Logging

#logger

Constructor Details

#initialize(host = nil, port = nil) ⇒ Server

Returns a new instance of Server.



47
48
49
50
51
# File 'lib/homeq/cp/server.rb', line 47

def initialize(host=nil, port=nil)
  @host = host || DEFAULT_HOST
  @port = port || DEFAULT_PORT
  @connections = []
end

Instance Attribute Details

#connectionsObject

Returns the value of attribute connections.



39
40
41
# File 'lib/homeq/cp/server.rb', line 39

def connections
  @connections
end

Instance Method Details

#startObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/homeq/cp/server.rb', line 53

def start
  begin
    @signature = EventMachine.start_server(@host,
                                           @port,
                                           CP::Connection) { |conn|
      conn.server = self
    }
  rescue Exception => e
    msg = "Error starting CP listener #{@host}:#{@port}. " +
      "Likely it's a config error. " +
      "Original error: #{e.message}"
    Base::System.instance.die(msg)
  else
    logger.info {
      "Control port running on #{@host}:#{@port}"
    }
  end
end

#stopObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/homeq/cp/server.rb', line 72

def stop
  EventMachine.stop_server(@signature)
  unless wait_for_connections
    # Still some running
    EventMachine.add_periodic_timer(1) {
      logger.info {
        '.'
      }
      wait_for_connections
    }
  end
end