Class: Combi::WebSocket

Inherits:
Bus
  • Object
show all
Defined in:
lib/combi/buses/web_socket.rb

Defined Under Namespace

Classes: Client, Server

Constant Summary

Constants inherited from Bus

Bus::RPC_DEFAULT_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Bus

#services

Instance Method Summary collapse

Methods inherited from Bus

#add_service, #enable, #log, #restart!

Constructor Details

#initialize(options) ⇒ WebSocket

Returns a new instance of WebSocket.



101
102
103
104
# File 'lib/combi/buses/web_socket.rb', line 101

def initialize(options)
  super
  @handlers = {}
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



99
100
101
# File 'lib/combi/buses/web_socket.rb', line 99

def handlers
  @handlers
end

#readyObject (readonly)

Returns the value of attribute ready.



99
100
101
# File 'lib/combi/buses/web_socket.rb', line 99

def ready
  @ready
end

Instance Method Details

#lookup_and_invoke_service(service_name, kind, payload) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/combi/buses/web_socket.rb', line 201

def lookup_and_invoke_service(service_name, kind, payload)
  handler = handlers[service_name.to_s]
  if handler
    service_instance = handler[:service_instance]
    if service_instance.respond_to? kind
      response = invoke_service(service_instance, kind, payload)
    else
      log "[WARNING] Service #{service_name}(#{service_instance.class.name}) does not respond to message #{kind}"
      response = {error: { klass: 'unknown action', message: kind } }
    end
  else
    log "[WARNING] Service #{service_name} not found"
    log "[WARNING] handlers: #{handlers.keys.inspect}"
    response = {error: 'unknown service'}
  end
end

#manage_request(env, handler) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/combi/buses/web_socket.rb', line 125

def manage_request(env, handler)
  require 'faye/websocket'

  return unless Faye::WebSocket.websocket?(env)
  @ws = ws = Faye::WebSocket.new(env)
  session = nil

  ws.on :message do |event|
    @machine.on_message(ws, session, event.data)
  end

  ws.on :open do |event|
    session = @machine.on_open(ws, handler)
  end

  ws.on :close do |event|
    @machine.on_close(session)
  end
  # Return async Rack response
  ws.rack_response
end

#manage_ws_event(ws, handler) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/combi/buses/web_socket.rb', line 147

def manage_ws_event(ws, handler)
  session = nil

  ws.onmessage do |raw_message|
    @machine.on_message(ws, session, raw_message)
  end

  ws.onopen do |handshake|
    session = @machine.on_open(ws, handler)
  end

  ws.onclose do
    @machine.on_close(session)
  end
end

#on_message(ws, message, session = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/combi/buses/web_socket.rb', line 163

def on_message(ws, message, session = nil)
  if message['correlation_id'] && message.has_key?('result')
    @response_store.handle_rpc_response(message)
    log "Stored message with correlation_id #{message['correlation_id']} - #{message.inspect[0..500]}"
    return
  end
  service_name = message['service']
  kind = message['kind']
  payload = message['payload'] || {}
  payload['session'] = session
  begin
    response = lookup_and_invoke_service(service_name, kind, payload)
  rescue RuntimeError => e
    response = {error: {klass: e.class.name, message: e.message, backtrace: e.backtrace } }
  end

  msg = {result: 'ok', correlation_id: message['correlation_id']}

  if response.respond_to? :succeed
    log "response is deferred"
    response.callback do |service_response|
      log "responding with deferred answer: #{service_response.inspect[0..500]}"
      msg[:response] = service_response.to_json
      ws.send(msg.to_json)
    end
    response.errback do |service_response|
      log "responding with deferred error: #{service_response.inspect[0..500]}"
      error_response = { error: service_response }
      msg[:response] = error_response.to_json
      ws.send(msg.to_json)
    end
  else
    log "responding with inmediate answer: #{response.inspect[0..500]}"
    msg[:response] = response.to_json
    ws.send(msg.to_json)
  end
end

#post_initializeObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/combi/buses/web_socket.rb', line 106

def post_initialize
  @ready = EventMachine::DefaultDeferrable.new
  @response_store = Combi::ResponseStore.new
  if @options[:remote_api]
    require 'eventmachine'
    @machine = Client.new(@options[:remote_api], @options[:handler], self)
  else
    @machine = Server.new(self)
  end
end

#request(name, kind, message, options = {}) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/combi/buses/web_socket.rb', line 224

def request(name, kind, message, options = {})
  options[:timeout] ||= RPC_DEFAULT_TIMEOUT
  msg = {
    service: name,
    kind: kind,
    payload: message
  }
  correlation_id = Combi::Correlation.generate
  msg[:correlation_id] = correlation_id
  waiter = EventedWaiter.wait_for(correlation_id, @response_store, options[:timeout])
  @ready.callback do |r|
    web_socket = @machine.ws || options[:ws]
    log "sending request #{msg.inspect}"
    web_socket.send msg.to_json unless web_socket.nil?
  end
  waiter
end

#respond_to(service_instance, action, options = {}) ⇒ Object



218
219
220
221
222
# File 'lib/combi/buses/web_socket.rb', line 218

def respond_to(service_instance, action, options = {})
  log "registering #{action}"
  handlers[action.to_s] = {service_instance: service_instance, options: options}
  log "handlers: #{handlers.keys.inspect}"
end

#start!Object



117
118
119
# File 'lib/combi/buses/web_socket.rb', line 117

def start!
  @machine.start!
end

#stop!Object



121
122
123
# File 'lib/combi/buses/web_socket.rb', line 121

def stop!
  @machine.stop!
end