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.



59
60
61
62
63
64
# File 'lib/log-courier/client.rb', line 59

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

  generate
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



54
55
56
# File 'lib/log-courier/client.rb', line 54

def events
  @events
end

#last_sequenceObject

Returns the value of attribute last_sequence.



55
56
57
# File 'lib/log-courier/client.rb', line 55

def last_sequence
  @last_sequence
end

#nextObject

Returns the value of attribute next.



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

def next
  @next
end

#nonceObject

Returns the value of attribute nonce.



53
54
55
# File 'lib/log-courier/client.rb', line 53

def nonce
  @nonce
end

#payloadObject

Returns the value of attribute payload.



57
58
59
# File 'lib/log-courier/client.rb', line 57

def payload
  @payload
end

#sequence_lenObject

Returns the value of attribute sequence_len.



56
57
58
# File 'lib/log-courier/client.rb', line 56

def sequence_len
  @sequence_len
end

Class Method Details

.get_json_adapterObject



39
40
41
42
# File 'lib/log-courier/client.rb', line 39

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

.get_json_parseerrorObject



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

def get_json_parseerror
  if @json_parseerror.nil?
    @json_parseerror = get_json_adapter.class::ParseError
  end
  @json_parseerror
end

Instance Method Details

#ack(sequence) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/log-courier/client.rb', line 84

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/log-courier/client.rb', line 66

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.finish()
  @last_sequence = 0
  @sequence_len = @events.length
end