summaryrefslogtreecommitdiffstats
path: root/lib/evendoors/spin.rb
blob: 69d204ddacd0bab507aa4628413d63a39ac15ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
# Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
#
# 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 <http://www.gnu.org/licenses/>.

#
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
            @debug_errors = o[:debug_errors]||o['debug_errors']||false
            @debug_routing = o[:debug_routing]||o['debug_routing']||false
            #
            if not o.empty?
                p ={'parent'=>self}
                o['spots'].each do |name,spot|
                    EvenDoors::Room.json_create(spot.merge!(p))
                end if o['spots']
                o['app_fifo'].each do |particle|
                    @app_fifo << EvenDoors::Particle.json_create(particle.merge!(p))
                end if o['app_fifo']
                o['sys_fifo'].each do |particle|
                    @sys_fifo <<  EvenDoors::Particle.json_create(particle.merge!(p))
                end if o['sys_fifo']
            end
        end
        #
        attr_accessor :run, :debug_errors, :debug_routing
        #
        def to_json *a
            {
                '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
            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 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
            @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
#
# EOF