Sha256: db45e510f1a8c03b9d55fa45ab0bb25e3ea087ee7f07bff4c0f5314ca98def43

Contents?: true

Size: 1.37 KB

Versions: 95

Compression:

Stored size: 1.37 KB

Contents

module Thin
  module Backends
    # Backend to act as a Swiftiply client (http://swiftiply.swiftcore.org).
    class SwiftiplyClient < Base
      attr_accessor :key
      
      attr_accessor :host, :port
      
      def initialize(host, port, options={})
        @host = host
        @port = port.to_i
        @key  = options[:swiftiply].to_s
        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

95 entries across 95 versions & 8 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
classiccms-0.7.4 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
classiccms-0.7.3 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
grockit-thin-0.8.2 lib/thin/backends/swiftiply_client.rb
macournoyer-thin-1.0.1 lib/thin/backends/swiftiply_client.rb
macournoyer-thin-1.1.0 lib/thin/backends/swiftiply_client.rb
michaelyta-thin-1.2.2 lib/thin/backends/swiftiply_client.rb
thin-1.6.2 lib/thin/backends/swiftiply_client.rb
thin-1.6.1 lib/thin/backends/swiftiply_client.rb
thin-1.6.0 lib/thin/backends/swiftiply_client.rb
classiccms-0.7.2 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
classiccms-0.7.1 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
classiccms-0.7.0 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
thin-1.5.1 lib/thin/backends/swiftiply_client.rb
classiccms-0.6.9 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
classiccms-0.6.8 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
classiccms-0.6.7 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
friendlyfashion-thin-1.4.1.1 lib/thin/backends/swiftiply_client.rb
classiccms-0.6.6 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb
classiccms-0.6.5 vendor/bundle/gems/thin-1.3.1/lib/thin/backends/swiftiply_client.rb