+os              = require 'os'
 _               = require 'lodash'
 assert          = require 'assert'
 amqp            = require 'amqp'

        assert _.isString(options.topic),   "topic required"
        assert _.isString(options.group),   "group required"

        "#{options.channel}:#{options.topic}:#{options.group}"

    _create_queue: (options) ->
        logger.trace("create queue", {options})

        logger.trace("create exchange: %s", options.exchange)

        exch_deferred = Q.defer()
        promise = exch_deferred.promise
        exch_name = options.channel
        exch_options =
            type: options.type or 'topic'
            autoDelete: false
            durable: options.durable

        if options.partition?
            exch_name = "#{exch_name}.#{options.group}-splitter"
            _.extend(exch_options,
                type: 'x-consistent-hash'
                autoDelete: true
            )
            exchange = null
            promise = promise.then @_create_bound_exchange.bind(@, options, connection)

        logger.trace("create exchange instance", {exch_name, exch_options})
        connection.exchange(exch_name, exch_options, exch_deferred.resolve)
        promise

    _create_bound_exchange: (options, connection, exchange) ->
        @_create_exchange_instance(_.omit(options, 'partition'), connection)
            .then @_bind_exchange.bind(@, exchange, options)

    _bind_exchange: (exchange, options, source) ->
        assert _.isString(options.topic), "topic option require"

        binding_key = options.topic
        exchange_deferred = Q.defer()

        logger.trace("binding exchange to other exchange", {exchange: exchange.name, source: source.name, binding_key})
        exchange.bind(source, binding_key, ->
            exchange_deferred.resolve(exchange)
        )
        exchange_deferred.promise


module.exports = (setup, options) ->
    setup = {server: setup} if _.isString(setup)
