Search in sources :

Example 1 with OperationState

use of com.ms.silverking.cloud.dht.client.OperationState in project SilverKing by Morgan-Stanley.

the class AsyncOperationImpl method addListener.

/**
 * Adds an operation listener. If the operation is already complete, the callback will be
 * immediately executed, possibly in the calling thread.
 * @param listener	update listener
 * @param listenStates	states to generate updates for
 */
public void addListener(AsyncOperationListener listener, OperationState... listenStates) {
    EnumSet<OperationState> _listenStates;
    OperationState opState;
    _listenStates = CollectionUtil.arrayToEnumSet(listenStates);
    opState = getState();
    lock.lock();
    try {
        // Trigger immediate updates for completion, but only for completion
        if (opState != OperationState.INCOMPLETE && _listenStates.contains(opState)) {
            listener.asyncOperationUpdated(this);
        } else {
            if (listeners == null) {
                listeners = new HashSet<>();
            }
            listeners.add(new Pair<>(listener, _listenStates));
        }
    } finally {
        lock.unlock();
    }
}
Also used : OperationState(com.ms.silverking.cloud.dht.client.OperationState)

Example 2 with OperationState

use of com.ms.silverking.cloud.dht.client.OperationState in project SilverKing by Morgan-Stanley.

the class HelloCallbackDHT2 method asyncOperationUpdated.

@Override
public void asyncOperationUpdated(AsyncOperation asyncOperation) {
    OperationState opState;
    opState = asyncOperation.getState();
    if (opState == OperationState.FAILED) {
        System.out.printf("Operation failed: %s\n", asyncOperation.getClass());
    } else if (opState == OperationState.INCOMPLETE || opState == OperationState.SUCCEEDED) {
        if (asyncOperation instanceof AsyncValueRetrieval) {
            AsyncValueRetrieval asyncValueRetrieval;
            Map<String, String> values;
            asyncValueRetrieval = (AsyncValueRetrieval) asyncOperation;
            try {
                values = asyncValueRetrieval.getLatestValues();
                for (Map.Entry<String, String> entry : values.entrySet()) {
                    System.out.printf("Complete: %s\t=>\t%s\n", entry.getKey(), entry.getValue());
                }
            } catch (RetrievalException e) {
                e.printStackTrace();
            }
        // System.out.printf("Incomplete keys: %d\n", asyncValueRetrieval.getIncompleteKeys().size());
        } else {
            System.out.printf("Operation incomplete: %s\n", asyncOperation.getClass());
        }
        if (opState == OperationState.SUCCEEDED) {
            s.release();
        }
    } else {
        s.release();
    }
// System.out.printf("%s %s %s\n", Thread.currentThread().getName(), asyncOperation.getClass(), opState);
}
Also used : AsyncValueRetrieval(com.ms.silverking.cloud.dht.client.AsyncValueRetrieval) RetrievalException(com.ms.silverking.cloud.dht.client.RetrievalException) HashMap(java.util.HashMap) Map(java.util.Map) OperationState(com.ms.silverking.cloud.dht.client.OperationState)

Example 3 with OperationState

use of com.ms.silverking.cloud.dht.client.OperationState in project SilverKing by Morgan-Stanley.

the class AsyncOperationImpl method addListeners.

/**
 * Adds multiple completion listeners. For any listener that is already complete, the callback will be
 * immediately executed, possibly in the calling thread.
 * @param listeners	update listeners
 * @param listenStates	states to generate updates for
 */
public void addListeners(Iterable<AsyncOperationListener> _listeners, OperationState... listenStates) {
    EnumSet<OperationState> _listenStates;
    OperationState opState;
    _listenStates = CollectionUtil.arrayToEnumSet(listenStates);
    opState = getState();
    lock.lock();
    try {
        // Trigger immediate updates for completion, but only for completion
        if (opState != OperationState.INCOMPLETE && _listenStates.contains(opState)) {
            for (AsyncOperationListener listener : _listeners) {
                listener.asyncOperationUpdated(this);
            }
        } else {
            if (listeners == null) {
                listeners = new HashSet<>();
            }
            for (AsyncOperationListener listener : _listeners) {
                listeners.add(new Pair<>(listener, _listenStates));
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : AsyncOperationListener(com.ms.silverking.cloud.dht.client.AsyncOperationListener) OperationState(com.ms.silverking.cloud.dht.client.OperationState)

Example 4 with OperationState

use of com.ms.silverking.cloud.dht.client.OperationState in project SilverKing by Morgan-Stanley.

the class AsyncOperationImpl method checkForUpdates.

/**
 * Called by subclasses to update incomplete state. Complete updates are handled inside of
 * setResult to ensure that completion results fire exactly once.
 */
protected void checkForUpdates() {
    if (listeners != null) {
        Set<Pair<AsyncOperationListener, EnumSet<OperationState>>> _listeners;
        OperationState opState;
        opState = getState();
        if (opState == OperationState.INCOMPLETE) {
            _listeners = null;
            lock.lock();
            try {
                if (listeners != null) {
                    _listeners = ImmutableSet.copyOf(listeners);
                }
            } finally {
                lock.unlock();
            }
            if (_listeners != null) {
                notificationWorker.fiterForUpdates(this, _listeners, opState);
            }
        }
    }
}
Also used : OperationState(com.ms.silverking.cloud.dht.client.OperationState) Pair(com.ms.silverking.collection.Pair)

Aggregations

OperationState (com.ms.silverking.cloud.dht.client.OperationState)4 AsyncOperationListener (com.ms.silverking.cloud.dht.client.AsyncOperationListener)1 AsyncValueRetrieval (com.ms.silverking.cloud.dht.client.AsyncValueRetrieval)1 RetrievalException (com.ms.silverking.cloud.dht.client.RetrievalException)1 Pair (com.ms.silverking.collection.Pair)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1