Class: PDTP::Server::Trust

Inherits:
Object
  • Object
show all
Defined in:
lib/pdtp/server/trust.rb

Overview

maintains trust information for a single client

Defined Under Namespace

Classes: Edge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(incoming = {}, outgoing = {}, implicit = {}) ⇒ Trust

Returns a new instance of Trust.



40
41
42
43
44
# File 'lib/pdtp/server/trust.rb', line 40

def initialize(incoming = {}, outgoing = {}, implicit = {})
  @incoming = incoming
  @outgoing = outgoing
  @implicit = implicit
end

Instance Attribute Details

#implicitObject (readonly)

Returns the value of attribute implicit.



38
39
40
# File 'lib/pdtp/server/trust.rb', line 38

def implicit
  @implicit
end

#outgoingObject (readonly)

Returns the value of attribute outgoing.



38
39
40
# File 'lib/pdtp/server/trust.rb', line 38

def outgoing
  @outgoing
end

Instance Method Details

#failure(node) ⇒ Object

I have failed to download a chunk from ‘node’



58
59
60
61
62
# File 'lib/pdtp/server/trust.rb', line 58

def failure(node)
  @outgoing[node] = Edge.new if @outgoing[node].nil?
  @outgoing[node].transfers += 1.0
  normalize
end

#normalizeObject

brings all trust values between 0 and 1



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
# File 'lib/pdtp/server/trust.rb', line 72

def normalize
  total_success = 0
  total_transfers = 0

  @outgoing.each do |_, link|
    total_success += link.success
    total_transfers += link.transfers
  end

  @outgoing.each { |_, link| link.trust = link.success / total_transfers }
  @outgoing.each do |target, link|
    [target.outgoing, target.implicit].each do |links|
      links.each do |nextlinktarget, nextlink|
        next unless outgoing[nextlinktarget].nil?
        next unless implicit[nextlinktarget].nil? or implicit[nextlinktarget].trust < link.trust * nextlink.trust
        
        implicit[nextlinktarget] = Edge.new(
          link.trust * nextlink.trust, 
          nextlink.success, 
          nextlink.transfers
        )
      end
    end
  end
end

#success(node) ⇒ Object

I have successfully downloaded a chunk from ‘node’



47
48
49
50
51
52
53
54
55
# File 'lib/pdtp/server/trust.rb', line 47

def success(node)
  if @outgoing[node].nil?
    @outgoing[node] = Edge.new
  else
    @outgoing[node].success += 1.0
    @outgoing[node].transfers += 1.0
  end
  normalize
end

#weight(node) ⇒ Object

returns a number from 0 to 1 saying how much I trust ‘node’



65
66
67
68
69
# File 'lib/pdtp/server/trust.rb', line 65

def weight(node)
  return @outgoing[node].trust unless @outgoing[node].nil?
  return @implicit[node].trust unless @implicit[node].nil?
  0
end