edoors-ruby 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+ #
4
+ # Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
5
+ #
6
+ # This file is part of edoors-ruby.
7
+ #
8
+ # edoors-ruby is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # edoors-ruby is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ #
22
+ module Edoors
23
+ #
24
+ class Iota
25
+ #
26
+ def initialize n, p
27
+ raise Edoors::Exception.new "Iota name #{n} is not valid" if n.include? Edoors::PATH_SEP
28
+ @name = n # unique in it's room
29
+ @parent = p # single direct parent
30
+ @viewer = nil # particle going through that position will be sent there readonly
31
+ @path = ( @parent ? @parent.path+Edoors::PATH_SEP : '') + @name
32
+ @spin = ( @parent ? @parent.spin : self )
33
+ if @parent
34
+ @parent.add_iota self
35
+ @spin.add_to_world self if @spin.is_a? Edoors::Spin
36
+ end
37
+ end
38
+ #
39
+ attr_reader :name, :path, :spin
40
+ attr_accessor :viewer, :parent
41
+ #
42
+ def start!
43
+ # override this to initialize your object on system start
44
+ end
45
+ #
46
+ def stop!
47
+ # override this to initialize your object on system stop
48
+ end
49
+ #
50
+ def hibernate!
51
+ # override this to save your object state on hibernate
52
+ {}
53
+ end
54
+ #
55
+ def resume! o
56
+ # override this to restore your object state on resume
57
+ end
58
+ #
59
+ def receive_p p
60
+ raise NoMethodError.new "receive_p(p) must be overridden"
61
+ end
62
+ #
63
+ end
64
+ #
65
+ end
66
+ #
67
+ # EOF
@@ -0,0 +1,70 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+ #
4
+ # Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
5
+ #
6
+ # This file is part of edoors-ruby.
7
+ #
8
+ # edoors-ruby is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # edoors-ruby is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ #
22
+ module Edoors
23
+ #
24
+ LNK_SRC = 'edoors_lnk_src'.freeze
25
+ LNK_DSTS = 'edoors_lnk_dsts'.freeze
26
+ LNK_FIELDS = 'edoors_lnk_fields'.freeze
27
+ LNK_CONDF = 'edoors_lnk_condf'.freeze
28
+ LNK_CONDV = 'edoors_lnk_condv'.freeze
29
+ #
30
+ class Link
31
+ #
32
+ def initialize src, dsts, fields=nil, cond_fields=nil, cond_value=nil
33
+ @src = src # link source name
34
+ @dsts = dsts # , separated destinations to apply to the particle on linking success
35
+ @fields = fields # , separated fields to apply to the particle on linking success
36
+ @cond_fields = cond_fields # , separated fields used to generate the link value with particle payload
37
+ @cond_value = cond_value # value which will be compared to the particle link value to link or not
38
+ @door = nil # pointer to the source
39
+ end
40
+ #
41
+ def to_json *a
42
+ {
43
+ 'kls' => self.class.name,
44
+ 'src' => @src,
45
+ 'dsts' => @dsts,
46
+ 'fields' => @fields,
47
+ 'cond_fields' => @cond_fields,
48
+ 'cond_value' => @cond_value
49
+ }.to_json *a
50
+ end
51
+ #
52
+ def self.json_create o
53
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
54
+ self.new o['src'], o['dsts'], o['fields'], o['cond_fields'], o['cond_value']
55
+ end
56
+ #
57
+ def self.from_particle_data p
58
+ Edoors::Link.new(p.get_data(Edoors::LNK_SRC), p.get_data(Edoors::LNK_DSTS),
59
+ p.get_data(Edoors::LNK_FIELDS), p.get_data(Edoors::LNK_CONDF),
60
+ p.get_data(Edoors::LNK_CONDV))
61
+ end
62
+ #
63
+ attr_accessor :door
64
+ attr_reader :src, :dsts, :fields, :cond_fields, :cond_value
65
+ #
66
+ end
67
+ #
68
+ end
69
+ #
70
+ # EOF
@@ -0,0 +1,235 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+ #
4
+ # Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
5
+ #
6
+ # This file is part of edoors-ruby.
7
+ #
8
+ # edoors-ruby is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # edoors-ruby is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ require 'time'
22
+ #
23
+ module Edoors
24
+ #
25
+ class Particle
26
+ #
27
+ def initialize o={}
28
+ @ts = Time.now # creation time
29
+ @src = nil # Iota where it's originated from
30
+ @dst = nil # Iota where it's heading to
31
+ @room = nil # Room path part of the current destination
32
+ @door = nil # Door path part of the current destination
33
+ @action = nil # action part of the current destination
34
+ @link_value = nil # the value computed with the link_fields values extracted from the payload
35
+ # used for pearing Particles in Boards and linking in routing process
36
+ @dsts = [] # fifo of path?action strings where to travel to
37
+ @link_fields = [] # the fields used to generate the link value
38
+ @payload = {} # the actual data carried by this particle
39
+ @merged = [] # list of merged particles
40
+ #
41
+ if not o.empty?
42
+ @ts = Time.parse(o['ts']) if o['ts']
43
+ @room = o['room']
44
+ @door = o['door']
45
+ @action = o['action']
46
+ @payload = o['payload']||{}
47
+ @src = o['spin'].search_down o['src'] if o['src']
48
+ @dst = o['spin'].search_down o['dst'] if o['dst']
49
+ o['dsts'].each do |dst| add_dsts dst end if o['dsts']
50
+ set_link_fields *o['link_fields'] if o['link_fields']
51
+ o['merged'].each do |particle|
52
+ merge! Particle.json_create(particle.merge!('spin'=>o['spin']))
53
+ end if o['merged']
54
+ end
55
+ end
56
+ #
57
+ def to_json *a
58
+ {
59
+ 'kls' => self.class.name,
60
+ 'ts' => @ts,
61
+ 'src' => (@src ? @src.path : nil ),
62
+ 'dst' => (@dst ? @dst.path : nil ),
63
+ 'room' => @room,
64
+ 'door' => @door,
65
+ 'action' => @action,
66
+ 'dsts' => @dsts,
67
+ 'link_fields' => @link_fields,
68
+ 'payload' => @payload,
69
+ 'merged' => @merged
70
+ }.to_json *a
71
+ end
72
+ #
73
+ def self.json_create o
74
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
75
+ self.new o
76
+ end
77
+ #
78
+ # called when released
79
+ def reset!
80
+ clear_merged! ( @src ? @src : ( @dst ? @dst : nil ) )
81
+ @ts = @src = @dst = @room = @door = @action = @link_value = nil
82
+ @dsts.clear
83
+ @link_fields.clear
84
+ @payload.clear
85
+ end
86
+ #
87
+ # called when sent
88
+ def init! src
89
+ @src = src
90
+ @ts = Time.now
91
+ @dst = @room = @door = @action = nil
92
+ end
93
+ #
94
+ attr_reader :ts, :src, :dst, :room, :door, :action, :link_value, :payload
95
+ #
96
+ # routing
97
+ #
98
+ def next_dst
99
+ @dsts[0]
100
+ end
101
+ #
102
+ def clear_dsts!
103
+ @dsts.clear
104
+ end
105
+ #
106
+ def add_dsts dsts
107
+ dsts.split(Edoors::LINK_SEP).each do |dst|
108
+ if dst.empty? or dst[0]==Edoors::PATH_SEP or dst[0]==Edoors::PATH_SEP or dst=~/\/\?/\
109
+ or dst=~/\/{2,}/ or dst=~/\s+/ or dst==Edoors::ACT_SEP
110
+ raise Edoors::Exception.new "destination #{dst} is not acceptable"
111
+ end
112
+ @dsts << dst
113
+ end
114
+ end
115
+ #
116
+ def add_dst a, d=''
117
+ add_dsts d+Edoors::ACT_SEP+a
118
+ end
119
+ #
120
+ def set_dst! a, d
121
+ @action = a
122
+ if d.is_a? Edoors::Iota
123
+ @dst = d
124
+ else
125
+ _split_path! d
126
+ end
127
+ end
128
+ #
129
+ def split_dst!
130
+ @dst = @room = @door = @action = nil
131
+ return if (n = next_dst).nil?
132
+ p, @action = n.split Edoors::ACT_SEP
133
+ _split_path! p
134
+ end
135
+ #
136
+ def _split_path! p
137
+ i = p.rindex Edoors::PATH_SEP
138
+ if i.nil?
139
+ @room = nil
140
+ @door = p
141
+ else
142
+ @room = p[0..i-1]
143
+ @door = p[i+1..-1]
144
+ end
145
+ @door = nil if @door.empty?
146
+ end
147
+ private :_split_path!
148
+ #
149
+ def dst_routed! dst
150
+ @dst = dst
151
+ @dsts.shift
152
+ end
153
+ #
154
+ def error! e, dst=nil
155
+ @action = Edoors::ACT_ERROR
156
+ @dst = dst||@src
157
+ @payload[Edoors::FIELD_ERROR_MSG]=e
158
+ end
159
+ #
160
+ def apply_link! lnk
161
+ init! lnk.door
162
+ clear_dsts!
163
+ add_dsts lnk.dsts
164
+ set_link_fields lnk.fields
165
+ end
166
+ #
167
+ # data manipulation
168
+ #
169
+ def []= k, v
170
+ @payload[k]=v
171
+ compute_link_value! if @link_fields.include? k
172
+ end
173
+ #
174
+ def set_data k, v
175
+ @payload[k] = v
176
+ compute_link_value! if @link_fields.include? k
177
+ end
178
+ #
179
+ def [] k
180
+ @payload[k]
181
+ end
182
+ #
183
+ def get_data k
184
+ @payload[k]
185
+ end
186
+ alias :data :get_data
187
+ #
188
+ def clone_data p
189
+ @payload = p.payload.clone
190
+ end
191
+ #
192
+ # link value and fields
193
+ #
194
+ def set_link_fields *args
195
+ @link_fields.clear if not @link_fields.empty?
196
+ args.compact!
197
+ args.each do |lfs|
198
+ lfs.split(',').each do |lf|
199
+ @link_fields << lf
200
+ end
201
+ end
202
+ compute_link_value!
203
+ end
204
+ #
205
+ def compute_link_value!
206
+ @link_value = @link_fields.inject('') { |s,lf| s+=@payload[lf].to_s if @payload[lf]; s }
207
+ end
208
+ #
209
+ # merge particles management
210
+ #
211
+ def merge! p
212
+ @merged << p
213
+ end
214
+ #
215
+ def merged i
216
+ @merged[i]
217
+ end
218
+ #
219
+ def merged_shift
220
+ @merged.shift
221
+ end
222
+ #
223
+ def clear_merged! r=nil
224
+ @merged.each do |p|
225
+ p.clear_merged! r
226
+ r.release_p p if r
227
+ end
228
+ @merged.clear
229
+ end
230
+ #
231
+ end
232
+ #
233
+ end
234
+ #
235
+ # EOF
@@ -0,0 +1,190 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+ #
4
+ # Copyright 2012 Jérémy Zurcher <jeremy@asynk.ch>
5
+ #
6
+ # This file is part of edoors-ruby.
7
+ #
8
+ # edoors-ruby is free software: you can redistribute it and/or modify
9
+ # it under the terms of the GNU Affero General Public License as published by
10
+ # the Free Software Foundation, either version 3 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # edoors-ruby is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU Affero General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU Affero General Public License
19
+ # along with edoors-ruby. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ #
22
+ module Edoors
23
+ #
24
+ ERROR_ROUTE_NS = 'routing error: no source'.freeze
25
+ ERROR_ROUTE_RRWD = 'routing error: right room, wrong door'.freeze
26
+ ERROR_ROUTE_DNE = 'routing error: does not exists'.freeze
27
+ ERROR_ROUTE_NDNL = 'routing error: no destination, no link'.freeze
28
+ ERROR_ROUTE_SND = 'routing error: system no destination'.freeze
29
+ #
30
+ class Room < Iota
31
+ #
32
+ def initialize n, p
33
+ super n, p
34
+ @iotas = {}
35
+ @links = {}
36
+ end
37
+ #
38
+ def to_json *a
39
+ {
40
+ 'kls' => self.class.name,
41
+ 'name' => @name,
42
+ 'iotas' => @iotas,
43
+ 'links' => @links
44
+ }.to_json *a
45
+ end
46
+ #
47
+ def self.json_create o
48
+ raise Edoors::Exception.new "JSON #{o['kls']} != #{self.name}" if o['kls'] != self.name
49
+ room = self.new o['name'], o['parent']
50
+ o['iotas'].each do |name,iota|
51
+ eval( iota['kls'] ).json_create(iota.merge!('parent'=>room))
52
+ end
53
+ o['links'].each do |src,links|
54
+ links.each do |link|
55
+ room.add_link Edoors::Link.json_create(link)
56
+ end
57
+ end
58
+ room
59
+ end
60
+ #
61
+ def add_iota s
62
+ raise Edoors::Exception.new "Iota #{s.name} already has #{s.parent.name} as parent" if not s.parent.nil? and s.parent!=self
63
+ raise Edoors::Exception.new "Iota #{s.name} already exists in #{path}" if @iotas.has_key? s.name
64
+ s.parent = self if s.parent.nil?
65
+ @iotas[s.name]=s
66
+ end
67
+ #
68
+ def add_link l
69
+ l.door = @iotas[l.src]
70
+ raise Edoors::Exception.new "Link source #{l.src} does not exist in #{path}" if l.door.nil?
71
+ (@links[l.src] ||= [])<< l
72
+ end
73
+ #
74
+ def start!
75
+ puts " * start #{path}" if @spin.debug_routing
76
+ @iotas.values.each do |iota| iota.start! end
77
+ end
78
+ #
79
+ def stop!
80
+ puts " * stop #{path}" if @spin.debug_routing
81
+ @iotas.values.each do |iota| iota.stop! end
82
+ end
83
+ #
84
+ def search_down spath
85
+ return self if spath==path
86
+ return nil if (spath=~/^#{path}\/(\w+)\/?/)!=0
87
+ if iota = @iotas[$1]
88
+ return iota if iota.path==spath # needed as Door doesn't implement #search_down
89
+ return iota.search_down spath
90
+ end
91
+ nil
92
+ end
93
+ #
94
+ def _try_links p
95
+ puts " -> try_links ..." if @spin.debug_routing
96
+ links = @links[p.src.name]
97
+ return false if links.nil?
98
+ pending_link = nil
99
+ apply_link = false
100
+ links.each do |link|
101
+ apply_link = link.cond_fields.nil? # unconditional link
102
+ p.set_link_fields link.cond_fields if not apply_link
103
+ if apply_link or (p.link_value==link.cond_value)
104
+ # link matches !
105
+ if pending_link
106
+ p2 = @spin.require_p p.class
107
+ p2.clone_data p
108
+ p2.apply_link! link
109
+ send_p p2
110
+ end
111
+ pending_link = link
112
+ end
113
+ end
114
+ if pending_link
115
+ p.apply_link! pending_link
116
+ _send false, p
117
+ end
118
+ pending_link
119
+ end
120
+ private :_try_links
121
+ #
122
+ def _route p
123
+ if p.room.nil? or p.room==path
124
+ if door = @iotas[p.door]
125
+ p.dst_routed! door
126
+ else
127
+ p.error! Edoors::ERROR_ROUTE_RRWD
128
+ end
129
+ elsif door = @spin.search_world(p.room+Edoors::PATH_SEP+p.door)
130
+ p.dst_routed! door
131
+ else
132
+ p.error! Edoors::ERROR_ROUTE_DNE
133
+ end
134
+ end
135
+ private :_route
136
+ #
137
+ def _send sys, p
138
+ if not sys and p.src.nil?
139
+ # do not route non system orphan particles !!
140
+ p.error! Edoors::ERROR_ROUTE_NS, @spin
141
+ elsif p.dst
142
+ # direct routing through pointer
143
+ return
144
+ elsif p.door
145
+ # direct routing through path
146
+ _route p
147
+ elsif p.next_dst
148
+ p.split_dst!
149
+ if p.door
150
+ _route p
151
+ elsif not sys
152
+ # boomerang
153
+ p.dst_routed! p.src
154
+ elsif p.action
155
+ p.dst_routed! @spin
156
+ end
157
+ elsif not sys and _try_links p
158
+ return
159
+ else
160
+ p.error!( sys ? Edoors::ERROR_ROUTE_SND : Edoors::ERROR_ROUTE_NDNL)
161
+ end
162
+ end
163
+ private :_send
164
+ #
165
+ def send_p p
166
+ puts " * send_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing
167
+ _send false, p
168
+ puts " -> #{p.dst.path}#{Edoors::ACT_SEP}#{p.action}" if @spin.debug_routing
169
+ @spin.post_p p
170
+ end
171
+ #
172
+ def send_sys_p p
173
+ puts " * send_sys_p #{(p.next_dst.nil? ? 'no dst' : p.next_dst)} ..." if @spin.debug_routing
174
+ _send true, p
175
+ puts " -> #{p.dst.path}#{Edoors::ACT_SEP}#{p.action}" if @spin.debug_routing
176
+ @spin.post_sys_p p
177
+ end
178
+ #
179
+ def process_sys_p p
180
+ if p.action==Edoors::SYS_ACT_ADD_LINK
181
+ add_link Edoors::Link.from_particle_data p
182
+ end
183
+ @spin.release_p p
184
+ end
185
+ #
186
+ end
187
+ #
188
+ end
189
+ #
190
+ # EOF