Class: Biryani::StreamsContext

Inherits:
Object
  • Object
show all
Defined in:
lib/biryani/streams_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc) ⇒ StreamsContext

Returns a new instance of StreamsContext.



5
6
7
8
9
# File 'lib/biryani/streams_context.rb', line 5

def initialize(proc)
  @h = {} # Hash<Integer, StreamContext>
  @proc = proc
  @tx = Ractor::Port.new
end

Instance Attribute Details

#txObject

Returns the value of attribute tx.



3
4
5
# File 'lib/biryani/streams_context.rb', line 3

def tx
  @tx
end

Instance Method Details

#[](stream_id) ⇒ StreamContext?

Parameters:

  • stream_id (Integer)

Returns:



30
31
32
# File 'lib/biryani/streams_context.rb', line 30

def [](stream_id)
  @h[stream_id]
end

#close_allObject



98
99
100
# File 'lib/biryani/streams_context.rb', line 98

def close_all
  each(&:close)
end

#closed_stream_idsArray<Integer>

Returns:

  • (Array<Integer>)


59
60
61
# File 'lib/biryani/streams_context.rb', line 59

def closed_stream_ids
  @h.filter { |_, ctx| ctx.closed? }.keys
end

#count_activeInteger

Returns:

  • (Integer)


49
50
51
# File 'lib/biryani/streams_context.rb', line 49

def count_active
  @h.values.filter(&:active?).length
end

#delete(stream_id) ⇒ Object

Parameters:

  • stream_id (Integer)


23
24
25
# File 'lib/biryani/streams_context.rb', line 23

def delete(stream_id)
  @h.delete(stream_id)
end

#each(&block) ⇒ Object



44
45
46
# File 'lib/biryani/streams_context.rb', line 44

def each(&block)
  @h.each_value(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/biryani/streams_context.rb', line 40

def empty?
  @h.empty?
end

#last_stream_idInteger

Returns:

  • (Integer)


64
65
66
# File 'lib/biryani/streams_context.rb', line 64

def last_stream_id
  @h.reject { |_, ctx| ctx.idle? }.keys.max || 0
end

#lengthInteger

Returns:

  • (Integer)


35
36
37
# File 'lib/biryani/streams_context.rb', line 35

def length
  @h.length
end

#new_context(stream_id, send_initial_window_size, recv_initial_window_size) ⇒ StreamContext

Parameters:

  • stream_id (Integer)
  • send_initial_window_size (Integer)
  • recv_initial_window_size (Integer)

Returns:



16
17
18
19
20
# File 'lib/biryani/streams_context.rb', line 16

def new_context(stream_id, send_initial_window_size, recv_initial_window_size)
  ctx = StreamContext.new(stream_id, send_initial_window_size, recv_initial_window_size, @proc, @tx)
  @h[stream_id] = ctx
  ctx
end

#receiving_continuation_stream_idInteger?

Returns:

  • (Integer, nil)


54
55
56
# File 'lib/biryani/streams_context.rb', line 54

def receiving_continuation_stream_id
  @h.find { |_, ctx| ctx.receiving_continuation? }&.first
end

#remove_closed(data_buffer) ⇒ Object

Parameters:



91
92
93
94
95
96
# File 'lib/biryani/streams_context.rb', line 91

def remove_closed(data_buffer)
  closed_ids = closed_stream_ids.filter { |id| !data_buffer.has?(id) }
  closed_ids.each do |id|
    @h.delete(id)
  end
end

#sendable_datas(stream_id, data, send_window, max_frame_size) ⇒ Array<Object>, String

Parameters:

  • stream_id (Integer)
  • data (String)
  • send_window (Window)
  • max_frame_size (Integer)

Returns:

  • (Array<Object>)

    frames

  • (String)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/biryani/streams_context.rb', line 75

def sendable_datas(stream_id, data, send_window, max_frame_size)
  len = [data.bytesize, send_window.length, @h[stream_id].send_window.length].min

  payload = data[0...len]
  remains = data[len..] || ''

  len = (len + max_frame_size - 1) / max_frame_size
  frames = payload.gsub(/.{1,#{max_frame_size}}/m).with_index.map do |s, index|
    end_stream = remains.empty? && index == len - 1
    Frame::Data.new(end_stream, stream_id, s, nil)
  end

  [frames, remains]
end