aberant-tuio-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 1
4
+ :major: 0
@@ -0,0 +1,114 @@
1
+ require 'rubygems'
2
+ require 'osc'
3
+
4
+
5
+
6
+ class TUIOClient
7
+ include OSC
8
+
9
+ def initialize( args = {} )
10
+ @host = args[:host] || 'localhost'
11
+ @port = args[:port] || 3333
12
+
13
+ @tuio_objects = { }
14
+ @tuio_cursors = { }
15
+
16
+ @osc = OSC::SimpleServer.new(@port)
17
+
18
+ @osc.add_method '/tuio/2Dobj', nil do |msg|
19
+ args = msg.to_a
20
+
21
+ case args.shift
22
+ when "set"
23
+ update_tuio_objects( args )
24
+ when "alive"
25
+ keep_alive( :tuio_objects, args )
26
+ end
27
+ end
28
+
29
+ @osc.add_method '/tuio/2Dcur', nil do |msg|
30
+ args = msg.to_a
31
+
32
+ case args.shift
33
+ when "set"
34
+ update_tuio_cursors args
35
+ when "alive"
36
+ keep_alive( :tuio_cursors, args )
37
+ end
38
+ end
39
+ end
40
+
41
+ def start
42
+ Thread.fork do
43
+ @osc.run
44
+ end
45
+ end
46
+
47
+ def tuio_objects
48
+ @tuio_objects
49
+ end
50
+
51
+ def tuio_object( id )
52
+ @tuio_objects[id]
53
+ end
54
+
55
+ def tuio_cursors
56
+ @tuio_cursors
57
+ end
58
+
59
+ def tuio_cursor( id )
60
+ @tuio_cursors[id]
61
+ end
62
+
63
+ private
64
+ #TODO clean up these, each_with_index methinks
65
+ def cur_args_to_hash( args )
66
+ {
67
+ :session_id => args[0],
68
+ :x_pos => args[1],
69
+ :y_pos => args[2],
70
+ :x_move => args[3],
71
+ :y_move => args[4],
72
+ :motion_acc => args[5],
73
+ }
74
+ end
75
+
76
+ def obj_args_to_hash( args )
77
+ { :session_id => args[0],
78
+ :class_id => args[1],
79
+ :x_pos => args[2],
80
+ :y_pos => args[3],
81
+ :angle => args[4],
82
+ :x_move => args[5],
83
+ :y_move => args[6],
84
+ :angle_move => args[7],
85
+ :motion_acc => args[8],
86
+ :rotation_acc => args[9],
87
+ }
88
+ end
89
+
90
+ def update_tuio_objects( args )
91
+ tuio_object = obj_args_to_hash( args )
92
+
93
+ @tuio_objects[tuio_object[:session_id]] = tuio_object
94
+ end
95
+
96
+ def update_tuio_cursors( args )
97
+ tuio_cursor = cur_args_to_hash( args )
98
+ @tuio_cursors[tuio_cursor[:session_id]] = tuio_cursor
99
+ end
100
+
101
+ def keep_alive( type, session_ids )
102
+ all_keys = send( type ).keys
103
+
104
+ dead = all_keys.reject { |key| session_ids.include? key }
105
+
106
+ dead.each do |d|
107
+ send( type ).delete( d )
108
+ end
109
+ end
110
+ end
111
+
112
+ if $0 == __FILE__
113
+ TUIOClient.new.start
114
+ end
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rr'
4
+ require 'osc'
5
+ require 'lib/tuio_client'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.mock_with RR::Adapters::Rspec
9
+ end
10
+
11
+ # monkey patch to get at osc core to send messages
12
+ class TUIOClient
13
+ def osc
14
+ @osc
15
+ end
16
+ end
17
+
18
+ # helper method for integration tests
19
+ def send_message( pattern, *msg )
20
+ osc_msg = OSC::Message.new( pattern, nil, *msg)
21
+
22
+ @server.osc.send( :sendmesg, osc_msg )
23
+ end
24
+
25
+ def setup_server
26
+ mock( socket = Object.new )
27
+
28
+ # stub out networking
29
+ stub(socket).bind("", 3333)
30
+ stub(UDPSocket).new { socket }
31
+
32
+ @server = TUIOClient.new
33
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "tuio objects" do
4
+ before :each do
5
+ setup_server
6
+ end
7
+
8
+ it 'should update tracking' do
9
+ send_message( '/tuio/2Dobj', "set", 49, 25, 0.38, 0.35, 3.14, 0.0, 0.0, 0.0, 0.0, 0.0 )
10
+
11
+ @server.tuio_objects.size.should == 1
12
+ @server.tuio_objects[49][:class_id].should == 25
13
+ end
14
+
15
+ it 'should only keep alive the objects the client says are alive' do
16
+ send_message( '/tuio/2Dobj', "set", 49, 25, 0.38, 0.35, 3.14, 0.0, 0.0, 0.0, 0.0, 0.0 )
17
+ send_message( '/tuio/2Dobj', "set", 51, 26, 0.12, 0.50, 3.14, 0.0, 0.0, 0.0, 0.0, 0.0 )
18
+
19
+ send_message( '/tuio/2Dobj', "alive", 49)
20
+
21
+ @server.tuio_objects.size.should == 1
22
+ end
23
+
24
+ end
25
+
26
+ describe "tuio_cursors" do
27
+ before :each do
28
+ setup_server
29
+ end
30
+
31
+ it 'should update tracking' do
32
+ send_message( '/tuio/2Dcur', "set", 22, 0.38, 0.35, 0.0, 0.0, 0.0 )
33
+
34
+ @server.tuio_cursors.size.should == 1
35
+ end
36
+
37
+ it 'should update tracking for multiples' do
38
+ send_message( '/tuio/2Dcur', "set", 22, 0.38, 0.35, 0.0, 0.0, 0.0 )
39
+ send_message( '/tuio/2Dcur', "set", 27, 0.12, 0.50, 0.0, 0.0, 0.0 )
40
+
41
+
42
+ @server.tuio_cursors.size.should == 2
43
+ end
44
+
45
+ it 'should only keep alive the cursors the client says are alive' do
46
+ send_message( '/tuio/2Dcur', "set", 22, 0.38, 0.35, 0.0, 0.0, 0.0 )
47
+ send_message( '/tuio/2Dcur', "set", 27, 0.12, 0.50, 0.0, 0.0, 0.0 )
48
+
49
+ send_message( '/tuio/2Dcur', "alive", 22)
50
+
51
+ @server.tuio_cursors.size.should == 1
52
+ @server.tuio_cursors[22][:session_id].should == 22
53
+
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aberant-tuio-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - aberant
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: qzzzq1@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - VERSION.yml
26
+ - lib/tuio_client.rb
27
+ - spec/spec_helper.rb
28
+ - spec/tuio_event_spec.rb
29
+ - README
30
+ has_rdoc: true
31
+ homepage: http://github.com/aberant/tuio-ruby
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --inline-source
35
+ - --charset=UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: inital gem
57
+ test_files: []
58
+