celluloid 0.2.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,30 +2,31 @@ require 'thread'
2
2
 
3
3
  module Celluloid
4
4
  class MailboxError < StandardError; end # you can't message the dead
5
-
5
+ class MailboxShutdown < StandardError; end # raised if the mailbox can no longer be used
6
+
6
7
  # Actors communicate with asynchronous messages. Messages are buffered in
7
8
  # Mailboxes until Actors can act upon them.
8
9
  class Mailbox
9
10
  include Enumerable
10
-
11
+
11
12
  def initialize
12
13
  @messages = []
13
14
  @lock = Mutex.new
14
- @condition = ConditionVariable.new
15
15
  @dead = false
16
+ @condition = ConditionVariable.new
16
17
  end
17
-
18
+
18
19
  # Add a message to the Mailbox
19
20
  def <<(message)
20
21
  @lock.synchronize do
21
22
  raise MailboxError, "dead recipient" if @dead
22
-
23
+
23
24
  @messages << message
24
25
  @condition.signal
25
26
  end
26
27
  nil
27
28
  end
28
-
29
+
29
30
  # Add a high-priority system event to the Mailbox
30
31
  def system_event(event)
31
32
  @lock.synchronize do
@@ -36,56 +37,68 @@ module Celluloid
36
37
  end
37
38
  nil
38
39
  end
39
-
40
+
40
41
  # Receive a message from the Mailbox
41
- def receive
42
+ def receive(&block)
42
43
  message = nil
43
-
44
+
44
45
  @lock.synchronize do
45
46
  raise MailboxError, "attempted to receive from a dead mailbox" if @dead
46
-
47
+
47
48
  begin
48
- if block_given?
49
- index = @messages.index do |msg|
50
- yield(msg) || msg.is_a?(Celluloid::SystemEvent)
51
- end
52
-
53
- message = @messages.slice!(index, 1).first if index
54
- else
55
- message = @messages.shift
56
- end
57
-
58
- raise message if message.is_a?(Celluloid::SystemEvent)
59
-
49
+ message = next_message(&block)
60
50
  @condition.wait(@lock) unless message
61
51
  end until message
62
52
  end
63
-
53
+
54
+ message
55
+ end
56
+
57
+ # Retrieve the next message in the mailbox
58
+ def next_message
59
+ message = nil
60
+
61
+ if block_given?
62
+ index = @messages.index do |msg|
63
+ yield(msg) || msg.is_a?(Celluloid::SystemEvent)
64
+ end
65
+
66
+ message = @messages.slice!(index, 1).first if index
67
+ else
68
+ message = @messages.shift
69
+ end
70
+
71
+ raise message if message.is_a?(Celluloid::SystemEvent)
64
72
  message
65
73
  end
66
-
74
+
67
75
  # Shut down this mailbox and clean up its contents
68
76
  def shutdown
77
+ messages = nil
78
+
69
79
  @lock.synchronize do
70
- @messages.each { |msg| msg.cleanup if msg.respond_to? :cleanup }
71
- @messages.clear
80
+ messages = @messages
81
+ @messages = []
72
82
  @dead = true
73
83
  end
84
+
85
+ messages.each { |msg| msg.cleanup if msg.respond_to? :cleanup }
86
+ true
74
87
  end
75
-
88
+
76
89
  # Cast to an array
77
90
  def to_a
78
91
  @lock.synchronize { @messages.dup }
79
92
  end
80
-
93
+
81
94
  # Iterate through the mailbox
82
95
  def each(&block)
83
96
  to_a.each(&block)
84
97
  end
85
-
98
+
86
99
  # Inspect the contents of the Mailbox
87
100
  def inspect
88
101
  "#<Celluloid::Mailbox:#{object_id} [#{map { |m| m.inspect }.join(', ')}]>"
89
102
  end
90
103
  end
91
- end
104
+ end
@@ -25,9 +25,4 @@ module Celluloid
25
25
  end
26
26
  end
27
27
  end
28
-
29
- # Extend the actor module with the registry methods
30
- module Actor
31
- extend Registry
32
- end
33
28
  end
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../../../spec/support/actor_examples', __FILE__)
2
+ require File.expand_path('../../../spec/support/mailbox_examples', __FILE__)
@@ -2,18 +2,18 @@ module Celluloid
2
2
  # Supervisors are actors that watch over other actors and restart them if
3
3
  # they crash
4
4
  class Supervisor
5
- include Actor
5
+ include Celluloid
6
6
  trap_exit :restart_actor
7
7
 
8
8
  # Retrieve the actor this supervisor is supervising
9
9
  attr_reader :actor
10
10
 
11
11
  def self.supervise(klass, *args, &block)
12
- spawn(nil, klass, *args, &block)
12
+ new(nil, klass, *args, &block)
13
13
  end
14
14
 
15
15
  def self.supervise_as(name, klass, *args, &block)
16
- spawn(name, klass, *args, &block)
16
+ new(name, klass, *args, &block)
17
17
  end
18
18
 
19
19
  def initialize(name, klass, *args, &block)
@@ -22,7 +22,7 @@ module Celluloid
22
22
  end
23
23
 
24
24
  def start_actor
25
- @actor = @klass.spawn_link(*@args, &@block)
25
+ @actor = @klass.new_link(*@args, &@block)
26
26
  Celluloid::Actor[@name] = @actor if @name
27
27
  end
28
28
 
@@ -0,0 +1,32 @@
1
+ require 'socket'
2
+
3
+ module Celluloid
4
+ # A TCPServer that runs as an actor
5
+ class TCPServer
6
+ include Celluloid::IO
7
+
8
+ # Bind a TCP server to the given host and port
9
+ def initialize(host, port)
10
+ @server = ::TCPServer.new host, port
11
+ run!
12
+ end
13
+
14
+ # Run the TCP server event loop
15
+ def run
16
+ while true
17
+ wait_readable(@server) { on_connect @server.accept }
18
+ end
19
+ end
20
+
21
+ # Terminate this server
22
+ def terminate
23
+ @server.close
24
+ super
25
+ end
26
+
27
+ # Called whenever a new connection is opened
28
+ def on_connect(connection)
29
+ connection.close
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
1
  module Celluloid
2
- VERSION = '0.2.2'
2
+ VERSION = '0.5.0'
3
3
  def self.version; VERSION; end
4
4
  end
metadata CHANGED
@@ -1,92 +1,92 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: celluloid
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
4
5
  prerelease:
5
- version: 0.2.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Tony Arcieri
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-09-28 00:00:00 -07:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-25 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: rake
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70182211411320 !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
25
22
  type: :development
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
23
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70182211411320
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70182211410760 !ruby/object:Gem::Requirement
31
28
  none: false
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: 2.3.0
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.0
36
33
  type: :development
37
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *70182211410760
38
36
  description: Celluloid is a concurrent object framework inspired by the Actor Model
39
- email:
37
+ email:
40
38
  - tony@medioh.com
41
39
  executables: []
42
-
43
40
  extensions: []
44
-
45
41
  extra_rdoc_files: []
46
-
47
- files:
42
+ files:
48
43
  - README.md
49
44
  - lib/celluloid/actor.rb
45
+ - lib/celluloid/actor_pool.rb
50
46
  - lib/celluloid/actor_proxy.rb
51
47
  - lib/celluloid/calls.rb
52
48
  - lib/celluloid/core_ext.rb
53
49
  - lib/celluloid/events.rb
54
50
  - lib/celluloid/future.rb
51
+ - lib/celluloid/io/actor.rb
52
+ - lib/celluloid/io/mailbox.rb
53
+ - lib/celluloid/io/reactor.rb
54
+ - lib/celluloid/io/waker.rb
55
+ - lib/celluloid/io.rb
55
56
  - lib/celluloid/linking.rb
56
57
  - lib/celluloid/mailbox.rb
57
58
  - lib/celluloid/registry.rb
58
59
  - lib/celluloid/responses.rb
60
+ - lib/celluloid/rspec.rb
59
61
  - lib/celluloid/signals.rb
60
62
  - lib/celluloid/supervisor.rb
63
+ - lib/celluloid/tcp_server.rb
61
64
  - lib/celluloid/version.rb
62
65
  - lib/celluloid.rb
63
- has_rdoc: true
64
66
  homepage: https://github.com/tarcieri/celluloid
65
- licenses:
67
+ licenses:
66
68
  - MIT
67
69
  post_install_message:
68
70
  rdoc_options: []
69
-
70
- require_paths:
71
+ require_paths:
71
72
  - lib
72
- required_ruby_version: !ruby/object:Gem::Requirement
73
+ required_ruby_version: !ruby/object:Gem::Requirement
73
74
  none: false
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: "0"
78
- required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
80
  none: false
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
83
84
  version: 1.3.6
84
85
  requirements: []
85
-
86
86
  rubyforge_project:
87
- rubygems_version: 1.6.2
87
+ rubygems_version: 1.8.10
88
88
  signing_key:
89
89
  specification_version: 3
90
90
  summary: Celluloid is a concurrent object framework inspired by the Actor Model
91
91
  test_files: []
92
-
92
+ has_rdoc: