class OSC::Server

Attributes

address[RW]
port[RW]

Public Class Methods

new(port, address = "127.0.0.1") click to toggle source
# File lib/ruby-osc/server.rb, line 5
def initialize(port, address = "127.0.0.1")
  @port, @address   = port, address
  @queue, @patterns = [], []
  @mutex = Mutex.new
  run
end

Public Instance Methods

add_pattern(pattern, &block) click to toggle source
# File lib/ruby-osc/server.rb, line 23
def add_pattern(pattern, &block)
  raise ArgumentError, "A block must be given" unless block
  @patterns << [pattern, block]
end
delete_pattern(pattern) click to toggle source
# File lib/ruby-osc/server.rb, line 28
def delete_pattern(pattern)
  @patterns.delete pattern
end
receive(data) click to toggle source
# File lib/ruby-osc/server.rb, line 32
def receive(data)
  case decoded = OSC.decode(data)
  when Bundle
    decoded.timetag.nil? ? decoded.each{ |m| dispatch m } : @mutex.synchronize{@queue.push(decoded)}
  when Message
    dispatch decoded
  end
rescue => e
  warn "Bad data received: #{ e }"
end
run() click to toggle source
# File lib/ruby-osc/server.rb, line 12
def run
  @connection = EventMachine.open_datagram_socket @address, @port, Connection, self
  check_queue
end
stop() click to toggle source
# File lib/ruby-osc/server.rb, line 17
def stop
  return unless @connection
  @connection.close_connection
  @timer.cancel
end

Private Instance Methods

check_queue() click to toggle source
# File lib/ruby-osc/server.rb, line 45
def check_queue
  @timer = EventMachine::PeriodicTimer.new 0.002 do
    now  = Time.now
    @mutex.synchronize do
      @queue.delete_if do |bundle|
        bundle.each{ |m| dispatch m } if delete = now >= bundle.timetag
        delete
      end
    end
  end
end
dispatch(message) click to toggle source
# File lib/ruby-osc/server.rb, line 57
def dispatch(message)
  @patterns.each do |pat, block|
    block.call(*message.to_a) if pat === message.address
  end
end