1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-27 00:32:07 +01:00

A packet reply timeout can be set which applies to all methods (from Sid).

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@10950 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2008-12-29 18:35:35 +00:00 committed by matt
parent afe2b9b584
commit ac2c9b09dc

View file

@ -1,184 +1,201 @@
/** /**
* $RCSfile$ * $RCSfile$
* $Revision: $ * $Revision: $
* $Date: $ * $Date: $
* *
* Copyright 2005-2007 Jive Software. * Copyright 2005-2007 Jive Software.
* *
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); * 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 not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.jivesoftware.smackx.commands; package org.jivesoftware.smackx.commands;
import org.jivesoftware.smack.PacketCollector; import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter; import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.Form; import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.packet.AdHocCommandData; import org.jivesoftware.smackx.packet.AdHocCommandData;
/** /**
* Represents a command that is in a remote location. Invoking one of the * Represents a command that is in a remote location. Invoking one of the
* {@link AdHocCommand.Action#execute execute}, {@link AdHocCommand.Action#next next}, * {@link AdHocCommand.Action#execute execute}, {@link AdHocCommand.Action#next next},
* {@link AdHocCommand.Action#prev prev}, {@link AdHocCommand.Action#cancel cancel} or * {@link AdHocCommand.Action#prev prev}, {@link AdHocCommand.Action#cancel cancel} or
* {@link AdHocCommand.Action#complete complete} actions results in executing that * {@link AdHocCommand.Action#complete complete} actions results in executing that
* action in the remote location. In response to that action the internal state * action in the remote location. In response to that action the internal state
* of the this command instance will change. For example, if the command is a * of the this command instance will change. For example, if the command is a
* single stage command, then invoking the execute action will execute this * single stage command, then invoking the execute action will execute this
* action in the remote location. After that the local instance will have a * action in the remote location. After that the local instance will have a
* state of "completed" and a form or notes that applies. * state of "completed" and a form or notes that applies.
* *
* @author Gabriel Guardincerri * @author Gabriel Guardincerri
* *
*/ */
public class RemoteCommand extends AdHocCommand { public class RemoteCommand extends AdHocCommand {
/** /**
* The connection that is used to execute this command * The connection that is used to execute this command
*/ */
private XMPPConnection connection; private XMPPConnection connection;
/** /**
* The full JID of the command host * The full JID of the command host
*/ */
private String jid; private String jid;
/** /**
* The session ID of this execution. * The session ID of this execution.
*/ */
private String sessionID; private String sessionID;
/**
* Creates a new RemoteCommand that uses an specific connection to execute a /**
* command identified by <code>node</code> in the host identified by * The number of milliseconds to wait for a response from the server
* <code>jid</code> * The default value is the default packet reply timeout (5000 ms).
* */
* @param connection the connection to use for the execution. private long packetReplyTimeout;
* @param node the identifier of the command.
* @param jid the JID of the host. /**
*/ * Creates a new RemoteCommand that uses an specific connection to execute a
protected RemoteCommand(XMPPConnection connection, String node, String jid) { * command identified by <code>node</code> in the host identified by
super(); * <code>jid</code>
this.connection = connection; *
this.jid = jid; * @param connection the connection to use for the execution.
this.setNode(node); * @param node the identifier of the command.
} * @param jid the JID of the host.
*/
@Override protected RemoteCommand(XMPPConnection connection, String node, String jid) {
public void cancel() throws XMPPException { super();
executeAction(Action.cancel, SmackConfiguration.getPacketReplyTimeout()); this.connection = connection;
} this.jid = jid;
this.setNode(node);
@Override this.packetReplyTimeout = SmackConfiguration.getPacketReplyTimeout();
public void complete(Form form) throws XMPPException { }
executeAction(Action.complete, form, SmackConfiguration.getPacketReplyTimeout());
} @Override
public void cancel() throws XMPPException {
@Override executeAction(Action.cancel, packetReplyTimeout);
public void execute() throws XMPPException { }
executeAction(Action.execute, SmackConfiguration.getPacketReplyTimeout());
} @Override
public void complete(Form form) throws XMPPException {
/** executeAction(Action.complete, form, packetReplyTimeout);
* Executes the command, waiting up to the <tt>timeout</tt> for a reply. }
* This is invoked only on the first stage of the
* command. It is invoked on every command. If there is a problem executing @Override
* the command it throws an XMPPException. public void execute() throws XMPPException {
* executeAction(Action.execute, packetReplyTimeout);
* @param timeout the length of time in ms to wait for a reply. }
* @throws XMPPException if there is an error executing the command.
*/ /**
public void execute(long timeout) throws XMPPException { * Executes the default action of the command with the information provided
executeAction(Action.execute, timeout); * in the Form. This form must be the anwser form of the previous stage. If
} * there is a problem executing the command it throws an XMPPException.
*
/** * @param form the form anwser of the previous stage.
* Executes the default action of the command with the information provided * @throws XMPPException if an error occurs.
* in the Form. This form must be the anwser form of the previous stage. If */
* there is a problem executing the command it throws an XMPPException. public void execute(Form form) throws XMPPException {
* executeAction(Action.execute, form, packetReplyTimeout);
* @param form the form anwser of the previous stage. }
* @throws XMPPException if an error occurs.
*/ @Override
public void execute(Form form) throws XMPPException { public void next(Form form) throws XMPPException {
executeAction(Action.execute, form, SmackConfiguration.getPacketReplyTimeout()); executeAction(Action.next, form, packetReplyTimeout);
} }
@Override @Override
public void next(Form form) throws XMPPException { public void prev() throws XMPPException {
executeAction(Action.next, form, SmackConfiguration.getPacketReplyTimeout()); executeAction(Action.prev, packetReplyTimeout);
} }
@Override private void executeAction(Action action, long packetReplyTimeout) throws XMPPException {
public void prev() throws XMPPException { executeAction(action, null, packetReplyTimeout);
executeAction(Action.prev, SmackConfiguration.getPacketReplyTimeout()); }
}
/**
private void executeAction(Action action, long timeout) throws XMPPException { * Executes the <code>action</codo> with the <code>form</code>.
executeAction(action, null, timeout); * The action could be any of the available actions. The form must
} * be the anwser of the previous stage. It can be <tt>null</tt> if it is the first stage.
*
/** * @param action the action to execute.
* Executes the <code>action</codo> with the <code>form</code>. * @param form the form with the information.
* The action could be any of the available actions. The form must * @param timeout the amount of time to wait for a reply.
* be the anwser of the previous stage. It can be <tt>null</tt> if it is the first stage. * @throws XMPPException if there is a problem executing the command.
* */
* @param action the action to execute. private void executeAction(Action action, Form form, long timeout) throws XMPPException {
* @param form the form with the information. // TODO: Check that all the required fields of the form were filled, if
* @param timeout the amount of time to wait for a reply. // TODO: not throw the corresponding exeption. This will make a faster response,
* @throws XMPPException if there is a problem executing the command. // TODO: since the request is stoped before it's sent.
*/ AdHocCommandData data = new AdHocCommandData();
private void executeAction(Action action, Form form, long timeout) throws XMPPException { data.setType(IQ.Type.SET);
// TODO: Check that all the required fields of the form were filled, if data.setTo(getOwnerJID());
// TODO: not throw the corresponding exeption. This will make a faster response, data.setNode(getNode());
// TODO: since the request is stoped before it's sent. data.setSessionID(sessionID);
AdHocCommandData data = new AdHocCommandData(); data.setAction(action);
data.setType(IQ.Type.SET);
data.setTo(getOwnerJID()); if (form != null) {
data.setNode(getNode()); data.setForm(form.getDataFormToSend());
data.setSessionID(sessionID); }
data.setAction(action);
PacketCollector collector = connection.createPacketCollector(
if (form != null) { new PacketIDFilter(data.getPacketID()));
data.setForm(form.getDataFormToSend());
} connection.sendPacket(data);
PacketCollector collector = connection.createPacketCollector( Packet response = collector.nextResult(timeout);
new PacketIDFilter(data.getPacketID()));
// Cancel the collector.
connection.sendPacket(data); collector.cancel();
if (response == null) {
Packet response = collector.nextResult(timeout); throw new XMPPException("No response from server on status set.");
}
// Cancel the collector. if (response.getError() != null) {
collector.cancel(); throw new XMPPException(response.getError());
if (response == null) { }
throw new XMPPException("No response from server on status set.");
} AdHocCommandData responseData = (AdHocCommandData) response;
if (response.getError() != null) { this.sessionID = responseData.getSessionID();
throw new XMPPException(response.getError()); super.setData(responseData);
} }
AdHocCommandData responseData = (AdHocCommandData) response; @Override
this.sessionID = responseData.getSessionID(); public String getOwnerJID() {
super.setData(responseData); return jid;
} }
@Override /**
public String getOwnerJID() { * Returns the number of milliseconds to wait for a respone. The
return jid; * {@link SmackConfiguration#getPacketReplyTimeout default} value
} * should be adjusted for commands that can take a long time to execute.
*
* @return the number of milliseconds to wait for responses.
*/
public long getPacketReplyTimeout() {
return packetReplyTimeout;
}
/**
* Returns the number of milliseconds to wait for a respone. The
* {@link SmackConfiguration#getPacketReplyTimeout default} value
* should be adjusted for commands that can take a long time to execute.
*
* @param packetReplyTimeout the number of milliseconds to wait for responses.
*/
public void setPacketReplyTimeout(long packetReplyTimeout) {
this.packetReplyTimeout = packetReplyTimeout;
}
} }