mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
SMACK-412 Split the ping implementation to a server ping to replace keepalive and a simplified ping manager for manual pings of other entities.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13569 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
a55b54f20b
commit
aab1dcdabe
19 changed files with 749 additions and 651 deletions
|
@ -17,80 +17,85 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
|
||||
public class ThreadedDummyConnection extends DummyConnection
|
||||
{
|
||||
private BlockingQueue<IQ> replyQ = new ArrayBlockingQueue<IQ>(1);
|
||||
private BlockingQueue<Packet> messageQ = new LinkedBlockingQueue<Packet>(5);
|
||||
|
||||
@Override
|
||||
public void sendPacket(Packet packet)
|
||||
{
|
||||
super.sendPacket(packet);
|
||||
|
||||
if ((packet instanceof IQ) && !replyQ.isEmpty())
|
||||
{
|
||||
// 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();
|
||||
replyPacket.setPacketID(packet.getPacketID());
|
||||
replyPacket.setFrom(packet.getTo());
|
||||
replyPacket.setTo(packet.getFrom());
|
||||
replyPacket.setType(Type.RESULT);
|
||||
|
||||
new ProcessQueue(replyQ).start();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
System.out.println("No messages to process");
|
||||
}
|
||||
|
||||
class ProcessQueue extends Thread
|
||||
{
|
||||
private BlockingQueue<? extends Packet> processQ;
|
||||
|
||||
ProcessQueue(BlockingQueue<? extends Packet> queue)
|
||||
{
|
||||
processQ = queue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
processPacket(processQ.take());
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
package org.jivesoftware.smack;
|
||||
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
|
||||
public class ThreadedDummyConnection extends DummyConnection {
|
||||
private BlockingQueue<IQ> replyQ = new ArrayBlockingQueue<IQ>(1);
|
||||
private BlockingQueue<Packet> messageQ = new LinkedBlockingQueue<Packet>(5);
|
||||
private volatile boolean timeout = false;
|
||||
|
||||
@Override
|
||||
public void sendPacket(Packet packet) {
|
||||
super.sendPacket(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.setPacketID(packet.getPacketID());
|
||||
replyPacket.setFrom(packet.getTo());
|
||||
replyPacket.setTo(packet.getFrom());
|
||||
replyPacket.setType(Type.RESULT);
|
||||
|
||||
new ProcessQueue(replyQ).start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling this method will cause the next sendPacket call with an IQ packet to timeout.
|
||||
* This is accomplished by simply stopping the auto creating of the reply packet
|
||||
* or processing one that was entered via {@link #processPacket(Packet)}.
|
||||
*/
|
||||
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
|
||||
System.out.println("No messages to process");
|
||||
}
|
||||
|
||||
class ProcessQueue extends Thread {
|
||||
private BlockingQueue<? extends Packet> processQ;
|
||||
|
||||
ProcessQueue(BlockingQueue<? extends Packet> queue) {
|
||||
processQ = queue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
processPacket(processQ.take());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue