brb 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ 0.2.1 (Apr 16, 2010)
2
+
3
+ * Automatically start EM if not started
4
+ * Deprecate the usage of BrB::Service.instance => use BrB::Service instead
5
+
1
6
  0.2.0 (Apr 16, 2010)
2
7
 
3
8
  * Releasing gem and wiki
@@ -1,6 +1,6 @@
1
1
  = BrB - Easy and Fast distributed ruby
2
2
 
3
- BrB is a simple and extremely fast interface for doing simple distributed ruby.
3
+ BrB is a simple, fully transparent and extremely fast interface for doing simple distributed ruby.
4
4
  The main power of the architecture is provided by EventMachine (Fast and reliable IO event library).
5
5
 
6
6
  BrB was build in order to achieve these 4 main goals :
@@ -1,37 +1,32 @@
1
- require 'lib/brb'
1
+ require File.join(File.dirname(__FILE__), '../init.rb')
2
2
 
3
3
  port = 5555
4
4
  host = 'localhost'
5
5
 
6
- EM::run do
7
- EM::set_quantum(20)
8
- # Connecting to the core server, retrieving its interface object : core
9
- # We do not want to expose an object, so the first parameter is nil
10
- core = BrB::Tunnel.create(nil, "brb://#{host}:#{port}", :silent => false)
11
-
12
- # Create thread here, as you can not block the EventMachine::run block
13
- Thread.new do
14
-
15
- # Calling 10 times an non blocking method on the distant core server
16
- 10.times do
17
- core.simple_api_method # Do not wait for response
18
- end
19
-
20
- # Calling 10 times again long treatment time distant methods
21
- 10.times do
22
- core.simple_long_api_method # Do not wait for response
23
- end
24
-
25
- # Calling a blocking method with _block on the distant core server :
26
- puts " >> Calling 1s call, and wait for response..."
27
- r = core.simple_api_method_block
28
- puts " > Api response : #{r}"
29
-
30
- puts " >> Calling long call, and wait for response..."
31
- r = core.simple_long_api_method_block
32
- puts " > Api long response : #{r}"
33
-
34
- # Our job is over, close
35
- EM.stop
36
- end
6
+ # Connecting to the core server, retrieving its interface object : core
7
+ # We do not want to expose an object, so the first parameter is nil
8
+ core = BrB::Tunnel.create(nil, "brb://#{host}:#{port}", :silent => false)
9
+
10
+ # Calling 10 times an non blocking method on the distant core server
11
+ 10.times do
12
+ core.simple_api_method # Do not wait for response
13
+ end
14
+
15
+ # Calling 10 times again long treatment time distant methods
16
+ 10.times do
17
+ core.simple_long_api_method # Do not wait for response
37
18
  end
19
+
20
+ # Calling a blocking method with _block on the distant core server :
21
+ puts " >> Calling 1s call, and wait for response..."
22
+ r = core.simple_api_method_block
23
+ puts " > Api response : #{r}"
24
+
25
+ puts " >> Calling long call, and wait for response..."
26
+ r = core.simple_long_api_method_block
27
+ puts " > Api long response : #{r}"
28
+
29
+ core.stop_service
30
+
31
+ # Our job is over, close event machine :
32
+ EM.stop
@@ -1,4 +1,4 @@
1
- require 'lib/brb'
1
+ require File.join(File.dirname(__FILE__), '../init.rb')
2
2
 
3
3
  class ExposedCoreObject
4
4
 
@@ -23,9 +23,6 @@ Thread.abort_on_exception = true
23
23
  port = 5555
24
24
  host = 'localhost'
25
25
 
26
- EM::run do
27
- EM::set_quantum(20)
28
-
29
- puts " > Starting the core on brb://#{host}:#{port}"
30
- BrB::Service.instance.start_service(:object => ExposedCoreObject.new, :silent => false, :host => host, :port => port)
31
- end
26
+ puts " > Starting the core on brb://#{host}:#{port}"
27
+ BrB::Service.start_service(:object => ExposedCoreObject.new, :silent => false, :host => host, :port => port)
28
+ EM.reactor_thread.join
data/lib/brb.rb CHANGED
@@ -1,48 +1,4 @@
1
1
  require File.join(File.dirname(__FILE__), 'brb', 'exception.rb')
2
2
  require File.join(File.dirname(__FILE__), 'brb', 'event_machine.rb')
3
+ require File.join(File.dirname(__FILE__), 'brb', 'service.rb')
3
4
  require File.join(File.dirname(__FILE__), 'brb', 'tunnel.rb')
4
- require 'singleton'
5
-
6
- #
7
- # Brb Main class used to do basic distributed ruby, Simple but fast
8
- # Use two distinct canal, one for the command reception, and the other one for send return value
9
- #
10
- module BrB
11
- class Service
12
- attr_reader :silent
13
- attr_reader :uri
14
-
15
- include Singleton
16
-
17
- # Start a server hosted on the object given,
18
- # If an uri is given, automatcilay connect to the distant brb object
19
- def start_service(opts = {}, &block)
20
- return if @em_signature
21
-
22
- @silent = opts[:silent]
23
-
24
- addr = "brb://#{opts[:host] || 'localhost'}:#{opts[:port] || 6200}"
25
-
26
- tputs " [BrB] Start service on #{addr} ..."
27
- @uri, @em_signature = BrBProtocol.open_server(addr, BrB::Tunnel::Handler, opts.merge(:block => block))
28
- tputs " [BrB] Service started on #{@uri}"
29
- end
30
-
31
- private
32
- def tputs(s)
33
- puts s if !@silent
34
- end
35
-
36
- public
37
- # Stop the Brb Service
38
- def stop_service
39
- return if !@em_signature
40
-
41
- tputs ' [BrB] Stop service'
42
- EM::stop_server(@em_signature)
43
- @em_signature = nil
44
- @uri = nil
45
- end
46
-
47
- end
48
- end
@@ -1,44 +1,91 @@
1
+ # Define a BrB::Protocol using event machine
1
2
  require 'eventmachine'
2
3
 
3
- # Define a BrBProtocol using event machine
4
- class BrBEventMachine
4
+ module BrB
5
+ class EventMachine
5
6
 
6
- def self.parse_uri(uri)
7
- if /^brb:\/\/(.+):([0-9]+)$/ =~ uri
8
- [$1, $2.to_i]
9
- else
10
- raise "Bad tcp BrB url: '#{uri}'"
11
- end
12
- end
7
+ class << self
8
+
9
+ private
10
+ # If EM::run has not been called yet, start the EM reactor in another thread.
11
+ def ensure_em_is_started!
12
+ if !EM::reactor_running?
13
+ # Launch event machine reactor
14
+ q = Queue.new
15
+ Thread.new do
16
+ EM::run do
17
+ q << true # Set to the calling thread that the reactor is running
18
+ #EM::set_quantum(20)
19
+ #EventMachine::epoll
20
+ end
21
+ end
22
+ # Wait for event machine running :
23
+ q.pop
24
+ end
25
+
26
+ end
27
+
28
+ public
29
+ def open(uri, klass, opts = {})
30
+ host, port = parse_uri(uri)
31
+ begin
32
+ ensure_em_is_started!
33
+
34
+ q = Queue.new
35
+ EM.schedule do
36
+ q << EM::connect(host, port, klass, opts.merge(:uri => "brb://#{host}:#{port}"))
37
+ end
38
+
39
+ # Wait for socket connection with the q.pop
40
+ return q.pop
41
+
42
+ rescue Exception => e
43
+ puts e.backtrace.join("\n")
44
+ raise "#{e} - #{uri}"
45
+ end
46
+ end
13
47
 
14
- def self.open(uri, klass, opts = {})
15
- host, port = parse_uri(uri)
16
- begin
17
- socket = EventMachine::connect host, port, klass, opts.merge(:uri => "brb://#{host}:#{port}")
48
+ def open_server(uri, klass, opts = {})
49
+ host, port = parse_uri(uri)
50
+ max = 80 # Nb try before giving up
51
+ begin
52
+ uri = "brb://#{host}:#{port}"
53
+ ensure_em_is_started!
18
54
 
19
- rescue Exception => e
20
- puts e.backtrace.join("\n")
21
- raise "#{e} - #{uri}"
55
+ # Schedule server creation for thread safety
56
+ q = Queue.new
57
+ EM.schedule do
58
+ q << EM::start_server(host, port, klass, opts.merge(:uri => uri))
59
+ end
60
+
61
+ # Wait for server creation with the q.pop
62
+ return uri, q.pop
63
+
64
+ rescue Exception => e
65
+ max -= 1
66
+ port += 1
67
+ retry if max > 0
68
+ puts e.backtrace.join("\n")
69
+ raise "#{e} - BrB Tcp Event machine Can not bind on #{host}:#{port}"
70
+ end
71
+
72
+ end
22
73
  end
23
- return socket
24
74
  end
75
+
76
+ class Protocol < EventMachine
77
+
78
+ class << self
79
+
80
+ def parse_uri(uri)
81
+ if /^brb:\/\/(.+):([0-9]+)$/ =~ uri
82
+ [$1, $2.to_i]
83
+ else
84
+ raise "Bad tcp BrB url: '#{uri}'"
85
+ end
86
+ end
25
87
 
26
- def self.open_server(uri, klass, opts = {})
27
- host, port = parse_uri(uri)
28
- max = 80
29
- begin
30
- #EventMachine::epoll
31
- uri = "brb://#{host}:#{port}"
32
- return uri, EventMachine::start_server(host, port, klass, opts.merge(:uri => uri))
33
- rescue Exception => e
34
- max -= 1
35
- port += 1
36
- retry if max > 0
37
- puts e.backtrace.join("\n")
38
- raise "#{e} - BrB Tcp Event machine Can not bind on #{host}:#{port}"
39
88
  end
40
89
  end
41
90
  end
42
91
 
43
- class BrBProtocol < BrBEventMachine
44
- end
@@ -0,0 +1 @@
1
+ # Future BrB custom exceptions will come here
@@ -0,0 +1,60 @@
1
+ #
2
+ # Brb Main class used to do basic distributed ruby, Simple but fast
3
+ # Use two distinct canal, one for the command reception, and the other one for send return value
4
+ #
5
+ module BrB
6
+ class Service
7
+ @@uri = nil
8
+ @@em_signature = nil
9
+ @@silent = true
10
+
11
+ class << self
12
+
13
+ private
14
+ def tputs(s)
15
+ puts s if !@@silent
16
+ end
17
+
18
+ public
19
+
20
+ # Start a server hosted on the object given,
21
+ # If an uri is given, automatcilay connect to the distant brb object
22
+ def start_service(opts = {}, &block)
23
+ return if @@em_signature
24
+
25
+ @@silent = opts[:silent]
26
+
27
+ addr = "brb://#{opts[:host] || 'localhost'}:#{opts[:port] || 6200}"
28
+
29
+ tputs " [BrB] Start service on #{addr} ..."
30
+ @@uri, @@em_signature = BrB::Protocol::open_server(addr, BrB::Tunnel::Handler, opts.merge(:block => block))
31
+ tputs " [BrB] Service started on #{@@uri}"
32
+ end
33
+
34
+ def uri
35
+ @@uri
36
+ end
37
+
38
+ # Stop the Brb Service
39
+ def stop_service
40
+ return if !@@em_signature or !EM::reactor_running?
41
+
42
+ tputs " [BrB] Stop service on #{@@uri}"
43
+ sign = @@em_signature
44
+ q = Queue.new # Creation of a Queue for waiting server to stop
45
+ EM::schedule do
46
+ q << EM::stop_server(sign)
47
+ end
48
+ q.pop
49
+ @@em_signature = nil
50
+ @@uri = nil
51
+ end
52
+
53
+ # Deprecated old method
54
+ def instance
55
+ puts "DEPRECATION WARNING: BrB::Service::instance is deprecated => Just use BrB::Service"
56
+ self
57
+ end
58
+ end
59
+ end
60
+ end
@@ -7,11 +7,11 @@ module BrB
7
7
  module Tunnel
8
8
 
9
9
  def self.create(object, uri = nil, opts = {}, &block)
10
- BrBProtocol.open(uri, BrB::Tunnel::Handler, opts.merge(:object => object, :block => block))
10
+ BrB::Protocol.open(uri, BrB::Tunnel::Handler, opts.merge(:object => object, :block => block))
11
11
  end
12
12
 
13
13
  # Brb interface Handler for Tunnel over Event machine
14
- class Handler < EventMachine::Connection
14
+ class Handler < ::EventMachine::Connection
15
15
  attr_reader :uri
16
16
 
17
17
  include BrB::Request
@@ -38,12 +38,12 @@ module BrB
38
38
  tputs " [BrB] Tunnel initialized on #{@uri}"
39
39
  @active = true
40
40
  if @block
41
- EventMachine.defer do
41
+ EM.defer do
42
42
  @block.call(:register, self)
43
43
  end
44
44
  end
45
45
  end
46
-
46
+
47
47
  def close_connection(after_writing = false)
48
48
  @active = false
49
49
  super
@@ -53,7 +53,7 @@ module BrB
53
53
  tputs ' [BrB] Tunnel service closed'
54
54
  @active = false
55
55
  if @block
56
- EventMachine.defer do
56
+ EM.defer do
57
57
  @block.call(:unregister, self)
58
58
  end
59
59
  end
@@ -63,7 +63,7 @@ module BrB
63
63
 
64
64
  @queue << obj
65
65
 
66
- EventMachine.defer do
66
+ EM.defer do
67
67
  treat_request(@queue.pop)
68
68
  end
69
69
 
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe :brb_service do
4
4
  before(:all) do
5
- @brb = BrB::Service.instance
5
+ @brb = BrB::Service
6
6
  @brb.stop_service
7
7
  @brb_test = BrBTest.new
8
8
  open_service(@brb_test)
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe :brb_service do
4
4
  before(:all) do
5
- @brb = BrB::Service.instance
5
+ @brb = BrB::Service
6
6
  @brb.stop_service
7
7
  @brb_test = BrBTest.new
8
8
  open_service(self)
@@ -5,7 +5,7 @@ $last_registered = nil
5
5
 
6
6
  describe :brb_service do
7
7
  before(:all) do
8
- @brb = BrB::Service.instance
8
+ @brb = BrB::Service
9
9
  @brb.stop_service
10
10
  @brb_test = BrBTest.new
11
11
  open_service(@brb_test)
@@ -6,30 +6,11 @@ Thread.abort_on_exception = true
6
6
  require File.dirname(__FILE__) + '/../init.rb'
7
7
 
8
8
  def open_service(object, host = 'localhost', port = 6200)
9
- if !EM::reactor_running?
10
- # Launch event machine reactor
11
- Thread.new do
12
- EM::run do
13
- EM::set_quantum(20)
14
- end
15
- end
16
- sleep 0.05
17
- end
18
- r = nil
19
- EM.schedule do
20
- r = BrB::Service.instance.start_service(:object => object, :silent => true, :host => host, :port => port)
21
- end
22
- sleep 0.1
23
- return r
9
+ BrB::Service.start_service(:object => object, :silent => true, :host => host, :port => port)
24
10
  end
25
11
 
26
12
  def connect_to_the_service(object_exposed, uri, &block)
27
- r = nil
28
- EM.schedule do
29
- r = BrB::Tunnel.create(object_exposed, uri, :silent => true, &block)
30
- end
31
- sleep 0.1
32
- return r
13
+ BrB::Tunnel.create(object_exposed, uri, :silent => true, &block)
33
14
  end
34
15
 
35
16
  class BrBTest
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Guillaume Luccisano
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-16 00:00:00 +02:00
17
+ date: 2010-04-19 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: "0"
30
30
  type: :runtime
31
31
  version_requirements: *id001
32
- description: BrB is a simple and extremely fast interface for doing simple distributed ruby and message passing
32
+ description: BrB is a simple, fully transparent and extremely fast interface for doing simple distributed ruby and message passing
33
33
  email: guillaume.luccisano@gmail.com
34
34
  executables: []
35
35
 
@@ -43,6 +43,7 @@ files:
43
43
  - lib/brb/event_machine.rb
44
44
  - lib/brb/exception.rb
45
45
  - lib/brb/request.rb
46
+ - lib/brb/service.rb
46
47
  - lib/brb/tunnel/shared.rb
47
48
  - lib/brb/tunnel.rb
48
49
  - lib/brb.rb
@@ -86,6 +87,6 @@ rubyforge_project: brb
86
87
  rubygems_version: 1.3.6
87
88
  signing_key:
88
89
  specification_version: 3
89
- summary: BrB is a simple and extremely fast interface for doing simple distributed ruby
90
+ summary: BrB is a simple, fully transparent and extremely fast interface for doing simple distributed ruby
90
91
  test_files: []
91
92