Module: LogjamAgent::Util

Included in:
ZMQForwarder
Defined in:
lib/logjam_agent/util.rb

Constant Summary collapse

BIG_ENDIAN =

copied from amqp protocol gem (slightly modified)

([1].pack("s") == "\x00\x01")
UINT64 =
"Q"
META_INFO_VERSION =
1
META_INFO_TAG =
0xcabd
META_INFO_DEVICE_NUMBER =
0
FIXNUM_MAX =

we assume we’re running on MRI ruby

2 ** (1.size * 8 - 2) - 1

Instance Method Summary collapse

Instance Method Details

#augment_connection_spec(spec, default_port) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/logjam_agent/util.rb', line 61

def augment_connection_spec(spec, default_port)
  protocol, host, port = %r{\A(?:([^:]+)://)?([^:]+)(?::(\d+))?\z}.match(spec).captures
  protocol ||= "tcp"
  port ||= default_port
  if protocol == "inproc"
    # should only be used for integration tests
    "#{protocol}://#{host}"
  else
    "#{protocol}://#{host}:#{port}"
  end
end

#next_fixnum(i) ⇒ Object



41
42
43
# File 'lib/logjam_agent/util.rb', line 41

def next_fixnum(i)
  (i+=1) > FIXNUM_MAX ? 1 : i
end

#pack_info(n, compression_method = LogjamAgent.compression_method) ⇒ Object



45
46
47
48
49
# File 'lib/logjam_agent/util.rb', line 45

def pack_info(n, compression_method = LogjamAgent.compression_method)
  info = [META_INFO_TAG, compression_method, META_INFO_VERSION, META_INFO_DEVICE_NUMBER].pack("nCCN")
  info << pack_uint64_big_endian(zclock_time)
  info << pack_uint64_big_endian(n)
end

#pack_uint64_big_endian(uint64) ⇒ Object



17
18
19
# File 'lib/logjam_agent/util.rb', line 17

def pack_uint64_big_endian(uint64)
  [uint64].pack(UINT64)
end

#unpack_info(info) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/logjam_agent/util.rb', line 51

def unpack_info(info)
  tag, compression_method, version, device = info[0..7].unpack("nCCN")
  zclock = unpack_uint64_big_endian(info[8..15])
  secs = zclock / 1000
  msecs = zclock % 1000
  sent = Time.at(secs) + 1000.0/msecs
  sequence = unpack_uint64_big_endian(info[16..23])
  [tag, compression_method, version, device, sent, sequence]
end

#unpack_uint64_big_endian(string) ⇒ Object



21
22
23
# File 'lib/logjam_agent/util.rb', line 21

def unpack_uint64_big_endian(string)
  string.unpack(UINT64)
end

#zclock_time(t = Time.now) ⇒ Object



37
38
39
# File 'lib/logjam_agent/util.rb', line 37

def zclock_time(t = Time.now)
  t.tv_sec*1000 + t.tv_usec/1000
end