Class: Protocol::HTTP2::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/http2/dependency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, id, dependent_id = 0, weight = DEFAULT_WEIGHT, children = nil) ⇒ Dependency



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/protocol/http2/dependency.rb', line 28

def initialize(connection, id, dependent_id = 0, weight = DEFAULT_WEIGHT, children = nil)
  @connection = connection
  @id = id
  
  # Stream priority:
  @dependent_id = dependent_id
  @weight = weight
  
  # A cache of dependencies that have child.dependent_id = self.id
  @children = children
end

Instance Attribute Details

#childrenObject

Cache of dependent children.



50
51
52
# File 'lib/protocol/http2/dependency.rb', line 50

def children
  @children
end

#connectionObject (readonly)

The connection this stream belongs to.



53
54
55
# File 'lib/protocol/http2/dependency.rb', line 53

def connection
  @connection
end

#dependent_idObject

The stream id that this stream depends on, according to the priority.



59
60
61
# File 'lib/protocol/http2/dependency.rb', line 59

def dependent_id
  @dependent_id
end

#idObject (readonly)

Stream ID (odd for client initiated streams, even otherwise).



56
57
58
# File 'lib/protocol/http2/dependency.rb', line 56

def id
  @id
end

#weightObject

The weight of the stream relative to other siblings.



62
63
64
# File 'lib/protocol/http2/dependency.rb', line 62

def weight
  @weight
end

Instance Method Details

#add_child(dependency) ⇒ Object



76
77
78
79
# File 'lib/protocol/http2/dependency.rb', line 76

def add_child(dependency)
  @children ||= {}
  @children[dependency.id] = dependency
end

#delete!Object



44
45
46
47
# File 'lib/protocol/http2/dependency.rb', line 44

def delete!
  @connection.dependencies.delete(@id)
  self.parent&.remove_child(self)
end

#exclusive_child(parent) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/protocol/http2/dependency.rb', line 85

def exclusive_child(parent)
  parent.children = @children
  
  @children.each_value do |child|
    child.dependent_id = parent.id
  end
  
  @children = {parent.id => parent}
  
  parent.dependent_id = @id
end

#irrelevant?Boolean



40
41
42
# File 'lib/protocol/http2/dependency.rb', line 40

def irrelevant?
  (@weight == DEFAULT_WEIGHT) && (@children.nil? || @children.empty?)
end

#parent(id = @dependent_id) ⇒ Object



97
98
99
# File 'lib/protocol/http2/dependency.rb', line 97

def parent(id = @dependent_id)
  @connection.dependency_for(id)
end

#parent=(dependency) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/protocol/http2/dependency.rb', line 101

def parent= dependency
  self.parent&.remove_child(self)
  
  @dependent_id = dependency.id
  
  dependency.add_child(self)
end

#priority(exclusive = false) ⇒ Object

The current local priority of the stream.



138
139
140
# File 'lib/protocol/http2/dependency.rb', line 138

def priority(exclusive = false)
  Priority.new(exclusive, @dependent_id, @weight)
end

#priority=(priority) ⇒ Object

Change the priority of the stream both locally and remotely.



132
133
134
135
# File 'lib/protocol/http2/dependency.rb', line 132

def priority= priority
  send_priority(priority)
  process_priority(priority)
end

#process_priority(priority) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/protocol/http2/dependency.rb', line 109

def process_priority priority
  dependent_id = priority.stream_dependency
  
  if dependent_id == @id
    raise ProtocolError, "Stream priority for stream id #{@id} cannot depend on itself!"
  end
  
  @weight = priority.weight
  
  if priority.exclusive
    self.parent&.remove_child(self)
    
    self.parent(dependent_id).exclusive_child(self)
  elsif dependent_id != @dependent_id
    self.parent&.remove_child(self)
    
    @dependent_id = dependent_id
    
    self.parent.add_child(self)
  end
end

#receive_priority(frame) ⇒ Object



146
147
148
# File 'lib/protocol/http2/dependency.rb', line 146

def receive_priority(frame)
  self.process_priority(frame.unpack)
end

#remove_child(dependency) ⇒ Object



81
82
83
# File 'lib/protocol/http2/dependency.rb', line 81

def remove_child(dependency)
  @children&.delete(dependency.id)
end

#send_priority(priority) ⇒ Object



142
143
144
# File 'lib/protocol/http2/dependency.rb', line 142

def send_priority(priority)
  @connection.send_priority(@id, priority)
end

#streamObject



64
65
66
# File 'lib/protocol/http2/dependency.rb', line 64

def stream
  @connection.streams[@id]
end

#streamsObject



68
69
70
71
72
73
74
# File 'lib/protocol/http2/dependency.rb', line 68

def streams
  if @children
    # TODO this O(N) operation affects performance.
    # It would be better to maintain a sorted list of children streams.
    @children.map{|id, dependency| dependency.stream}.compact
  end
end