tribe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'rdoc', '~> 3.12'
5
+ gem 'rspec', '~> 2.10.0'
6
+ gem 'bundler', '>= 1.1.0'
7
+ gem 'jeweler', '~> 1.8.3'
8
+ gem 'simplecov', '>= 0', :require => false
9
+ gem 'rb-fsevent', :require => false
10
+ gem 'guard', '~> 1.1.1'
11
+ gem 'guard-rspec', '~> 1.0.0'
12
+ #gem 'guard-bundler', '~> 0.1.3'
13
+ gem 'growl'
14
+ platform(:ruby) { gem 'debugger' }
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ columnize (0.3.6)
5
+ debugger (1.1.4)
6
+ columnize (>= 0.3.1)
7
+ debugger-linecache (~> 1.1.1)
8
+ debugger-ruby_core_source (~> 1.1.3)
9
+ debugger-linecache (1.1.1)
10
+ debugger-ruby_core_source (>= 1.1.1)
11
+ debugger-ruby_core_source (1.1.3)
12
+ diff-lcs (1.1.3)
13
+ ffi (1.0.11)
14
+ ffi (1.0.11-java)
15
+ git (1.2.5)
16
+ growl (1.0.3)
17
+ guard (1.1.1)
18
+ listen (>= 0.4.2)
19
+ thor (>= 0.14.6)
20
+ guard-rspec (1.0.0)
21
+ guard (>= 1.1)
22
+ jeweler (1.8.3)
23
+ bundler (~> 1.0)
24
+ git (>= 1.2.5)
25
+ rake
26
+ rdoc
27
+ json (1.7.3)
28
+ json (1.7.3-java)
29
+ listen (0.4.4)
30
+ rb-fchange (~> 0.0.5)
31
+ rb-fsevent (~> 0.9.1)
32
+ rb-inotify (~> 0.8.8)
33
+ multi_json (1.3.6)
34
+ rake (0.9.2.2)
35
+ rb-fchange (0.0.5)
36
+ ffi
37
+ rb-fsevent (0.9.1)
38
+ rb-inotify (0.8.8)
39
+ ffi (>= 0.5.0)
40
+ rdoc (3.12)
41
+ json (~> 1.4)
42
+ rspec (2.10.0)
43
+ rspec-core (~> 2.10.0)
44
+ rspec-expectations (~> 2.10.0)
45
+ rspec-mocks (~> 2.10.0)
46
+ rspec-core (2.10.1)
47
+ rspec-expectations (2.10.0)
48
+ diff-lcs (~> 1.1.3)
49
+ rspec-mocks (2.10.1)
50
+ simplecov (0.6.4)
51
+ multi_json (~> 1.0)
52
+ simplecov-html (~> 0.5.3)
53
+ simplecov-html (0.5.3)
54
+ thor (0.15.2)
55
+
56
+ PLATFORMS
57
+ java
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ bundler (>= 1.1.0)
62
+ debugger
63
+ growl
64
+ guard (~> 1.1.1)
65
+ guard-rspec (~> 1.0.0)
66
+ jeweler (~> 1.8.3)
67
+ rb-fsevent
68
+ rdoc (~> 3.12)
69
+ rspec (~> 2.10.0)
70
+ simplecov
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ #guard :bundler do
2
+ # watch('Gemfile')
3
+ #end
4
+
5
+ guard :rspec, :version => 2 do
6
+ watch(%r{^spec/.+_spec\.rb$})
7
+ watch(%r{^lib/(.+)\.rb$}) { |m| r = "spec/lib/#{m[1]}_spec.rb"; puts r; r }
8
+ watch('spec/spec_helper.rb') { "spec" }
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Chad Remesch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = tribe
2
+
3
+ Actor Model for Ruby. Experimental.
4
+
5
+ == Contributing to tribe
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Chad Remesch. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "tribe"
18
+ gem.homepage = "http://github.com/chadrem/tribe"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Actor Model for Ruby}
21
+ gem.description = %Q{Actor Model for Ruby. Can support millions of actors (limited by memory). Currently experimental and not recommended for production.}
22
+ gem.email = "chad@remesch.com"
23
+ gem.authors = ["Chad Remesch"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "tribe #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
50
+
51
+ begin
52
+ require 'debugger'
53
+ rescue LoadError
54
+ end
55
+
56
+ require 'tribe'
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/tribe.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'singleton'
2
+ require 'thread'
3
+
4
+ require 'tribe/actor'
5
+ require 'tribe/singleton'
6
+ require 'tribe/worker'
7
+ require 'tribe/scheduler'
8
+ require 'tribe/registry'
9
+
10
+ module Tribe
11
+ def self.scheduler
12
+ Tribe::Scheduler.instance
13
+ end
14
+
15
+ def self.registry
16
+ Tribe::Registry.instance
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ module Tribe
2
+ class Actor
3
+ attr_reader :name
4
+
5
+ def initialize(options = {})
6
+ @process_lock = Mutex.new
7
+ @name = options[:name].freeze
8
+
9
+ Tribe.registry.register(self)
10
+ end
11
+
12
+ def method_missing(method, *args, &block)
13
+ m = method.to_s
14
+ bang = m[-1] == '!'
15
+
16
+ if bang && respond_to?(m.chop!)
17
+ async(m, *args)
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ def async(method, *args)
24
+ Tribe.scheduler.schedule do
25
+ process(:method => method, :args => args)
26
+ end
27
+
28
+ true
29
+ end
30
+
31
+ private
32
+ def process(message)
33
+ @process_lock.synchronize do
34
+ send(message[:method], *message[:args])
35
+ end
36
+ end
37
+
38
+ def shutdown
39
+ Tribe.registry.unregistry(self)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ module Tribe
2
+ class Registry < Tribe::Singleton
3
+
4
+ def initialize
5
+ @actors_by_oid = {}
6
+ @actors_by_name = {}
7
+ end
8
+
9
+ def register(actor)
10
+ @actors_by_oid[actor.object_id] = actor
11
+
12
+ if actor.name
13
+ @actors_by_name[actor.name] = actor
14
+ end
15
+ end
16
+
17
+ def unregister(actor)
18
+ @actors_by_oid.delete(actor.object_id)
19
+
20
+ if actor.name
21
+ @actors_by_name.delete(actor.name)
22
+ end
23
+ end
24
+
25
+ def [](val)
26
+ @actors_by_name[val]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ module Tribe
2
+ class Scheduler < Tribe::Singleton
3
+ def initialize(opts = {})
4
+ @count = opts[:count] || 20
5
+ @workers = []
6
+ @messages = Queue.new
7
+
8
+ spawn(@count)
9
+ end
10
+
11
+ def schedule(&block)
12
+ @messages.push({ :command => :perform, :task => block })
13
+ end
14
+
15
+ def shutdown
16
+ @count.times do
17
+ @messages.push({ :command => :shutdown })
18
+ end
19
+
20
+ @workers.each do |worker|
21
+ worker.join
22
+ end
23
+ end
24
+
25
+ private
26
+ def spawn(count)
27
+ count.times do
28
+ worker = Worker.new(@messages)
29
+ @workers.push(worker)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module Tribe
2
+ class Singleton
3
+ include ::Singleton
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ module Tribe
2
+ class Worker
3
+ def initialize(queue, options = {})
4
+ @queue = queue
5
+ @thread = Thread.new { main }
6
+ end
7
+
8
+ def join
9
+ @thread.join
10
+ end
11
+
12
+ private
13
+ def main
14
+ while (message = @queue.pop)
15
+ case message[:command]
16
+ when :shutdown
17
+ return
18
+ when :perform
19
+ begin
20
+ message[:task].call
21
+ rescue Exception => e
22
+ puts "Worker caught exception: #{e.message}\n#{e.backtrace.join("\n")}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ class TestActor < Tribe::Actor
4
+ attr_reader :some_arg
5
+
6
+ def on_test_handler(arg)
7
+ @some_arg = arg
8
+ end
9
+ end
10
+
11
+ describe Tribe::Actor do
12
+ before(:each) do
13
+ @actor = TestActor.new
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tribe::Registry do
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tribe::Scheduler do
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tribe::Worker do
4
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+
5
+ require 'simplecov'
6
+ SimpleCov.start
7
+
8
+ require 'tribe'
9
+
10
+ # Requires supporting files with custom matchers and macros, etc,
11
+ # in ./support/ and its subdirectories.
12
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
13
+
14
+ RSpec.configure do |config|
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,231 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tribe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chad Remesch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.10.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.10.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.1.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rb-fsevent
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.1.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.1.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: guard-rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.0.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.0.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: growl
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: debugger
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ description: Actor Model for Ruby. Can support millions of actors (limited by memory).
175
+ Currently experimental and not recommended for production.
176
+ email: chad@remesch.com
177
+ executables: []
178
+ extensions: []
179
+ extra_rdoc_files:
180
+ - LICENSE.txt
181
+ - README.rdoc
182
+ files:
183
+ - .document
184
+ - .rspec
185
+ - Gemfile
186
+ - Gemfile.lock
187
+ - Guardfile
188
+ - LICENSE.txt
189
+ - README.rdoc
190
+ - Rakefile
191
+ - VERSION
192
+ - lib/tribe.rb
193
+ - lib/tribe/actor.rb
194
+ - lib/tribe/registry.rb
195
+ - lib/tribe/scheduler.rb
196
+ - lib/tribe/singleton.rb
197
+ - lib/tribe/worker.rb
198
+ - spec/lib/tribe/actor_spec.rb
199
+ - spec/lib/tribe/registry_spec.rb
200
+ - spec/lib/tribe/scheduler_spec.rb
201
+ - spec/lib/tribe/worker_spec.rb
202
+ - spec/spec_helper.rb
203
+ homepage: http://github.com/chadrem/tribe
204
+ licenses:
205
+ - MIT
206
+ post_install_message:
207
+ rdoc_options: []
208
+ require_paths:
209
+ - lib
210
+ required_ruby_version: !ruby/object:Gem::Requirement
211
+ none: false
212
+ requirements:
213
+ - - ! '>='
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ segments:
217
+ - 0
218
+ hash: -1241874824708307471
219
+ required_rubygems_version: !ruby/object:Gem::Requirement
220
+ none: false
221
+ requirements:
222
+ - - ! '>='
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ requirements: []
226
+ rubyforge_project:
227
+ rubygems_version: 1.8.23
228
+ signing_key:
229
+ specification_version: 3
230
+ summary: Actor Model for Ruby
231
+ test_files: []