mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-09-26 17:49:33 +02:00
b5f9d4d7a3
This also removes the powermock dependency. Although powermock is a fine library, it currently prevents dropping Junit4. And since we only use the Whitebox API of powermock, this simply replaced powermock's Whitebox with our own.
116 lines
3.8 KiB
Java
116 lines
3.8 KiB
Java
/**
|
|
*
|
|
* Copyright the original author or authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package org.jivesoftware.smack;
|
|
|
|
import java.io.IOException;
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
import java.util.concurrent.BlockingQueue;
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
import org.jivesoftware.smack.packet.IQ.Type;
|
|
import org.jivesoftware.smack.packet.Message;
|
|
import org.jivesoftware.smack.packet.Stanza;
|
|
|
|
/**
|
|
* A threaded dummy connection.
|
|
* @author Robin Collier
|
|
*
|
|
*/
|
|
public class ThreadedDummyConnection extends DummyConnection {
|
|
private static final Logger LOGGER = Logger.getLogger(ThreadedDummyConnection.class.getName());
|
|
|
|
private final BlockingQueue<IQ> replyQ = new ArrayBlockingQueue<>(1);
|
|
private final BlockingQueue<Stanza> messageQ = new LinkedBlockingQueue<>(5);
|
|
private volatile boolean timeout = false;
|
|
|
|
@Override
|
|
protected void sendStanzaInternal(Stanza packet) {
|
|
super.sendStanzaInternal(packet);
|
|
|
|
if (packet instanceof IQ && !timeout) {
|
|
timeout = false;
|
|
// Set reply packet to match one being sent. We haven't started the
|
|
// other thread yet so this is still safe.
|
|
IQ replyPacket = replyQ.peek();
|
|
|
|
// If no reply has been set via addIQReply, then we create a simple reply
|
|
if (replyPacket == null) {
|
|
replyPacket = IQ.createResultIQ((IQ) packet);
|
|
replyQ.add(replyPacket);
|
|
}
|
|
replyPacket.setStanzaId(packet.getStanzaId());
|
|
replyPacket.setTo(packet.getFrom());
|
|
if (replyPacket.getType() == null) {
|
|
replyPacket.setType(Type.result);
|
|
}
|
|
|
|
new ProcessQueue(replyQ).start();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Calling this method will cause the next sendStanza call with an IQ stanza to timeout.
|
|
* This is accomplished by simply stopping the auto creating of the reply stanza
|
|
* or processing one that was entered via {@link #processStanza(Stanza)}.
|
|
*/
|
|
public void setTimeout() {
|
|
timeout = true;
|
|
}
|
|
|
|
public void addMessage(Message msgToProcess) {
|
|
messageQ.add(msgToProcess);
|
|
}
|
|
|
|
public void addIQReply(IQ reply) {
|
|
replyQ.add(reply);
|
|
}
|
|
|
|
public void processMessages() {
|
|
if (!messageQ.isEmpty())
|
|
new ProcessQueue(messageQ).start();
|
|
else
|
|
LOGGER.warning("No messages to process");
|
|
}
|
|
|
|
class ProcessQueue extends Thread {
|
|
private BlockingQueue<? extends Stanza> processQ;
|
|
|
|
ProcessQueue(BlockingQueue<? extends Stanza> queue) {
|
|
processQ = queue;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
processStanza(processQ.take());
|
|
} catch (InterruptedException e) {
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static ThreadedDummyConnection newInstance() throws SmackException, IOException, XMPPException, InterruptedException {
|
|
ThreadedDummyConnection threadedDummyConnection = new ThreadedDummyConnection();
|
|
threadedDummyConnection.connect();
|
|
return threadedDummyConnection;
|
|
}
|
|
|
|
}
|