Class: Smartware::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/smartware/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Service

Returns a new instance of Service.



5
6
7
8
9
10
# File 'lib/smartware/service.rb', line 5

def initialize(config_file)
  @config = YAML.load File.read(config_file)
  @interfaces = []
  @devices = []
  @shutdown_holders = 0
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/smartware/service.rb', line 3

def config
  @config
end

Instance Method Details

#publish_event(key, *args) ⇒ Object



56
57
58
# File 'lib/smartware/service.rb', line 56

def publish_event(key, *args)
  @event_channel.push [key, args]
end

#publish_reliable_event(key, *args) ⇒ Object



60
61
62
# File 'lib/smartware/service.rb', line 60

def publish_reliable_event(key, *args)
  @reliable_event_channel.push [key, args]
end

#startObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/smartware/service.rb', line 12

def start
  EventMachine.epoll
  EventMachine.run do
    @event_channel = EventMachine::Channel.new
    @event_channel.subscribe do |(key, args)|
      @pubsub.publish_event key, *args
    end

    @reliable_event_channel = EventMachine::Channel.new
    @reliable_event_channel.subscribe do |(key, args)|
      @pubsub.publish_reliable_event key, *args
    end

    @pubsub = PubSubServer.new "localhost", (@config["pubsub_port"] || 6100)
    @pubsub.repush = ->(connection) do
      @devices.each do |device|
        device.repush_events connection
      end
    end

    @config["interfaces"].each do |config|
      interface = Smartware::Interface.const_get(config['name']).new config, self
      @devices << interface

      if config.include? "uri"
        @interfaces << DRb::DRbServer.new(config["uri"], interface)
      end
    end

    unless @config["connection_timeout"].nil?
      Thread.new do
        begin
          monitor = ConnectionMonitor.new @config["connection_timeout"].to_i

          monitor.run
        rescue => e
          Smartware::Logging.logger.error "Exception in connection monitor thread: #{e}"
          e.backtrace.each { |line| Smartware::Logging.logger.error line }
        end
      end
    end
  end
end

#stopObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/smartware/service.rb', line 64

def stop
  @interfaces.each &:stop_service

  callback = method :decr_shutdown_holders

  @devices.each do |interface|
    if interface.shutdown callback
      @shutdown_holders += 1
    end
  end

  EventMachine.stop if @shutdown_holders == 0
end