osc-ruby 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,28 +1,53 @@
1
- require File.join( File.dirname( __FILE__ ), "osc_argument" )
1
+ require File.join(File.dirname(__FILE__), "osc_argument")
2
2
 
3
3
  module OSC
4
4
  class OSCInt32 < OSCArgument
5
- def tag() 'i' end
6
- def encode() [@val].pack('N').force_encoding("BINARY") end
5
+ def tag
6
+ 'i'
7
+ end
8
+
9
+ def encode
10
+ [@val].pack('N').force_encoding("BINARY")
11
+ end
7
12
  end
8
13
 
9
14
  class OSCFloat32 < OSCArgument
10
- def tag() 'f' end
11
- def encode() [@val].pack('g').force_encoding("BINARY") end
15
+ def tag
16
+ 'f'
17
+ end
18
+
19
+ def encode
20
+ [@val].pack('g').force_encoding("BINARY")
21
+ end
12
22
  end
13
23
 
14
24
  class OSCDouble64 < OSCArgument
15
- def tag() 'd' end
16
- def encode() [@val].pack('G').force_encoding("BINARY") end
25
+ def tag
26
+ 'd'
27
+ end
28
+
29
+ def encode
30
+ [@val].pack('G').force_encoding("BINARY")
31
+ end
17
32
  end
18
33
 
19
34
  class OSCString < OSCArgument
20
- def tag() 's' end
21
- def encode() padding(@val.sub(/\000.*\z/, '') + "\000").force_encoding("BINARY") end
35
+ def tag
36
+ 's'
37
+ end
38
+
39
+ def encode
40
+ padding(@val.sub(/\000.*\z/, '') + "\000").force_encoding("BINARY")
41
+ end
22
42
  end
23
43
 
24
44
  class OSCBlob < OSCArgument
25
- def tag() 'b' end
26
- def encode() padding([@val.size].pack('N') + @val).force_encoding("BINARY") end
45
+ def tag
46
+ 'b'
47
+ end
48
+
49
+ def encode
50
+ padding([@val.size].pack('N') + @val).force_encoding("BINARY")
51
+ end
27
52
  end
28
53
  end
@@ -1,8 +1,8 @@
1
1
  module OSC
2
- class Server
3
- def initialize( port )
2
+ class Server
3
+ def initialize(port)
4
4
  @socket = UDPSocket.new
5
- @socket.bind( '', port )
5
+ @socket.bind('', port)
6
6
  @matchers = []
7
7
  @queue = Queue.new
8
8
  end
@@ -17,76 +17,75 @@ module OSC
17
17
  @socket.close
18
18
  end
19
19
 
20
- def add_method( address_pattern, &proc )
21
- matcher = AddressPattern.new( address_pattern )
20
+ def add_method(address_pattern, &proc)
21
+ matcher = AddressPattern.new(address_pattern)
22
22
 
23
23
  @matchers << [matcher, proc]
24
24
  end
25
25
 
26
- private
26
+ private
27
27
 
28
28
  def start_detector
29
29
  begin
30
- detector
30
+ detector
31
31
  rescue
32
- Thread.main.raise $!
32
+ Thread.main.raise($!)
33
33
  end
34
34
  end
35
35
 
36
36
  def start_dispatcher
37
37
  Thread.fork do
38
- begin
39
- dispatcher
40
- rescue
41
- Thread.main.raise $!
42
- end
38
+ begin
39
+ dispatcher
40
+ rescue
41
+ Thread.main.raise($!)
42
+ end
43
43
  end
44
44
  end
45
45
 
46
46
  def dispatcher
47
47
  loop do
48
- mesg = @queue.pop
49
- dispatch_message( mesg )
48
+ mesg = @queue.pop
49
+ dispatch_message(mesg)
50
50
  end
51
51
  end
52
52
 
53
- def dispatch_message( message )
54
- diff = ( message.time || 0 ) - Time.now.to_ntp
53
+ def dispatch_message(message)
54
+ diff = (message.time || 0) - Time.now.to_ntp
55
55
 
56
56
  if diff <= 0
57
- sendmesg( message)
57
+ sendmesg(message)
58
58
  else # spawn a thread to wait until it's time
59
59
  Thread.fork do
60
- sleep( diff )
61
- sendmesg( mesg )
62
- Thread.exit
63
- end
60
+ sleep(diff)
61
+ sendmesg(mesg)
62
+ Thread.exit
63
+ end
64
64
  end
65
65
  end
66
66
 
67
67
  def sendmesg(mesg)
68
68
  @matchers.each do |matcher, proc|
69
- if matcher.match?( mesg.address )
70
- proc.call( mesg )
71
- end
69
+ if matcher.match?(mesg.address)
70
+ proc.call(mesg)
71
+ end
72
72
  end
73
73
  end
74
74
 
75
75
  def detector
76
76
  loop do
77
- osc_data, network = @socket.recvfrom( 16384 )
78
- begin
77
+ osc_data, network = @socket.recvfrom(16384)
78
+ begin
79
79
  ip_info = Array.new
80
80
  ip_info << network[1]
81
81
  ip_info.concat(network[2].split('.'))
82
- OSCPacket.messages_from_network( osc_data, ip_info ).each do |message|
83
- @queue.push(message)
82
+ OSCPacket.messages_from_network( osc_data, ip_info ).each do |message|
83
+ @queue.push(message)
84
84
  end
85
85
 
86
- rescue EOFError
87
- end
86
+ rescue(EOFError)
87
+ end
88
88
  end
89
89
  end
90
-
91
90
  end
92
91
  end
@@ -1,56 +1,55 @@
1
1
  class MessageBuilder
2
-
3
2
  def initialize
4
3
  @address = ""
5
4
  @tags = []
6
5
  @values = []
7
6
  @time = nil
8
7
  end
9
-
10
- def with_address( addr )
8
+
9
+ def with_address(addr)
11
10
  @address = addr
12
11
  self
13
12
  end
14
-
15
- def with_float( float )
16
- with_arg( "f", float )
13
+
14
+ def with_float(float)
15
+ with_arg("f", float)
17
16
  self
18
17
  end
19
18
 
20
- def with_double( double )
21
- with_arg( "d", double)
19
+ def with_double(double)
20
+ with_arg("d", double)
22
21
  self
23
22
  end
24
-
25
- def with_int( int )
26
- with_arg( "i", int )
23
+
24
+ def with_int(int)
25
+ with_arg("i", int)
27
26
  self
28
27
  end
29
-
30
- def with_string( string )
31
- with_arg( "s", string )
28
+
29
+ def with_string(string)
30
+ with_arg("s", string)
32
31
  self
33
32
  end
34
-
35
- def with_blob( blob )
36
- with_arg( "b", blob )
33
+
34
+ def with_blob(blob)
35
+ with_arg( "b", blob)
37
36
  self
38
37
  end
39
-
40
- def with_time( time )
38
+
39
+ def with_time(time)
41
40
  @time = time
42
41
  end
43
-
42
+
44
43
  def build
45
- message = OSC::Message.new( @address , *@values)
44
+ message = OSC::Message.new(@address , *@values)
46
45
  message.time = @time
47
46
  message
48
47
  end
49
-
50
- private
51
48
 
52
- def with_arg( tag, value )
53
- @tags << tag
54
- @values << value
49
+ private
50
+
51
+ def with_arg(tag, value)
52
+ @tags << tag
53
+ @values << value
55
54
  end
56
55
  end
@@ -1,4 +1,4 @@
1
- require File.join( File.dirname(__FILE__) , '..', 'spec_helper' )
1
+ require File.join(File.dirname(__FILE__) , '..', 'spec_helper')
2
2
 
3
3
  describe OSC::AddressPattern do
4
4
  # it "..." do
@@ -13,81 +13,81 @@ describe OSC::AddressPattern do
13
13
  # end
14
14
 
15
15
  it "should match anything if the pattern is nil" do
16
- ap = OSC::AddressPattern.new( nil )
16
+ ap = OSC::AddressPattern.new(nil)
17
17
 
18
- ap.match?( "/some/nonsense" ).must_equal true
19
- ap.match?( "/completely.different" ).must_equal true
18
+ _(ap.match?("/some/nonsense")).must_equal(true)
19
+ _(ap.match?("/completely.different")).must_equal(true)
20
20
  end
21
21
 
22
22
  it "should match based on a regex" do
23
- ap = OSC::AddressPattern.new( /hi/ )
23
+ ap = OSC::AddressPattern.new(/hi/)
24
24
 
25
- ap.match?( '/hi' ).must_equal true
26
- ap.match?( '/hidden' ).must_equal true
25
+ _(ap.match?('/hi')).must_equal(true)
26
+ _(ap.match?('/hidden')).must_equal(true)
27
27
 
28
- ap.match?( '/bye' ).must_equal false
28
+ _(ap.match?('/bye')).must_equal(false)
29
29
  end
30
30
 
31
31
  it "should return a regex if the pattern is a string" do
32
- ap = OSC::AddressPattern.new( "/hi" )
32
+ ap = OSC::AddressPattern.new("/hi")
33
33
 
34
- ap.match?( '/hi' ).must_equal true
34
+ _(ap.match?('/hi')).must_equal(true)
35
35
 
36
- ap.match?( ' /hi' ).must_equal false
37
- ap.match?( '/ahi' ).must_equal false
38
- ap.match?( '/hidden' ).must_equal false
39
- ap.match?( '/bye' ).must_equal false
36
+ _(ap.match?(' /hi')).must_equal(false)
37
+ _(ap.match?('/ahi')).must_equal(false)
38
+ _(ap.match?('/hidden')).must_equal(false)
39
+ _(ap.match?('/bye')).must_equal(false)
40
40
  end
41
41
 
42
42
  it "should match with question mark" do
43
- ap = OSC::AddressPattern.new( "/h?l" )
44
-
45
- ap.match?( '/hal' ).must_equal true
46
- ap.match?( '/hel' ).must_equal true
47
- ap.match?( '/hil' ).must_equal true
48
- ap.match?( '/hol' ).must_equal true
49
- ap.match?( '/hul' ).must_equal true
50
- ap.match?( '/hub' ).must_equal false
43
+ ap = OSC::AddressPattern.new("/h?l")
44
+
45
+ _(ap.match?('/hal')).must_equal(true)
46
+ _(ap.match?('/hel')).must_equal(true)
47
+ _(ap.match?('/hil')).must_equal(true)
48
+ _(ap.match?('/hol')).must_equal(true)
49
+ _(ap.match?('/hul')).must_equal(true)
50
+ _(ap.match?('/hub')).must_equal(false)
51
51
  end
52
52
 
53
53
  it "should match with *" do
54
- ap = OSC::AddressPattern.new( "/believ*d" )
54
+ ap = OSC::AddressPattern.new("/believ*d")
55
55
 
56
- ap.match?( '/believd' ).must_equal true
57
- ap.match?( '/believed' ).must_equal true
58
- ap.match?( '/believeeed' ).must_equal true
59
- ap.match?( '/believaeeeioud' ).must_equal true
60
- ap.match?( '/believaeeeioud' ).must_equal true
56
+ _(ap.match?('/believd')).must_equal(true)
57
+ _(ap.match?('/believed')).must_equal(true)
58
+ _(ap.match?('/believeeed')).must_equal(true)
59
+ _(ap.match?('/believaeeeioud')).must_equal(true)
60
+ _(ap.match?('/believaeeeioud')).must_equal(true)
61
61
  end
62
62
 
63
63
  it "should match with []" do
64
- ap = OSC::AddressPattern.new( "/believ[aeiou]d" )
65
-
66
- ap.match?( '/believad' ).must_equal true
67
- ap.match?( '/believed' ).must_equal true
68
- ap.match?( '/believid' ).must_equal true
69
- ap.match?( '/believod' ).must_equal true
70
- ap.match?( '/believud' ).must_equal true
71
- ap.match?( '/believkd' ).must_equal false
64
+ ap = OSC::AddressPattern.new("/believ[aeiou]d")
65
+
66
+ _(ap.match?('/believad')).must_equal(true)
67
+ _(ap.match?('/believed')).must_equal(true)
68
+ _(ap.match?('/believid')).must_equal(true)
69
+ _(ap.match?('/believod')).must_equal(true)
70
+ _(ap.match?('/believud')).must_equal(true)
71
+ _(ap.match?('/believkd')).must_equal(false)
72
72
  end
73
73
 
74
74
  it "should match with [!]" do
75
- ap = OSC::AddressPattern.new( "/believ[!aeiou]d" )
76
-
77
- ap.match?( '/believad' ).must_equal false
78
- ap.match?( '/believed' ).must_equal false
79
- ap.match?( '/believid' ).must_equal false
80
- ap.match?( '/believod' ).must_equal false
81
- ap.match?( '/believud' ).must_equal false
82
- ap.match?( '/believkd' ).must_equal true
83
- ap.match?( '/believzd' ).must_equal true
75
+ ap = OSC::AddressPattern.new("/believ[!aeiou]d")
76
+
77
+ _(ap.match?('/believad')).must_equal(false)
78
+ _(ap.match?('/believed')).must_equal(false)
79
+ _(ap.match?('/believid')).must_equal(false)
80
+ _(ap.match?('/believod')).must_equal(false)
81
+ _(ap.match?('/believud')).must_equal(false)
82
+ _(ap.match?('/believkd')).must_equal(true)
83
+ _(ap.match?('/believzd')).must_equal(true)
84
84
  end
85
85
 
86
86
  it "should match with {}" do
87
- ap = OSC::AddressPattern.new( "/{hi,bye}" )
87
+ ap = OSC::AddressPattern.new("/{hi,bye}")
88
88
 
89
- ap.match?( '/hi' ).must_equal true
90
- ap.match?( '/bye' ).must_equal true
91
- ap.match?( '/greetings' ).must_equal false
89
+ _(ap.match?('/hi')).must_equal(true)
90
+ _(ap.match?('/bye')).must_equal(true)
91
+ _(ap.match?('/greetings')).must_equal(false)
92
92
  end
93
93
  end
@@ -1,45 +1,43 @@
1
- # sanity checks
2
1
  require File.join( File.dirname(__FILE__) , '..', 'spec_helper' )
3
2
 
4
3
  describe MessageBuilder do
5
4
  before :each do
6
5
  @builder = MessageBuilder.new
7
6
  end
8
-
7
+
9
8
  it "encodes just the address" do
10
9
  mesg = @builder.with_address("/hi")
11
- mesg.build.encode.must_equal binary_string("/hi\000,\000\000\000")
10
+ _(mesg.build.encode).must_equal binary_string("/hi\000,\000\000\000")
12
11
  end
13
-
12
+
14
13
  it "encodes single int values" do
15
14
  mesg = @builder.with_address("/hi").
16
- with_int(33)
17
-
18
- mesg.build.encode.must_equal binary_string("/hi\000,i\000\000\000\000\000!")
15
+ with_int(33)
16
+ _(mesg.build.encode).must_equal binary_string("/hi\000,i\000\000\000\000\000!")
19
17
  end
20
-
18
+
21
19
  it "encodes single string values" do
22
20
  mesg = @builder.with_address("/hi").
23
- with_string("hello")
24
-
25
- mesg.build.encode.must_equal binary_string("/hi\000,s\000\000hello\000\000\000")
21
+ with_string("hello")
22
+
23
+ _(mesg.build.encode).must_equal binary_string("/hi\000,s\000\000hello\000\000\000")
26
24
  end
27
-
25
+
28
26
  it "encodes single float values" do
29
27
  mesg = @builder.with_address("/hi").
30
- with_float(3.14159)
31
-
32
- mesg.build.encode.must_equal binary_string("/hi\000,f\000\000@I\017\320")
28
+ with_float(3.14159)
29
+
30
+ _(mesg.build.encode).must_equal binary_string("/hi\000,f\000\000@I\017\320")
33
31
  end
34
-
32
+
35
33
  it "encodes multiple floats" do
36
34
  mesg = @builder.with_address("/hi").
37
- with_float(3.14159).
38
- with_float(4.5)
39
-
40
- mesg.build.encode.must_equal [47, 104, 105, 0, 44, 102, 102, 0, 64, 73, 15, 208, 64, 144, 0, 0].pack("C*")
35
+ with_float(3.14159).
36
+ with_float(4.5)
37
+
38
+ _(mesg.build.encode).must_equal [47, 104, 105, 0, 44, 102, 102, 0, 64, 73, 15, 208, 64, 144, 0, 0].pack("C*")
41
39
  end
42
-
40
+
43
41
  def binary_string(string)
44
42
  string.force_encoding("BINARY")
45
43
  end