Class: LogCourier::PendingPayload

Inherits:
Object
  • Object
show all
Defined in:
lib/log-courier/client.rb

Overview

Describes a pending payload

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events, nonce) ⇒ PendingPayload

Returns a new instance of PendingPayload.



49
50
51
52
53
54
# File 'lib/log-courier/client.rb', line 49

def initialize(events, nonce)
  @events = events
  @nonce = nonce

  generate
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



44
45
46
# File 'lib/log-courier/client.rb', line 44

def events
  @events
end

#last_sequenceObject

Returns the value of attribute last_sequence.



45
46
47
# File 'lib/log-courier/client.rb', line 45

def last_sequence
  @last_sequence
end

#nextObject

Returns the value of attribute next.



42
43
44
# File 'lib/log-courier/client.rb', line 42

def next
  @next
end

#nonceObject

Returns the value of attribute nonce.



43
44
45
# File 'lib/log-courier/client.rb', line 43

def nonce
  @nonce
end

#payloadObject

Returns the value of attribute payload.



47
48
49
# File 'lib/log-courier/client.rb', line 47

def payload
  @payload
end

#sequence_lenObject

Returns the value of attribute sequence_len.



46
47
48
# File 'lib/log-courier/client.rb', line 46

def sequence_len
  @sequence_len
end

Class Method Details

.get_json_adapterObject



36
37
38
39
# File 'lib/log-courier/client.rb', line 36

def get_json_adapter
  @json_adapter = MultiJson.adapter.instance if @json_adapter.nil?
  return @json_adapter
end

Instance Method Details

#ack(sequence) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/log-courier/client.rb', line 74

def ack(sequence)
  if sequence <= @last_sequence
    return 0, false
  elsif sequence >= @sequence_len
    lines = @sequence_len - @last_sequence
    @last_sequence = sequence
    @payload = nil
    @events = []
    return lines, true
  end

  lines = sequence - @last_sequence
  @last_sequence = sequence
  @payload = nil
  @events.shift(lines)
  return lines, false
end

#generateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/log-courier/client.rb', line 56

def generate
  fail ArgumentError, 'Corrupt payload' if @events.length == 0

  buffer = Zlib::Deflate.new

  # Write each event in JSON format
  events.each do |event|
    json_data = self.class.get_json_adapter.dump(event)
    # Add length and then the data
    buffer << [json_data.length].pack('N') << json_data
  end

  # Generate and store the payload
  @payload = nonce + buffer.flush(Zlib::FINISH)
  @last_sequence = 0
  @sequence_len = @events.length
end