aberant-osc-ruby 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,16 +2,71 @@
2
2
 
3
3
  http://opensoundcontrol.org/
4
4
 
5
- == GOAL
5
+ == DESCRIPTION
6
6
 
7
- I've used the OSC gem originally created by Tadayoshi Funaba and wanted to update it for ruby 1.9 compatibility. I'm also writing rspec tests for this library to allow others to more easily understand/update the code. It's getting closer to awesome every day...
7
+ I've used the OSC gem originally created by Tadayoshi Funaba and wanted to update it for ruby 1.9 compatibility. I'm also writing rspec tests for this library to allow others to more easily understand/update the code.
8
8
 
9
- Guaranteed to be compatible with tuio-ruby
9
+ Compatible with ruby 1.8, 1.9, and jruby
10
10
 
11
+ == INSTALL
12
+
13
+ sudo gem install aberant-osc-ruby
14
+
15
+ for the EMServer, you will need EventMachine
16
+
17
+ sudo gem install eventmachine
18
+
19
+ == EVENT MACHINE EXAMPLE
20
+
21
+ # compatible with ruby 1.8, 1.9, and jruby
22
+ require 'rubygems'
23
+ require 'osc-ruby'
24
+ require 'osc-ruby/em_server'
25
+
26
+ @server = OSC::EMServer.new( 3333 )
27
+ @client = OSC::Client.new( 'localhost', 3333 )
28
+
29
+ @server.add_method '/greeting' do | message |
30
+ puts message.to_a
31
+ end
32
+
33
+ Thread.new do
34
+ @server.run
35
+ end
36
+
37
+ @client.send( OSC::Message.new( "/greeting" , "hullo!" ))
38
+
39
+ sleep( 3 )
40
+
41
+ == CLASSIC EXAMPLE
42
+
43
+ # compatible with ruby 1.8
44
+ require 'rubygems'
45
+ require 'osc-ruby'
46
+
47
+
48
+ @server = OSC::Server.new( 3333 )
49
+ @client = OSC::Client.new( 'localhost', 3333 )
50
+
51
+ @server.add_method '/greeting' do | message |
52
+ puts message.inspect
53
+ end
54
+
55
+ Thread.new do
56
+ @server.run
57
+ end
58
+
59
+ @client.send( OSC::Message.new( "/greeting", "hullo!" ))
60
+
61
+ sleep( 3 )
62
+
63
+
11
64
  == CREDITS
12
65
 
13
66
  Originally created by...
67
+
14
68
  Tadayoshi Funaba
69
+
15
70
  http://www.funaba.org/en/
16
71
 
17
72
  thx also to Toby Tripp and Obtiva
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 1
4
- :patch: 6
3
+ :minor: 2
4
+ :patch: 0
@@ -4,7 +4,6 @@
4
4
  require 'forwardable'
5
5
  require 'socket'
6
6
  require 'thread'
7
- require 'monitor'
8
7
 
9
8
 
10
9
  $:.unshift( File.dirname( __FILE__ ) )
@@ -1,19 +1,9 @@
1
+ require File.join( File.dirname( __FILE__ ), "packet" )
2
+
1
3
  module OSC
2
4
  class Bundle < Packet
3
- extend Forwardable
4
- include Enumerable
5
-
6
5
  attr_accessor :timetag
7
6
 
8
- undef_method :zip
9
-
10
- # de = (Array.instance_methods - self.instance_methods)
11
- # de -= %w(assoc flatten flatten! pack rassoc transpose)
12
- # de += %w(include? sort)
13
-
14
- # def_delegators(:@args, *de)
15
-
16
-
17
7
  def initialize(timetag=nil, *args)
18
8
  @timetag = timetag
19
9
  @args = args
@@ -0,0 +1,68 @@
1
+ require 'rubygems'
2
+ require 'eventmachine'
3
+ require File.join( File.dirname( __FILE__), '..', 'osc-ruby')
4
+
5
+
6
+ module OSC
7
+ Channel = EM::Channel.new
8
+
9
+ class Connection < EventMachine::Connection
10
+
11
+ def receive_data data
12
+ Channel << OSC::OSCPacket.messages_from_network( data )
13
+ end
14
+ end
15
+
16
+
17
+ class EMServer
18
+
19
+ def initialize( port = 3333 )
20
+ @port = port
21
+ setup_dispatcher
22
+ @tuples = []
23
+ end
24
+
25
+ def run
26
+ EM::run { EM::open_datagram_socket "localhost", @port, Connection }
27
+ end
28
+
29
+ def add_method(address_pattern, &proc)
30
+ matcher = AddressPattern.new( address_pattern )
31
+
32
+ @tuples << [matcher, proc]
33
+ end
34
+
35
+ private
36
+ def setup_dispatcher
37
+ Channel.subscribe do |messages|
38
+ messages.each do |message|
39
+ diff = ( message.time || 0 ) - Time.now.to_ntp
40
+
41
+ if diff <= 0
42
+ sendmesg( message )
43
+ else
44
+ EM.defer do
45
+ sleep( diff )
46
+ sendmesg( message )
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ def sendmesg(mesg)
54
+ @tuples.each do |matcher, obj|
55
+ if matcher.match?( mesg.address )
56
+ obj.call( mesg )
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+
64
+
65
+
66
+
67
+
68
+
@@ -1,18 +1,10 @@
1
+ require File.join( File.dirname( __FILE__ ), "packet" )
2
+
1
3
  module OSC
2
4
  class Message < Packet
3
- extend Forwardable
4
- # include Enumerable
5
-
6
5
  attr_accessor :address
7
6
  attr_accessor :time
8
-
9
- # undef_method :zip
10
7
 
11
- # de = (Array.instance_methods - self.instance_methods)
12
- # de -= %w(assoc flatten flatten! pack rassoc transpose)
13
- # de += %w(include? sort)
14
-
15
- # def_delegators(:@args, *de)
16
8
 
17
9
  def self.new_with_time( address, time, tags=nil, *args )
18
10
  message = new( address, tags, *args )
@@ -20,29 +12,17 @@ module OSC
20
12
  message
21
13
  end
22
14
 
23
- def initialize(address, tags=nil, *args)
15
+ def initialize(address, *args)
24
16
  @address = address
25
17
  @args = []
26
18
 
27
-
28
19
  args.each_with_index do |arg, i|
29
- if tags && tags[i]
30
- case tags[i]
31
- when ?i; @args << OSCInt32.new(arg)
32
- when ?f; @args << OSCFloat32.new(arg)
33
- when ?s; @args << OSCString.new(arg)
34
- when ?b; @args << OSCBlob.new(arg)
35
- when ?*; @args << arg
36
- else; raise ArgumentError, 'unknown type'
37
- end
38
- else
39
- case arg
40
- when Integer; @args << OSCInt32.new(arg)
41
- when Float; @args << OSCFloat32.new(arg)
42
- when String; @args << OSCString.new(arg)
43
- when OSCArgument; @args << arg
44
- end
45
- end
20
+ case arg
21
+ when Integer; @args << OSCInt32.new(arg)
22
+ when Float; @args << OSCFloat32.new(arg)
23
+ when String; @args << OSCString.new(arg)
24
+ when OSCArgument; @args << arg
25
+ end
46
26
  end
47
27
  end
48
28
 
@@ -1,6 +1,6 @@
1
1
  module OSC
2
2
  class NetworkPacket
3
- def initialize(str)
3
+ def initialize(str)
4
4
  @str, @index = str, 0
5
5
  end
6
6
 
@@ -3,6 +3,7 @@ require 'ostruct'
3
3
 
4
4
  module OSC
5
5
  class OSCPacket
6
+
6
7
  def self.messages_from_network( string )
7
8
  messages = []
8
9
  osc = new( string )
@@ -51,10 +52,9 @@ module OSC
51
52
 
52
53
  def get_string
53
54
  result = ''
54
- until (c = @packet.getc) == 0
55
+ until (c = @packet.getc) == string_delemeter
55
56
  result << c
56
57
  end
57
-
58
58
  @packet.skip_padding
59
59
  result
60
60
  end
@@ -109,5 +109,10 @@ module OSC
109
109
  def bundle?
110
110
  !(@packet.to_s =~ /\A\#bundle/).nil?
111
111
  end
112
+
113
+ def string_delemeter
114
+ # ruby 1.9 has multicharacter support
115
+ RUBY_VERSION.include?( '1.9' ) ? "\x00" : 0
116
+ end
112
117
  end
113
118
  end
@@ -1,4 +1,4 @@
1
- require 'osc-ruby/osc_argument'
1
+ require File.join( File.dirname( __FILE__ ), "osc_argument" )
2
2
 
3
3
  module OSC
4
4
  class OSCInt32 < OSCArgument
@@ -55,16 +55,19 @@ private
55
55
  def dispatcher
56
56
  loop do
57
57
  mesg = @queue.pop
58
-
59
58
  dispatch_message( mesg )
60
59
  end
61
60
  end
62
61
 
63
62
  def detector
64
63
  loop do
65
- pa = @socket.recv(16384)
64
+ pa, network = @socket.recvfrom(16384)
66
65
  begin
67
- OSCPacket.messages_from_network(pa).each{|x| @queue.push(x)}
66
+
67
+ OSCPacket.messages_from_network(pa).each do |x|
68
+ @queue.push(x)
69
+ end
70
+
68
71
  rescue EOFError
69
72
  end
70
73
  end
@@ -37,7 +37,7 @@ class MessageBuilder
37
37
  end
38
38
 
39
39
  def build
40
- message = OSC::Message.new( @address , @tags.join, *@values)
40
+ message = OSC::Message.new( @address , *@values)
41
41
  message.time = @time
42
42
  message
43
43
  end
@@ -2,5 +2,5 @@ require File.join( File.dirname(__FILE__) , '..', 'spec_helper' )
2
2
 
3
3
 
4
4
  describe OSC::Bundle do
5
-
5
+
6
6
  end
@@ -26,7 +26,7 @@ describe OSC::Message do
26
26
  end
27
27
 
28
28
  it "should have accept int arguments" do
29
- m = OSC::Message.new( "/hi", "i", 42 )
29
+ m = OSC::Message.new( "/hi", 42 )
30
30
  m.to_a.should == [42]
31
31
  m.tags.should == "i"
32
32
  end
@@ -4,7 +4,6 @@ describe OSC::OSCInt32 do
4
4
  it "should not blow up" do
5
5
  OSC::OSCInt32.new 1
6
6
  end
7
-
8
7
  end
9
8
 
10
9
  describe OSC::OSCFloat32 do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aberant-osc-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aberant
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-29 00:00:00 -07:00
12
+ date: 2009-09-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -32,6 +32,7 @@ files:
32
32
  - lib/osc-ruby/core_ext/numeric.rb
33
33
  - lib/osc-ruby/core_ext/object.rb
34
34
  - lib/osc-ruby/core_ext/time.rb
35
+ - lib/osc-ruby/em_server.rb
35
36
  - lib/osc-ruby/message.rb
36
37
  - lib/osc-ruby/network_packet.rb
37
38
  - lib/osc-ruby/osc_argument.rb