// Path: emitter/src/main/java/io/emitter/KeyGenRequest.java
// public class KeyGenRequest
// {
// 	
// 	public String key;
// 	public String channel;
// 	public String type;
// 	public int ttl;
// 	
// 	/**
// 	 * Constructs a new instance of a request.
// 	 */
// 	private KeyGenRequest(){
// 		
// 	}
// 	
// 	/**
// 	 * Constructs a new instance of a request.
// 	 */
// 	public KeyGenRequest(String key, String channel, String type, int ttl) {
// 		 this.key = key;
// 		 this.channel = channel;
// 		 this.type = type;
// 		 this.ttl = ttl;
// 	}
// 	
// 	/**
// 	 * Converts the request to JSON format.
// 	 * @return JSON-formatted string.
// 	 */
// 	public String toJson(){
// 		Map args = new HashMap();
// 		args.put(JsonWriter.TYPE, false);
// 		return JsonWriter.objectToJson(this, args);
// 	}
// 	
// 	
// }
// 
// Path: emitter/src/main/java/io/emitter/KeyGenResponse.java
// public class KeyGenResponse
// {
// 	public int status;
// 	public String key;
// 	public String channel;
// 	
// 	/**
// 	 * Constructs a new instance of a request.
// 	 */
// 	private KeyGenResponse(){
// 		
// 	}
// 	
// 	
// 	/**
// 	 * Deserializes the JSON key-gen response.
// 	 */
// 	public static KeyGenResponse fromJson(String json){
// 		Map args = new HashMap();
// 		args.put(JsonReader.USE_MAPS, true);
// 		JsonObject data = (JsonObject) JsonReader.jsonToJava(json, args);
// 		KeyGenResponse response = new KeyGenResponse();
// 		
// 		if (data.containsKey("status"))
// 			response.status = safeLongToInt((Long) data.get("status"));
// 		
// 		if (data.containsKey("key"))
// 			response.key = (String) data.get("key");
// 		
// 		if (data.containsKey("channel"))
// 			response.channel = (String) data.get("channel");
// 
// 		return response;
// 	}
// 	
// 	
// 	/**
// 	 * Converts the request to JSON format.
// 	 * @return JSON-formatted string.
// 	 */
// 	public String toString(){
// 		Map args = new HashMap();
// 		args.put(JsonWriter.TYPE, false);
// 		return JsonWriter.objectToJson(this, args);
// 	}
// 	
// 	/**
// 	 * Safely casts a long to int.
// 	 */
// 	private static int safeLongToInt(long l) {
// 	    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
// 	        throw new IllegalArgumentException
// 	            (l + " cannot be cast to int without changing its value.");
// 	    }
// 	    return (int) l;
// 	}
// 	
// 	
// }


import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.UTF8Buffer;
import org.fusesource.hawtdispatch.DispatchQueue;
import org.fusesource.hawtdispatch.Task;
import io.emitter.KeyGenRequest;
import io.emitter.KeyGenResponse;
import java.util.ArrayList;
import java.util.LinkedList;
import static org.fusesource.hawtbuf.Buffer.utf8;
    public FutureConnection(CallbackConnection next) {
        this.next = next;
        this.next.listener(new Listener() {

            public void onConnected() {
                connected = true;
            }

            public void onDisconnected() {
                connected = false;
            }

            public void onPublish(UTF8Buffer topic, Buffer payload, Runnable onComplete) {
                getDispatchQueue().assertExecuting();
                deliverMessage(new Message(getDispatchQueue(), topic, payload, onComplete));
            }

            public void onFailure(Throwable value) {
                getDispatchQueue().assertExecuting();
                ArrayList<Promise<?>> tmp = new ArrayList<Promise<?>>(receiveFutures);
                receiveFutures.clear();
                for (Promise<?> future : tmp) {
                    future.onFailure(value);
                }
                connected = false;
            }
        });
    }

    void deliverMessage(Message msg) {
// Path: emitter/src/main/java/io/emitter/KeyGenRequest.java
// public class KeyGenRequest
// {
// 	
// 	public String key;
// 	public String channel;
// 	public String type;
// 	public int ttl;
// 	
// 	/**
// 	 * Constructs a new instance of a request.
// 	 */
// 	private KeyGenRequest(){
// 		
// 	}
// 	
// 	/**
// 	 * Constructs a new instance of a request.
// 	 */
// 	public KeyGenRequest(String key, String channel, String type, int ttl) {
// 		 this.key = key;
// 		 this.channel = channel;
// 		 this.type = type;
// 		 this.ttl = ttl;
// 	}
// 	
// 	/**
// 	 * Converts the request to JSON format.
// 	 * @return JSON-formatted string.
// 	 */
// 	public String toJson(){
// 		Map args = new HashMap();
// 		args.put(JsonWriter.TYPE, false);
// 		return JsonWriter.objectToJson(this, args);
// 	}
// 	
// 	
// }
// 
// Path: emitter/src/main/java/io/emitter/KeyGenResponse.java
// public class KeyGenResponse
// {
// 	public int status;
// 	public String key;
// 	public String channel;
// 	
// 	/**
// 	 * Constructs a new instance of a request.
// 	 */
// 	private KeyGenResponse(){
// 		
// 	}
// 	
// 	
// 	/**
// 	 * Deserializes the JSON key-gen response.
// 	 */
// 	public static KeyGenResponse fromJson(String json){
// 		Map args = new HashMap();
// 		args.put(JsonReader.USE_MAPS, true);
// 		JsonObject data = (JsonObject) JsonReader.jsonToJava(json, args);
// 		KeyGenResponse response = new KeyGenResponse();
// 		
// 		if (data.containsKey("status"))
// 			response.status = safeLongToInt((Long) data.get("status"));
// 		
// 		if (data.containsKey("key"))
// 			response.key = (String) data.get("key");
// 		
// 		if (data.containsKey("channel"))
// 			response.channel = (String) data.get("channel");
// 
// 		return response;
// 	}
// 	
// 	
// 	/**
// 	 * Converts the request to JSON format.
// 	 * @return JSON-formatted string.
// 	 */
// 	public String toString(){
// 		Map args = new HashMap();
// 		args.put(JsonWriter.TYPE, false);
// 		return JsonWriter.objectToJson(this, args);
// 	}
// 	
// 	/**
// 	 * Safely casts a long to int.
// 	 */
// 	private static int safeLongToInt(long l) {
// 	    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
// 	        throw new IllegalArgumentException
// 	            (l + " cannot be cast to int without changing its value.");
// 	    }
// 	    return (int) l;
// 	}
// 	
// 	
// }


// Path: emitter/src/main/java/io/emitter/mqtt/client/FutureConnection.java
import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.UTF8Buffer;
import org.fusesource.hawtdispatch.DispatchQueue;
import org.fusesource.hawtdispatch.Task;
import io.emitter.KeyGenRequest;
import io.emitter.KeyGenResponse;
import java.util.ArrayList;
import java.util.LinkedList;
import static org.fusesource.hawtbuf.Buffer.utf8;

    public FutureConnection(CallbackConnection next) {
        this.next = next;
        this.next.listener(new Listener() {

            public void onConnected() {
                connected = true;
            }

            public void onDisconnected() {
                connected = false;
            }

            public void onPublish(UTF8Buffer topic, Buffer payload, Runnable onComplete) {
                getDispatchQueue().assertExecuting();
                deliverMessage(new Message(getDispatchQueue(), topic, payload, onComplete));
            }

            public void onFailure(Throwable value) {
                getDispatchQueue().assertExecuting();
                ArrayList<Promise<?>> tmp = new ArrayList<Promise<?>>(receiveFutures);
                receiveFutures.clear();
                for (Promise<?> future : tmp) {
                    future.onFailure(value);
                }
                connected = false;
            }
        });
    }

    void deliverMessage(Message msg) {
        if( receiveFutures.isEmpty() ) {