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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#! /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
#
# 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
#
@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 = 'edoors-hibernate-'+n+'.json'
@debug_garbage = o[:debug_garbage]||o['debug_garbage']||false
@debug_routing = o[:debug_routing]||o['debug_routing']||false
#
if not o.empty?
room = o['inner_room']
if room
room['iotas'].each do |name,iota|
eval( iota['kls'] ).json_create(iota.merge!('parent'=>self))
end
room['links'].each do |src,links|
links.each do |link|
add_link Edoors::Link.json_create(link)
end
end
end
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_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
{
'kls' => self.class.name,
'timestamp' => Time.now,
'name' => @name,
'hibernation' => @hibernation,
'inner_room' => { :iotas=>@iotas, :links=>@links },
'sys_fifo' => @sys_fifo,
'app_fifo' => @app_fifo,
'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
@sys_fifo.clear
@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
while p2=p.merged_shift
release_p p2
end
p.reset!
( @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?
p = l.pop
return p_kls.new if p.nil?
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!
hibernate! p[FIELD_HIBERNATE_PATH]
else
super p
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
@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
#
# 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
#
end
#
end
#
# EOF
|