All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fusesource.stomp.client.FutureConnection Maven / Gradle / Ivy

There is a newer version: 1.19
Show newest version
/**
 * Copyright (C) 2010-2011, FuseSource Corp.  All rights reserved.
 *
 *     http://fusesource.com
 *
 * The software in this package is published under the terms of the
 * CDDL license a copy of which has been included with this distribution
 * in the license.txt file.
 */
package org.fusesource.stomp.client;

import org.fusesource.hawtbuf.AsciiBuffer;
import org.fusesource.hawtdispatch.DispatchQueue;
import org.fusesource.hawtdispatch.Task;
import org.fusesource.stomp.codec.StompFrame;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 

*

* * @author Hiram Chirino */ public class FutureConnection { private final CallbackConnection connection; private LinkedList> receiveFutures = new LinkedList>(); private LinkedList receivedFrames = new LinkedList(); FutureConnection(CallbackConnection connection) { this.connection = connection; this.connection.receive(new Callback() { @Override public void onFailure(Throwable value) { getDispatchQueue().assertExecuting(); ArrayList> tmp = new ArrayList>(receiveFutures); receiveFutures.clear(); for (Promise future : tmp) { future.onFailure(value); } } @Override public void onSuccess(StompFrame value) { getDispatchQueue().assertExecuting(); if( receiveFutures.isEmpty() ) { receivedFrames.add(value); } else { receiveFutures.removeFirst().onSuccess(value); } } }); this.connection.resume(); } public StompFrame connectedFrame() { return connection.connectedFrame(); } private DispatchQueue getDispatchQueue() { return this.connection.getDispatchQueue(); } public Future close() { final Promise future = new Promise(); connection.close(new Runnable() { public void run() { future.onSuccess(null); } }); return future; } public AsciiBuffer nextId() { return connection.nextId(); } public AsciiBuffer nextId(String prefix) { return connection.nextId(prefix); } public Future request(final StompFrame frame) { final Promise future = new Promise(); connection.getDispatchQueue().execute(new Task() { public void run() { connection.request(frame, future); } }); return future; } public Future send(final StompFrame frame) { final Promise future = new Promise(); connection.getDispatchQueue().execute(new Task() { public void run() { connection.send(frame, future); } }); return future; } public Future receive() { final Promise future = new Promise(); getDispatchQueue().execute(new Task(){ public void run() { if( connection.getFailure()!=null ) { future.onFailure(connection.getFailure()); } else { if( receivedFrames.isEmpty() ) { receiveFutures.add(future); } else { future.onSuccess(receivedFrames.removeFirst()); } } } }); return future; } public void resume() { connection.resume(); } public void suspend() { connection.suspend(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy