Class: Edoors::Room

Inherits:
Iota
  • Object
show all
Defined in:
lib/edoors/room.rb

Direct Known Subclasses

Spin

Instance Attribute Summary

Attributes inherited from Iota

#name, #parent, #path, #spin, #viewer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Iota

#hibernate!, #receive_p, #resume!

Constructor Details

#initialize(n, p) ⇒ Room

creates a Room object from the arguments.

Parameters:

  • n (String)

    the name of this Room

  • p (Iota)

    the parent



37
38
39
40
41
# File 'lib/edoors/room.rb', line 37

def initialize n, p
    super n, p
    @iotas = {}
    @links = {}
end

Class Method Details

.from_particle(p, s) ⇒ Object

creates a Room object from the data of a particle

Parameters:

  • p (Particle)

    the Particle to get Room attributes from



80
81
82
# File 'lib/edoors/room.rb', line 80

def self.from_particle p, s
    Edoors::Room.new(p.get_data(Edoors::IOTA_NAME), s)
end

.json_create(o) ⇒ Object

creates a Room object from a JSON data

Parameters:

  • o (Hash)

    belongs to JSON parser

Raises:

  • Edoors::Exception if the json kls attribute is wrong



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/edoors/room.rb', line 62

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

Instance Method Details

#add_iota(i) ⇒ Object

adds the given Iota to this Room

Parameters:

  • i (Iota)

    the Iota to add

Raises:

  • Edoors::Exception if i already has a parent or if a Iota with the same name already exists



90
91
92
93
94
95
# File 'lib/edoors/room.rb', line 90

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

Parameters:

  • l (Link)

    the Link to add

Raises:

  • Edoors::Exception if the link source can't be found in this Room



103
104
105
106
107
# File 'lib/edoors/room.rb', line 103

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

#process_sys_p(p) ⇒ Object

Note:

the Particle is automatically released

process the given system Particle

Parameters:

  • p (Particle)

    the Particle to be processed



253
254
255
256
257
258
259
260
# File 'lib/edoors/room.rb', line 253

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

#search_down(spath) ⇒ Iota

search through all children for a matching Iota

Parameters:

  • spath (String)

    the full path of the earch Iota

Returns:

  • (Iota)

    if found

See Also:



135
136
137
138
139
140
141
142
143
# File 'lib/edoors/room.rb', line 135

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

#send_p(p) ⇒ Object

send the given Particle to application Particle fifo

Parameters:



229
230
231
232
233
234
# File 'lib/edoors/room.rb', line 229

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_sys_p(p) ⇒ Object

send the given Particle to system Particle fifo

Parameters:



240
241
242
243
244
245
# File 'lib/edoors/room.rb', line 240

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

#start!Object

forward the call to each children



113
114
115
116
# File 'lib/edoors/room.rb', line 113

def start!
    puts " * start #{path}" if @spin.debug_routing
    @iotas.values.each do |iota| iota.start! end
end

#stop!Object

forward the call to each children



122
123
124
125
# File 'lib/edoors/room.rb', line 122

def stop!
    puts " * stop #{path}" if @spin.debug_routing
    @iotas.values.each do |iota| iota.stop! end
end

#to_json(*a) ⇒ Object

called by JSON#generate to serialize the Room object into JSON data

Parameters:

  • a (Array)

    belongs to JSON generator



47
48
49
50
51
52
53
54
# File 'lib/edoors/room.rb', line 47

def to_json *a
    {
        'kls'   => self.class.name,
        'name'  => @name,
        'iotas' => @iotas,
        'links' => @links
    }.to_json *a
end