summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2012-05-11 09:32:28 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2012-05-12 09:38:00 +0200
commitc57ae27428b28db3d5972147ba914434b8b72d30 (patch)
treeaeb348150ea0615dd2c8a6cf4d67462bcf4cab23 /lib
parent4d75d72f0173567cc013eda0d4d78af442dc2a1d (diff)
downloadedoors-ruby-c57ae27428b28db3d5972147ba914434b8b72d30.zip
edoors-ruby-c57ae27428b28db3d5972147ba914434b8b72d30.tar.gz
Spin: all clas level stuff become instance level
Diffstat (limited to 'lib')
-rw-r--r--lib/evendoors/spin.rb159
1 files changed, 70 insertions, 89 deletions
diff --git a/lib/evendoors/spin.rb b/lib/evendoors/spin.rb
index 93921a7..2ba63a4 100644
--- a/lib/evendoors/spin.rb
+++ b/lib/evendoors/spin.rb
@@ -23,121 +23,102 @@ module EvenDoors
#
class Spin < Room
#
- @pool = {} # per particle class free list
- @sys_fifo = [] # system particles fifo list
- @app_fifo = [] # application particles fifo list
- #
- @spin = nil
- @run = false
- @debug_routing = false
- @debug_errors = false
- #
- class << self
+ def initialize n, o={}
+ super n, nil
#
- attr_accessor :spin, :run, :debug_routing, :debug_errors
+ @pool = {} # per particle class free list
+ @sys_fifo = [] # system particles fifo list
+ @app_fifo = [] # application particles fifo list
#
- def to_json *a
- {
- 'sys_fifo' => @sys_fifo,
- 'app_fifo' => @app_fifo,
- 'debug_routing' => @debug_routing,
- 'debug_errors' => @debug_errors
- }.to_json(*a)
- end
+ @run = false
+ @debug_errors = o[:debug_errors]||o['debug_errors']||false
+ @debug_routing = o[:debug_routing]||o['debug_routing']||false
#
- def static_json_create o
- @debug_routing = o['debug_routing']
- @debug_errors = o['debug_errors']
+ if not o.empty?
+ o['spots'].each do |name,spot|
+ add_spot EvenDoors::Room.json_create spot
+ end if o['spots']
o['app_fifo'].each do |particle|
@app_fifo << EvenDoors::Particle.json_create(particle)
- end
+ end if o['app_fifo']
o['sys_fifo'].each do |particle|
@sys_fifo << EvenDoors::Particle.json_create(particle)
- end
- end
- #
- def release_p p
- # hope there is no circular loop
- while p2=p.merged_shift
- release_p p2
- end
- ( @pool[p.class] ||= [] ) << p
- end
- #
- def require_p p_kls
- l = @pool[p_kls]
- return p_kls.new if l.nil?
- p = l.pop
- return p_kls.new if p.nil?
- p.reset!
- p
- end
- #
- def send_p p
- @app_fifo << p
- end
- #
- def send_sys_p p
- @sys_fifo << p
- end
- #
- def spin!
- while @run and (@sys_fifo.length>0 or @app_fifo.length>0)
- while @run and @sys_fifo.length>0
- p = @sys_fifo.shift
- p.dst.process_sys_p p
- end
- while @run and @app_fifo.length>0
- p = @app_fifo.shift
- p.dst.process_p p
- break
- end
- end
- end
- #
- def clear!
- @spin= nil
- @pool.clear
- @sys_fifo.clear
- @app_fifo.clear
+ end if o['sys_fifo']
end
- #
end
#
- def initialize n, args={}
- super n, nil
- raise EvenDoors::Exception.new "do not try to initialize more than one spin" if not self.class.spin.nil?
- self.class.spin = self
- self.class.debug_errors = args[:debug_errors] || false
- self.class.debug_routing = args[:debug_routing] || false
- end
+ attr_accessor :run, :debug_errors, :debug_routing
#
def to_json *a
{
- 'kls' => self.class.name,
- 'name' => @name,
- 'spots' => @spots,
- 'static' => EvenDoors::Spin
+ 'kls' => self.class.name,
+ 'name' => @name,
+ 'spots' => @spots,
+ 'sys_fifo' => @sys_fifo,
+ 'app_fifo' => @app_fifo,
+ 'debug_errors' => @debug_errors,
+ 'debug_routing' => @debug_routing
}.to_json(*a)
end
#
def self.json_create o
raise EvenDoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
- spin = self.new(o['name'])
- o['spots'].each do |name,spot|
- spin.add_spot EvenDoors::Room.json_create spot
+ self.new o['name'], o
+ end
+ #
+ def clear!
+ @pool.clear
+ @sys_fifo.clear
+ @app_fifo.clear
+ end
+ #
+ #
+ def release_p p
+ # hope there is no circular loop
+ while p2=p.merged_shift
+ release_p p2
end
- EvenDoors::Spin.static_json_create o['static']
- spin
+ ( @pool[p.class] ||= [] ) << p
+ end
+ #
+ def require_p p_kls
+ l = @pool[p_kls]
+ return p_kls.new if l.nil?
+ p = l.pop
+ return p_kls.new if p.nil?
+ p.reset!
+ p
+ end
+ #
+ def send_p p
+ @app_fifo << p
+ end
+ #
+ def send_sys_p p
+ @sys_fifo << p
end
#
def spin!
@spots.values.each do |spot| spot.start! end
- self.class.run = true
- self.class.spin!
+ @run = true
+ while @run and (@sys_fifo.length>0 or @app_fifo.length>0)
+ while @run and @sys_fifo.length>0
+ p = @sys_fifo.shift
+ p.dst.process_sys_p p
+ end
+ while @run and @app_fifo.length>0
+ p = @app_fifo.shift
+ p.dst.process_p p
+ break
+ end
+ end
@spots.values.each do |spot| spot.stop! end
end
#
+ def stop!
+ @run=false
+ end
+ #
end
#
end