Class: Plog::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/plog/client.rb

Constant Summary collapse

PROTOCOL_VERSION =

The protocol version spoken by this client.

Packets::PROTOCOL_VERSION
RECV_SIZE =
65_536
DEFAULT_OPTIONS =
{
  :host => '127.0.0.1',
  :port => 23456,
  # Use the socket's default value unless this option is specified.
  :send_buffer_size => nil,
  :chunk_size => 64000,

  :large_message_threshold => nil,
  :on_large_message => nil,
  :logger => Logger.new(nil)
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/plog/client.rb', line 38

def initialize(options={})
  options = DEFAULT_OPTIONS.merge(options)
  @host = options[:host]
  @port = options[:port]
  @send_buffer_size = options[:send_buffer_size]
  @chunk_size = options[:chunk_size]
  @large_message_threshold = options[:large_message_threshold]
  @on_large_message = options[:on_large_message]
  @logger = options[:logger]

  @message_id_mutex = Mutex.new
  reset_message_id
end

Instance Attribute Details

#chunk_sizeObject (readonly)

Returns the value of attribute chunk_size.



30
31
32
# File 'lib/plog/client.rb', line 30

def chunk_size
  @chunk_size
end

#hostObject (readonly)

Returns the value of attribute host.



27
28
29
# File 'lib/plog/client.rb', line 27

def host
  @host
end

#large_message_thresholdObject (readonly)

Returns the value of attribute large_message_threshold.



32
33
34
# File 'lib/plog/client.rb', line 32

def large_message_threshold
  @large_message_threshold
end

#last_message_idObject (readonly)

Returns the value of attribute last_message_id.



36
37
38
# File 'lib/plog/client.rb', line 36

def last_message_id
  @last_message_id
end

#loggerObject (readonly)

Returns the value of attribute logger.



34
35
36
# File 'lib/plog/client.rb', line 34

def logger
  @logger
end

#on_large_messageObject (readonly)

Returns the value of attribute on_large_message.



33
34
35
# File 'lib/plog/client.rb', line 33

def on_large_message
  @on_large_message
end

#portObject (readonly)

Returns the value of attribute port.



28
29
30
# File 'lib/plog/client.rb', line 28

def port
  @port
end

#send_buffer_sizeObject (readonly)

Returns the value of attribute send_buffer_size.



29
30
31
# File 'lib/plog/client.rb', line 29

def send_buffer_size
  @send_buffer_size
end

Instance Method Details

#resetObject



89
90
91
92
# File 'lib/plog/client.rb', line 89

def reset
  reset_message_id
  close_socket
end

#send(message, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/plog/client.rb', line 57

def send(message, options = {})
  # Interpret the encoding of the string as binary so that chunking occurs
  # at the byte-level and not at the character-level.
  message = message.dup.force_encoding('BINARY')
  notify_large_message(message) if large_message?(message)

  message_id = next_message_id
  message_length = message.length
  message_checksum = Checksum.compute(message)
  chunks = chunk_string(message, chunk_size)

  logger.debug { "Plog: sending (#{message_id}; #{chunks.length} chunk(s))" }
  chunks.each_with_index do |data, index|
    send_to_socket(
      Packets::MultipartMessage.encode(
        message_id,
        message_length,
        message_checksum,
        chunk_size,
        chunks.count,
        index,
        data,
        options
      ))
  end

  message_id
rescue => e
  logger.error { "Plog: error sending message: #{e}" }
  raise e
end

#socketObject



94
95
96
# File 'lib/plog/client.rb', line 94

def socket
  @socket ||= open_socket
end

#stats(timeout = 3.0) ⇒ Object



52
53
54
55
# File 'lib/plog/client.rb', line 52

def stats(timeout = 3.0)
  send_to_socket("\0\0stats")
  JSON.parse receive_packet_from_socket(timeout)
end