diff options
Diffstat (limited to 'tests/plugins/org.eclipse.tcf.debug.test/src/org/eclipse/tcf/debug/test/util/ChannelQuery.java')
-rw-r--r-- | tests/plugins/org.eclipse.tcf.debug.test/src/org/eclipse/tcf/debug/test/util/ChannelQuery.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/plugins/org.eclipse.tcf.debug.test/src/org/eclipse/tcf/debug/test/util/ChannelQuery.java b/tests/plugins/org.eclipse.tcf.debug.test/src/org/eclipse/tcf/debug/test/util/ChannelQuery.java new file mode 100644 index 000000000..5ee352b34 --- /dev/null +++ b/tests/plugins/org.eclipse.tcf.debug.test/src/org/eclipse/tcf/debug/test/util/ChannelQuery.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2012 Wind River Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Wind River Systems - initial API and implementation + *******************************************************************************/ +package org.eclipse.tcf.debug.test.util; + +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.eclipse.tcf.protocol.IChannel; + +/** + * Query extension which automatically completes when a TCF channel is closed. + */ +abstract public class ChannelQuery<V> extends Query<V> { + + private final IChannel fChannel; + + public ChannelQuery(IChannel channel) { + fChannel = channel; + } + + @Override + final protected void execute(final DataCallback<V> callback) { + final AtomicBoolean done = new AtomicBoolean(false); + final IChannel.IChannelListener channelListener = new IChannel.IChannelListener() { + @Override + public void onChannelClosed(Throwable error) { + if (!done.getAndSet(true)) { + fChannel.removeChannelListener(this); + callback.done(new IOException("Channel closed.", error)); + } + } + @Override + public void congestionLevel(int level) {} + @Override + public void onChannelOpened() {} + }; + fChannel.addChannelListener(channelListener); + + channelExecute(new DataCallback<V>(callback) { + @Override + protected void handleCompleted() { + if (!done.getAndSet(true)) { + fChannel.removeChannelListener(channelListener); + callback.done(getData(), getError()); + } + } + }); + + } + + abstract protected void channelExecute(final DataCallback<V> callback); +} |