From acca3bce7b5fe9a915c2cf809a9adbd0205b4426 Mon Sep 17 00:00:00 2001 From: Ismail Akram Date: Thu, 23 May 2024 19:00:51 +0500 Subject: [PATCH 1/3] Update wampproto and use session details in realm --- Gemfile | 4 +++- lib/wamp/connection/websocket_connection.rb | 10 +--------- lib/wamp/router/connection.rb | 7 ------- lib/wamp/router/realm.rb | 5 +++-- 4 files changed, 7 insertions(+), 19 deletions(-) diff --git a/Gemfile b/Gemfile index e405f36..ec34c33 100644 --- a/Gemfile +++ b/Gemfile @@ -12,4 +12,6 @@ gem "rubocop", "~> 1.21" gem "async", "~> 2.9" -gem "wampproto", github: "xconnio/wampproto.rb", branch: :main, require: true +gem "wampproto", "~> 0.1.1", require: true + +gem "debug", "~> 1.9", groups: %i[development test] diff --git a/lib/wamp/connection/websocket_connection.rb b/lib/wamp/connection/websocket_connection.rb index 3e1b75e..b5978a3 100644 --- a/lib/wamp/connection/websocket_connection.rb +++ b/lib/wamp/connection/websocket_connection.rb @@ -3,13 +3,6 @@ require "wampproto" require_relative "websocket_client" -# extending the class -class Wampproto::Joiner # rubocop:disable Style/ClassAndModuleChildren - def joined? - state == STATE_JOINED - end -end - module Wamp module Connection # Conn @@ -19,14 +12,13 @@ class WebSocketConnection < Session def initialize(url = "ws://localhost:8080/ws", joiner = Wampproto::Joiner.new("realm1")) super(joiner) @url = url - @store = {} @websocket = Wamp::Connection::WebsocketClient.new(self, protocols) end def run websocket.run ensure - p "Close" + p %i[run close] websocket.close end diff --git a/lib/wamp/router/connection.rb b/lib/wamp/router/connection.rb index 31e6999..b102239 100644 --- a/lib/wamp/router/connection.rb +++ b/lib/wamp/router/connection.rb @@ -2,13 +2,6 @@ require "websocket/driver" -# extending the class -class Wampproto::Acceptor # rubocop:disable Style/ClassAndModuleChildren - def accepted? - state == STATE_WELCOME_SENT - end -end - # Testing class Authenticator def self.authenticate(request) diff --git a/lib/wamp/router/realm.rb b/lib/wamp/router/realm.rb index d7832b3..4471da7 100644 --- a/lib/wamp/router/realm.rb +++ b/lib/wamp/router/realm.rb @@ -30,10 +30,11 @@ def initialize(name) def attach_client(client) session_id = client.session_id + session_details = client.acceptor.session_details clients[session_id] = client - broker.add_session(session_id) - dealer.add_session(session_id) + broker.add_session(session_details) + dealer.add_session(session_details) end def detach_client(client) From 833a0dfac084eee3a8de6781afd627da4a12d377 Mon Sep 17 00:00:00 2001 From: Ismail Akram Date: Sun, 26 May 2024 03:12:53 +0500 Subject: [PATCH 2/3] Add xconn executable to register procedures and start router --- bin/xconn | 58 ++++++++++++++++++++++++++++++++++ example/sample/example.rb | 23 ++++++++++++++ example/sample/included_app.rb | 12 +++++++ lib/wamp.rb | 1 + lib/wamp/app.rb | 39 +++++++++++++++++++++++ lib/wamp/router/connection.rb | 40 ++++++++++++----------- lib/wamp/router/server.rb | 19 ++++++++--- wamp.gemspec | 1 + 8 files changed, 169 insertions(+), 24 deletions(-) create mode 100755 bin/xconn create mode 100644 example/sample/example.rb create mode 100644 example/sample/included_app.rb create mode 100644 lib/wamp/app.rb diff --git a/bin/xconn b/bin/xconn new file mode 100755 index 0000000..d65e997 --- /dev/null +++ b/bin/xconn @@ -0,0 +1,58 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "wamp" + +require "optparse" + +options = { host: "127.0.0.1", port: 8080, realm: "realm1", directory: "." } +OptionParser.new do |opts| + opts.banner = "Usage: xconn [options]" + + opts.on("-hHOST", "--host HOST", "WebSocket host (default: ws://127.0.0.1:8080/ws)") do |host| + options[:host] = host + end + + opts.on("-rREALM", "--realm REALM", "Realm (default: reaml1)") do |realm| + options[:realm] = realm + end + + opts.on("-pPORT", "--port PORT", "Port (default: 8080)") do |port| + options[:port] = port + end + + opts.on("-dDIRECTORY", "--directory Directory", 'Directory (default: ".")') do |directory| + options[:directory] = directory + end + + opts.on("-aAPP", "--app App", 'App (default: "App")') do |app| + options[:app] = app + end +end.parse! + +def create_client_session(router) + session = Wamp::Connection::Session.new + + server_session = Wamp::Router::Client.new + server_session.router = router + server_session.connection = session + + session.stream = server_session + session +end + +router = Wamp::Router::Base.new +router.add_realm(options.fetch(:realm)) + +client = create_client_session(router).tap(&:on_open) +Pathname.new(options[:directory]).glob("**/*.rb").each { |file| require file.to_s } +app = Object.const_get(options[:app]) +app.new.procedures.each do |procedure, func| + client.api.register(procedure, func) do + puts "Registered procedure: #{procedure}" + end +end + +server = Wamp::Router::Server.new(router, options) +server.run diff --git a/example/sample/example.rb b/example/sample/example.rb new file mode 100644 index 0000000..8c429ad --- /dev/null +++ b/example/sample/example.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "wamp" +require "date" +require_relative "included_app" + +# SystemApp +class Example < Wamp::App + register what: "io.xconn.echo", current_date: "io.xconn.date.get" + + def initialize + super + include_app(IncludedApp.new, "test.") + end + + def what(invocation) + Wampproto::Message::Yield.new(invocation.request_id, {}, *invocation.args, **invocation.kwargs) + end + + def current_date(invocation) + Wampproto::Message::Yield.new(invocation.request_id, {}, Date.today.iso8601) + end +end diff --git a/example/sample/included_app.rb b/example/sample/included_app.rb new file mode 100644 index 0000000..aa48bc3 --- /dev/null +++ b/example/sample/included_app.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "wamp" + +# Class that can be included +class IncludedApp < Wamp::App + register echo: "foo.bar.echo" + + def echo(invocation) + Wampproto::Message::Yield.new(invocation.request_id, {}, "INCLUDED", name: "Ismail") + end +end diff --git a/lib/wamp.rb b/lib/wamp.rb index f3ebcd0..88885ac 100644 --- a/lib/wamp.rb +++ b/lib/wamp.rb @@ -6,6 +6,7 @@ require_relative "wamp/connection/websocket_connection" require_relative "wamp/serializer" require_relative "wamp/router" +require_relative "wamp/app" module Wamp class Error < StandardError; end diff --git a/lib/wamp/app.rb b/lib/wamp/app.rb new file mode 100644 index 0000000..551a974 --- /dev/null +++ b/lib/wamp/app.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Wamp + # WAMPApp + class App + def initialize + @procedures = {} + end + + def include_app(app, prefix = "") + if prefix&.length&.zero? + procedures.merge!(app.procedures) + else + app.procedures.each do |procedure, func| + procedures.merge!({ "#{prefix}#{procedure}" => func }) + end + end + end + + def procedures + return {} if self.class.procedures && self.class.procedures.empty? + return @procedures if @procedures.any? + + self.class.procedures.map do |procedure, registration_name| + @procedures[registration_name.to_s] = method(procedure) + end + @procedures + end + + class << self + attr_reader :procedures + + def register(procedures = {}) + @procedures ||= {} + @procedures.merge!(procedures) + end + end + end +end diff --git a/lib/wamp/router/connection.rb b/lib/wamp/router/connection.rb index b102239..4f85f34 100644 --- a/lib/wamp/router/connection.rb +++ b/lib/wamp/router/connection.rb @@ -12,7 +12,7 @@ def self.authenticate(request) module Wamp module Router # TOP Level Doc - class Connection < Client + class Connection < Client # rubocop:disable Metrics/ClassLength include WebSocket::Driver::EventEmitter CONNECTING = 0 OPEN = 1 @@ -61,18 +61,28 @@ def finalize_close socket.close end - def listen(&block) + def listen(&block) # rubocop:disable Metrics/MethodLength return unless [CONNECTING, OPEN].include?(@ready_state) - data = socket.read_nonblock(4096, exception: false) - case data - when :wait_readable - # do nothing - when nil - block&.call - @driver.close - else - receive_data(data) + begin + data = socket.read_nonblock(4096, exception: false) + case data + when :wait_readable + # do nothing + when nil + block&.call + @driver.close + else + receive_data(data) + end + rescue StandardError + begin + block&.call + @driver.close + rescue StandardError + # Errno::ECONNRESET + puts "Failed to handle: Errno::ECONNRESET" + end end end @@ -120,14 +130,6 @@ def close(_code, _reason) @driver.close end - def encode(wamp_message) - coder.encode wamp_message - end - - def decode(websocket_message) - coder.decode websocket_message - end - attr_reader :serializer def choose_serializer_from(protocols) diff --git a/lib/wamp/router/server.rb b/lib/wamp/router/server.rb index a62fc4a..3f92cde 100644 --- a/lib/wamp/router/server.rb +++ b/lib/wamp/router/server.rb @@ -9,18 +9,20 @@ module Wamp module Router # Connection Handler class Server - attr_reader :selector + attr_reader :selector, :options - def initialize + def initialize(router, options = {}) + @options = options @selector = NIO::Selector.new - @router = Wamp::Router::Base.new - @router.add_realm("realm1") + @router = router + # @router.add_realm(options.fetch(:realm, "realm1")) end def run trap("INT") { throw :ctrl_c } create_tcp_server + options_message catch :ctrl_c do loop do accept_connection @@ -28,8 +30,15 @@ def run end end + def options_message + host = options.fetch(:host, "127.0.0.1") + port = options.fetch(:port, 8080) + realm = options.fetch(:realm, "realm1") + puts "Starting router on ws://#{host}:#{port}/ws and added Realm: #{realm}" + end + def create_tcp_server - server = TCPServer.new("127.0.0.1", 8080) + server = TCPServer.new(options.fetch(:host, "127.0.0.1"), options.fetch(:port, 8080)) selector.register(server, :r) end diff --git a/wamp.gemspec b/wamp.gemspec index 9aacbb1..b37a800 100644 --- a/wamp.gemspec +++ b/wamp.gemspec @@ -27,6 +27,7 @@ Gem::Specification.new do |spec| end spec.bindir = "bin" spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) } + spec.executables << "xconn" spec.require_paths = ["lib"] spec.add_dependency "cbor", "~> 0.5.9.8" From 1ea2494e51249c83e38ce59eea958cc8ebeda3cc Mon Sep 17 00:00:00 2001 From: Ismail Akram Date: Wed, 29 May 2024 23:37:14 +0500 Subject: [PATCH 3/3] Add public message APIs instead of using wampproto messages --- Rakefile | 2 ++ example/sample/example.rb | 10 ++++----- example/sample/included_app.rb | 2 +- lib/tasks/tasks.rake | 24 ++++++++++++++++++++++ lib/wamp.rb | 1 + lib/wamp/message_handler/base.rb | 4 +++- lib/wamp/message_handler/error.rb | 6 +++++- lib/wamp/message_handler/invocation.rb | 13 +++++++++--- lib/wamp/message_handler/published.rb | 6 +++++- lib/wamp/message_handler/registered.rb | 6 +++++- lib/wamp/message_handler/result.rb | 6 +++++- lib/wamp/message_handler/subscribed.rb | 6 +++++- lib/wamp/message_handler/unregistered.rb | 6 +++++- lib/wamp/message_handler/unsubscribed.rb | 6 +++++- lib/wamp/router/connection.rb | 4 +++- lib/wamp/type.rb | 16 +++++++++++++++ lib/wamp/type/error.rb | 24 ++++++++++++++++++++++ lib/wamp/type/event.rb | 16 +++++++++++++++ lib/wamp/type/invocation.rb | 16 +++++++++++++++ lib/wamp/type/publication.rb | 14 +++++++++++++ lib/wamp/type/registration.rb | 14 +++++++++++++ lib/wamp/type/result.rb | 16 +++++++++++++++ lib/wamp/type/subscription.rb | 14 +++++++++++++ lib/wamp/type/success.rb | 8 ++++++++ spec/wamp/integration/registration_spec.rb | 8 ++++---- spec/wamp/integration/subscription_spec.rb | 8 ++++---- spec/wamp/message_handler/api_spec.rb | 10 ++++----- 27 files changed, 235 insertions(+), 31 deletions(-) create mode 100644 lib/tasks/tasks.rake create mode 100644 lib/wamp/type.rb create mode 100644 lib/wamp/type/error.rb create mode 100644 lib/wamp/type/event.rb create mode 100644 lib/wamp/type/invocation.rb create mode 100644 lib/wamp/type/publication.rb create mode 100644 lib/wamp/type/registration.rb create mode 100644 lib/wamp/type/result.rb create mode 100644 lib/wamp/type/subscription.rb create mode 100644 lib/wamp/type/success.rb diff --git a/Rakefile b/Rakefile index cca7175..c3564c2 100644 --- a/Rakefile +++ b/Rakefile @@ -10,3 +10,5 @@ require "rubocop/rake_task" RuboCop::RakeTask.new task default: %i[spec rubocop] + +Dir.glob("lib/tasks/**/*.rake").each { |r| load r } diff --git a/example/sample/example.rb b/example/sample/example.rb index 8c429ad..f32603a 100644 --- a/example/sample/example.rb +++ b/example/sample/example.rb @@ -6,18 +6,18 @@ # SystemApp class Example < Wamp::App - register what: "io.xconn.echo", current_date: "io.xconn.date.get" + register echo: "io.xconn.echo", current_date: "io.xconn.date.get" def initialize super include_app(IncludedApp.new, "test.") end - def what(invocation) - Wampproto::Message::Yield.new(invocation.request_id, {}, *invocation.args, **invocation.kwargs) + def echo(invocation) + Wamp::Type::Result.new(args: invocation.args, kwargs: invocation.kwargs, details: invocation.details) end - def current_date(invocation) - Wampproto::Message::Yield.new(invocation.request_id, {}, Date.today.iso8601) + def current_date(_invocation) + Wamp::Type::Result.new(args: Date.today.iso8601) end end diff --git a/example/sample/included_app.rb b/example/sample/included_app.rb index aa48bc3..0581ecb 100644 --- a/example/sample/included_app.rb +++ b/example/sample/included_app.rb @@ -7,6 +7,6 @@ class IncludedApp < Wamp::App register echo: "foo.bar.echo" def echo(invocation) - Wampproto::Message::Yield.new(invocation.request_id, {}, "INCLUDED", name: "Ismail") + Wamp::Type::Result.new(args: invocation.args, kwargs: invocation.kwargs, details: invocation.details) end end diff --git a/lib/tasks/tasks.rake b/lib/tasks/tasks.rake new file mode 100644 index 0000000..4388ea4 --- /dev/null +++ b/lib/tasks/tasks.rake @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +namespace :xconn do + desc "Start SAMPLE app" + task :run, [:host, :port, :realm, :directory, :app] do |_t, args| + host = args[:host] || "127.0.0.1" + port = args[:port] || 8080 + realm = args[:realm] || "realm1" + app = args[:app] || "Example" + directory = args[:directory] || "./example/sample" + + command = "bin/xconn -h #{host} -p #{port} -r #{realm} -d #{directory} -a #{app}" + + puts "Running: #{command}" + + trap("SIGINT") do + puts "\nReceived SIGINT. Gracefully exiting..." + + exit 0 + end + + `#{command}` + end +end diff --git a/lib/wamp.rb b/lib/wamp.rb index 88885ac..8db1c2d 100644 --- a/lib/wamp.rb +++ b/lib/wamp.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "wamp/version" +require_relative "wamp/type" require_relative "wamp/message_handler" require_relative "wamp/connection/session" require_relative "wamp/connection/websocket_connection" diff --git a/lib/wamp/message_handler/base.rb b/lib/wamp/message_handler/base.rb index f900a00..535d7bb 100644 --- a/lib/wamp/message_handler/base.rb +++ b/lib/wamp/message_handler/base.rb @@ -40,10 +40,12 @@ def identity message.request_id end - def deliver_response + def deliver_response(response) callback = stored_data.fetch(:callback, proc {}) return unless callback + return callback.call(response) unless response.nil? + callback.call(message) end diff --git a/lib/wamp/message_handler/error.rb b/lib/wamp/message_handler/error.rb index ebfd884..5418dd0 100644 --- a/lib/wamp/message_handler/error.rb +++ b/lib/wamp/message_handler/error.rb @@ -7,7 +7,11 @@ class Error < Base def handle validate_received_message - stored_data[:callback].call(message) + stored_data[:callback].call(response) + end + + def response + Type::Error.new(uri: message.error, details: message.details) end end end diff --git a/lib/wamp/message_handler/invocation.rb b/lib/wamp/message_handler/invocation.rb index 23a4f08..90e0ea3 100644 --- a/lib/wamp/message_handler/invocation.rb +++ b/lib/wamp/message_handler/invocation.rb @@ -15,12 +15,19 @@ def alt_store_key "registration_#{message.registration_id}" end + def invocation_response + Type::Invocation.new(args: message.args, kwargs: message.kwargs, details: message.details) + end + private def send_yield_message(handler) - result = handler.call(message) - yield_message = result if result.instance_of?(Wampproto::Message::Yield) - yield_message ||= Wampproto::Message::Yield.new(message.request_id, {}, result) + result = handler.call(invocation_response) + yield_message = if result.instance_of?(Type::Result) + Wampproto::Message::Yield.new(message.request_id, result.details, *result.args, **result.kwargs) + else + Wampproto::Message::Yield.new(message.request_id, {}, result) + end send_serialized yield_message end end diff --git a/lib/wamp/message_handler/published.rb b/lib/wamp/message_handler/published.rb index f5c3386..cf2ab18 100644 --- a/lib/wamp/message_handler/published.rb +++ b/lib/wamp/message_handler/published.rb @@ -7,7 +7,11 @@ class Published < Base def handle validate_received_message - deliver_response + deliver_response(response) + end + + def response + Type::Publication.new(publication_id: message.publication_id) end end end diff --git a/lib/wamp/message_handler/registered.rb b/lib/wamp/message_handler/registered.rb index 5168ace..cb2ce38 100644 --- a/lib/wamp/message_handler/registered.rb +++ b/lib/wamp/message_handler/registered.rb @@ -10,7 +10,11 @@ def handle store[alt_store_key] = { handler: stored_data.fetch(:handler), procedure: stored_data.fetch(:procedure) } store_procedure - deliver_response + deliver_response(response) + end + + def response + Type::Registration.new(registration_id: message.registration_id) end def alt_store_key diff --git a/lib/wamp/message_handler/result.rb b/lib/wamp/message_handler/result.rb index 7165cf8..44b5cf1 100644 --- a/lib/wamp/message_handler/result.rb +++ b/lib/wamp/message_handler/result.rb @@ -6,7 +6,11 @@ module MessageHandler class Result < Base def handle validate_received_message - stored_data.fetch(:handler).call(message) + stored_data.fetch(:handler).call(response) + end + + def response + Type::Result.new(args: message.args, kwargs: message.kwargs, details: message.details) end end end diff --git a/lib/wamp/message_handler/subscribed.rb b/lib/wamp/message_handler/subscribed.rb index 206efb6..0f91519 100644 --- a/lib/wamp/message_handler/subscribed.rb +++ b/lib/wamp/message_handler/subscribed.rb @@ -10,7 +10,11 @@ def handle store[alt_store_key] = { handler: stored_data.fetch(:handler), topic: stored_data.fetch(:topic) } store_topic - deliver_response + deliver_response(response) + end + + def response + Type::Subscription.new(subscription_id: message.subscription_id) end def alt_store_key diff --git a/lib/wamp/message_handler/unregistered.rb b/lib/wamp/message_handler/unregistered.rb index 96af562..e8ca389 100644 --- a/lib/wamp/message_handler/unregistered.rb +++ b/lib/wamp/message_handler/unregistered.rb @@ -9,7 +9,11 @@ def handle delete_procedure store.delete(alt_store_key) - deliver_response + deliver_response(response) + end + + def response + Type::Success.new end def alt_store_key diff --git a/lib/wamp/message_handler/unsubscribed.rb b/lib/wamp/message_handler/unsubscribed.rb index 7d49b2a..9fdf4d3 100644 --- a/lib/wamp/message_handler/unsubscribed.rb +++ b/lib/wamp/message_handler/unsubscribed.rb @@ -8,7 +8,11 @@ def handle validate_received_message delete_topic store.delete(alt_store_key) - deliver_response + deliver_response(response) + end + + def response + Type::Success.new end private diff --git a/lib/wamp/router/connection.rb b/lib/wamp/router/connection.rb index 4f85f34..1bafa15 100644 --- a/lib/wamp/router/connection.rb +++ b/lib/wamp/router/connection.rb @@ -75,7 +75,9 @@ def listen(&block) # rubocop:disable Metrics/MethodLength else receive_data(data) end - rescue StandardError + rescue StandardError => e + puts e.message + puts e.backtrace begin block&.call @driver.close diff --git a/lib/wamp/type.rb b/lib/wamp/type.rb new file mode 100644 index 0000000..c850455 --- /dev/null +++ b/lib/wamp/type.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require_relative "type/success" +require_relative "type/subscription" +require_relative "type/publication" +require_relative "type/event" +require_relative "type/registration" +require_relative "type/result" +require_relative "type/invocation" +require_relative "type/error" + +module Wamp + # Public Message Types + module Type + end +end diff --git a/lib/wamp/type/error.rb b/lib/wamp/type/error.rb new file mode 100644 index 0000000..e4f261a --- /dev/null +++ b/lib/wamp/type/error.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# [8, 32, 123, {}, "wamp.error.not_authorized"] +# [8, 34, 123, {}, "wamp.error.no_such_subscription"] +# [8, 16, 123, {}, "wamp.error.not_authorized"] +# [8, 64, 123, {}, "wamp.error.procedure_already_exists"] +# [8, 66, 123, {}, "wamp.error.no_such_registration] +# [8, 68, 123, {}, "com.myapp.error.object_write_protected", ["Object is write protected."], {"severity": 3}] +# [8, 48, 123, {}, "com.myapp.error.object_write_protected", ["Object is write protected."], {"severity": 3}] +module Wamp + module Type + # Error Type + class Error + attr_reader :uri, :args, :kwargs, :details + + def initialize(uri:, args: [], kwargs: {}, details: {}) + @uri = uri + @args = args + @kwargs = kwargs + @details = details + end + end + end +end diff --git a/lib/wamp/type/event.rb b/lib/wamp/type/event.rb new file mode 100644 index 0000000..99b3747 --- /dev/null +++ b/lib/wamp/type/event.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Wamp + module Type + # Event Type + class Event + attr_reader :args, :kwargs, :details + + def initialize(args: [], kwargs: {}, details: {}) + @args = args + @kwargs = kwargs + @details = details + end + end + end +end diff --git a/lib/wamp/type/invocation.rb b/lib/wamp/type/invocation.rb new file mode 100644 index 0000000..5b3588c --- /dev/null +++ b/lib/wamp/type/invocation.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Wamp + module Type + # Invocation Type + class Invocation + attr_reader :args, :kwargs, :details + + def initialize(args: [], kwargs: {}, details: {}) + @args = args + @kwargs = kwargs + @details = details + end + end + end +end diff --git a/lib/wamp/type/publication.rb b/lib/wamp/type/publication.rb new file mode 100644 index 0000000..40ea3f6 --- /dev/null +++ b/lib/wamp/type/publication.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Wamp + module Type + # Publication Type + class Publication + attr_reader :publication_id + + def initialize(publication_id: nil) + @publication_id = publication_id + end + end + end +end diff --git a/lib/wamp/type/registration.rb b/lib/wamp/type/registration.rb new file mode 100644 index 0000000..ed507e7 --- /dev/null +++ b/lib/wamp/type/registration.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Wamp + module Type + # Registration Type + class Registration + attr_reader :registration_id + + def initialize(registration_id: nil) + @registration_id = registration_id + end + end + end +end diff --git a/lib/wamp/type/result.rb b/lib/wamp/type/result.rb new file mode 100644 index 0000000..606447c --- /dev/null +++ b/lib/wamp/type/result.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Wamp + module Type + # Result Type + class Result + attr_reader :args, :kwargs, :details + + def initialize(args: [], kwargs: {}, details: {}) + @args = args + @kwargs = kwargs + @details = details + end + end + end +end diff --git a/lib/wamp/type/subscription.rb b/lib/wamp/type/subscription.rb new file mode 100644 index 0000000..04d9cbc --- /dev/null +++ b/lib/wamp/type/subscription.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Wamp + module Type + # Subscription Type + class Subscription + attr_reader :subscription_id + + def initialize(subscription_id: nil) + @subscription_id = subscription_id + end + end + end +end diff --git a/lib/wamp/type/success.rb b/lib/wamp/type/success.rb new file mode 100644 index 0000000..8a7bb5d --- /dev/null +++ b/lib/wamp/type/success.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Wamp + module Type + # Success Type + Success = Struct.new + end +end diff --git a/spec/wamp/integration/registration_spec.rb b/spec/wamp/integration/registration_spec.rb index d8cdc07..b867e44 100644 --- a/spec/wamp/integration/registration_spec.rb +++ b/spec/wamp/integration/registration_spec.rb @@ -27,7 +27,7 @@ def create_client_session(router) expect do client.api.register(procedure, handler) do |response| counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Registered) + expect(response).to be_an_instance_of(Wamp::Type::Registration) end end.to change { counter }.by(1) end @@ -43,7 +43,7 @@ def create_client_session(router) expect do client2.api.call(procedure) do |response| counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Result) + expect(response).to be_an_instance_of(Wamp::Type::Result) end end.to change { counter }.by(1) end @@ -58,7 +58,7 @@ def create_client_session(router) expect do client.api.unregister(procedure) do |response| counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Unregistered) + expect(response).to be_an_instance_of(Wamp::Type::Success) end end.to change { counter }.by(1) end @@ -71,7 +71,7 @@ def create_client_session(router) expect do client.api.unregister(procedure) do |response| counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Error) + expect(response).to be_an_instance_of(Wamp::Type::Error) end end.to change { counter }.by(1) end diff --git a/spec/wamp/integration/subscription_spec.rb b/spec/wamp/integration/subscription_spec.rb index 23a9bdc..bfd9383 100644 --- a/spec/wamp/integration/subscription_spec.rb +++ b/spec/wamp/integration/subscription_spec.rb @@ -27,7 +27,7 @@ def create_client_session(router) expect do client.api.subscribe(topic, handler) do |response| subscription_counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Subscribed) + expect(response).to be_an_instance_of(Wamp::Type::Subscription) end end.to change { subscription_counter }.by(1) end @@ -47,7 +47,7 @@ def create_client_session(router) expect do client2.api.publish(topic) do |response| counter += 1 - expect(response).to be_instance_of(Wampproto::Message::Published) + expect(response).to be_instance_of(Wamp::Type::Publication) end end.to change { counter }.by(1) end @@ -61,7 +61,7 @@ def create_client_session(router) expect do client.api.unsubscribe(topic) do |response| counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Unsubscribed) + expect(response).to be_an_instance_of(Wamp::Type::Success) end end.to change { counter }.by(1) end @@ -72,7 +72,7 @@ def create_client_session(router) expect do client.api.unsubscribe(129_876) do |response| counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Error) + expect(response).to be_an_instance_of(Wamp::Type::Error) end end.to change { counter }.by(1) end diff --git a/spec/wamp/message_handler/api_spec.rb b/spec/wamp/message_handler/api_spec.rb index b9ec671..2c381fd 100644 --- a/spec/wamp/message_handler/api_spec.rb +++ b/spec/wamp/message_handler/api_spec.rb @@ -94,13 +94,13 @@ def received_message(message) connection.on_join do |api| handler = proc { |msg| msg } api.register(procedure, handler) do |response| - expect(response).to be_an_instance_of(Wampproto::Message::Registered) + expect(response).to be_an_instance_of(Wamp::Type::Registration) end received_registered expect(connection.store).to include("registration_#{registration_id}") api.unregister(procedure) do |response| - expect(response).to be_an_instance_of(Wampproto::Message::Unregistered) + expect(response).to be_an_instance_of(Wamp::Type::Success) end received_unregistered expect(connection.store).not_to include("registration_#{registration_id}") @@ -112,10 +112,10 @@ def received_message(message) it "registers and receives invocation" do connection.on_join do |api| - handler = proc { |msg| expect(msg).to be_an_instance_of(Wampproto::Message::Invocation) } + handler = proc { |msg| expect(msg).to be_an_instance_of(Wamp::Type::Invocation) } api.register(procedure, handler) do |response| - expect(response).to be_an_instance_of(Wampproto::Message::Registered) + expect(response).to be_an_instance_of(Wamp::Type::Registration) end received_registered expect(connection.store).to include("registration_#{registration_id}") @@ -142,7 +142,7 @@ def received_message(message) counter = 1 api.call(procedure, {}, 2) do |response| counter += 1 - expect(response).to be_an_instance_of(Wampproto::Message::Result) + expect(response).to be_an_instance_of(Wamp::Type::Result) end expect(connection.store).to include("request_#{request_id}")