Class: SmartfoxJruby::SfsWorker::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/smartfox_jruby/sfs_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(smart_fox, opts = {}) ⇒ Worker

Returns a new instance of Worker.



114
115
116
117
118
119
120
121
122
123
# File 'lib/smartfox_jruby/sfs_worker.rb', line 114

def initialize(smart_fox, opts = {})
  @send_queue = []
  @processors = []
  @events_queue = []
  @opts = opts
  @opts[:timeout] ||= 20
  @smart_fox = smart_fox
  @mutex = Mutex.new
  @send_qm = Mutex.new
end

Instance Attribute Details

#events_queueObject (readonly)

Returns the value of attribute events_queue.



109
110
111
# File 'lib/smartfox_jruby/sfs_worker.rb', line 109

def events_queue
  @events_queue
end

#optsObject (readonly)

Returns the value of attribute opts.



112
113
114
# File 'lib/smartfox_jruby/sfs_worker.rb', line 112

def opts
  @opts
end

#processorsObject (readonly)

Returns the value of attribute processors.



110
111
112
# File 'lib/smartfox_jruby/sfs_worker.rb', line 110

def processors
  @processors
end

#send_queueObject (readonly)

Returns the value of attribute send_queue.



108
109
110
# File 'lib/smartfox_jruby/sfs_worker.rb', line 108

def send_queue
  @send_queue
end

#smart_foxObject (readonly)

Returns the value of attribute smart_fox.



111
112
113
# File 'lib/smartfox_jruby/sfs_worker.rb', line 111

def smart_fox
  @smart_fox
end

Instance Method Details

#all_events_caught?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/smartfox_jruby/sfs_worker.rb', line 187

def all_events_caught?
  processors.blank? || processors.collect { |p| p.mandatory? }.blank?
end

#append_processor(opts = {}, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/smartfox_jruby/sfs_worker.rb', line 125

def append_processor(opts = {}, &block)
  debug "appending processor with context #{opts[:context]}"
  if opts[:context]
    context = opts[:context].append(&block)
  else
    context = processors.last.append(&block)
  end
  ContextWorker.new(context, self)
end

#expect(name, opts = {}, &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/smartfox_jruby/sfs_worker.rb', line 152

def expect(name, opts = {}, &block)
  debug "Expecting to get response #{name} with context #{opts[:context]}"
  unless opts[:context].blank?
    if opts[:context].is_a?(Processor)
      context = opts[:context].chain(name, &block)
    elsif opts[:context].is_a?(Request)
      context = Processor.new(name, :context => opts[:context], &block)
      @mutex.synchronize { processors << context }
    end
  else
    context = Processor.new(name, &block)
    @mutex.synchronize { processors << context }
  end
  ContextWorker.new(context, self)
end

#perform!Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/smartfox_jruby/sfs_worker.rb', line 173

def perform!
  while !all_events_caught?
    while !send_queue.blank?
      req = nil
      @send_qm.synchronize { req = send_queue.shift }
      debug "sending request #{req.name}..."
      smart_fox.send(req.to_extension_request) unless smart_fox.blank?
      @last_act = Time.now.to_i
    end
    process_events
    check_timeouts
  end
end

#request(name, data = {}, opts = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/smartfox_jruby/sfs_worker.rb', line 135

def request(name, data = {}, opts = {})
  debug "create request #{name} with context #{opts[:context]} and opts #{opts[:serialize_opts]}"
  data = data.to_sfsobject(opts[:serialize_opts]) if data.is_a?(Hash)
  req = Request.new(name, data)
  if !opts[:context].blank? && opts[:context].is_a?(Processor)
    debug "appending #{req} to processor #{opts[:context]}"
    context = opts[:context].append {
      @send_qm.synchronize { send_queue << req }
    }
  else
    debug "adding #{req} to send_queue \n #{dump_state}"
    context = req
    @send_qm.synchronize { send_queue << req }
  end
  ContextWorker.new(context, self)
end

#response(name, data = {}) ⇒ Object



168
169
170
171
# File 'lib/smartfox_jruby/sfs_worker.rb', line 168

def response(name, data = {})
  info "Got response #{name} (#{data.to_json})..."
  @mutex.synchronize { events_queue << Response.new(name, data) }
end

#wait_all_events_caughtObject



191
192
193
194
195
196
197
198
# File 'lib/smartfox_jruby/sfs_worker.rb', line 191

def wait_all_events_caught
  debug "Waiting all events being caught..."
  begin
    wait_with_timeout { all_events_caught? }
  rescue WaitTimeoutException => e
    raise "Failed to catch all the events:"+ dump_state
  end
end