Skip to content

Commit

Permalink
Integrate Client message handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
rubyonrails3 committed May 15, 2024
1 parent 6d9edaf commit cb18138
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 260 deletions.
9 changes: 7 additions & 2 deletions lib/wamp/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

require_relative "message_handler/base"

require_relative "message_handler/hello"
require_relative "message_handler/welcome"
require_relative "message_handler/challenge"
require_relative "message_handler/goodbye"

require_relative "message_handler/subscribe"
require_relative "message_handler/subscribed"
require_relative "message_handler/unsubscribe"
Expand Down Expand Up @@ -32,9 +37,9 @@ module MessageHandler
# instantiate correct handler
module ClassMethods
def resolve(data, connection)
return handle_when_not_joined(data, connection) unless connection.joiner.joined?
# return handle_when_not_joined(data, connection) unless connection.joiner.joined?

message = connection.session.receive(data)
message = connection.joiner.serializer.deserialize(data)
klass_name = demodulize(message.class.name)
klass = constantize("Wamp::MessageHandler::#{klass_name}")
klass.new(message, connection)
Expand Down
27 changes: 3 additions & 24 deletions lib/wamp/message_handler/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ module MessageHandler
class Api
extend Forwardable

attr_reader :connection, :session_id
attr_reader :connection, :session_id, :id_gen

def initialize(connection)
@connection = connection
end

def send_protocol_violation(text, *args, **kwargs)
message = Message::Abort.new({ message: text }, "wamp.error.protocol_violation", *args, **kwargs)
manager = Manager::Event::Abort.new(message, self)
connection.transmit(message.payload)
manager.emit_event(message)
@id_gen = Wampproto::IdGenerator.new
end

def subscribe(topic, handler, options = {}, &block)
Expand Down Expand Up @@ -63,25 +57,10 @@ def unregister(registration_id, &block)
action.send_message(&block)
end

def on_message(message)
manager = Manager::Event.resolve(message, self)
manager.emit_event(message)
end

def create_request_id
next_request_id
end

private

def next_request_id
@next_request_id = create_request_id_generator unless defined?(@next_request_id)
@next_request_id.call
end

def create_request_id_generator
request_id = 0
-> { request_id += 1 }
id_gen.next
end
end
end
Expand Down
22 changes: 14 additions & 8 deletions lib/wamp/message_handler/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def initialize(message, connection)
@connection = connection
end

def handle
raise NotImplementedError
end

def send_message
raise NotImplementedError
end

private

def stored_data
@stored_data ||= store.delete(store_key) || {}
end
Expand All @@ -30,21 +40,17 @@ def identity
message.request_id
end

def handle
raise NotImplementedError
end

def send_message
raise NotImplementedError
end

def deliver_response
callback = stored_data.fetch(:callback, proc {})
return unless callback

callback.call(message)
end

def validate_received_message
connection.session.receive_message(message)
end

def send_serialized(message)
connection.transmit session.send_message(message)
end
Expand Down
12 changes: 2 additions & 10 deletions lib/wamp/message_handler/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ module Wamp
module MessageHandler
# Call
class Call < Base
# Call and Result share the same request_id
# Invocation and Yield share the same request_id
def handle
invocation = find_connection.handle_call(message)
connection.invocation_requests[invocation.request_id] = message.request_id
connection.transmit invocation
end

def send_message(handler)
send_serialized message
store[store_key] = { handler: handler, callback: handler }

store[store_key] = { handler: handler }
send_serialized message
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/wamp/message_handler/challenge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Wamp
module MessageHandler
# Challenge
class Challenge < Hello
def handle
connection.transmit connection.joiner.receive(connection.joiner.serializer.serialize(message))
end
end
end
end
4 changes: 3 additions & 1 deletion lib/wamp/message_handler/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ module MessageHandler
# Call handler with error message
class Error < Base
def handle
stored_data[:handler].call(message)
validate_received_message

stored_data[:callback].call(message)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/wamp/message_handler/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module MessageHandler
# publish event to subscriber
class Event < Base
def handle
validate_received_message

store[alt_store_key].fetch(:handler).call(message)
end

Expand Down
19 changes: 19 additions & 0 deletions lib/wamp/message_handler/goodbye.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Wamp
module MessageHandler
# send unregister message
class Goodbye < Base
def send_message(&callback)
store[store_key] = { callback: callback }

send_serialized message
end

def handle
goodbye = Wampproto::Message::Goodbye.new({}, "wamp.close.goodbye_and_out")
send_serialized goodbye
end
end
end
end
13 changes: 8 additions & 5 deletions lib/wamp/message_handler/hello.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
module Wamp
module MessageHandler
# Hello
class Hello
def initialize
@message = message
@connection = connection
class Hello < Base
def handle
msg, is_welcome = connection.acceptor.receive(connection.serializer.serialize(message))
connection.transmit msg
connection.router.attach_client(connection) if is_welcome
end

def send_message; end
def send_message
connection.transmit connection.joiner.send_hello
end
end
end
end
1 change: 1 addition & 0 deletions lib/wamp/message_handler/invocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module MessageHandler
# Call
class Invocation < Base
def handle
connection.session.receive_message(message)
data = store.fetch(alt_store_key)

send_yield_message data.fetch(:handler)
Expand Down
6 changes: 2 additions & 4 deletions lib/wamp/message_handler/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ module MessageHandler
# Publish message
class Publish < Base
def send_message(&callback)
send_serialized message

return unless message.options[:acknowledge]
store[store_key] = { callback: callback } if message.options[:acknowledge]

store[store_key] = { callback: callback }
send_serialized message
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/wamp/message_handler/published.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module MessageHandler
# Published confirmation message
class Published < Base
def handle
validate_received_message

deliver_response
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/wamp/message_handler/registered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module MessageHandler
# Registered callback
class Registered < Base
def handle
validate_received_message

store[alt_store_key] = { handler: stored_data.fetch(:handler), procedure: stored_data.fetch(:procedure) }
store_procedure

Expand Down
1 change: 1 addition & 0 deletions lib/wamp/message_handler/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module MessageHandler
# Result
class Result < Base
def handle
validate_received_message
stored_data.fetch(:handler).call(message)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/wamp/message_handler/subscribed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module MessageHandler
# Receive subscribed
class Subscribed < Base
def handle
validate_received_message

store[alt_store_key] = { handler: stored_data.fetch(:handler), topic: stored_data.fetch(:topic) }
store_topic

Expand Down
2 changes: 2 additions & 0 deletions lib/wamp/message_handler/unregistered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module MessageHandler
# callback for unregister message
class Unregistered < Base
def handle
validate_received_message

delete_procedure store.delete(alt_store_key)

deliver_response
Expand Down
1 change: 1 addition & 0 deletions lib/wamp/message_handler/unsubscribed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module MessageHandler
# Receive unsubscribed
class Unsubscribed < Base
def handle
validate_received_message
delete_topic store.delete(alt_store_key)

deliver_response
Expand Down
12 changes: 12 additions & 0 deletions lib/wamp/message_handler/welcome.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Wamp
module MessageHandler
# Welcome
class Welcome < Base
def handle
connection.executor&.call(connection.api)
end
end
end
end
13 changes: 1 addition & 12 deletions lib/wamp/message_handler/yield.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
module Wamp
module MessageHandler
# Yield
class Yield
def initialize
@message = message
@connection = connection
end

def handle
call_request_id = connection.invocation_requests.delete(message.request_id)
handler = call_requests.delete(call_request_id)
handler.call(message)
end

class Yield < Base
def send_message(handler)
connection.transmit message
connection.call_requests[message.request_id] = handler
Expand Down
Loading

0 comments on commit cb18138

Please sign in to comment.