lib/osc-ruby/server.rb in osc-ruby-1.1.3 vs lib/osc-ruby/server.rb in osc-ruby-1.1.4

- old
+ new

@@ -1,10 +1,10 @@ module OSC - class Server - def initialize( port ) + class Server + def initialize(port) @socket = UDPSocket.new - @socket.bind( '', port ) + @socket.bind('', port) @matchers = [] @queue = Queue.new end def run @@ -15,78 +15,77 @@ def stop @socket.close end - def add_method( address_pattern, &proc ) - matcher = AddressPattern.new( address_pattern ) + def add_method(address_pattern, &proc) + matcher = AddressPattern.new(address_pattern) @matchers << [matcher, proc] end -private + private def start_detector begin - detector + detector rescue - Thread.main.raise $! + Thread.main.raise($!) end end def start_dispatcher Thread.fork do - begin - dispatcher - rescue - Thread.main.raise $! - end + begin + dispatcher + rescue + Thread.main.raise($!) + end end end def dispatcher loop do - mesg = @queue.pop - dispatch_message( mesg ) + mesg = @queue.pop + dispatch_message(mesg) end end - def dispatch_message( message ) - diff = ( message.time || 0 ) - Time.now.to_ntp + def dispatch_message(message) + diff = (message.time || 0) - Time.now.to_ntp if diff <= 0 - sendmesg( message) + sendmesg(message) else # spawn a thread to wait until it's time Thread.fork do - sleep( diff ) - sendmesg( mesg ) - Thread.exit - end + sleep(diff) + sendmesg(mesg) + Thread.exit + end end end def sendmesg(mesg) @matchers.each do |matcher, proc| - if matcher.match?( mesg.address ) - proc.call( mesg ) - end + if matcher.match?(mesg.address) + proc.call(mesg) + end end end def detector loop do - osc_data, network = @socket.recvfrom( 16384 ) - begin + osc_data, network = @socket.recvfrom(16384) + begin ip_info = Array.new ip_info << network[1] ip_info.concat(network[2].split('.')) - OSCPacket.messages_from_network( osc_data, ip_info ).each do |message| - @queue.push(message) + OSCPacket.messages_from_network( osc_data, ip_info ).each do |message| + @queue.push(message) end - rescue EOFError - end + rescue(EOFError) + end end end - end end \ No newline at end of file