summaryrefslogtreecommitdiffstats
path: root/lib/edoors/spin.rb
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2012-06-27 11:49:10 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2012-06-27 11:49:10 +0200
commit06fe1fe549d6c7dbb643a07a485d24c3e45216d3 (patch)
tree90947a79c9732a3424b28fd972de06e070e95c53 /lib/edoors/spin.rb
parent2526cd9aef3bb7dc04901be4067dd211f03fe9b5 (diff)
parent7cfcad744d3e18cbe8a9f71c43a0cad1e8fdd6b4 (diff)
downloadedoors-ruby-06fe1fe549d6c7dbb643a07a485d24c3e45216d3.zip
edoors-ruby-06fe1fe549d6c7dbb643a07a485d24c3e45216d3.tar.gz
Merge branch 'rewrite0'
Diffstat (limited to 'lib/edoors/spin.rb')
-rw-r--r--lib/edoors/spin.rb93
1 files changed, 90 insertions, 3 deletions
diff --git a/lib/edoors/spin.rb b/lib/edoors/spin.rb
index 9f62b47..7efacf1 100644
--- a/lib/edoors/spin.rb
+++ b/lib/edoors/spin.rb
@@ -23,6 +23,25 @@ module Edoors
#
class Spin < Room
#
+ # creates a Spin object from the arguments.
+ #
+ # @param [String] n the name of this Spin
+ # @param [Hash] o a customizable set of options
+ #
+ # @option o 'debug_garbage' [String Symbol]
+ # output debug information about automatic garbage
+ # @option o 'debug_routing' [String Symbol]
+ # output debug information about routing
+ # @option o 'hibernation' [Boolean]
+ # if set to true Iota#start! won't be called within Spin#spin!
+ # @option o 'inner_room' [Hash]
+ # composed of 2 keys, 'iotas' and 'links' use to repopulate the super class Room
+ # @option o 'app_fifo' [Array]
+ # list of Particle to feed @app_fifo
+ # @option o 'sys_fifo' [Array]
+ # list of Particle to feed @sys_fifo
+ #
+ #
def initialize n, o={}
super n, nil
#
@@ -34,7 +53,7 @@ module Edoors
@run = false
@hibernation = o['hibernation']||false
@hibernate_path = 'edoors-hibernate-'+n+'.json'
- @debug_errors = o[:debug_errors]||o['debug_errors']||false
+ @debug_garbage = o[:debug_garbage]||o['debug_garbage']||false
@debug_routing = o[:debug_routing]||o['debug_routing']||false
#
if not o.empty?
@@ -58,7 +77,11 @@ module Edoors
end
end
#
- attr_accessor :run, :hibernate_path, :debug_errors, :debug_routing
+ attr_accessor :run, :hibernate_path, :debug_garbage, :debug_routing
+ #
+ # called by JSON#generate to serialize the Spin object into JSON data
+ #
+ # @param [Array] a belongs to JSON generator
#
def to_json *a
{
@@ -69,25 +92,46 @@ module Edoors
'inner_room' => { :iotas=>@iotas, :links=>@links },
'sys_fifo' => @sys_fifo,
'app_fifo' => @app_fifo,
- 'debug_errors' => @debug_errors,
+ 'debug_garbage' => @debug_garbage,
'debug_routing' => @debug_routing
}.to_json(*a)
end
#
+ # creates a Spin object from a JSON data
+ #
+ # @param [Hash] o belongs to JSON parser
+ #
+ # @raise Edoors::Exception if the json kls attribute is wrong
+ #
def self.json_create o
raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
self.new o['name'], o
end
#
+ # add the given Iota to the global Hash
+ #
+ # @param [Iota] iota the Iota to register
+ #
+ # @see Room#_route @world hash is used for routing
+ #
def add_to_world iota
@world[iota.path] = iota
end
#
+ # search the global Hash for the matching Iota
+ #
+ # @param [String] path the path to the desired Iota
+ #
+ # @see Room#_route @world hash is used for routing
+ #
def search_world path
@world[path]
end
#
+ # clears all the structures
+ #
def clear!
+ @links.clear
@iotas.clear
@world.clear
@pool.clear
@@ -95,6 +139,13 @@ module Edoors
@app_fifo.clear
end
#
+ # releases the given Particle
+ #
+ # @parama [Particle] p the Particle to be released
+ #
+ # @note the Particle is stored into Hash @pool to be reused as soon as needed
+ #
+ # @see Particle#reset! the Particle is reseted before beeing stored
#
def release_p p
# hope there is no circular loop
@@ -105,6 +156,12 @@ module Edoors
( @pool[p.class] ||= [] ) << p
end
#
+ # requires a Particle of the given Class
+ #
+ # @param [Class] p_kls the desired Class of Particle
+ #
+ # @note if there is no Particle of the given Class, one is created
+ #
def require_p p_kls
l = @pool[p_kls]
return p_kls.new if l.nil?
@@ -113,14 +170,26 @@ module Edoors
p
end
#
+ # add the given Particle to the application Particle fifo
+ #
+ # @param [Particle] p the Particle to add
+ #
def post_p p
@app_fifo << p
end
#
+ # add the given Particle to the system Particle fifo
+ #
+ # @param [Particle] p the Particle to add
+ #
def post_sys_p p
@sys_fifo << p
end
#
+ # process the given particle
+ #
+ # @param [Particle] p the Particle to be processed
+ #
def process_sys_p p
if p.action==Edoors::SYS_ACT_HIBERNATE
stop!
@@ -130,6 +199,12 @@ module Edoors
end
end
#
+ # starts the system mainloop
+ #
+ # first Iota#start! is called on each children unless the system is resuming from hibernation
+ # then while there is Particle in the fifo, first process all system Particle then 1 application Particle
+ # after all Iota#stop! is called on each children, unless the system is going into hibernation
+ #
def spin!
@iotas.values.each do |iota| iota.start! end unless @hibernation
@run = true
@@ -148,15 +223,27 @@ module Edoors
@iotas.values.each do |iota| iota.stop! end unless @hibernation
end
#
+ # stops the spinning
+ #
def stop!
@run=false
end
#
+ # sends the system into hibernation
+ #
+ # @param [String] path the path to the hibernation file
+ #
+ # the system is serialized into JSON data and flushed to disk
+ #
def hibernate! path=nil
@hibernation = true
File.open(path||@hibernate_path,'w') do |f| f << JSON.pretty_generate(self) end
end
#
+ # resumes the system from the given hibernation file
+ #
+ # @param [String] path the hibernation file to load the system from
+ #
def self.resume! path
self.json_create JSON.load File.open(path,'r') { |f| f.read }
end