Class: Node

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/routing/routing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Node

Returns a new instance of Node.



21
22
23
24
25
# File 'lib/appswarm/routing/routing.rb', line 21

def initialize(id)
  @id=id
  @neighbors=[]
  @distance={}
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



20
21
22
# File 'lib/appswarm/routing/routing.rb', line 20

def id
  @id
end

#neighborsObject (readonly)

Returns the value of attribute neighbors.



20
21
22
# File 'lib/appswarm/routing/routing.rb', line 20

def neighbors
  @neighbors
end

Instance Method Details

#==(o) ⇒ Object



59
60
61
# File 'lib/appswarm/routing/routing.rb', line 59

def ==(o)
  @id==o.id
end

#add(n) ⇒ Object



27
28
29
30
# File 'lib/appswarm/routing/routing.rb', line 27

def add(n)
  @neighbors << n
  @distance[n]=1 
end

#distance(n) ⇒ Object



49
50
51
52
53
# File 'lib/appswarm/routing/routing.rb', line 49

def distance(n)
  return 0 if n==self
  return @distance[n] if @distance.key?(n)
  MYMAX
end

#neighborHood(len = 3) ⇒ Object



55
56
57
# File 'lib/appswarm/routing/routing.rb', line 55

def neighborHood(len=3)
  @distance.keys.select{|k|@distance[k]<=len}-[self]
end

#setDistance(n, d) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/appswarm/routing/routing.rb', line 41

def setDistance(n,d)
  if @distance[n].nil? or @distance[n]>d
    @distance[n]=d
    true
  else
    false
  end
end

#short_to_sObject



32
33
34
# File 'lib/appswarm/routing/routing.rb', line 32

def short_to_s
  "("+@id.to_s+")"
end

#to_sObject



36
37
38
39
# File 'lib/appswarm/routing/routing.rb', line 36

def to_s
#    "["[email protected]_s+":"[email protected]{|n|n.short_to_s+"="+@distance[n].to_s}.join(",")+"]"
  "["+@id.to_s+"]" #:"[email protected]{|n|n.short_to_s+"="+@distance[n].to_s}.join(",")+"]"
end