Sha256: 9b5218c9d64f9e18ac82d1bcd33b630f36fa6bb845178782a83e58d1ae0c7022

Contents?: true

Size: 1.91 KB

Versions: 27

Compression:

Stored size: 1.91 KB

Contents

module LogjamAgent
  module Util
    # copied from amqp protocol gem (slightly modified)
    BIG_ENDIAN = ([1].pack("s") == "\x00\x01")

    UINT64 = "Q"

    META_INFO_VERSION = 1
    META_INFO_TAG = 0xcabd
    META_INFO_DEVICE_NUMBER = 0

    # we assume we're running on MRI ruby
    FIXNUM_MAX = 2 ** (1.size * 8 - 2) - 1

    if BIG_ENDIAN

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

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

    else

      def pack_uint64_big_endian(uint64)
        [uint64].pack(UINT64).bytes.map(&:chr).reverse.join
      end

      def unpack_uint64_big_endian(string)
        string.bytes.map(&:chr).reverse.join.unpack(UINT64)[0]
      end

    end

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

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

    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

    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

    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
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
logjam_agent-0.39.1 lib/logjam_agent/util.rb
logjam_agent-0.39.0 lib/logjam_agent/util.rb
logjam_agent-0.38.5 lib/logjam_agent/util.rb
logjam_agent-0.38.4 lib/logjam_agent/util.rb
logjam_agent-0.38.3 lib/logjam_agent/util.rb
logjam_agent-0.38.2 lib/logjam_agent/util.rb
logjam_agent-0.38.1 lib/logjam_agent/util.rb
logjam_agent-0.38.0 lib/logjam_agent/util.rb
logjam_agent-0.37.1 lib/logjam_agent/util.rb
logjam_agent-0.37.0 lib/logjam_agent/util.rb
logjam_agent-0.36.0 lib/logjam_agent/util.rb
logjam_agent-0.35.1 lib/logjam_agent/util.rb
logjam_agent-0.35.0 lib/logjam_agent/util.rb
logjam_agent-0.34.3 lib/logjam_agent/util.rb
logjam_agent-0.34.2 lib/logjam_agent/util.rb
logjam_agent-0.34.1 lib/logjam_agent/util.rb
logjam_agent-0.34.0 lib/logjam_agent/util.rb
logjam_agent-0.33.3 lib/logjam_agent/util.rb
logjam_agent-0.33.2 lib/logjam_agent/util.rb
logjam_agent-0.33.1 lib/logjam_agent/util.rb