Sha256: 08fe90a1996d0623eb12749747ed4bd971e26f20c0ee5dc5909e170f210f40b3

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module Thin
  module Backends
    class SwiftiplyClient < Base
      attr_accessor :key
      
      attr_accessor :host, :port
      
      def initialize(host, port, key=nil)
        @host = host
        @port = port.to_i
        @key  = key || ''
        super()
      end

      # Connect the server
      def connect
        EventMachine.connect(@host, @port, SwiftiplyConnection, &method(:initialize_connection))
      end

      # Stops the server
      def disconnect
        EventMachine.stop
      end

      def to_s
        "#{@host}:#{@port} swiftiply"
      end
    end    
  end

  class SwiftiplyConnection < Connection
    def connection_completed
      send_data swiftiply_handshake(@backend.key)
    end
    
    def persistent?
      true
    end
    
    def unbind
      super
      EventMachine.add_timer(rand(2)) { reconnect(@backend.host, @backend.port) } if @backend.running?
    end
    
    protected
      def swiftiply_handshake(key)
        'swiftclient' << host_ip.collect { |x| sprintf('%02x', x.to_i)}.join << sprintf('%04x', @backend.port) << sprintf('%02x', key.length) << key
      end
      
      # For some reason Swiftiply request the current host
      def host_ip
        Socket.gethostbyname(@backend.host)[3].unpack('CCCC') rescue [0,0,0,0]
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thin-0.7.1 lib/thin/backends/swiftiply_client.rb
thin-0.7.1-x86-mswin32-60 lib/thin/backends/swiftiply_client.rb