Class: Dakwak::Station

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/dakwak/messaging/station.rb

Overview

The station is the interface for opening and closing communication channels.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

included, #log, #log_indent, #log_indent_dec, #log_indent_inc, #logging_context

Methods included from Silencable

#silence, #silent?

Constructor Details

#initializeStation

Returns a new instance of Station.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dakwak/messaging/station.rb', line 111

def initialize
  logging_context( "Station" )

  @channels = {}
  @config = {
    :host      => "127.0.0.1",
    :port      => 5672,
    :user      => "guest",
    :pass      => "guest",
    :vhost     => "/",
    :ssl       => false,
    :frame_max => 131072
  }

  state(:inactive)

  Configurator.subscribe("Station", self)

  # log_info "engaged"

  super()
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



17
18
19
# File 'lib/dakwak/messaging/station.rb', line 17

def channels
  @channels
end

Class Method Details

.instanceObject



12
13
14
# File 'lib/dakwak/messaging/station.rb', line 12

def instance
  Dakwak.station
end

Instance Method Details

#active?Boolean

The station is active if it has any channels currently open.

Returns:

  • (Boolean)


89
90
91
# File 'lib/dakwak/messaging/station.rb', line 89

def active?
  @state == :active
end

#connection_optionsObject



103
104
105
# File 'lib/dakwak/messaging/station.rb', line 103

def connection_options()
  @config
end

#inactive?Boolean

The station is inactive if there are no open channels.

Returns:

  • (Boolean)


94
95
96
# File 'lib/dakwak/messaging/station.rb', line 94

def inactive?
  @state == :inactive
end

#open_channel(name, opts = {}, &callback) ⇒ Object Also known as: get_channel

Opens a channel identified by ‘name’ if it’s not already open.

This method is asynchronous.

Aliases: get_channel



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dakwak/messaging/station.rb', line 24

def open_channel(name, opts = {}, &callback) # :yields: channel

  if shutting_down?
    log_warn "Station is shutting down, channels will not be opened."
    return nil
  end

  if @channels[name]
    callback.call(@channels[name]) if callback
    return @channels[name]
  end

  c = Channel.new(name)
  @channels[name] = c
  c.open(opts) { |c|
    state(:active)

    log_debug "Channel #{c.name} is now open"
    callback.call(c) if callback
  }

  return c
end

#set_option(k, v) ⇒ Object



107
108
109
# File 'lib/dakwak/messaging/station.rb', line 107

def set_option(k, v)
  @config[k.to_sym] = v
end

#shutdown(&callback) ⇒ Object

Turns off the station and all open channels. A callback must be provided that will be called when the station is shut down.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dakwak/messaging/station.rb', line 52

def shutdown(&callback)
  raise RuntimeError.new "Station::shutdown() must be called with a callback block" unless callback

  if inactive?
    log_warn "Rejecting shutdown request; the station is currently inactive."
    callback.call
    return nil
  end

  if shutting_down?
    log_warn "Rejecting shutdown request; the station is already shutting down!"
    callback.call
    return nil
  end

  state(:shutting_down)

  # register the callback
  @on_shutdown = callback

  log_info "Closing #{@channels.size} open channels..."

  channels_closed = 0
  @channels.each_pair { |name, c|
    c.close {
      channels_closed += 1
      if channels_closed == @channels.size then
        state(:inactive)

        log_info "All channels are cleanly closed. Shutdown complete."
        @on_shutdown.call
      end
    }
  }
end

#shutting_down?Boolean

When shutting down, it is not permitted to attempt to open any channels.

Returns:

  • (Boolean)


99
100
101
# File 'lib/dakwak/messaging/station.rb', line 99

def shutting_down?
  @state == :shutting_down
end