-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from xconnio/runner
Add xconn executable and registrable app class
- Loading branch information
Showing
34 changed files
with
404 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.