Sha256: fadd742f59bbabcdffb236c37321309fdc1ca2c8f7679f1cefe636473e17aa1a

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

class TCPSocket
  include Fastdfs::Client

  attr_accessor :host, :port, :cmd, :recv_offset, :connect_state

  def initialize(host, port)
    @host = host
    @port = port
    @recv_offset = 0
    @connect_state = true
    @cmd = nil
  end

  def write(*args)
    pkg = args[0].unpack("C*")
    @cmd ||= pkg[8] 
  end

  def recv(len)
    recv_data = recv_config[@cmd.to_s] || {}
    data = recv_data.key?(:recv_bytes) ? recv_data[:recv_bytes].call(len) : nil
    @recv_offset = len
    data
  end

  def close
    @recv_offset = 0
    @connect_state = false
    @cmd = nil
  end

  def closed?
    @connect_state
  end

  private 

  def recv_config
    {
      "101" => {
        recv_bytes: lambda do |len|
          header = ProtoCommon.header_bytes(CMD::RESP_CODE, 0)
          header[7] = ProtoCommon::TRACKER_BODY_LEN

          group_name = Utils.array_merge([].fill(0, 0...16), TestConfig::GROUP_NAME.bytes)
          ip = Utils.array_merge([].fill(0, 0...15), TestConfig::STORAGE_IP.bytes)
          port = Utils.number_to_Buffer(TestConfig::STORAGE_PORT.to_i)
          store_path = Array(TestConfig::STORE_PATH)

          (header+group_name+ip+port+store_path)[@recv_offset...@recv_offset+len].pack("C*")
        end
      },
      "11" => {
        recv_bytes: lambda do |len|
          header = ProtoCommon.header_bytes(CMD::RESP_CODE, 0)
          group_name = Utils.array_merge([].fill(0, 0...16), TestConfig::GROUP_NAME.bytes)
          file_name = TestConfig::FILE_NAME.bytes
          res = (group_name + file_name)
          header[7] = (header + res).length
          res = (header + res)
          
          res[@recv_offset...@recv_offset+len].pack("C*")
        end
      },
      "12" => {
        recv_bytes: lambda do |len|
          header = ProtoCommon.header_bytes(CMD::RESP_CODE, 0)
          header.pack("C*")
        end
      }
    }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fastdfs-client-0.0.3 spec/mock_tcp_socket.rb
fastdfs-client-0.0.2 spec/mock_tcp_socket.rb