Sha256: 73ef475f7cd32b8f8e4ede04e2fb5aef247958ec34159bf5060f8cdc9dc16858

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module Thin
  module Connectors
    class SwiftiplyClient < Connector
      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(@connector.key)
    end
    
    def persistent?
      true
    end
    
    def unbind
      super
      EventMachine.add_timer(rand(2)) { reconnect(@connector.host, @connector.port) } if @connector.running?
    end
    
    protected
      def swiftiply_handshake(key)
        'swiftclient' << host_ip.collect { |x| sprintf('%02x', x.to_i)}.join << sprintf('%04x', @connector.port) << sprintf('%02x', key.length) << key
      end
      
      # For some reason Swiftiply request the current host
      def host_ip
        Socket.gethostbyname(@connector.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.0-x86-mswin32-60 lib/thin/connectors/swiftiply_client.rb
thin-0.7.0 lib/thin/connectors/swiftiply_client.rb