phidgets-ffi 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'phidgets-ffi'
3
+
4
+ Phidgets::Log.enable(:verbose)
5
+
6
+ puts "Library Version: #{Phidgets::FFI.library_version}"
7
+ Phidgets::Dictionary.new do |dict|
8
+ dict.on_error do |obj, code, reason|
9
+ puts "Error (#{code}): #{reason}"
10
+ end
11
+ dict.on_connect do |obj|
12
+ print "Connected!\n"
13
+ end
14
+ dict.on_disconnect do |obj|
15
+ print "Disconnected!\n"
16
+ end
17
+
18
+ dict.on_change('fnord') do |obj, key, val, reason|
19
+ print "Fnord key: #{key} => #{val}, #{reason}\n"
20
+ end
21
+ dict.on_change do |obj, key, val, reason|
22
+ print "Every key: #{key} => #{val}, #{reason}\n"
23
+ end
24
+ dict.on_change('test') do |obj, key, val, reason|
25
+ print "Keys matching test: #{key} => #{val}, #{reason}\n"
26
+ end
27
+
28
+ puts "Listening to: #{dict.listeners.inspect}"
29
+
30
+ dict.remove_on_change('fnord')
31
+
32
+ puts "Listening to: #{dict.listeners.inspect}"
33
+
34
+ dict["fnord"] = "This is a fnordic key"
35
+ dict["test"] = "This is a key that is called test"
36
+ dict["fnord"] = nil
37
+ dict["snoop"] = nil
38
+ 20.times do |i|
39
+ print "#{i}\n"
40
+ begin
41
+ dict["testerson"] = "This is another test matching key"
42
+ rescue Phidgets::Error::NetworkNotConnected
43
+ end
44
+ sleep 1
45
+ end
46
+ end
@@ -11,6 +11,7 @@ require 'phidgets-ffi/ffi/interface_kit'
11
11
 
12
12
  require 'phidgets-ffi/common'
13
13
  require 'phidgets-ffi/log'
14
+ require 'phidgets-ffi/dictionary'
14
15
  require 'phidgets-ffi/error'
15
16
  require 'phidgets-ffi/manager'
16
17
  require 'phidgets-ffi/servo'
@@ -0,0 +1,161 @@
1
+ module Phidgets
2
+ class Dictionary
3
+
4
+ Klass = Phidgets::FFI::CPhidgetDictionary
5
+ include Utility
6
+
7
+ def initialize(options={}, &block)
8
+ @listeners = {}
9
+ @options = {:address => 'localhost', :port => 5001, :server_id => nil, :password => nil}.merge(options)
10
+
11
+ create
12
+ if block_given?
13
+ open(@options)
14
+ yield self
15
+ close
16
+ delete_dict
17
+ end
18
+ end
19
+
20
+ def create
21
+ ptr = ::FFI::MemoryPointer.new(:pointer, 1)
22
+ Klass.create(ptr)
23
+ @handle = ptr.get_pointer(0)
24
+ true
25
+ end
26
+
27
+ def open(options)
28
+ password = (options[:password].nil? ? nil : options[:password].to_s)
29
+ if !options[:server_id].nil?
30
+ Klass.openRemote(@handle, options[:server_id].to_s, password)
31
+ else
32
+ Klass.openRemoteIP(@handle, options[:address].to_s, options[:port].to_i, password)
33
+ end
34
+ sleep 1
35
+ true
36
+ end
37
+
38
+ def close
39
+ Klass.close(@handle)
40
+ true
41
+ end
42
+
43
+ def delete_dict
44
+ Klass.delete(@handle)
45
+ true
46
+ end
47
+
48
+ def []=(key, val, persistent=false)
49
+ # If we are assigning something to nil, let's remove it
50
+ if val.nil?
51
+ delete(key)
52
+ else
53
+ persistent = (persistent ? 1 : 0)
54
+ Klass.addKey(@handle, key.to_s, val.to_s, persistent)
55
+ val.to_s
56
+ end
57
+ end
58
+ alias_method :add, :[]=
59
+ alias_method :put, :[]=
60
+
61
+ def [](key)
62
+ ptr = ::FFI::MemoryPointer.new(:string, 8192)
63
+ Klass.getKey(@handle, key, ptr, 8192)
64
+ ptr.get_string(0)
65
+ end
66
+ alias_method :get, :[]
67
+
68
+ def delete(pattern)
69
+ Klass.removeKey(@handle, (pattern.kind_of?(Regexp) ? pattern.source : pattern.to_s))
70
+ true
71
+ end
72
+
73
+ def on_connect(obj=nil, &block)
74
+ @on_connect_obj = obj
75
+ @on_connect = Proc.new { |handle, obj_ptr|
76
+ # On connect, we'll need to re-add all of our change handlers
77
+ @listeners.each_pair do |pattern, (listener, proc)|
78
+ begin
79
+ next if status != :connected
80
+ Klass.set_OnKeyChange_Handler(@handle, listener, pattern, proc, pointer_for(obj))
81
+ sleep 0.5
82
+ rescue
83
+ Phidgets::Log.error("#{self.class}::on_connect", $!.to_s)
84
+ end
85
+ end
86
+ yield object_for(obj_ptr)
87
+ }
88
+ Klass.set_OnServerConnect_Handler(@handle, @on_connect, pointer_for(obj))
89
+ end
90
+
91
+ def on_disconnect(obj=nil, &block)
92
+ @on_disconnect_obj = obj
93
+ @on_disconnect = Proc.new { |handle, obj_ptr|
94
+ # On disconnect, we'll need to remove all of our change handlers
95
+ @listeners.each_pair do |pattern, (listener, proc)|
96
+ Klass.remove_OnKeyChange_Handler(listener.get_pointer(0))
97
+ end
98
+ yield object_for(obj_ptr)
99
+ }
100
+ Klass.set_OnServerDisconnect_Handler(@handle, @on_disconnect, pointer_for(obj))
101
+ end
102
+
103
+ def on_error(obj=nil, &block)
104
+ @on_error_obj = obj
105
+ @on_error = Proc.new { |handle, obj_ptr, code, error|
106
+ yield object_for(obj_ptr), code, error
107
+ }
108
+ Klass.set_OnError_Handler(@handle, @on_error, pointer_for(obj))
109
+ end
110
+
111
+
112
+ def on_change(pattern=".*", obj=nil, &block)
113
+ pattern = (pattern.kind_of?(Regexp) ? pattern.source : pattern.to_s)
114
+ @listeners[pattern] = [
115
+ ::FFI::MemoryPointer.new(:pointer),
116
+ Proc.new { |handle, obj_ptr, key, value, reason|
117
+ yield object_for(obj_ptr), key, value, reason
118
+ }
119
+ ]
120
+ Klass.set_OnKeyChange_Handler(@handle, @listeners[pattern][0], pattern, @listeners[pattern][1], pointer_for(obj))
121
+ sleep 0.5
122
+ end
123
+
124
+ def remove_on_change(pattern)
125
+ pattern = (pattern.kind_of?(Regexp) ? pattern.source : pattern.to_s)
126
+ if @listeners.has_key?(pattern)
127
+ listener, proc = @listeners.delete(pattern)
128
+ Klass.remove_OnKeyChange_Handler(listener.get_pointer(0))
129
+ sleep 0.5
130
+ true
131
+ else
132
+ nil
133
+ end
134
+ end
135
+
136
+ def listeners
137
+ @listeners.keys
138
+ end
139
+
140
+ def server_id
141
+ ptr = ::FFI::MemoryPointer.new(:string)
142
+ Klass.getServerID(@handle, ptr)
143
+ ptr.get_string(0)
144
+ end
145
+
146
+ def status
147
+ ptr = ::FFI::MemoryPointer.new(:int)
148
+ Klass.getServerStatus(@handle, ptr)
149
+ Phidgets::FFI::ServerStatus[ptr.get_int(0)]
150
+ end
151
+
152
+ def server_address
153
+ str_ptr, int_ptr = ::FFI::MemoryPointer.new(:string), ::FFI::MemoryPointer.new(:int)
154
+ Klass.getServerID(@handle, str_ptr, int_ptr)
155
+ strPtr = str_ptr.get_pointer(0)
156
+ address = (strPtr.null? ? nil : strPtr.read_string)
157
+ port = int_ptr.get_int(0)
158
+ [address, port]
159
+ end
160
+ end
161
+ end
@@ -3,6 +3,11 @@ module Phidgets
3
3
  typedef :pointer, :phid
4
4
  typedef :pointer, :user_ptr
5
5
 
6
+ ServerStatus = enum(
7
+ :disconnected, 0,
8
+ :connected
9
+ )
10
+
6
11
  DeviceStatus = enum(
7
12
  :detached, 0,
8
13
  :attached
@@ -8,7 +8,10 @@ module Phidgets
8
8
  :removed,
9
9
  :unchanged
10
10
  )
11
+
11
12
  attach_function :CPhidgetDictionary_create, [:dict], :int #int CPhidgetDictionary_create(CPhidgetDictionaryHandle *dict);
13
+ attach_function :CPhidgetDictionary_openRemote, [:dict, :string, :string], :int #int CPhidgetDictionary_openRemote(CPhidgetDictionaryHandle dict, const char *serverID, const char *password);
14
+ attach_function :CPhidgetDictionary_openRemoteIP, [:dict, :string, :int, :string], :int #int CPhidgetDictionary_openRemoteIP(CPhidgetDictionaryHandle dict, const char *address, int port, const char *password);
12
15
  attach_function :CPhidgetDictionary_close, [:dict], :int #int CPhidgetDictionary_close(CPhidgetDictionaryHandle dict);
13
16
  attach_function :CPhidgetDictionary_delete, [:dict], :int #int CPhidgetDictionary_delete(CPhidgetDictionaryHandle dict);
14
17
  callback :CPhidgetDictionary_set_OnError_Callback, [:dict, :pointer, :int, :string], :int
@@ -23,8 +26,6 @@ module Phidgets
23
26
  attach_function :CPhidgetDictionary_getServerID, [:dict, :pointer], :int #int CPhidgetDictionary_getServerID(CPhidgetDictionaryHandle dict, const char **serverID);
24
27
  attach_function :CPhidgetDictionary_getServerAddress, [:dict, :pointer, :pointer], :int #int CPhidgetDictionary_getServerAddress(CPhidgetDictionaryHandle dict, const char **address, int *port);
25
28
  attach_function :CPhidgetDictionary_getServerStatus, [:dict, :pointer], :int #int CPhidgetDictionary_getServerStatus(CPhidgetDictionaryHandle dict, int *serverStatus);
26
- attach_function :CPhidgetDictionary_openRemote, [:dict, :string, :string], :int #int CPhidgetDictionary_openRemote(CPhidgetDictionaryHandle dict, const char *serverID, const char *password);
27
- attach_function :CPhidgetDictionary_openRemoteIP, [:dict, :string, :int, :string], :int #int CPhidgetDictionary_openRemoteIP(CPhidgetDictionaryHandle dict, const char *address, int port, const char *password);
28
29
  callback :CPhidgetDictionary_set_OnKeyChange_Callback, [:dict, :pointer, :string, :string, KeyChangeReason], :int
29
30
  attach_function :CPhidgetDictionary_set_OnKeyChange_Handler, [:dict, :pointer, :string, :CPhidgetDictionary_set_OnKeyChange_Callback, :pointer], :int #int CPhidgetDictionary_set_OnKeyChange_Handler(CPhidgetDictionaryHandle dict, CPhidgetDictionaryListenerHandle *dictlistener, const char *pattern, CPhidgetDictionary_OnKeyChange_Function fptr, void *userPtr);
30
31
  attach_function :CPhidgetDictionary_remove_OnKeyChange_Handler, [:dict], :int #int CPhidgetDictionary_remove_OnKeyChange_Handler(CPhidgetDictionaryListenerHandle dictlistener);
@@ -1,5 +1,5 @@
1
1
  module Phidgets
2
2
  module FFI
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phidgets-ffi
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelley Reynolds
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-13 00:00:00 -04:00
18
+ date: 2011-07-17 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -65,6 +65,7 @@ files:
65
65
  - LICENSE
66
66
  - README.rdoc
67
67
  - Rakefile
68
+ - examples/dictionary.rb
68
69
  - examples/ffi/dictionary.rb
69
70
  - examples/ffi/interface_kit.rb
70
71
  - examples/ffi/library_version.rb
@@ -77,6 +78,7 @@ files:
77
78
  - examples/servo.rb
78
79
  - lib/phidgets-ffi.rb
79
80
  - lib/phidgets-ffi/common.rb
81
+ - lib/phidgets-ffi/dictionary.rb
80
82
  - lib/phidgets-ffi/error.rb
81
83
  - lib/phidgets-ffi/ffi/common.rb
82
84
  - lib/phidgets-ffi/ffi/constants.rb