2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. 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
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
|
2006-06-10 01:24:54 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Writer;
|
2007-03-09 03:02:27 +01:00
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
2006-06-10 01:24:54 +02:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2006-02-09 00:11:21 +01:00
|
|
|
* Writes packets to a XMPP server. Packets are sent using a dedicated thread. Packet
|
|
|
|
* interceptors can be registered to dynamically modify packets before they're actually
|
|
|
|
* sent. Packet listeners can be registered to listen for all outgoing packets.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2010-02-09 12:55:56 +01:00
|
|
|
* @see Connection#addPacketInterceptor
|
|
|
|
* @see Connection#addPacketSendingListener
|
|
|
|
*
|
2003-01-13 17:58:47 +01:00
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
class PacketWriter {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
private Thread writerThread;
|
2006-09-14 21:16:40 +02:00
|
|
|
private Thread keepAliveThread;
|
2003-01-13 17:58:47 +01:00
|
|
|
private Writer writer;
|
|
|
|
private XMPPConnection connection;
|
2007-03-09 03:02:27 +01:00
|
|
|
private final BlockingQueue<Packet> queue;
|
2006-09-14 21:16:40 +02:00
|
|
|
private boolean done;
|
2006-02-09 00:11:21 +01:00
|
|
|
|
2006-06-12 18:32:18 +02:00
|
|
|
/**
|
|
|
|
* Timestamp when the last stanza was sent to the server. This information is used
|
|
|
|
* by the keep alive process to only send heartbeats when the connection has been idle.
|
|
|
|
*/
|
|
|
|
private long lastActive = System.currentTimeMillis();
|
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Creates a new packet writer with the specified connection.
|
|
|
|
*
|
|
|
|
* @param connection the connection.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
protected PacketWriter(XMPPConnection connection) {
|
2007-03-09 03:02:27 +01:00
|
|
|
this.queue = new ArrayBlockingQueue<Packet>(500, true);
|
2003-01-13 17:58:47 +01:00
|
|
|
this.connection = connection;
|
2006-09-14 21:16:40 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the writer in order to be used. It is called at the first connection and also
|
|
|
|
* is invoked if the connection is disconnected by an error.
|
|
|
|
*/
|
|
|
|
protected void init() {
|
2003-01-13 17:58:47 +01:00
|
|
|
this.writer = connection.writer;
|
2006-09-14 21:16:40 +02:00
|
|
|
done = false;
|
2003-01-15 16:01:01 +01:00
|
|
|
|
|
|
|
writerThread = new Thread() {
|
|
|
|
public void run() {
|
2006-09-14 21:16:40 +02:00
|
|
|
writePackets(this);
|
2003-01-15 16:01:01 +01:00
|
|
|
}
|
|
|
|
};
|
2007-02-04 21:52:45 +01:00
|
|
|
writerThread.setName("Smack Packet Writer (" + connection.connectionCounterValue + ")");
|
2003-01-15 16:01:01 +01:00
|
|
|
writerThread.setDaemon(true);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Sends the specified packet to the server.
|
|
|
|
*
|
|
|
|
* @param packet the packet to send.
|
|
|
|
*/
|
2003-01-13 17:58:47 +01:00
|
|
|
public void sendPacket(Packet packet) {
|
2003-01-15 16:01:01 +01:00
|
|
|
if (!done) {
|
2005-11-02 00:49:07 +01:00
|
|
|
// Invoke interceptors for the new packet that is about to be sent. Interceptors
|
|
|
|
// may modify the content of the packet.
|
2010-02-09 12:55:56 +01:00
|
|
|
connection.firePacketInterceptors(packet);
|
2005-11-02 00:49:07 +01:00
|
|
|
|
2007-03-09 03:02:27 +01:00
|
|
|
try {
|
|
|
|
queue.put(packet);
|
|
|
|
}
|
|
|
|
catch (InterruptedException ie) {
|
|
|
|
ie.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
2007-01-11 20:01:24 +01:00
|
|
|
synchronized (queue) {
|
2004-04-08 01:49:00 +02:00
|
|
|
queue.notifyAll();
|
2003-01-15 16:01:01 +01:00
|
|
|
}
|
2006-02-09 00:11:21 +01:00
|
|
|
|
|
|
|
// Process packet writer listeners. Note that we're using the sending
|
|
|
|
// thread so it's expected that listeners are fast.
|
2010-02-09 12:55:56 +01:00
|
|
|
connection.firePacketSendingListeners(packet);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Starts the packet writer thread and opens a connection to the server. The
|
|
|
|
* packet writer will continue writing packets until {@link #shutdown} or an
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
public void startup() {
|
|
|
|
writerThread.start();
|
|
|
|
}
|
|
|
|
|
2006-06-12 18:32:18 +02:00
|
|
|
/**
|
|
|
|
* Starts the keep alive process. A white space (aka heartbeat) is going to be
|
|
|
|
* sent to the server every 30 seconds (by default) since the last stanza was sent
|
|
|
|
* to the server.
|
|
|
|
*/
|
|
|
|
void startKeepAliveProcess() {
|
|
|
|
// Schedule a keep-alive task to run if the feature is enabled. will write
|
|
|
|
// out a space character each time it runs to keep the TCP/IP connection open.
|
|
|
|
int keepAliveInterval = SmackConfiguration.getKeepAliveInterval();
|
|
|
|
if (keepAliveInterval > 0) {
|
2007-02-21 18:38:05 +01:00
|
|
|
KeepAliveTask task = new KeepAliveTask(keepAliveInterval);
|
|
|
|
keepAliveThread = new Thread(task);
|
|
|
|
task.setThread(keepAliveThread);
|
2006-06-12 18:32:18 +02:00
|
|
|
keepAliveThread.setDaemon(true);
|
2007-02-04 21:52:45 +01:00
|
|
|
keepAliveThread.setName("Smack Keep Alive (" + connection.connectionCounterValue + ")");
|
2006-06-12 18:32:18 +02:00
|
|
|
keepAliveThread.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-27 04:29:04 +02:00
|
|
|
void setWriter(Writer writer) {
|
|
|
|
this.writer = writer;
|
|
|
|
}
|
|
|
|
|
2003-01-15 16:01:01 +01:00
|
|
|
/**
|
|
|
|
* Shuts down the packet writer. Once this method has been called, no further
|
|
|
|
* packets will be written to the server.
|
|
|
|
*/
|
|
|
|
public void shutdown() {
|
|
|
|
done = true;
|
2007-01-11 20:01:24 +01:00
|
|
|
synchronized (queue) {
|
|
|
|
queue.notifyAll();
|
|
|
|
}
|
2003-01-15 16:01:01 +01:00
|
|
|
}
|
|
|
|
|
2007-02-19 06:48:57 +01:00
|
|
|
/**
|
|
|
|
* Cleans up all resources used by the packet writer.
|
|
|
|
*/
|
|
|
|
void cleanup() {
|
2010-02-09 12:55:56 +01:00
|
|
|
connection.interceptors.clear();
|
|
|
|
connection.sendListeners.clear();
|
2007-02-19 06:48:57 +01:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Returns the next available packet from the queue for writing.
|
|
|
|
*
|
|
|
|
* @return the next packet for writing.
|
|
|
|
*/
|
|
|
|
private Packet nextPacket() {
|
2007-01-11 20:01:24 +01:00
|
|
|
Packet packet = null;
|
|
|
|
// Wait until there's a packet or we're done.
|
|
|
|
while (!done && (packet = queue.poll()) == null) {
|
|
|
|
try {
|
|
|
|
synchronized (queue) {
|
|
|
|
queue.wait();
|
2006-06-12 18:32:18 +02:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2007-01-11 20:01:24 +01:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Do nothing
|
2004-01-13 19:13:23 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2007-01-11 20:01:24 +01:00
|
|
|
return packet;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
private void writePackets(Thread thisThread) {
|
2003-01-13 17:58:47 +01:00
|
|
|
try {
|
|
|
|
// Open the stream.
|
2005-08-27 04:29:04 +02:00
|
|
|
openStream();
|
2003-01-13 17:58:47 +01:00
|
|
|
// Write out packets from the queue.
|
2006-09-14 21:16:40 +02:00
|
|
|
while (!done && (writerThread == thisThread)) {
|
2003-01-13 17:58:47 +01:00
|
|
|
Packet packet = nextPacket();
|
2004-01-13 19:13:23 +01:00
|
|
|
if (packet != null) {
|
2004-01-28 22:30:37 +01:00
|
|
|
synchronized (writer) {
|
|
|
|
writer.write(packet.toXML());
|
|
|
|
writer.flush();
|
2006-06-12 18:32:18 +02:00
|
|
|
// Keep track of the last time a stanza was sent to the server
|
|
|
|
lastActive = System.currentTimeMillis();
|
2004-01-28 22:30:37 +01:00
|
|
|
}
|
2004-01-13 19:13:23 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2007-02-19 06:48:57 +01:00
|
|
|
// Flush out the rest of the queue. If the queue is extremely large, it's possible
|
|
|
|
// we won't have time to entirely flush it before the socket is forced closed
|
|
|
|
// by the shutdown process.
|
2007-01-11 20:01:24 +01:00
|
|
|
try {
|
|
|
|
synchronized (writer) {
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
Packet packet = queue.remove();
|
|
|
|
writer.write(packet.toXML());
|
|
|
|
}
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2007-02-19 06:48:57 +01:00
|
|
|
// Delete the queue contents (hopefully nothing is left).
|
|
|
|
queue.clear();
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
// Close the stream.
|
|
|
|
try {
|
2003-05-16 17:42:41 +02:00
|
|
|
writer.write("</stream:stream>");
|
2003-01-13 17:58:47 +01:00
|
|
|
writer.flush();
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
finally {
|
|
|
|
try {
|
|
|
|
writer.close();
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ioe){
|
2003-01-27 16:56:21 +01:00
|
|
|
if (!done) {
|
2003-09-19 00:35:42 +02:00
|
|
|
done = true;
|
|
|
|
connection.packetReader.notifyConnectionError(ioe);
|
2003-01-27 16:56:21 +01:00
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
2003-11-15 20:24:00 +01:00
|
|
|
|
2005-08-27 04:29:04 +02:00
|
|
|
/**
|
|
|
|
* Sends to the server a new stream element. This operation may be requested several times
|
|
|
|
* so we need to encapsulate the logic in one place. This message will be sent while doing
|
|
|
|
* TLS, SASL and resource binding.
|
|
|
|
*
|
|
|
|
* @throws IOException If an error occurs while sending the stanza to the server.
|
|
|
|
*/
|
|
|
|
void openStream() throws IOException {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder stream = new StringBuilder();
|
2005-08-27 04:29:04 +02:00
|
|
|
stream.append("<stream:stream");
|
2010-02-09 12:55:56 +01:00
|
|
|
stream.append(" to=\"").append(connection.getServiceName()).append("\"");
|
2005-08-27 04:29:04 +02:00
|
|
|
stream.append(" xmlns=\"jabber:client\"");
|
|
|
|
stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\"");
|
2006-07-18 08:47:38 +02:00
|
|
|
stream.append(" version=\"1.0\">");
|
2005-08-27 04:29:04 +02:00
|
|
|
writer.write(stream.toString());
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
|
2004-01-28 22:30:37 +01:00
|
|
|
/**
|
|
|
|
* A TimerTask that keeps connections to the server alive by sending a space
|
2004-08-09 23:15:04 +02:00
|
|
|
* character on an interval.
|
2004-01-28 22:30:37 +01:00
|
|
|
*/
|
2004-03-15 19:59:41 +01:00
|
|
|
private class KeepAliveTask implements Runnable {
|
|
|
|
|
|
|
|
private int delay;
|
2006-09-14 21:16:40 +02:00
|
|
|
private Thread thread;
|
2004-03-15 19:59:41 +01:00
|
|
|
|
|
|
|
public KeepAliveTask(int delay) {
|
|
|
|
this.delay = delay;
|
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
protected void setThread(Thread thread) {
|
|
|
|
this.thread = thread;
|
|
|
|
}
|
|
|
|
|
2004-01-28 22:30:37 +01:00
|
|
|
public void run() {
|
2006-06-10 01:24:54 +02:00
|
|
|
try {
|
|
|
|
// Sleep 15 seconds before sending first heartbeat. This will give time to
|
|
|
|
// properly finish TLS negotiation and then start sending heartbeats.
|
|
|
|
Thread.sleep(15000);
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2006-09-14 21:16:40 +02:00
|
|
|
while (!done && keepAliveThread == thread) {
|
2004-01-28 22:30:37 +01:00
|
|
|
synchronized (writer) {
|
2006-06-12 18:32:18 +02:00
|
|
|
// Send heartbeat if no packet has been sent to the server for a given time
|
|
|
|
if (System.currentTimeMillis() - lastActive >= delay) {
|
|
|
|
try {
|
|
|
|
writer.write(" ");
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2004-01-28 22:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
2004-03-15 23:58:48 +01:00
|
|
|
try {
|
2004-08-09 23:15:04 +02:00
|
|
|
// Sleep until we should write the next keep-alive.
|
2004-03-15 23:58:48 +01:00
|
|
|
Thread.sleep(delay);
|
|
|
|
}
|
2006-06-12 18:32:18 +02:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2004-01-28 22:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-05-16 17:42:41 +02:00
|
|
|
}
|