Sha256: 6c1f979cfcf859395762955b64cf67c7cf0832cf8cad00ec3d801416da91d3b1

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

require 'eventmachine'

module OSC
  Channel = EM::Channel.new

  class Connection < EventMachine::Connection
    def receive_data data
     ip_info = get_peername[2,6].unpack "nC4"  
     Channel << OSC::OSCPacket.messages_from_network( data, ip_info )
    end
  end

  class EMServer

    def initialize( port = 3333 )
      @port = port
      setup_dispatcher
      @tuples = []
    end

    def run
      EM::run { EM::open_datagram_socket "0.0.0.0",  @port, Connection }
    end

    def add_method(address_pattern, &proc)
      matcher = AddressPattern.new( address_pattern )

      @tuples << [matcher, proc]
    end

  private
    def setup_dispatcher
      Channel.subscribe do  |messages|
        messages.each do |message|
          diff =  ( message.time || 0 ) - Time.now.to_ntp

          if diff <=  0
            sendmesg( message )
          else
            EM.defer  do
              sleep(  diff  )
              sendmesg( message )
            end
          end
        end
      end
    end

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






Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
osc-ruby-1.1.3 lib/osc-ruby/em_server.rb
osc-ruby-1.1.2 lib/osc-ruby/em_server.rb
osc-ruby-1.1.1 lib/osc-ruby/em_server.rb
osc-ruby-1.1.0 lib/osc-ruby/em_server.rb
osc-ruby-1.0.0 lib/osc-ruby/em_server.rb