class Patch::IO::OSC::Server
OSC
server
Attributes
Public Class Methods
@param [Fixnum] id @param [Fixnum] port @param [Hash] options @option options [Hash] :echo @option options [Log] :log
# File lib/patch/io/osc/server.rb, line 17 def initialize(id, port, options = {}) @log = options[:log] @server = nil @active = false @id = id @is_failsafe = true configure(port, options) end
Public Instance Methods
Is the server active? @return [Boolean]
# File lib/patch/io/osc/server.rb, line 29 def active? @active end
Disable the message handlers @return [Boolean]
# File lib/patch/io/osc/server.rb, line 50 def disable(patch) addresses = get_addresses(patch) addresses.select { |address| @server.remove_method(address) }.any? end
Listen for messages @param [::Patch::Patch] patch The patch to use for context @param [Proc] callback A callback to fire when messages are received @return [Boolean] Whether any actions were configured
# File lib/patch/io/osc/server.rb, line 59 def listen(patch, &callback) addresses = get_addresses(patch) addresses.select { |address| listen_for(address, patch, &callback) }.any? end
Start the server @return [Boolean] Whether the server was started
# File lib/patch/io/osc/server.rb, line 35 def start @active = true @connection = @server.run true end
Stop the server @return [Boolean]
# File lib/patch/io/osc/server.rb, line 43 def stop @active = false true end
Protected Instance Methods
@param [::Patch::Patch] patch The patch to use for context @return [Array<String, Regexp>]
# File lib/patch/io/osc/server.rb, line 68 def get_addresses(patch) actions = ::Patch::IO::OSC::Action.osc_actions(patch.actions) actions.map { |action| action[:osc][:address] }.compact.uniq end
Handle a new message @param [::Patch::Patch] patch A patch for context @param [OSC::Message] message The OSC
message object @param [Proc] callback A callback to fire when a message or messages is received @return [Array<Patch::Message>]
# File lib/patch/io/osc/server.rb, line 78 def handle_message_received(patch, raw_osc, &callback) messages = ::Patch::IO::OSC::Message.to_patch_messages(patch, raw_osc) echo(patch, raw_osc) if echo? # yield to custom behavior yield(messages) if block_given? messages end
Private Instance Methods
@param [Fixnum] port @param [Hash] options @option options [Hash] :echo @return [Boolean]
# File lib/patch/io/osc/server.rb, line 92 def configure(port, options = {}) configure_server(port) unless options[:echo].nil? configure_echo(options[:echo][:host], options[:echo][:port]) end true end
Configure the echo client @param [String] host @param [Fixnum] echo @param [Hash] options @param [Log] :log @return [::Patch::IO::OSC::Client]
# File lib/patch/io/osc/server.rb, line 149 def configure_echo(host, port, options = {}) @client = Client.new(host, port, :log => options.fetch(:log, @log)) end
Configure the underlying server @param [Fixnum] port @return [::OSC::Server]
# File lib/patch/io/osc/server.rb, line 114 def configure_server(port) @server = ::OSC::EMServer.new(port) if @log @server.add_method(/.*/) { |message| @log.puts("Received: #{message.address}") } end @server end
Echo a message back to the client to update the UI or whatever @param [::Patch::Patch] patch @param [OSC::Message] osc_message @return [Boolean] Whether the echo occurred
# File lib/patch/io/osc/server.rb, line 132 def echo(patch, osc_message) begin @client.puts(patch, osc_message) true rescue Exception => exception # failsafe @log.exception(exception) if @log ::Thread.main.raise(exception) unless @is_failsafe false end end
Will received messages be echoed back to the client? @return [Boolean]
# File lib/patch/io/osc/server.rb, line 124 def echo? !@client.nil? end
Listen for messages on the given address @param [::Patch::Patch] patch The patch to use for context @param [Proc] callback A callback to fire when messages are received @return [Boolean] Whether an action was configured
# File lib/patch/io/osc/server.rb, line 104 def listen_for(address, patch, &callback) @server.add_method(address) do |message| handle_message_received(patch, message, &callback) end true end