Class: Mu::Pcap::StreamPacketizer

Inherits:
Object
  • Object
show all
Defined in:
lib/mu/pcap/stream_packetizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ StreamPacketizer

Returns a new instance of StreamPacketizer.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mu/pcap/stream_packetizer.rb', line 12

def initialize parser
    @parser = parser
    @key_to_idx = Hash.new do |hash,key|
        if hash.size >= 2
            raise ArgumentError, "Only two endpoints are allowed in a transaction"
        end
        hash[key] = hash.size
    end
    @sent_messages = [[], []].freeze
    @inner_pair = IOPair.stream_pair
    @io_pair = @inner_pair.map{|io| IOWrapper.new io, parser}.freeze
end

Instance Attribute Details

#io_pairObject (readonly)

Returns the value of attribute io_pair.



11
12
13
# File 'lib/mu/pcap/stream_packetizer.rb', line 11

def io_pair
  @io_pair
end

#parserObject (readonly)

Returns the value of attribute parser.



11
12
13
# File 'lib/mu/pcap/stream_packetizer.rb', line 11

def parser
  @parser
end

Instance Method Details

#extra_bytes(w_key) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/mu/pcap/stream_packetizer.rb', line 32

def extra_bytes w_key
    w_key = w_key.inspect

    ridx       = @key_to_idx[w_key] ^ 1
    reader = @io_pair[ridx]
    incomplete =  reader.unread
    incomplete.empty? ? nil : incomplete.dup
end

#msg_count(key) ⇒ Object



25
26
27
28
29
30
# File 'lib/mu/pcap/stream_packetizer.rb', line 25

def msg_count key
    key = key.inspect
    widx       = @key_to_idx[key]
    messages = @sent_messages[widx]
    messages.size
end

#next_msg(key) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/mu/pcap/stream_packetizer.rb', line 60

def next_msg key
    key = key.inspect
    idx = @key_to_idx[key] 
    if m = @sent_messages[idx].shift
        return m.dup
    else
        nil
    end
end

#push(key, bytes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mu/pcap/stream_packetizer.rb', line 41

def push key, bytes
    key = key.inspect
    widx       = @key_to_idx[key]
    writer     = @io_pair[widx]
    raw_writer = @inner_pair[widx]
    raw_writer.write bytes

    messages = @sent_messages[widx]

    ridx = widx ^ 1
    reader = @io_pair[ridx]
    while msg = reader.read
        messages << msg
        writer.record_write bytes
    end

    nil
end