Class: Concur::EventMachineFuture2

Inherits:
Object
  • Object
show all
Includes:
Future
Defined in:
lib/futures/event_machine_future.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Future

#future?

Constructor Details

#initialize(http_data, &block) ⇒ EventMachineFuture2

Returns a new instance of EventMachineFuture2.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/futures/event_machine_future.rb', line 115

def initialize(http_data, &block)
  @http_data = http_data
  if block_given?
    @blk = block
  end

  puts 'http_data=' + http_data.inspect

  req = EventMachine::HttpRequest.new(http_data[:base_url])

  opts = {:timeout => http_data[:timeout], :head => http_data[:headers]} #, :ssl => true

  if http_data[:http_method] == :post
    http = req.post opts.merge(:path=>http_data[:path], :body=>http_data[:body])
  elsif http_data[:http_method] == :put
    http = req.put opts.merge(:path=>http_data[:path], :body=>http_data[:body])
  elsif http_data[:http_method] == :head
    http = req.head opts.merge(:path=>http_data[:path])
  elsif  http_data[:http_method] == :delete
    http = req.delete opts.merge(:path=>http_data[:path])
  else
    http = req.get opts.merge(:path=>http_data[:path], :query=>http_data[:query])
  end

  if http.error.empty?
    http.errback {
      begin
        puts 'Uh oh'
        p http.response_header.status
        p http.response_header
        p http.response
        @ex = StandardError.new("ERROR: #{http.response_header.status} #{http.response}")
      rescue => ex
        @ex = ex
      end
      self.complete
    }
    http.callback {
      begin
        puts 'success callback'
#            puts 'status=' + http.response_header.status
#            puts 'response header=' + http.response_header
        if @blk
          @result = @blk.call(HttpResponseWrapper.new(http))
        else
          @result = HttpResponseWrapper.new(http)
        end
      rescue => ex
        @ex = ex
      end
      self.complete
    }
  else
    p http.error.inspect
    @ex = StandardError.new(http.error)
    self.complete
  end


end

Instance Attribute Details

#exObject

Returns the value of attribute ex.



113
114
115
# File 'lib/futures/event_machine_future.rb', line 113

def ex
  @ex
end

#resultObject

Returns the value of attribute result.



113
114
115
# File 'lib/futures/event_machine_future.rb', line 113

def result
  @result
end

Instance Method Details

#completeObject



177
178
179
# File 'lib/futures/event_machine_future.rb', line 177

def complete
  @complete = true
end

#complete?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/futures/event_machine_future.rb', line 181

def complete?
  @complete
end

#getObject

Returns results. Will wait for thread to complete execution if not already complete.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/futures/event_machine_future.rb', line 186

def get
#      @thread.value
#      if !@complete
#        # todo: gotta be a better way
##          puts 'sleeping'
##          sleep 0.1
#        @fiber = Fiber.new do
#          while !@complete
#            sleep 0.1
#          end
#          Fiber.yield # give back control
#        end
#        @fiber.resume # start fiber
#      end
  while !@complete
    sleep 0.1
  end
  return get_response
end

#get_responseObject



206
207
208
209
210
211
# File 'lib/futures/event_machine_future.rb', line 206

def get_response
  if @ex
    raise @ex
  end
  @result
end