Class: OSC::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-osc/server.rb

Defined Under Namespace

Classes: Connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, address = "127.0.0.1") ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
# 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

Instance Attribute Details

#addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/ruby-osc/server.rb', line 3

def address
  @address
end

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/ruby-osc/server.rb', line 3

def port
  @port
end

Instance Method Details

#add_pattern(pattern, &block) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
# 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) ⇒ Object



28
29
30
# File 'lib/ruby-osc/server.rb', line 28

def delete_pattern(pattern)
  @patterns.delete pattern
end

#receive(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# 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

#runObject



12
13
14
15
# File 'lib/ruby-osc/server.rb', line 12

def run
  @connection = EventMachine.open_datagram_socket @address, @port, Connection, self
  check_queue
end

#stopObject



17
18
19
20
21
# File 'lib/ruby-osc/server.rb', line 17

def stop
  return unless @connection
  @connection.close_connection
  @timer.cancel
end