Skip to content

Commit

Permalink
Merge pull request #14 from xconnio/runner
Browse files Browse the repository at this point in the history
Add xconn executable and registrable  app class
  • Loading branch information
rubyonrails3 authored May 30, 2024
2 parents 17dd39b + 1ea2494 commit 5afd8b5
Show file tree
Hide file tree
Showing 34 changed files with 404 additions and 67 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
58 changes: 58 additions & 0 deletions bin/xconn
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions example/sample/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "wamp"
require "date"
require_relative "included_app"

# SystemApp
class Example < Wamp::App
register echo: "io.xconn.echo", current_date: "io.xconn.date.get"

def initialize
super
include_app(IncludedApp.new, "test.")
end

def echo(invocation)
Wamp::Type::Result.new(args: invocation.args, kwargs: invocation.kwargs, details: invocation.details)
end

def current_date(_invocation)
Wamp::Type::Result.new(args: Date.today.iso8601)
end
end
12 changes: 12 additions & 0 deletions example/sample/included_app.rb
Original file line number Diff line number Diff line change
@@ -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)
Wamp::Type::Result.new(args: invocation.args, kwargs: invocation.kwargs, details: invocation.details)
end
end
24 changes: 24 additions & 0 deletions lib/tasks/tasks.rake
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/wamp.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# 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"
require_relative "wamp/serializer"
require_relative "wamp/router"
require_relative "wamp/app"

module Wamp
class Error < StandardError; end
Expand Down
39 changes: 39 additions & 0 deletions lib/wamp/app.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 1 addition & 9 deletions lib/wamp/connection/websocket_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/wamp/message_handler/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion lib/wamp/message_handler/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions lib/wamp/message_handler/invocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/wamp/message_handler/published.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/wamp/message_handler/registered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/wamp/message_handler/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/wamp/message_handler/subscribed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/wamp/message_handler/unregistered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/wamp/message_handler/unsubscribed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5afd8b5

Please sign in to comment.