summaryrefslogtreecommitdiffstats
path: root/lib/edoors
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2012-06-10 11:11:28 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2012-06-10 11:11:28 +0200
commit6553912a6dff9b2c8bd3f06cb5f10488eef2c18c (patch)
tree804cbb720c993afd5453ea4a8086b264e22382fe /lib/edoors
parent74d45762846ea7e6ac09f65fb31672b822f7a349 (diff)
downloadedoors-ruby-6553912a6dff9b2c8bd3f06cb5f10488eef2c18c.zip
edoors-ruby-6553912a6dff9b2c8bd3f06cb5f10488eef2c18c.tar.gz
project iotas->edoors-ruby, module Iotas->Edoors
Diffstat (limited to 'lib/edoors')
-rw-r--r--lib/edoors/board.rb69
-rw-r--r--lib/edoors/door.rb94
-rw-r--r--lib/edoors/iota.rb67
-rw-r--r--lib/edoors/link.rb70
-rw-r--r--lib/edoors/particle.rb231
-rw-r--r--lib/edoors/room.rb190
-rw-r--r--lib/edoors/spin.rb159
7 files changed, 880 insertions, 0 deletions
diff --git a/lib/edoors/board.rb b/lib/edoors/board.rb
new file mode 100644
index 0000000..a837512
--- /dev/null
+++ b/lib/edoors/board.rb
@@ -0,0 +1,69 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
+#
+# This file is part of edoors-ruby.
+#
+# edoors-ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# edoors-ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
+
+#
+module Edoors
+ #
+ ACT_FOLLOW = 'follow'.freeze
+ #
+ class Board < Door
+ #
+ def initialize n, p
+ super n, p
+ @postponed = {}
+ end
+ #
+ def to_json *a
+ {
+ 'kls' => self.class.name,
+ 'name' => @name,
+ 'postponed' => @postponed
+ }.merge(hibernate!).to_json *a
+ end
+ #
+ def self.json_create o
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
+ board = self.new o['name'], o['parent']
+ o['postponed'].each do |link_value,particle|
+ board.process_p Edoors::Particle.json_create(particle.merge!('spin'=>board.spin))
+ end
+ board.resume! o
+ board
+ end
+ #
+ def process_p p
+ @viewer.receive_p p if @viewer
+ if p.action!=Edoors::ACT_ERROR
+ p2 = @postponed[p.link_value] ||= p
+ return if p2==p
+ @postponed.delete p.link_value
+ p,p2 = p2,p if p.action==Edoors::ACT_FOLLOW
+ p.merge! p2
+ end
+ @saved = p
+ receive_p p
+ garbage if not @saved.nil?
+ end
+ #
+ end
+ #
+end
+#
+# EOF
diff --git a/lib/edoors/door.rb b/lib/edoors/door.rb
new file mode 100644
index 0000000..ecbf4a9
--- /dev/null
+++ b/lib/edoors/door.rb
@@ -0,0 +1,94 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
+#
+# This file is part of edoors-ruby.
+#
+# edoors-ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# edoors-ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
+
+#
+module Edoors
+ #
+ class Door < Iota
+ #
+ def initialize n, p
+ super n, p
+ @saved = nil
+ end
+ #
+ def to_json *a
+ {
+ 'kls' => self.class.name,
+ 'name' => @name
+ }.merge(hibernate!).to_json *a
+ end
+ #
+ def self.json_create o
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
+ door = self.new o['name'], o['parent']
+ door.resume! o
+ door
+ end
+ #
+ def require_p p_kls
+ @spin.require_p p_kls
+ end
+ #
+ def release_p p
+ @saved=nil if @saved==p # particle is released, all is good
+ @spin.release_p p
+ end
+ #
+ def garbage
+ puts " ! #{path} didn't give back #{@saved}" if @spin.debug_errors
+ puts "\t#{@saved.data Edoors::FIELD_ERROR_MSG}" if @saved.action==Edoors::ACT_ERROR
+ release_p @saved
+ @saved = nil
+ end
+ #
+ def process_p p
+ @viewer.receive_p p if @viewer
+ @saved = p
+ receive_p p
+ garbage if not @saved.nil?
+ end
+ #
+ def process_sys_p p
+ # nothing todo with it now
+ @spin.release_p p
+ end
+ #
+ def _send sys, p, a=nil, d=nil
+ p.init! self
+ p.set_dst! a, d||self if a
+ @saved=nil if @saved==p # particle is sent back the data, all is good
+ # daddy will know what to do
+ sys ? @parent.send_sys_p(p) : @parent.send_p(p)
+ end
+ private :_send
+ #
+ def send_p p, a=nil, d=nil
+ _send false, p, a, d
+ end
+ #
+ def send_sys_p p, a=nil, d=nil
+ _send true, p, a, d
+ end
+ #
+ end
+ #
+end
+#
+# EOF
diff --git a/lib/edoors/iota.rb b/lib/edoors/iota.rb
new file mode 100644
index 0000000..47a2eb6
--- /dev/null
+++ b/lib/edoors/iota.rb
@@ -0,0 +1,67 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
+#
+# This file is part of edoors-ruby.
+#
+# edoors-ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# edoors-ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
+
+#
+module Edoors
+ #
+ class Iota
+ #
+ def initialize n, p
+ raise Edoors::Exception.new "Iota name #{n} is not valid" if n.include? Edoors::PATH_SEP
+ @name = n # unique in it's room
+ @parent = p # single direct parent
+ @viewer = nil # particle going through that position will be sent there readonly
+ @path = ( @parent ? @parent.path+Edoors::PATH_SEP : '') + @name
+ @spin = ( @parent ? @parent.spin : self )
+ if @parent
+ @parent.add_iota self
+ @spin.add_to_world self if @spin.is_a? Edoors::Spin
+ end
+ end
+ #
+ attr_reader :name, :path, :spin
+ attr_accessor :viewer, :parent
+ #
+ def start!
+ # override this to initialize your object on system start
+ end
+ #
+ def stop!
+ # override this to initialize your object on system stop
+ end
+ #
+ def hibernate!
+ # override this to save your object state on hibernate
+ {}
+ end
+ #
+ def resume! o
+ # override this to restore your object state on resume
+ end
+ #
+ def receive_p p
+ raise NoMethodError.new "receive_p(p) must be overridden"
+ end
+ #
+ end
+ #
+end
+#
+# EOF
diff --git a/lib/edoors/link.rb b/lib/edoors/link.rb
new file mode 100644
index 0000000..b6a3fb9
--- /dev/null
+++ b/lib/edoors/link.rb
@@ -0,0 +1,70 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
+#
+# This file is part of edoors-ruby.
+#
+# edoors-ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# edoors-ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
+
+#
+module Edoors
+ #
+ LNK_SRC = 'edoors_lnk_src'.freeze
+ LNK_DSTS = 'edoors_lnk_dsts'.freeze
+ LNK_FIELDS = 'edoors_lnk_fields'.freeze
+ LNK_CONDF = 'edoors_lnk_condf'.freeze
+ LNK_CONDV = 'edoors_lnk_condv'.freeze
+ #
+ class Link
+ #
+ def initialize src, dsts, fields=nil, cond_fields=nil, cond_value=nil
+ @src = src # link source name
+ @dsts = dsts # , separated destinations to apply to the particle on linking success
+ @fields = fields # , separated fields to apply to the particle on linking success
+ @cond_fields = cond_fields # , separated fields used to generate the link value with particle payload
+ @cond_value = cond_value # value which will be compared to the particle link value to link or not
+ @door = nil # pointer to the source
+ end
+ #
+ def to_json *a
+ {
+ 'kls' => self.class.name,
+ 'src' => @src,
+ 'dsts' => @dsts,
+ 'fields' => @fields,
+ 'cond_fields' => @cond_fields,
+ 'cond_value' => @cond_value
+ }.to_json *a
+ end
+ #
+ def self.json_create o
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
+ self.new o['src'], o['dsts'], o['fields'], o['cond_fields'], o['cond_value']
+ end
+ #
+ def self.from_particle_data p
+ Edoors::Link.new(p.get_data(Edoors::LNK_SRC), p.get_data(Edoors::LNK_DSTS),
+ p.get_data(Edoors::LNK_FIELDS), p.get_data(Edoors::LNK_CONDF),
+ p.get_data(Edoors::LNK_CONDV))
+ end
+ #
+ attr_accessor :door
+ attr_reader :src, :dsts, :fields, :cond_fields, :cond_value
+ #
+ end
+ #
+end
+#
+# EOF
diff --git a/lib/edoors/particle.rb b/lib/edoors/particle.rb
new file mode 100644
index 0000000..c17f551
--- /dev/null
+++ b/lib/edoors/particle.rb
@@ -0,0 +1,231 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
+#
+# This file is part of edoors-ruby.
+#
+# edoors-ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# edoors-ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
+
+require 'time'
+#
+module Edoors
+ #
+ class Particle
+ #
+ def initialize o={}
+ @ts = Time.now # creation time
+ @src = nil # Iota where it's originated from
+ @dst = nil # Iota where it's heading to
+ @room = nil # Room path part of the current destination
+ @door = nil # Door path part of the current destination
+ @action = nil # action part of the current destination
+ @link_value = nil # the value computed with the link_fields values extracted from the payload
+ # used for pearing Particles in Boards and linking in routing process
+ @dsts = [] # fifo of path?action strings where to travel to
+ @link_fields = [] # the fields used to generate the link value
+ @payload = {} # the actual data carried by this particle
+ @merged = [] # list of merged particles
+ #
+ if not o.empty?
+ @ts = Time.parse(o['ts']) if o['ts']
+ @room = o['room']
+ @door = o['door']
+ @action = o['action']
+ @payload = o['payload']||{}
+ @src = o['spin'].search_down o['src'] if o['src']
+ @dst = o['spin'].search_down o['dst'] if o['dst']
+ o['dsts'].each do |dst| add_dsts dst end if o['dsts']
+ set_link_fields *o['link_fields'] if o['link_fields']
+ o['merged'].each do |particle|
+ merge! Particle.json_create(particle.merge!('spin'=>o['spin']))
+ end if o['merged']
+ end
+ end
+ #
+ def to_json *a
+ {
+ 'kls' => self.class.name,
+ 'ts' => @ts,
+ 'src' => (@src ? @src.path : nil ),
+ 'dst' => (@dst ? @dst.path : nil ),
+ 'room' => @room,
+ 'door' => @door,
+ 'action' => @action,
+ 'dsts' => @dsts,
+ 'link_fields' => @link_fields,
+ 'payload' => @payload,
+ 'merged' => @merged
+ }.to_json *a
+ end
+ #
+ def self.json_create o
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
+ self.new o
+ end
+ #
+ # called when released
+ def reset!
+ @ts = @src = @dst = @room = @door = @action = @link_value = nil
+ @dsts.clear
+ @link_fields.clear
+ @payload.clear
+ @merged.clear
+ end
+ #
+ # called when sent
+ def init! src
+ @src = src
+ @ts = Time.now
+ @dst = @room = @door = @action = nil
+ end
+ #
+ attr_reader :ts, :src, :dst, :room, :door, :action, :link_value, :payload
+ #
+ # routing
+ #
+ def next_dst
+ @dsts[0]
+ end
+ #
+ def clear_dsts!
+ @dsts.clear
+ end
+ #
+ def add_dsts dsts
+ dsts.split(Edoors::LINK_SEP).each do |dst|
+ if dst.empty? or dst[0]==Edoors::PATH_SEP or dst[0]==Edoors::PATH_SEP or dst=~/\/\?/\
+ or dst=~/\/{2,}/ or dst=~/\s+/ or dst==Edoors::ACT_SEP
+ raise Edoors::Exception.new "destination #{dst} is not acceptable"
+ end
+ @dsts << dst
+ end
+ end
+ #
+ def add_dst a, d=''
+ add_dsts d+Edoors::ACT_SEP+a
+ end
+ #
+ def set_dst! a, d
+ @action = a
+ if d.is_a? Edoors::Iota
+ @dst = d
+ else
+ _split_path! d
+ end
+ end
+ #
+ def split_dst!
+ @dst = @room = @door = @action = nil
+ return if (n = next_dst).nil?
+ p, @action = n.split Edoors::ACT_SEP
+ _split_path! p
+ end
+ #
+ def _split_path! p
+ i = p.rindex Edoors::PATH_SEP
+ if i.nil?
+ @room = nil
+ @door = p
+ else
+ @room = p[0..i-1]
+ @door = p[i+1..-1]
+ end
+ @door = nil if @door.empty?
+ end
+ private :_split_path!
+ #
+ def dst_routed! dst
+ @dst = dst
+ @dsts.shift
+ end
+ #
+ def error! e, dst=nil
+ @action = Edoors::ACT_ERROR
+ @dst = dst||@src
+ @payload[Edoors::FIELD_ERROR_MSG]=e
+ end
+ #
+ def apply_link! lnk
+ init! lnk.door
+ clear_dsts!
+ add_dsts lnk.dsts
+ set_link_fields lnk.fields
+ end
+ #
+ # data manipulation
+ #
+ def []= k, v
+ @payload[k]=v
+ compute_link_value! if @link_fields.include? k
+ end
+ #
+ def set_data k, v
+ @payload[k] = v
+ compute_link_value! if @link_fields.include? k
+ end
+ #
+ def [] k
+ @payload[k]
+ end
+ #
+ def get_data k
+ @payload[k]
+ end
+ alias :data :get_data
+ #
+ def clone_data p
+ @payload = p.payload.clone
+ end
+ #
+ # link value and fields
+ #
+ def set_link_fields *args
+ @link_fields.clear if not @link_fields.empty?
+ args.compact!
+ args.each do |lfs|
+ lfs.split(',').each do |lf|
+ @link_fields << lf
+ end
+ end
+ compute_link_value!
+ end
+ #
+ def compute_link_value!
+ @link_value = @link_fields.inject('') { |s,lf| s+=@payload[lf].to_s if @payload[lf]; s }
+ end
+ #
+ # merge particles management
+ #
+ def merge! p
+ @merged << p
+ end
+ #
+ def merged i
+ @merged[i]
+ end
+ #
+ def merged_shift
+ @merged.shift
+ end
+ #
+ def clear_merged!
+ @merged.clear
+ end
+ #
+ end
+ #
+end
+#
+# EOF
diff --git a/lib/edoors/room.rb b/lib/edoors/room.rb
new file mode 100644
index 0000000..1c855c1
--- /dev/null
+++ b/lib/edoors/room.rb
@@ -0,0 +1,190 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
+#
+# This file is part of edoors-ruby.
+#
+# edoors-ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# edoors-ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
+
+#
+module Edoors
+ #
+ ERROR_ROUTE_NS = 'routing error: no source'.freeze
+ ERROR_ROUTE_RRWD = 'routing error: right room, wrong door'.freeze
+ ERROR_ROUTE_DNE = 'routing error: does not exists'.freeze
+ ERROR_ROUTE_NDNL = 'routing error: no destination, no link'.freeze
+ ERROR_ROUTE_SND = 'routing error: system no destination'.freeze
+ #
+ class Room < Iota
+ #
+ def initialize n, p
+ super n, p
+ @iotas = {}
+ @links = {}
+ end
+ #
+ def to_json *a
+ {
+ 'kls' => self.class.name,
+ 'name' => @name,
+ 'iotas' => @iotas,
+ 'links' => @links
+ }.to_json *a
+ end
+ #
+ def self.json_create o
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
+ room = self.new o['name'], o['parent']
+ o['iotas'].each do |name,iota|
+ eval( iota['kls'] ).json_create(iota.merge!('parent'=>room))
+ end
+ o['links'].each do |src,links|
+ links.each do |link|
+ room.add_link Edoors::Link.json_create(link)
+ end
+ end
+ room
+ end
+ #
+ def add_iota s
+ raise Edoors::Exception.new "Iota #{s.name} already has #{s.parent.name} as parent" if not s.parent.nil? and s.parent!=self
+ raise Edoors::Exception.new "Iota #{s.name} already exists in #{path}" if @iotas.has_key? s.name
+ s.parent = self if s.parent.nil?
+ @iotas[s.name]=s
+ end
+ #
+ def add_link l
+ l.door = @iotas[l.src]
+ raise Edoors::Exception.new "Link source #{l.src} does not exist in #{path}" if l.door.nil?
+ (@links[l.src] ||= [])<< l
+ end
+ #
+ def start!
+ puts " * start #{path}" if @spin.debug_routing
+ @iotas.values.each do |iota| iota.start! end
+ end
+ #
+ def stop!
+ puts " * stop #{path}" if @spin.debug_routing
+ @iotas.values.each do |iota| iota.stop! end
+ end
+ #
+ def search_down spath
+ return self if spath==path
+ return nil if (spath=~/^#{path}\/(\w+)\/?/)!=0
+ if iota = @iotas[$1]
+ return iota if iota.path==spath # needed as Door doesn't implement #search_down
+ return iota.search_down spath
+ end
+ nil
+ end
+ #
+ def _try_links p
+ puts " -> try_links ..." if @spin.debug_routing
+ links = @links[p.src.name]
+ return false if links.nil?
+ pending_link = nil
+ apply_link = false
+ links.each do |link|
+ apply_link = link.cond_fields.nil? # unconditional link
+ p.set_link_fields link.cond_fields if not apply_link
+ if apply_link or (p.link_value==link.cond_value)
+ # link matches !
+ if pending_link
+ p2 = @spin.require_p p.class
+ p2.clone_data p
+ p2.apply_link! link
+ send_p p2
+ end
+ pending_link = link
+ end
+ end
+ if pending_link
+ p.apply_link! pending_link
+ _send false, p
+ end
+ pending_link
+ end
+ private :_try_links
+ #
+ def _route p
+ if p.room.nil? or p.room==path
+ if door = @iotas[p.door]
+ p.dst_routed! door
+ else
+ p.error! Edoors::ERROR_ROUTE_RRWD
+ end
+ elsif door = @spin.search_world(p.room+Edoors::PATH_SEP+p.door)
+ p.dst_routed! door
+ else
+ p.error! Edoors::ERROR_ROUTE_DNE
+ end
+ end
+ private :_route
+ #
+ def _send sys, p
+ if not sys and p.src.nil?
+ # do not route non system orphan particles !!
+ p.error! Edoors::ERROR_ROUTE_NS, @spin
+ elsif p.dst
+ # direct routing through pointer
+ return
+ elsif p.door
+ # direct routing through path
+ _route p
+ elsif p.next_dst
+ p.split_dst!
+ if p.door
+ _route p
+ elsif not sys
+ # boomerang
+ p.dst_routed! p.src
+ elsif p.action
+ p.dst_routed! @spin
+ end
+ elsif not sys and _try_links p
+ return
+ else
+ p.error!( sys ? Edoors::ERROR_ROUTE_SND : Edoors::ERROR_ROUTE_NDNL)
+ end
+ end
+ private :_send
+ #
+ def send_p p
+ puts " * send_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing
+ _send false, p
+ puts " -> #{p.dst.path}#{Edoors::ACT_SEP}#{p.action}" if @spin.debug_routing
+ @spin.post_p p
+ end
+ #
+ def send_sys_p p
+ puts " * send_sys_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing
+ _send true, p
+ puts " -> #{p.dst.path}#{Edoors::ACT_SEP}#{p.action}" if @spin.debug_routing
+ @spin.post_sys_p p
+ end
+ #
+ def process_sys_p p
+ if p.action==Edoors::SYS_ACT_ADD_LINK
+ add_link Edoors::Link.from_particle_data p
+ end
+ @spin.release_p p
+ end
+ #
+ end
+ #
+end
+#
+# EOF
diff --git a/lib/edoors/spin.rb b/lib/edoors/spin.rb
new file mode 100644
index 0000000..2bb76f8
--- /dev/null
+++ b/lib/edoors/spin.rb
@@ -0,0 +1,159 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
+#
+# This file is part of edoors-ruby.
+#
+# edoors-ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# edoors-ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
+
+#
+module Edoors
+ #
+ class Spin < Room
+ #
+ def initialize n, o={}
+ super n, nil
+ #
+ @pool = {} # per particle class free list
+ @world = {} # global iotas index
+ @sys_fifo = [] # system particles fifo list
+ @app_fifo = [] # application particles fifo list
+ #
+ @run = false
+ @hibernation = o['hibernation']||false
+ @hibernate_path = 'iotas-hibernate-'+n+'.json'
+ @debug_errors = o[:debug_errors]||o['debug_errors']||false
+ @debug_routing = o[:debug_routing]||o['debug_routing']||false
+ #
+ if not o.empty?
+ o['iotas'].each do |name,iota|
+ Edoors::Room.json_create(iota.merge!('parent'=>self))
+ end if o['iotas']
+ o['app_fifo'].each do |particle|
+ @app_fifo << Edoors::Particle.json_create(particle.merge!('spin'=>self))
+ end if o['app_fifo']
+ o['sys_fifo'].each do |particle|
+ @sys_fifo << Edoors::Particle.json_create(particle.merge!('spin'=>self))
+ end if o['sys_fifo']
+ end
+ end
+ #
+ attr_accessor :run, :hibernate_path, :debug_errors, :debug_routing
+ #
+ def to_json *a
+ {
+ 'kls' => self.class.name,
+ 'timestamp' => Time.now,
+ 'name' => @name,
+ 'hibernation' => @hibernation,
+ 'iotas' => @iotas,
+ '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 Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
+ self.new o['name'], o
+ end
+ #
+ def add_to_world iota
+ @world[iota.path] = iota
+ end
+ #
+ def search_world path
+ @world[path]
+ end
+ #
+ def clear!
+ @iotas.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
+ p.reset!
+ ( @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
+ end
+ #
+ def post_p p
+ @app_fifo << p
+ end
+ #
+ def post_sys_p p
+ @sys_fifo << p
+ end
+ #
+ def process_sys_p p
+ if p.action==Edoors::SYS_ACT_HIBERNATE
+ stop!
+ hibernate! p[FIELD_HIBERNATE_PATH]
+ else
+ super p
+ end
+ end
+ #
+ def spin!
+ @iotas.values.each do |iota| iota.start! end unless @hibernation
+ @run = true
+ @hibernation = false
+ 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
+ @iotas.values.each do |iota| iota.stop! end unless @hibernation
+ end
+ #
+ def stop!
+ @run=false
+ end
+ #
+ def hibernate! path=nil
+ @hibernation = true
+ File.open(path||@hibernate_path,'w') do |f| f << JSON.pretty_generate(self) end
+ end
+ #
+ def self.resume! path
+ self.json_create JSON.load File.open(path,'r') { |f| f.read }
+ end
+ #
+ end
+ #
+end
+#
+# EOF