RingyDingy 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,12 @@
1
+ = 1.1.0
2
+
3
+ * Added ring_server executable (Rinda::RingServer wrapper)
4
+ * Daemon mode
5
+ * Rinda::RingServer service listing
6
+ * Remote verbose mode enable-disable
7
+ * Switched to Hoe
8
+
9
+ = 1.0.0
10
+
11
+ Birthday!
12
+
File without changes
data/Manifest.txt CHANGED
@@ -1,7 +1,10 @@
1
- CHANGES
2
- LICENSE
1
+ History.txt
2
+ LICENSE.txt
3
3
  Manifest.txt
4
- README
4
+ README.txt
5
5
  Rakefile
6
+ bin/ring_server
6
7
  lib/ringy_dingy.rb
8
+ lib/ringy_dingy/ring_server.rb
9
+ test/test_ring_server.rb
7
10
  test/test_ringy_dingy.rb
@@ -19,6 +19,10 @@ will re-register its service with the RingServer when it reappears.
19
19
  Similarly, the RingServer will automatically drop registrations by a RingyDingy
20
20
  that it can't communicate with after a short timeout.
21
21
 
22
+ RingyDingy also includes a RingServer wrapper that adds verbose mode to see
23
+ what services as they register and expire and an option to list all available
24
+ services on the network.
25
+
22
26
  == Installing RingyDingy
23
27
 
24
28
  Just install the gem:
@@ -36,3 +40,18 @@ Just install the gem:
36
40
  RingyDingy.new(my_drb_service).run
37
41
 
38
42
  DRb.thread.join
43
+
44
+ == Using RingyDingy::RingServer
45
+
46
+ To start a RingServer:
47
+
48
+ $ ring_server
49
+
50
+ To list services on the network:
51
+
52
+ $ ring_server -l
53
+
54
+ To enable or disable verbose mode remotely:
55
+
56
+ $ ring_server --set-logging=true/false
57
+
data/Rakefile CHANGED
@@ -1,56 +1,17 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'rake/testtask'
4
- require 'rake/rdoctask'
5
- require 'rake/gempackagetask'
1
+ require 'hoe'
6
2
 
7
- $VERBOSE = nil
3
+ require './lib/ringy_dingy.rb'
8
4
 
9
- spec = Gem::Specification.new do |s|
10
- s.name = 'RingyDingy'
11
- s.version = '1.0.0'
12
- s.summary = 'RingyDingy is a little boat that keeps your DRb service afloat!'
13
- s.description = 'RingyDingy automatically re-registers your DRb service with a RingServer should communication with the RingServer stop.'
14
- s.author = 'Eric Hodel'
15
- s.email = 'drbrain@segment7.net'
5
+ Hoe.new 'RingyDingy', RingyDingy::VERSION do |p|
6
+ p.summary = 'RingyDingy is a little boat that keeps your DRb service afloat!'
7
+ p.description = 'RingyDingy automatically re-registers your DRb service with a RingServer should communication with the RingServer stop.'
8
+ p.url = 'http://seattlerb.rubyforge.org/RingyDingy'
9
+ p.author = 'Eric Hodel'
10
+ p.email = 'drbrain@segment7.net'
11
+ p.rubyforge_name = 'seattlerb'
12
+ p.changes = File.read('History.txt').scan(/\A(=.*?)(=|\Z)/m).first.first
16
13
 
17
- s.has_rdoc = true
18
- s.files = File.read('Manifest.txt').split($/)
19
- s.require_path = 'lib'
14
+ p.extra_deps << ['ZenTest', '>= 3.4.0']
20
15
  end
21
16
 
22
- desc 'Run tests'
23
- task :default => [ :test ]
24
-
25
- Rake::TestTask.new('test') do |t|
26
- t.libs << 'test'
27
- t.pattern = 'test/test_*.rb'
28
- t.verbose = true
29
- end
30
-
31
- desc 'Update Manifest.txt'
32
- task :update_manifest do
33
- sh "find . -type f | sed -e 's%./%%' | egrep -v 'svn|swp|~' | egrep -v '^(doc|pkg)/' | sort > Manifest.txt"
34
- end
35
-
36
- desc 'Generate RDoc'
37
- Rake::RDocTask.new :rdoc do |rd|
38
- rd.rdoc_dir = 'doc'
39
- rd.rdoc_files.add 'lib', 'README', 'LICENSE', 'CHANGES'
40
- rd.main = 'README'
41
- rd.options << '-d' if `which dot` =~ /\/dot/
42
- rd.options << '-t RingyDingy Documentation'
43
- end
44
-
45
- desc 'Build Gem'
46
- Rake::GemPackageTask.new spec do |pkg|
47
- pkg.need_tar = true
48
- end
49
-
50
- desc 'Clean up'
51
- task :clean => [ :clobber_rdoc, :clobber_package ]
52
-
53
- desc 'Clean up'
54
- task :clobber => [ :clean ]
55
-
56
17
  # vim: syntax=Ruby
data/bin/ring_server ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'ringy_dingy/ring_server'
5
+
6
+ RingyDingy::RingServer.run
7
+
data/lib/ringy_dingy.rb CHANGED
@@ -2,6 +2,8 @@ require 'English'
2
2
  require 'drb'
3
3
  require 'rinda/ring'
4
4
 
5
+ $TESTING = defined? $TESTING
6
+
5
7
  ##
6
8
  # RingyDingy registers a DRb service with a Rinda::RingServer and re-registers
7
9
  # the service if communication with the Rinda::RingServer is ever lost.
@@ -19,6 +21,8 @@ require 'rinda/ring'
19
21
 
20
22
  class RingyDingy
21
23
 
24
+ VERSION = '1.1.0'
25
+
22
26
  ##
23
27
  # Interval to check the RingServer for our registration information.
24
28
 
@@ -45,7 +49,7 @@ class RingyDingy
45
49
  # optional identifier +name+.
46
50
 
47
51
  def initialize(object, service = :RingyDingy, name = nil)
48
- DRb.start_service
52
+ DRb.start_service unless DRb.primary_server
49
53
 
50
54
  @identifier = [Socket.gethostname.downcase, $PID, name].compact.join '_'
51
55
  @object = object
@@ -0,0 +1,278 @@
1
+ require 'rinda/ring'
2
+ require 'rinda/tuplespace'
3
+ require 'ringy_dingy'
4
+ require 'optparse'
5
+
6
+ ##
7
+ # RingyDingy::RingServer provides a friendly wrapper around Rinda::RingServer.
8
+ #
9
+ # When running on the command line, RingyDingy::RingServer's verbose mode may
10
+ # be toggled remotely via the --set-verbose option.
11
+ #
12
+ # = Usage
13
+ #
14
+ # == Starting a RingServer
15
+ #
16
+ # From the command line:
17
+ #
18
+ # ring_server -d
19
+ #
20
+ # or from Ruby:
21
+ #
22
+ # RingyDingy::RingServer.new.run
23
+ #
24
+ # == List Services
25
+ #
26
+ # From the command line (after starting a RingServer):
27
+ #
28
+ # ring_server -l
29
+ #
30
+ # or from Ruby:
31
+ #
32
+ # RingyDingy::RingServer.list
33
+ #
34
+ # == Verbose mode
35
+ #
36
+ # Changing verbose mode for the server (when not a daemon).
37
+ #
38
+ # ring_server --set-verbose=true/false
39
+ #
40
+ # or from Ruby:
41
+ #
42
+ # RingyDingy::RingServer.set_verbose true/false
43
+
44
+ class RingyDingy::RingServer
45
+
46
+ ##
47
+ # Verbose setting for this RingyDingy::RingServer.
48
+
49
+ attr_reader :verbose
50
+
51
+ ##
52
+ # Print all available services on all available Rinda::RingServers to
53
+ # stdout.
54
+
55
+ def self.list
56
+ DRb.start_service unless DRb.primary_server
57
+
58
+ rf = Rinda::RingFinger.new
59
+
60
+ services = Hash.new { |h,k| h[k] = {} }
61
+
62
+ rf.lookup_ring do |ts|
63
+ found_services = ts.read_all [:name, nil, DRbObject, nil]
64
+ found_services.each do |service|
65
+ # #hash and #eql? will not always work
66
+ ref = "#{service[2].__drburi}:#{service[2].__drbref}"
67
+ services[ts.__drburi][ref] = service
68
+ end
69
+ end
70
+
71
+ services.each do |ring_server, services|
72
+ puts "Services on #{ring_server}:"
73
+ services.values.each do |s|
74
+ puts "%p, %p\n\tURI: %s ref: %d" %
75
+ [s[1], s[3], s[2].__drburi, s[2].__drbref]
76
+ end
77
+ end
78
+ end
79
+
80
+ ##
81
+ # Enables or disables verobse mode on all available Rinda::RingServers
82
+ # depending upon +boolean+.
83
+
84
+ def self.set_verbose(boolean)
85
+ DRb.start_service unless DRb.primary_server
86
+
87
+ rf = Rinda::RingFinger.new
88
+
89
+ rf.lookup_ring do |ts|
90
+ ts.write [:RingyDingy, :verbose, boolean]
91
+ end
92
+ end
93
+
94
+ ##
95
+ # Process +args+ into an options Hash. See also #new.
96
+
97
+ def self.process_args(args)
98
+ options = {}
99
+ options[:Verbose] = false
100
+
101
+ op = OptionParser.new do |op|
102
+ op.program_name = 'ring_server'
103
+ op.version = RingyDingy::VERSION
104
+ op.release = nil
105
+
106
+ op.banner = "Usage: #{name} [options]"
107
+ op.separator ''
108
+ op.separator 'Run, find, or modify the behavior of a Rinda::RingServer.'
109
+ op.separator ''
110
+ op.separator 'With no arguments a Rinda::RingServer is started and runs in the foreground.'
111
+ op.separator ''
112
+
113
+ op.separator 'RingServer options:'
114
+ op.on("-d", "--daemon",
115
+ "Run a RingServer as a daemon") do |val|
116
+ options[:Daemon] = val
117
+ end
118
+
119
+ op.on("-v", "--verbose",
120
+ "Enable verbose mode") do |val|
121
+ options[:Verbose] = val
122
+ end
123
+
124
+ op.separator ''
125
+ op.separator 'Miscellaneous options:'
126
+
127
+ op.on("-l", "--list",
128
+ "List services on available RingServers") do |val|
129
+ options[:List] = val
130
+ end
131
+
132
+ op.on( "--set-verbose=BOOLEAN", TrueClass,
133
+ "Enable or disable verbose mode on available",
134
+ "RingServers (except daemon RingServers)") do |val|
135
+ options[:SetVerbose] = val
136
+ end
137
+ end
138
+
139
+ op.parse! args
140
+
141
+ return options
142
+ end
143
+
144
+ ##
145
+ # Run appropriately.
146
+
147
+ def self.run(args = ARGV)
148
+ DRb.start_service unless DRb.primary_server
149
+
150
+ options = process_args args
151
+
152
+ if options.include? :List then
153
+ list
154
+ exit
155
+ elsif options.include? :SetVerbose then
156
+ set_verbose options[:SetVerbose]
157
+ exit
158
+ elsif options.include? :Daemon then
159
+ require 'webrick/server'
160
+ WEBrick::Daemon.start
161
+ end
162
+
163
+ new(options).run
164
+ end
165
+
166
+ ##
167
+ # Prints usage message +message+ if present then OptionParser +op+.
168
+
169
+ def self.usage(op, message = nil)
170
+ if message then
171
+ $stderr.puts message
172
+ $stderr.puts
173
+ end
174
+
175
+ $stderr.puts op
176
+ exit 1
177
+ end
178
+
179
+ ##
180
+ # Creates a new RingyDingy::RingServer.
181
+ #
182
+ # +options+ may contain:
183
+ # [:Daemon] In daemon-mode #verbose may not be changed.
184
+ # [:Verbose] In verbose mode service registrations and expirations are
185
+ # logged to $stderr.
186
+
187
+ def initialize(options)
188
+ @ts = Rinda::TupleSpace.new
189
+
190
+ @registrations = nil
191
+ @expirations = nil
192
+
193
+ @daemon = options[:Daemon]
194
+
195
+ @verbose = false
196
+ self.verbose = options[:Verbose] and not @daemon
197
+ end
198
+
199
+ ##
200
+ # Disables service registration and expiration logging.
201
+
202
+ def disable_activity_logging
203
+ @registrations.cancel if @registrations and @registrations.alive?
204
+ @expirations.cancel if @expirations and @expirations.alive?
205
+
206
+ $stderr.puts 'registration and expiration logging disabled'
207
+ end
208
+
209
+ ##
210
+ # Enables service registration and expiration logging.
211
+
212
+ def enable_activity_logging
213
+ log 'registration and expiration logging enabled'
214
+
215
+ @registrations = @ts.notify 'write', [:name, nil, DRbObject, nil]
216
+ @expirations = @ts.notify 'delete', [:name, nil, DRbObject, nil]
217
+
218
+ Thread.start do
219
+ @registrations.each do |(_,t)|
220
+ $stderr.puts "registered %p, %p\n\tURI: %s ref: %d" %
221
+ [t[1], t[3], t[2].__drburi, t[2].__drbref]
222
+ end
223
+ end
224
+
225
+ Thread.start do
226
+ @expirations.each do |(_,t)|
227
+ $stderr.puts "expired %p, %p\n\tURI: %s ref: %d" %
228
+ [t[1], t[3], t[2].__drburi, t[2].__drbref]
229
+ end
230
+ end
231
+ end
232
+
233
+ ##
234
+ # Logs a message to $stderr when @verbose is true.
235
+
236
+ def log(message)
237
+ return unless @verbose
238
+ $stderr.puts message
239
+ end
240
+
241
+ ##
242
+ # Worker thread to monitor remote toggling of verbose mode.
243
+
244
+ def monitor_verbose
245
+ Thread.start do
246
+ loop do
247
+ self.verbose = @ts.take([:RingyDingy, :verbose, nil])[2]
248
+ end
249
+ end
250
+ end
251
+
252
+ ##
253
+ # Starts a new RingServer::RingyDingy.
254
+
255
+ def run
256
+ DRb.start_service unless DRb.primary_server
257
+
258
+ log "listening on #{DRb.uri}"
259
+
260
+ monitor_verbose
261
+
262
+ Rinda::RingServer.new @ts
263
+
264
+ DRb.thread.join
265
+ end
266
+
267
+ ##
268
+ # Sets verbose to +value+ when @daemon is not true.
269
+
270
+ def verbose=(value)
271
+ return if @daemon or @verbose == value
272
+ @verbose = value
273
+ @verbose ? enable_activity_logging : disable_activity_logging
274
+ @verbose
275
+ end
276
+
277
+ end
278
+
@@ -0,0 +1,123 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'test/zentest_assertions'
5
+
6
+ $TESTING = true
7
+
8
+ require 'ringy_dingy/ring_server'
9
+
10
+ DRb.start_service
11
+
12
+ class RingyDingy::RingServer
13
+ attr_accessor :expirations, :registrations, :ts
14
+ attr_reader :verbose
15
+ end
16
+
17
+ class TestRingServer < Test::Unit::TestCase
18
+
19
+ def setup
20
+ util_capture do
21
+ @rs = RingyDingy::RingServer.new :Verbose => true
22
+ end
23
+ end
24
+
25
+ def test_initialize_verbose_daemon
26
+ rs = RingyDingy::RingServer.new :Verbose => true, :Daemon => true
27
+ assert_equal false, rs.verbose
28
+ end
29
+
30
+ def test_disable_activity_logging
31
+ @rs.registrations = @rs.ts.notify 'write', [nil]
32
+ @rs.expirations = @rs.ts.notify 'delete', [nil]
33
+
34
+ out, err = util_capture do
35
+ @rs.disable_activity_logging
36
+ end
37
+
38
+ assert_equal true, @rs.registrations.canceled?
39
+ assert_equal true, @rs.expirations.canceled?
40
+
41
+ assert_equal "registration and expiration logging disabled\n", err.string
42
+ end
43
+
44
+ def test_enable_activity_logging
45
+ @rs.registrations.cancel
46
+ @rs.expirations.cancel
47
+
48
+ out, err = util_capture do
49
+ @rs.enable_activity_logging
50
+ @rs.ts.write [:name, :Test, DRbObject.new(self), ''], 0
51
+ end
52
+
53
+ assert_equal true, @rs.registrations.alive?
54
+ assert_equal true, @rs.expirations.alive?
55
+
56
+ expected = <<-EOF
57
+ registration and expiration logging enabled
58
+ registered :Test, ""
59
+ \tURI: #{DRb.uri} ref: #{self.object_id}
60
+ expired :Test, ""
61
+ \tURI: #{DRb.uri} ref: #{self.object_id}
62
+ EOF
63
+
64
+ assert_equal expected, err.string
65
+ end
66
+
67
+ def disabled_test_monitor_verbose
68
+ util_capture do
69
+ assert_equal true, @rs.verbose
70
+ @rs.ts.write [:RingyDingy, :verbose, false]
71
+ assert_equal false, @rs.verbose
72
+ @rs.ts.write [:RingyDingy, :verbose, true]
73
+ assert_equal true, @rs.verbose
74
+ end
75
+ end
76
+
77
+ def test_verbose_equals_false
78
+ assert_equal true, @rs.verbose
79
+
80
+ out, err = util_capture do
81
+ @rs.verbose = false
82
+ end
83
+
84
+ assert_equal '', out.string
85
+ assert_equal "registration and expiration logging disabled\n", err.string
86
+ end
87
+
88
+ def test_verbose_equals_no_change
89
+ assert_equal true, @rs.verbose
90
+
91
+ out, err = util_capture do
92
+ @rs.verbose = true
93
+ end
94
+
95
+ assert_equal '', out.string
96
+ assert_equal '', err.string
97
+ end
98
+
99
+ def test_verbose_equals_true
100
+ util_capture do @rs.verbose = false end
101
+
102
+ out, err = util_capture do
103
+ @rs.verbose = true
104
+ end
105
+
106
+ assert_equal '', out.string
107
+ assert_equal "registration and expiration logging enabled\n", err.string
108
+ end
109
+
110
+ def test_verbose_equals_true_daemon
111
+ @rs.instance_variable_set :@daemon, true
112
+ util_capture do @rs.verbose = false end
113
+
114
+ out, err = util_capture do
115
+ @rs.verbose = true
116
+ end
117
+
118
+ assert_equal '', out.string
119
+ assert_equal '', err.string
120
+ end
121
+
122
+ end
123
+
metadata CHANGED
@@ -3,14 +3,15 @@ rubygems_version: 0.8.99
3
3
  specification_version: 1
4
4
  name: RingyDingy
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2006-09-09 00:00:00 -07:00
6
+ version: 1.1.0
7
+ date: 2006-10-21 00:00:00 -06:00
8
8
  summary: RingyDingy is a little boat that keeps your DRb service afloat!
9
9
  require_paths:
10
10
  - lib
11
+ - test
11
12
  email: drbrain@segment7.net
12
- homepage:
13
- rubyforge_project:
13
+ homepage: http://seattlerb.rubyforge.org/RingyDingy
14
+ rubyforge_project: seattlerb
14
15
  description: RingyDingy automatically re-registers your DRb service with a RingServer should communication with the RingServer stop.
15
16
  autorequire:
16
17
  default_executable:
@@ -29,12 +30,15 @@ post_install_message:
29
30
  authors:
30
31
  - Eric Hodel
31
32
  files:
32
- - CHANGES
33
- - LICENSE
33
+ - History.txt
34
+ - LICENSE.txt
34
35
  - Manifest.txt
35
- - README
36
+ - README.txt
36
37
  - Rakefile
38
+ - bin/ring_server
37
39
  - lib/ringy_dingy.rb
40
+ - lib/ringy_dingy/ring_server.rb
41
+ - test/test_ring_server.rb
38
42
  - test/test_ringy_dingy.rb
39
43
  test_files: []
40
44
 
@@ -42,11 +46,28 @@ rdoc_options: []
42
46
 
43
47
  extra_rdoc_files: []
44
48
 
45
- executables: []
46
-
49
+ executables:
50
+ - ring_server
47
51
  extensions: []
48
52
 
49
53
  requirements: []
50
54
 
51
- dependencies: []
52
-
55
+ dependencies:
56
+ - !ruby/object:Gem::Dependency
57
+ name: hoe
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Version::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.1.2
64
+ version:
65
+ - !ruby/object:Gem::Dependency
66
+ name: ZenTest
67
+ version_requirement:
68
+ version_requirements: !ruby/object:Gem::Version::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.4.0
73
+ version:
data/CHANGES DELETED
@@ -1,4 +0,0 @@
1
- = 1.0.0
2
-
3
- Birthday!
4
-