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
|
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
require 'edoors'
HBN_PATH='hibernate.json'
#
class InputDoor < Edoors::Door
#
@count = 0
#
class << self
attr_accessor :count
end
#
def initialize n, p
super n, p
@lines = [ "#{name} says : hello", "world ( from #{path} )" ]
@idx = 0
end
#
def start!
puts " -> start #{self.class.name} (#{@path})"
# stimulate myself
p = require_p Edoors::Particle
# p.add_dst Edoors::ACT_GET, path
send_p p, Edoors::ACT_GET
end
#
def stop!
puts " >- stop #{self.class.name} (#{@path})"
end
#
def hibernate!
puts " !! hibernate #{self.class.name} (#{@path})"
# we want to remember where we are in the data flow
{'idx'=>@idx}
end
#
def resume! o
puts " !! resume #{self.class.name} (#{@path})"
# restore idx
@idx = o['idx']
end
#
def receive_p p
puts " @ #{self.class.name} (#{@path}) receive_p : #{p.action}"
if p.action==Edoors::ACT_GET
p.reset!
p.set_data 'line', @lines[@idx]
p.set_data 'f0', 'v0'
p.set_data 'f1', 'v1'
p.set_data 'f2', 'v2'
send_p p # will follow the link
@idx+=1
if @idx<@lines.length
# there is more to read, restimulate myself
p = require_p Edoors::Particle
p.add_dst Edoors::ACT_GET, name
send_p p
end
else
# we can release it or let the Door do it
release_p p
end
# I want to hibernate now!
self.class.count+=1
if self.class.count==3
p = require_p Edoors::Particle
p[Edoors::FIELD_HIBERNATE_PATH] = HBN_PATH
p.add_dst Edoors::SYS_ACT_HIBERNATE
send_sys_p p
end
end
#
end
#
class ConcatBoard < Edoors::Board
#
def initialize n, p, m=false
super n, p
@manual = m
end
#
def start!
puts " -> start #{self.class.name} (#{@path})"
end
#
def stop!
puts " >- stop #{self.class.name} (#{@path})"
end
#
def receive_p p
puts " @ #{self.class.name} receive_p : #{p.action}"
if p.action==Edoors::ACT_ERROR
#
else
if @manual
# cleanup unnecessary p2 Particle
p2 = p.merged_shift
p.set_data 'line', (p.data('line')+' '+p2.data('line'))
release_p p2
else
# Or let the system do it
p.set_data 'line', (p.data('line')+' '+p.merged(0).data('line'))
end
send_p p
end
end
#
end
#
class OutputDoor < Edoors::Door
#
def initialize n, p, c=false
super n, p
@clean = c
end
#
def start!
puts " -> start #{self.class.name} (#{@path})"
end
#
def stop!
puts " >- stop #{self.class.name} (#{@path})"
end
#
def receive_p p
puts " #==> #{self.class.name} (#{@path}) receive_p : #{p.get_data('line')}"
if @clean
release_p p
else
# we do nothing Edoors::Door#process_p will detect it and release it
end
end
#
end
#
spin = Edoors::Spin.new 'dom0', :debug_routing=>false, :debug_garbage=>true
#
room0 = Edoors::Room.new 'room0', spin
room1 = Edoors::Room.new 'room1', spin
#
input0 = InputDoor.new 'input0', room0
output0 = OutputDoor.new 'output0', room0
#
input1 = InputDoor.new 'input1', room1
output1 = OutputDoor.new 'output1', room1, true
concat1 = ConcatBoard.new 'concat1', room1
#
room0.add_link Edoors::Link.new('input0', 'output0', nil, nil)
#
p0 = spin.require_p Edoors::Particle
p0.set_data Edoors::LNK_SRC, 'input1'
p0.set_data Edoors::LNK_DSTS, ['concat1?follow','output1']
p0.set_data Edoors::LNK_KEYS, ['f0','f2']
p0.set_data Edoors::LNK_VALUE, {'f0'=>'v0','f1'=>'v1','f2'=>'v2'}
p0.add_dst Edoors::SYS_ACT_ADD_LINK, room1.path
room1.send_sys_p p0 # send_sys_p -> room0 -> spin -> room1 -> input1
#
spin.spin!
#
dom0 = Edoors::Spin.resume! HBN_PATH
dom0.spin!
File.unlink HBN_PATH if File.exists? HBN_PATH
#
# EOF
|