Sha256: 9586e4268c987da04152529c94d51b323cbb77a20f4b7a9c6a69ba637c5b27ba

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module OSC
  class  Server

    def initialize(port)
      @socket = UDPSocket.new
      @socket.bind('', port)
      @cb = []
      @queue = Queue.new
    end
    
    def run
      start_dispatcher
      
      start_detector
    end
    
    def stop
      @socket.close
    end

    def add_method(address_pattern, &proc)
      matcher = AddressPattern.new( address_pattern )
      
      @cb << [matcher, proc]
    end

private

    def start_detector
      begin
	      detector
      rescue
	      Thread.main.raise $!
      end
    end
  
    def start_dispatcher
      Thread.fork do
	      begin
	        dispatcher
	      rescue
	        Thread.main.raise $!
	      end
      end
    end

    def sendmesg(mesg)
      @cb.each do |matcher, obj|
	      if matcher.match?( mesg.address )
	        obj.call( mesg )
	      end
      end
    end

    def dispatcher
      loop do
	      mesg = @queue.pop
	      
        dispatch_message( mesg )
      end
    end

    def detector
      loop do
	      pa = @socket.recv(16384)
	      begin
	        OSCPacket.messages_from_network(pa).each{|x| @queue.push(x)}
	      rescue EOFError
	      end
      end
    end
    
    def dispatch_message( message )
      diff = ( message.time || 0 ) - Time.now.to_ntp
      
      if diff <= 0
        sendmesg( message)
      else # spawn a thread to wait until it's time
        Thread.fork do
    	    sleep(diff)
    	    sendmesg(mesg)
    	    Thread.exit
    	  end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aberant-osc-ruby-0.1.6 lib/osc-ruby/server.rb