grove 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,24 @@
1
+ # Grove
2
+
3
+ Invoke DRb services with no configuration
4
+
5
+ ## Example
6
+
7
+ server.rb:
8
+
9
+ require 'lib/grove'
10
+
11
+ class Calculator
12
+ def self.add(a, b)
13
+ a + b
14
+ end
15
+ end
16
+
17
+ Grove.run_service(Calculator)
18
+
19
+ client.rb:
20
+
21
+ require 'lib/grove'
22
+
23
+ puts Grove::Calculator.add(2, 2)
24
+
@@ -0,0 +1,34 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "grove"
7
+ gem.summary = "Invoke DRb services with no configuration"
8
+ gem.email = "bagilevi@gmail.com"
9
+ gem.homepage = "http://github.com/bagilevi/grove"
10
+ gem.authors = [ "Levente Bagi" ]
11
+ gem.version = "0.1"
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: gem install jeweler"
15
+ end
16
+
17
+ require 'rake/rdoctask'
18
+ Rake::RDocTask.new do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'differ'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README*')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new do |t|
28
+ t.libs << "test"
29
+ t.test_files = FileList['test/*_test.rb']
30
+ t.verbose = true
31
+ end
32
+
33
+ task :default => :test
34
+
@@ -0,0 +1,42 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{grove}
8
+ s.version = "0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Levente Bagi"]
12
+ s.date = %q{2011-04-05}
13
+ s.email = %q{bagilevi@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README"
16
+ ]
17
+ s.files = [
18
+ "README",
19
+ "Rakefile",
20
+ "lib/grove.rb",
21
+ "test/grove_test.rb",
22
+ "test/helpers.rb"
23
+ ]
24
+ s.homepage = %q{http://github.com/bagilevi/grove}
25
+ s.require_paths = ["lib"]
26
+ s.rubygems_version = %q{1.6.2}
27
+ s.summary = %q{Invoke DRb services with no configuration}
28
+ s.test_files = [
29
+ "test/grove_test.rb",
30
+ "test/helpers.rb"
31
+ ]
32
+
33
+ if s.respond_to? :specification_version then
34
+ s.specification_version = 3
35
+
36
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
37
+ else
38
+ end
39
+ else
40
+ end
41
+ end
42
+
@@ -0,0 +1,91 @@
1
+ require 'drb'
2
+
3
+ module Grove
4
+
5
+
6
+ ## Client
7
+
8
+
9
+ @@services = {}
10
+
11
+ def self.const_missing const
12
+ if @@services.has_key? const
13
+ @@services[const]
14
+ else
15
+ @@services[const] = get const
16
+ end
17
+ end
18
+
19
+ def self.get const
20
+ service_config = config[const]
21
+ ::DRbObject.new_with_uri(service_config)
22
+ end
23
+
24
+
25
+ ## Server
26
+
27
+
28
+ def self.run_service front_object
29
+ trap("INT") { puts; exit }
30
+ uri = nil
31
+ t = Thread.new {
32
+ $SAFE = 1
33
+ server = ::DRb.start_service(nil, front_object)
34
+ uri = server.uri
35
+ ::DRb.thread.join
36
+ }
37
+ while uri.nil?
38
+ sleep 0.01
39
+ end
40
+ set_config(front_object.name, uri)
41
+ puts "Started #{front_object.name} service on #{uri}"
42
+ t.join
43
+ end
44
+
45
+
46
+ ### Config
47
+
48
+
49
+ def self.config
50
+ if File.exists?(config_file)
51
+ File.open(config_file, File::RDONLY) { |f| read_config(f) }
52
+ else
53
+ {}
54
+ end
55
+ end
56
+
57
+ def self.set_config const, service_config
58
+ File.open(config_file, File::CREAT|File::RDWR) { |f|
59
+ f.flock(File::LOCK_EX)
60
+ read_config(f)
61
+ @@config[const.to_sym] = service_config
62
+ f.truncate 0
63
+ f.rewind
64
+ write_config(f)
65
+ }
66
+ end
67
+
68
+ def self.read_config(f)
69
+ @@config = Hash[(
70
+ f.read.split(/[\r\n]+/).map do |line|
71
+ a = line.split(':',2).map(&:strip)
72
+ if a.size
73
+ [a[0].to_sym, a[1]]
74
+ end
75
+ end.compact
76
+ )]
77
+ end
78
+
79
+ def self.write_config(f)
80
+ @@config.each do |key, value|
81
+ f.puts "#{key}:#{value}"
82
+ end
83
+ end
84
+
85
+ def self.config_file
86
+ "#{ENV['HOME']}/.ruby-grove"
87
+ end
88
+
89
+ end
90
+
91
+
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'test/helpers'
3
+
4
+ class GroveTest < Test::Unit::TestCase
5
+ include ProcessTestHelper
6
+
7
+ def test_basic
8
+ server_process "
9
+ class Calculator
10
+ def self.add(a, b)
11
+ a + b
12
+ end
13
+ end
14
+ Grove.run_service(Calculator)
15
+ "
16
+ client_process "
17
+ puts Grove::Calculator.add(2, 2).inspect
18
+ "
19
+ assert_equal "4\n", processes.last.output
20
+ end
21
+
22
+ end
@@ -0,0 +1,74 @@
1
+ module ProcessTestHelper
2
+ class GroveProcess
3
+ @@counter = 0
4
+ def initialize(file_content)
5
+ @filename = "#{dir}/testfile-#{@@counter += 1}.rb"
6
+ File.open(@filename, 'w') do |f|
7
+ f.write <<-TESTFILE
8
+ require 'lib/grove'
9
+ #{file_content}
10
+ TESTFILE
11
+ end
12
+ end
13
+ def dir
14
+ File.expand_path(File.dirname(__FILE__) + "/..")
15
+ end
16
+ def run
17
+ Dir.chdir(dir)
18
+ pipe = IO.popen("ruby #{@filename} 2>&1")
19
+ @pid = pipe.pid
20
+ @output = pipe.read
21
+ end
22
+ attr_reader :output
23
+ def kill
24
+ Process.kill("HUP", @pid)
25
+ end
26
+ def cleanup
27
+ kill
28
+ File.delete(@filename)
29
+ end
30
+ end
31
+
32
+ def process code
33
+ p = GroveProcess.new(code)
34
+ @processes << p
35
+ p.run
36
+ end
37
+
38
+ def server_process code
39
+ Thread.new {
40
+ process code
41
+ }
42
+ end
43
+
44
+ def client_process code
45
+ p = GroveProcess.new(code)
46
+ @processes << p
47
+
48
+ t0 = Time.now
49
+ loop do
50
+ p.run
51
+ if p.output.include?('#<Errno::ECONNREFUSED: Connection refused - connect(2)> (DRb::DRbConnError)')
52
+ puts "- isclude"
53
+ if Time.now - t0 > 3
54
+ break
55
+ end
56
+ sleep 0.1
57
+ else
58
+ break
59
+ end
60
+ end
61
+ end
62
+
63
+ attr_reader :processes
64
+
65
+ def setup
66
+ @processes = []
67
+ end
68
+
69
+ def teardown
70
+ @processes.each(&:cleanup)
71
+ end
72
+
73
+ end
74
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grove
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Levente Bagi
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-05 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: bagilevi@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - README
31
+ - Rakefile
32
+ - grove.gemspec
33
+ - lib/grove.rb
34
+ - test/grove_test.rb
35
+ - test/helpers.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/bagilevi/grove
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.6.2
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Invoke DRb services with no configuration
70
+ test_files:
71
+ - test/grove_test.rb
72
+ - test/helpers.rb