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
256
257
258
259
260
261
262
263
264
265
266
|
#! /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
#
# creates a Room object from the arguments.
#
# @param [String] n the name of this Room
# @param [Iota] p the parent
#
def initialize n, p
super n, p
@iotas = {}
@links = {}
end
#
# called by JSON#generate to serialize the Room object into JSON data
#
# @param [Array] a belongs to JSON generator
#
def to_json *a
{
'kls' => self.class.name,
'name' => @name,
'iotas' => @iotas,
'links' => @links
}.to_json *a
end
#
# creates a Room object from a JSON data
#
# @param [Hash] o belongs to JSON parse
#
# @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
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
#
# creates a Room object from the data of a particle
#
# @param [Particle] p the Particle to get Room attributes from
#
def self.from_particle p, s
Edoors::Room.new(p.get_data(Edoors::IOTA_NAME), s)
end
#
# adds the given Iota to this Room
#
# @param [Iota] i the Iota to add
#
# @raise Edoors::Exception if i already has a parent or if a Iota with the same name already exists
#
def add_iota i
raise Edoors::Exception.new "Iota #{i.name} already has #{i.parent.name} as parent" if not i.parent.nil? and i.parent!=self
raise Edoors::Exception.new "Iota #{i.name} already exists in #{path}" if @iotas.has_key? i.name
i.parent = self if i.parent.nil?
@iotas[i.name]=i
end
#
# adds the given Link to this Room
#
# @param [Link] l the Link to add
#
# @raise Edoors::Exception if the link source can't be found in this Room
#
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
#
# forward the call to each children
#
# @see Spin#spin! called on system bootup
#
def start!
puts " * start #{path}" if @spin.debug_routing
@iotas.values.each do |iota| iota.start! end
end
#
# forward the call to each children
#
# @see Spin#spin! called on system shutdown
#
def stop!
puts " * stop #{path}" if @spin.debug_routing
@iotas.values.each do |iota| iota.stop! end
end
#
# search through all children for a matching Iota
#
# @param [String] spath the full path of the earch Iota
#
# @return [Iota] if found
#
# @see Particle#initialize used to transform @src and @dst JSON data into refrences
#
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
#
# search for a matching link
#
# @param [Particle] p the Particle searching for a matching link
#
def _try_links p
puts " -> try_links ..." if @spin.debug_routing
links = @links[p.src.name]
return false if links.nil?
pending_link = nil
links.each do |link|
if p.link_with? link
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
end
pending_link
end
private :_try_links
#
# route the given Particle
#
# @param [Particle] p the Particle to be routed
#
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
#
# send the given Particle
#
# @param [Particle] p the Particle to send
# @param [Boolean] sys if true send to system Particle fifo
#
def _send p, sys=false
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
#
# send the given Particle to user Particle fifo
#
# @param [Particle] p the Particle to send
#
def send_p p
puts " * send_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing
_send p
puts " -> #{p.dst.path}#{Edoors::ACT_SEP}#{p.action}" if @spin.debug_routing
@spin.post_p p
end
#
# send the given Particle to system Particle fifo
#
# @param [Particle] p the Particle to send
#
def send_sys_p p
puts " * send_sys_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing
_send p, true
puts " -> #{p.dst.path}#{Edoors::ACT_SEP}#{p.action}" if @spin.debug_routing
@spin.post_sys_p p
end
#
# process the given system Particle
#
# @param [Particle] p the Particle to be processed
#
# @note the Particle is automatically released
#
def process_sys_p p
if p.action==Edoors::SYS_ACT_ADD_LINK
add_link Edoors::Link.from_particle p
elsif p.action==Edoors::SYS_ACT_ADD_ROOM
Edoors::Room.from_particle p, self
end
@spin.release_p p
end
#
end
#
end
#
# EOF
|