From 3e01a17ec9ed22867b409ffedab20e160de90ff7 Mon Sep 17 00:00:00 2001 From: Jérémy Zurcher Date: Fri, 18 May 2012 11:40:38 +0200 Subject: social skills failure ;) evendoors-ruby is renamed into iotas --- lib/evendoors.rb | 64 -------------- lib/evendoors/board.rb | 67 --------------- lib/evendoors/door.rb | 91 -------------------- lib/evendoors/link.rb | 64 -------------- lib/evendoors/particle.rb | 213 ---------------------------------------------- lib/evendoors/room.rb | 185 ---------------------------------------- lib/evendoors/spin.rb | 150 -------------------------------- lib/evendoors/spot.rb | 64 -------------- lib/iotas.rb | 64 ++++++++++++++ lib/iotas/board.rb | 67 +++++++++++++++ lib/iotas/door.rb | 91 ++++++++++++++++++++ lib/iotas/link.rb | 64 ++++++++++++++ lib/iotas/particle.rb | 213 ++++++++++++++++++++++++++++++++++++++++++++++ lib/iotas/room.rb | 185 ++++++++++++++++++++++++++++++++++++++++ lib/iotas/spin.rb | 150 ++++++++++++++++++++++++++++++++ lib/iotas/spot.rb | 64 ++++++++++++++ 16 files changed, 898 insertions(+), 898 deletions(-) delete mode 100644 lib/evendoors.rb delete mode 100644 lib/evendoors/board.rb delete mode 100644 lib/evendoors/door.rb delete mode 100644 lib/evendoors/link.rb delete mode 100644 lib/evendoors/particle.rb delete mode 100644 lib/evendoors/room.rb delete mode 100644 lib/evendoors/spin.rb delete mode 100644 lib/evendoors/spot.rb create mode 100644 lib/iotas.rb create mode 100644 lib/iotas/board.rb create mode 100644 lib/iotas/door.rb create mode 100644 lib/iotas/link.rb create mode 100644 lib/iotas/particle.rb create mode 100644 lib/iotas/room.rb create mode 100644 lib/iotas/spin.rb create mode 100644 lib/iotas/spot.rb (limited to 'lib') diff --git a/lib/evendoors.rb b/lib/evendoors.rb deleted file mode 100644 index e57cf26..0000000 --- a/lib/evendoors.rb +++ /dev/null @@ -1,64 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -# -module EvenDoors - # - PATH_SEP = '/'.freeze - LINK_SEP = ','.freeze - ACT_SEP = '?'.freeze - # - ACT_GET = 'get'.freeze - ACT_FOLLOW = 'follow'.freeze - ACT_ERROR = 'error'.freeze - # - SYS_ACT_HIBERNATE = 'hibernate'.freeze - SYS_ACT_ADD_LINK = 'sys_add_link'.freeze - # - 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 - # - FIELD_ERROR_MSG = 'edoors_error'.freeze - FIELD_HIBERNATE_PATH= 'hibernate_path'.freeze - # - ERROR_ROUTE_NS = 'routing error: no source'.freeze - ERROR_ROUTE_RRWD = 'routing error: right room, wrong door'.freeze - ERROR_ROUTE_DDWR = 'routing error: drill down, wrong room'.freeze - ERROR_ROUTE_TRWR = 'routing error: top room, wrong room'.freeze - ERROR_ROUTE_NDNL = 'routing error: no destination, no link'.freeze - ERROR_ROUTE_SND = 'routing error: system no destination'.freeze - # - class Exception < ::Exception; end - # -end -# -require 'json' -require 'evendoors/particle' -require 'evendoors/spot' -require 'evendoors/room' -require 'evendoors/spin' -require 'evendoors/door' -require 'evendoors/board' -require 'evendoors/link' -# -# EOF diff --git a/lib/evendoors/board.rb b/lib/evendoors/board.rb deleted file mode 100644 index a81b31a..0000000 --- a/lib/evendoors/board.rb +++ /dev/null @@ -1,67 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -# -module EvenDoors - # - 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 EvenDoors::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 EvenDoors::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!=EvenDoors::ACT_ERROR - p2 = @postponed[p.link_value] ||= p - return if p2==p - @postponed.delete p.link_value - p,p2 = p2,p if p.action==EvenDoors::ACT_FOLLOW - p.merge! p2 - end - @saved = p - receive_p p - garbage if not @saved.nil? - end - # - end - # -end -# -# EOF diff --git a/lib/evendoors/door.rb b/lib/evendoors/door.rb deleted file mode 100644 index bcf8534..0000000 --- a/lib/evendoors/door.rb +++ /dev/null @@ -1,91 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -# -module EvenDoors - # - class Door < Spot - # - 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 EvenDoors::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 - p = @spin.require_p p_kls - p.src = self - p - 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 EvenDoors::FIELD_ERROR_MSG}" if @saved.action==EvenDoors::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_p p - p.src = self - @saved=nil if @saved==p # particle is sent back the data, all is good - @parent.send_p p # daddy will know what to do - end - # - def send_sys_p p - p.src = self - @saved=nil if @saved==p # particle is sent back the data, all is good - @parent.send_sys_p p # daddy will know what to do - end - # - end - # -end -# -# EOF diff --git a/lib/evendoors/link.rb b/lib/evendoors/link.rb deleted file mode 100644 index 1e11191..0000000 --- a/lib/evendoors/link.rb +++ /dev/null @@ -1,64 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -# -module EvenDoors - # - 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 EvenDoors::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 - EvenDoors::Link.new(p.get_data(EvenDoors::LNK_SRC), p.get_data(EvenDoors::LNK_DSTS), - p.get_data(EvenDoors::LNK_FIELDS), p.get_data(EvenDoors::LNK_CONDF), - p.get_data(EvenDoors::LNK_CONDV)) - end - # - attr_accessor :door - attr_reader :src, :dsts, :fields, :cond_fields, :cond_value - # - end - # -end -# -# EOF diff --git a/lib/evendoors/particle.rb b/lib/evendoors/particle.rb deleted file mode 100644 index b3bfa82..0000000 --- a/lib/evendoors/particle.rb +++ /dev/null @@ -1,213 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -require 'time' -# -module EvenDoors - # - class Particle - # - def initialize o={} - @ts = Time.now # creation time - @src = nil # Spot where it's originated from - @dst = nil # Spot 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 EvenDoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name - self.new o - end - # - def reset! - @ts = Time.now - @src = @dst = @room = @door = @action = @link_value = nil - @dsts.clear - @link_fields.clear - @payload.clear - @merged.clear - end - # - attr_accessor :src - attr_reader :ts, :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(EvenDoors::LINK_SEP).each do |dst| - if dst.empty? or dst[0]==EvenDoors::PATH_SEP or dst[0]==EvenDoors::PATH_SEP or dst=~/\/\?/\ - or dst=~/\/{2,}/ or dst=~/\s+/ or dst==EvenDoors::ACT_SEP - raise EvenDoors::Exception.new "destination #{dst} is not acceptable" - end - @dsts << dst - end - end - # - def set_dst! a, d='' - @dst = @room = @door = @action = nil - clear_dsts! - add_dsts d+EvenDoors::ACT_SEP+a - end - # - def split_dst! - @dst = @room = @door = @action = nil - return if (n = next_dst).nil? - p, @action = n.split EvenDoors::ACT_SEP - i = p.rindex EvenDoors::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 - # - def dst_routed! dst - @dst = dst - @dsts.shift - end - # - def error! e, dst=nil - @action = EvenDoors::ACT_ERROR - @dst = dst||@src - @payload[EvenDoors::FIELD_ERROR_MSG]=e - end - # - def apply_link! lnk - @src = 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/evendoors/room.rb b/lib/evendoors/room.rb deleted file mode 100644 index caaf565..0000000 --- a/lib/evendoors/room.rb +++ /dev/null @@ -1,185 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -# -module EvenDoors - # - class Room < Spot - # - def initialize n, p - super n, p - @spots = {} - @links = {} - end - # - def to_json *a - { - 'kls' => self.class.name, - 'name' => @name, - 'spots' => @spots, - 'links' => @links - }.to_json *a - end - # - def self.json_create o - raise EvenDoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name - room = self.new o['name'], o['parent'] - o['spots'].each do |name,spot| - eval( spot['kls'] ).json_create(spot.merge!('parent'=>room)) - end - o['links'].each do |src,links| - links.each do |link| - room.add_link EvenDoors::Link.json_create(link) - end - end - room - end - # - def add_spot s - raise EvenDoors::Exception.new "Spot #{s.name} already has #{s.parent.name} as parent" if not s.parent.nil? and s.parent!=self - raise EvenDoors::Exception.new "Spot #{s.name} already exists in #{path}" if @spots.has_key? s.name - s.parent = self if s.parent.nil? - @spots[s.name]=s - end - # - def add_link l - l.door = @spots[l.src] - raise EvenDoors::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 - @spots.values.each do |spot| spot.start! end - end - # - def stop! - puts " * stop #{path}" if @spin.debug_routing - @spots.values.each do |spot| spot.stop! end - end - # - def search_down spath - return self if spath==path - return nil if (spath=~/^#{path}\/(\w+)\/?/)!=0 - if spot = @spots[$1] - return spot if spot.path==spath # needed as Door doesn't implement #search_down - return spot.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_p p - end - (not pending_link.nil?) - end - # - def route_p p - if p.room.nil? or p.room==path - if door = @spots[p.door] - p.dst_routed! door - else - p.error! EvenDoors::ERROR_ROUTE_RRWD - end - elsif (p.room=~/^#{path}\/(.*)/)==0 - room, *more = $1.split EvenDoors::PATH_SEP - if child=@spots[room] - child.route_p p - else - p.error! EvenDoors::ERROR_ROUTE_DDWR - end - elsif @parent - @parent.route_p p - else - p.error! EvenDoors::ERROR_ROUTE_TRWR - end - end - # - def send_p p - puts " * send_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing - if p.src.nil? - # do not route orphan particles !! - p.error! EvenDoors::ERROR_ROUTE_NS, @spin - elsif p.next_dst - p.split_dst! - if p.door - route_p p - else - # boomerang - p.dst_routed! p.src - end - elsif try_links p - return - else - p.error! EvenDoors::ERROR_ROUTE_NDNL - end - puts " -> #{p.dst.path}#{EvenDoors::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 - if p.next_dst - p.split_dst! - if p.door - route_p p - elsif p.action - p.dst_routed! @spin - end - else - p.error! EvenDoors::ERROR_ROUTE_SND - end - puts " -> #{p.dst.path}#{EvenDoors::ACT_SEP}#{p.action}" if @spin.debug_routing - @spin.post_sys_p p - end - # - def process_sys_p p - if p.action==EvenDoors::SYS_ACT_ADD_LINK - add_link EvenDoors::Link.from_particle_data p - end - @spin.release_p p - end - # - end - # -end -# -# EOF diff --git a/lib/evendoors/spin.rb b/lib/evendoors/spin.rb deleted file mode 100644 index 5361a76..0000000 --- a/lib/evendoors/spin.rb +++ /dev/null @@ -1,150 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -# -module EvenDoors - # - class Spin < Room - # - def initialize n, o={} - super n, nil - # - @pool = {} # per particle class free list - @sys_fifo = [] # system particles fifo list - @app_fifo = [] # application particles fifo list - # - @run = false - @hibernation = o['hibernation']||false - @hibernate_path = 'evendoors-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['spots'].each do |name,spot| - EvenDoors::Room.json_create(spot.merge!('parent'=>self)) - end if o['spots'] - o['app_fifo'].each do |particle| - @app_fifo << EvenDoors::Particle.json_create(particle.merge!('spin'=>self)) - end if o['app_fifo'] - o['sys_fifo'].each do |particle| - @sys_fifo << EvenDoors::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, - '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 - self.new o['name'], o - end - # - def clear! - @spots.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 - ( @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 post_p p - @app_fifo << p - end - # - def post_sys_p p - @sys_fifo << p - end - # - def process_sys_p p - if p.action==EvenDoors::SYS_ACT_HIBERNATE - stop! - hibernate! p[FIELD_HIBERNATE_PATH] - else - super p - end - end - # - def spin! - @spots.values.each do |spot| spot.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 - @spots.values.each do |spot| spot.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 diff --git a/lib/evendoors/spot.rb b/lib/evendoors/spot.rb deleted file mode 100644 index cbaba7a..0000000 --- a/lib/evendoors/spot.rb +++ /dev/null @@ -1,64 +0,0 @@ -#! /usr/bin/env ruby -# -*- coding: UTF-8 -*- -# -# Copyright 2012 Jérémy Zurcher -# -# This file is part of evendoors-ruby. -# -# evendoors-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. -# -# evendoors-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 evendoors-ruby. If not, see . - -# -module EvenDoors - # - class Spot - # - def initialize n, p - @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+EvenDoors::PATH_SEP : '') + @name - @spin = ( @parent ? @parent.spin : self ) - @parent.add_spot self if @parent - raise EvenDoors::Exception.new "Spot name #{name} is not valid" if @name.include? EvenDoors::PATH_SEP - end - # - attr_reader :name, :path, :spin - attr_accessor :viewer, :parent - # - def start! - # override this to initialize yout object on stystem start - end - # - def stop! - # override this to initialize yout object on stystem 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/iotas.rb b/lib/iotas.rb new file mode 100644 index 0000000..24c021e --- /dev/null +++ b/lib/iotas.rb @@ -0,0 +1,64 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +# +module Iotas + # + PATH_SEP = '/'.freeze + LINK_SEP = ','.freeze + ACT_SEP = '?'.freeze + # + ACT_GET = 'get'.freeze + ACT_FOLLOW = 'follow'.freeze + ACT_ERROR = 'error'.freeze + # + SYS_ACT_HIBERNATE = 'hibernate'.freeze + SYS_ACT_ADD_LINK = 'sys_add_link'.freeze + # + 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 + # + FIELD_ERROR_MSG = 'edoors_error'.freeze + FIELD_HIBERNATE_PATH= 'hibernate_path'.freeze + # + ERROR_ROUTE_NS = 'routing error: no source'.freeze + ERROR_ROUTE_RRWD = 'routing error: right room, wrong door'.freeze + ERROR_ROUTE_DDWR = 'routing error: drill down, wrong room'.freeze + ERROR_ROUTE_TRWR = 'routing error: top room, wrong room'.freeze + ERROR_ROUTE_NDNL = 'routing error: no destination, no link'.freeze + ERROR_ROUTE_SND = 'routing error: system no destination'.freeze + # + class Exception < ::Exception; end + # +end +# +require 'json' +require 'iotas/particle' +require 'iotas/spot' +require 'iotas/room' +require 'iotas/spin' +require 'iotas/door' +require 'iotas/board' +require 'iotas/link' +# +# EOF diff --git a/lib/iotas/board.rb b/lib/iotas/board.rb new file mode 100644 index 0000000..8c1991f --- /dev/null +++ b/lib/iotas/board.rb @@ -0,0 +1,67 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +# +module Iotas + # + 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 Iotas::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 Iotas::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!=Iotas::ACT_ERROR + p2 = @postponed[p.link_value] ||= p + return if p2==p + @postponed.delete p.link_value + p,p2 = p2,p if p.action==Iotas::ACT_FOLLOW + p.merge! p2 + end + @saved = p + receive_p p + garbage if not @saved.nil? + end + # + end + # +end +# +# EOF diff --git a/lib/iotas/door.rb b/lib/iotas/door.rb new file mode 100644 index 0000000..8474b09 --- /dev/null +++ b/lib/iotas/door.rb @@ -0,0 +1,91 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +# +module Iotas + # + class Door < Spot + # + 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 Iotas::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 + p = @spin.require_p p_kls + p.src = self + p + 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 Iotas::FIELD_ERROR_MSG}" if @saved.action==Iotas::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_p p + p.src = self + @saved=nil if @saved==p # particle is sent back the data, all is good + @parent.send_p p # daddy will know what to do + end + # + def send_sys_p p + p.src = self + @saved=nil if @saved==p # particle is sent back the data, all is good + @parent.send_sys_p p # daddy will know what to do + end + # + end + # +end +# +# EOF diff --git a/lib/iotas/link.rb b/lib/iotas/link.rb new file mode 100644 index 0000000..b1fd9ba --- /dev/null +++ b/lib/iotas/link.rb @@ -0,0 +1,64 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +# +module Iotas + # + 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 Iotas::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 + Iotas::Link.new(p.get_data(Iotas::LNK_SRC), p.get_data(Iotas::LNK_DSTS), + p.get_data(Iotas::LNK_FIELDS), p.get_data(Iotas::LNK_CONDF), + p.get_data(Iotas::LNK_CONDV)) + end + # + attr_accessor :door + attr_reader :src, :dsts, :fields, :cond_fields, :cond_value + # + end + # +end +# +# EOF diff --git a/lib/iotas/particle.rb b/lib/iotas/particle.rb new file mode 100644 index 0000000..ffb141b --- /dev/null +++ b/lib/iotas/particle.rb @@ -0,0 +1,213 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +require 'time' +# +module Iotas + # + class Particle + # + def initialize o={} + @ts = Time.now # creation time + @src = nil # Spot where it's originated from + @dst = nil # Spot 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 Iotas::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name + self.new o + end + # + def reset! + @ts = Time.now + @src = @dst = @room = @door = @action = @link_value = nil + @dsts.clear + @link_fields.clear + @payload.clear + @merged.clear + end + # + attr_accessor :src + attr_reader :ts, :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(Iotas::LINK_SEP).each do |dst| + if dst.empty? or dst[0]==Iotas::PATH_SEP or dst[0]==Iotas::PATH_SEP or dst=~/\/\?/\ + or dst=~/\/{2,}/ or dst=~/\s+/ or dst==Iotas::ACT_SEP + raise Iotas::Exception.new "destination #{dst} is not acceptable" + end + @dsts << dst + end + end + # + def set_dst! a, d='' + @dst = @room = @door = @action = nil + clear_dsts! + add_dsts d+Iotas::ACT_SEP+a + end + # + def split_dst! + @dst = @room = @door = @action = nil + return if (n = next_dst).nil? + p, @action = n.split Iotas::ACT_SEP + i = p.rindex Iotas::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 + # + def dst_routed! dst + @dst = dst + @dsts.shift + end + # + def error! e, dst=nil + @action = Iotas::ACT_ERROR + @dst = dst||@src + @payload[Iotas::FIELD_ERROR_MSG]=e + end + # + def apply_link! lnk + @src = 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/iotas/room.rb b/lib/iotas/room.rb new file mode 100644 index 0000000..569c921 --- /dev/null +++ b/lib/iotas/room.rb @@ -0,0 +1,185 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +# +module Iotas + # + class Room < Spot + # + def initialize n, p + super n, p + @spots = {} + @links = {} + end + # + def to_json *a + { + 'kls' => self.class.name, + 'name' => @name, + 'spots' => @spots, + 'links' => @links + }.to_json *a + end + # + def self.json_create o + raise Iotas::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name + room = self.new o['name'], o['parent'] + o['spots'].each do |name,spot| + eval( spot['kls'] ).json_create(spot.merge!('parent'=>room)) + end + o['links'].each do |src,links| + links.each do |link| + room.add_link Iotas::Link.json_create(link) + end + end + room + end + # + def add_spot s + raise Iotas::Exception.new "Spot #{s.name} already has #{s.parent.name} as parent" if not s.parent.nil? and s.parent!=self + raise Iotas::Exception.new "Spot #{s.name} already exists in #{path}" if @spots.has_key? s.name + s.parent = self if s.parent.nil? + @spots[s.name]=s + end + # + def add_link l + l.door = @spots[l.src] + raise Iotas::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 + @spots.values.each do |spot| spot.start! end + end + # + def stop! + puts " * stop #{path}" if @spin.debug_routing + @spots.values.each do |spot| spot.stop! end + end + # + def search_down spath + return self if spath==path + return nil if (spath=~/^#{path}\/(\w+)\/?/)!=0 + if spot = @spots[$1] + return spot if spot.path==spath # needed as Door doesn't implement #search_down + return spot.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_p p + end + (not pending_link.nil?) + end + # + def route_p p + if p.room.nil? or p.room==path + if door = @spots[p.door] + p.dst_routed! door + else + p.error! Iotas::ERROR_ROUTE_RRWD + end + elsif (p.room=~/^#{path}\/(.*)/)==0 + room, *more = $1.split Iotas::PATH_SEP + if child=@spots[room] + child.route_p p + else + p.error! Iotas::ERROR_ROUTE_DDWR + end + elsif @parent + @parent.route_p p + else + p.error! Iotas::ERROR_ROUTE_TRWR + end + end + # + def send_p p + puts " * send_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing + if p.src.nil? + # do not route orphan particles !! + p.error! Iotas::ERROR_ROUTE_NS, @spin + elsif p.next_dst + p.split_dst! + if p.door + route_p p + else + # boomerang + p.dst_routed! p.src + end + elsif try_links p + return + else + p.error! Iotas::ERROR_ROUTE_NDNL + end + puts " -> #{p.dst.path}#{Iotas::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 + if p.next_dst + p.split_dst! + if p.door + route_p p + elsif p.action + p.dst_routed! @spin + end + else + p.error! Iotas::ERROR_ROUTE_SND + end + puts " -> #{p.dst.path}#{Iotas::ACT_SEP}#{p.action}" if @spin.debug_routing + @spin.post_sys_p p + end + # + def process_sys_p p + if p.action==Iotas::SYS_ACT_ADD_LINK + add_link Iotas::Link.from_particle_data p + end + @spin.release_p p + end + # + end + # +end +# +# EOF diff --git a/lib/iotas/spin.rb b/lib/iotas/spin.rb new file mode 100644 index 0000000..03c0141 --- /dev/null +++ b/lib/iotas/spin.rb @@ -0,0 +1,150 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +# +module Iotas + # + class Spin < Room + # + def initialize n, o={} + super n, nil + # + @pool = {} # per particle class free list + @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['spots'].each do |name,spot| + Iotas::Room.json_create(spot.merge!('parent'=>self)) + end if o['spots'] + o['app_fifo'].each do |particle| + @app_fifo << Iotas::Particle.json_create(particle.merge!('spin'=>self)) + end if o['app_fifo'] + o['sys_fifo'].each do |particle| + @sys_fifo << Iotas::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, + '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 Iotas::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name + self.new o['name'], o + end + # + def clear! + @spots.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 + ( @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 post_p p + @app_fifo << p + end + # + def post_sys_p p + @sys_fifo << p + end + # + def process_sys_p p + if p.action==Iotas::SYS_ACT_HIBERNATE + stop! + hibernate! p[FIELD_HIBERNATE_PATH] + else + super p + end + end + # + def spin! + @spots.values.each do |spot| spot.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 + @spots.values.each do |spot| spot.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 diff --git a/lib/iotas/spot.rb b/lib/iotas/spot.rb new file mode 100644 index 0000000..aca0c2b --- /dev/null +++ b/lib/iotas/spot.rb @@ -0,0 +1,64 @@ +#! /usr/bin/env ruby +# -*- coding: UTF-8 -*- +# +# Copyright 2012 Jérémy Zurcher +# +# This file is part of iotas. +# +# iotas 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. +# +# iotas 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 iotas. If not, see . + +# +module Iotas + # + class Spot + # + def initialize n, p + @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+Iotas::PATH_SEP : '') + @name + @spin = ( @parent ? @parent.spin : self ) + @parent.add_spot self if @parent + raise Iotas::Exception.new "Spot name #{name} is not valid" if @name.include? Iotas::PATH_SEP + 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 -- cgit v1.2.3