001/**
002 *
003 * Copyright 2009 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack;
018
019import java.net.InetAddress;
020import java.util.concurrent.TimeUnit;
021
022import javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.SmackException.NoResponseException;
025import org.jivesoftware.smack.SmackException.NotConnectedException;
026import org.jivesoftware.smack.SmackException.OutgoingQueueFullException;
027import org.jivesoftware.smack.XMPPException.XMPPErrorException;
028import org.jivesoftware.smack.filter.IQReplyFilter;
029import org.jivesoftware.smack.filter.StanzaFilter;
030import org.jivesoftware.smack.iqrequest.IQRequestHandler;
031import org.jivesoftware.smack.packet.ExtensionElement;
032import org.jivesoftware.smack.packet.IQ;
033import org.jivesoftware.smack.packet.Message;
034import org.jivesoftware.smack.packet.MessageBuilder;
035import org.jivesoftware.smack.packet.Nonza;
036import org.jivesoftware.smack.packet.Presence;
037import org.jivesoftware.smack.packet.PresenceBuilder;
038import org.jivesoftware.smack.packet.Stanza;
039import org.jivesoftware.smack.packet.StanzaFactory;
040import org.jivesoftware.smack.packet.XmlElement;
041import org.jivesoftware.smack.util.Consumer;
042import org.jivesoftware.smack.util.Predicate;
043import org.jivesoftware.smack.util.XmppElementUtil;
044
045import org.jxmpp.jid.DomainBareJid;
046import org.jxmpp.jid.EntityFullJid;
047
048/**
049 * The XMPPConnection interface provides an interface for connections from a client to an XMPP server and
050 * implements shared methods which are used by the different types of connections (e.g.
051 * {@link org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection} or <code>XMPPTCPConnection</code>). To create a connection to an XMPP server
052 * a simple usage of this API might look like the following:
053 *
054 * <pre>{@code
055 * // Create the configuration for this new connection
056 * XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
057 * configBuilder.setUsernameAndPassword("username", "password");
058 * configBuilder.setXmppDomain("jabber.org");
059 *
060 * AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
061 * connection.connect();
062 * connection.login();
063 *
064 * Message message = connection.getStanzaFactory().buildMessageStanza()
065 *     .to("mark@example.org)
066 *     .setBody("Hi, how are you?")
067 *     .build();
068 * connection.sendStanza(message);
069 *
070 * connection.disconnect();
071 * }</pre>
072 * <p>
073 * Note that the XMPPConnection interface does intentionally not declare any methods that manipulate
074 * the connection state, e.g. <code>connect()</code>, <code>disconnect()</code>. You should use the
075 * most-generic superclass connection type that is able to provide the methods you require. In most cases
076 * this should be {@link AbstractXMPPConnection}. And use or hand out instances of the
077 * XMPPConnection interface when you don't need to manipulate the connection state.
078 * </p>
079 * <p>
080 * XMPPConnections can be reused between connections. This means that an Connection may be connected,
081 * disconnected and then connected again. Listeners of the XMPPConnection will be retained across
082 * connections.
083 * </p>
084 * <h2>Processing Incoming Stanzas</h2>
085 * Smack provides a flexible framework for processing incoming stanzas using two constructs:
086 * <ul>
087 *  <li>{@link StanzaCollector}: lets you synchronously wait for new stanzas</li>
088 *  <li>{@link StanzaListener}: an interface for asynchronously notifying you of incoming stanzas</li>
089 * </ul>
090 *
091 * <h2>Incoming Stanza Listeners</h2>
092 * Most callbacks (listeners, handlers, …) than you can add to a connection come in three different variants:
093 * <ul>
094 * <li>asynchronous - e.g., {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)}</li>
095 * <li>synchronous  - e.g., {@link #addSyncStanzaListener(StanzaListener, StanzaFilter)}</li>
096 * <li>other        - e.g., {@link #addStanzaListener(StanzaListener, StanzaFilter)}</li>
097 * </ul>
098 * <p>
099 * Asynchronous callbacks are run decoupled from the connections main event loop. Hence, a callback triggered by
100 * stanza B may (appear to) invoked before a callback triggered by stanza A, even though stanza A arrived before B.
101 * </p>
102 * <p>
103 * Synchronous callbacks are invoked concurrently, but it is ensured that the same callback is never run concurrently
104 * and that they are executed in order. That is, if both stanza A and B trigger the same callback, and A arrives before
105 * B, then the callback will be invoked with A first, and then B. Furthermore, those callbacks are not executed within
106 * the main loop. However it is still advisable that those callbacks do not block or only block briefly.
107 * </p>
108 * <p>
109 * Other callbacks are run synchronous to the main event loop of a connection and are executed within the main loop.
110 * <b>This means that if such a callback blocks, the main event loop also blocks, which can easily cause deadlocks.
111 * Therefore, you should avoid using those callbacks unless you know what you are doing.</b>
112 * </p>
113 * <h2>Stanza Filters</h2>
114 * Stanza filters allow you to define the predicates for which listeners or collectors should be invoked. For more
115 * information about stanza filters, see {@link org.jivesoftware.smack.filter}.
116 * <h2>Provider Architecture</h2>
117 * XMPP is an extensible protocol. Smack allows for this extensible with its provider architecture that allows to
118 * plug-in providers that are able to parse the various XML extension elements used for XMPP's extensibility. For
119 * more information see {@link org.jivesoftware.smack.provider}.
120 * <h2>Debugging</h2>
121 * See {@link org.jivesoftware.smack.debugger} for Smack's API to debug XMPP connections.
122 * <h2>Modular Connection Architecture</h2>
123 * Smack's new modular connection architecture will one day replace the monolithic architecture. Its main entry
124 * point {@link org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection} has more information.
125 *
126 * @author Matt Tucker
127 * @author Guenther Niess
128 * @author Florian Schmaus
129 */
130public interface XMPPConnection {
131
132    /**
133     * Returns the XMPP Domain of the service provided by the XMPP server and used for this connection. After
134     * authenticating with the server the returned value may be different.
135     *
136     * @return the XMPP domain of this XMPP session.
137     */
138    DomainBareJid getXMPPServiceDomain();
139
140    /**
141     * Returns the host name of the server where the XMPP server is running. This would be the
142     * IP address of the server or a name that may be resolved by a DNS server.
143     *
144     * @return the host name of the server where the XMPP server is running or null if not yet connected.
145     */
146    String getHost();
147
148    /**
149     * Returns the port number of the XMPP server for this connection. The default port
150     * for normal connections is 5222.
151     *
152     * @return the port number of the XMPP server or 0 if not yet connected.
153     */
154    int getPort();
155
156    /**
157     * Returns the full XMPP address of the user that is logged in to the connection or
158     * <code>null</code> if not logged in yet. An XMPP address is in the form
159     * username@server/resource.
160     *
161     * @return the full XMPP address of the user logged in.
162     */
163    EntityFullJid getUser();
164
165    /**
166     * Returns the local address currently in use for this connection, or <code>null</code> if
167     * this is invalid for the type of underlying connection.
168     *
169     * @return the local address currently in use for this connection
170     */
171    InetAddress getLocalAddress();
172
173    /**
174     * Returns the stream ID for this connection, which is the value set by the server
175     * when opening an XMPP stream. This value will be <code>null</code> if not connected to the server.
176     *
177     * @return the ID of this connection returned from the XMPP server or <code>null</code> if
178     *      not connected to the server.
179     * @see <a href="http://xmpp.org/rfcs/rfc6120.html#streams-attr-id">RFC 6120 § 4.7.3. id</a>
180     */
181    String getStreamId();
182
183    /**
184     * Returns true if currently connected to the XMPP server.
185     *
186     * @return true if connected.
187     */
188    boolean isConnected();
189
190    /**
191     * Returns true if currently authenticated by successfully calling the login method.
192     *
193     * @return true if authenticated.
194     */
195    boolean isAuthenticated();
196
197    /**
198     * Returns true if currently authenticated anonymously.
199     *
200     * @return true if authenticated anonymously.
201     */
202    boolean isAnonymous();
203
204    /**
205     * Returns true if the connection to the server has successfully negotiated encryption.
206     *
207     * @return true if a secure connection to the server.
208     */
209    boolean isSecureConnection();
210
211    /**
212     * Returns true if network traffic is being compressed. When using stream compression network
213     * traffic can be reduced up to 90%. Therefore, stream compression is ideal when using a slow
214     * speed network connection. However, the server will need to use more CPU time in order to
215     * un/compress network data so under high load the server performance might be affected.
216     *
217     * @return true if network traffic is being compressed.
218     */
219    boolean isUsingCompression();
220
221    StanzaFactory getStanzaFactory();
222
223    /**
224     * Sends the specified stanza to the server.
225     *
226     * @param stanza the stanza to send.
227     * @throws NotConnectedException if the connection is not connected.
228     * @throws InterruptedException if the calling thread was interrupted.
229     * */
230    void sendStanza(Stanza stanza) throws NotConnectedException, InterruptedException;
231
232    void sendStanzaNonBlocking(Stanza stanza) throws NotConnectedException, OutgoingQueueFullException;
233
234    /**
235     * Try to send the given stanza. Returns {@code true} if the stanza was successfully put into the outgoing stanza
236     * queue, otherwise, if {@code false} is returned, the stanza could not be scheduled for sending (for example
237     * because the outgoing element queue is full). Note that this means that the stanza possibly was not put onto the
238     * wire, even if {@code true} is returned, it just has been successfully scheduled for sending.
239     * <p>
240     * <b>Note:</b> Implementations are not required to provide that functionality. In that case this method is mapped
241     * to {@link #sendStanza(Stanza)} and will possibly block until the stanza could be scheduled for sending.
242     * </p>
243     *
244     * @param stanza the stanza to send.
245     * @return {@code true} if the stanza was successfully scheduled to be sent, {@code false} otherwise.
246     * @throws NotConnectedException if the connection is not connected.
247     * @since 4.4.0
248     * @deprecated use {@link #sendStanzaNonBlocking(Stanza)} instead.
249     */
250    // TODO: Remove in Smack 4.7.
251    @Deprecated
252    boolean trySendStanza(Stanza stanza) throws NotConnectedException;
253
254    /**
255     * Try to send the given stanza. Returns {@code true} if the stanza was successfully put into the outgoing stanza
256     * queue within the given timeout period, otherwise, if {@code false} is returned, the stanza could not be scheduled
257     * for sending (for example because the outgoing element queue is full). Note that this means that the stanza
258     * possibly was not put onto the wire, even if {@code true} is returned, it just has been successfully scheduled for
259     * sending.
260     * <p>
261     * <b>Note:</b> Implementations are not required to provide that functionality. In that case this method is mapped
262     * to {@link #sendStanza(Stanza)} and will possibly block until the stanza could be scheduled for sending.
263     * </p>
264     *
265     * @param stanza the stanza to send.
266     * @param timeout how long to wait before giving up, in units of {@code unit}.
267     * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter.
268     * @return {@code true} if the stanza was successfully scheduled to be sent, {@code false} otherwise.
269     * @throws NotConnectedException if the connection is not connected.
270     * @throws InterruptedException if the calling thread was interrupted.
271     * @since 4.4.0
272     * @deprecated use {@link #sendStanzaNonBlocking(Stanza)} instead.
273     */
274    // TODO: Remove in Smack 4.7.
275    @Deprecated
276    boolean trySendStanza(Stanza stanza, long timeout, TimeUnit unit)  throws NotConnectedException, InterruptedException;
277
278    /**
279     * Send a Nonza.
280     * <p>
281     * <b>This method is not meant for end-user usage!</b> It allows sending plain stream elements, which should not be
282     * done by a user manually. <b>Doing so may result in a unstable or unusable connection.</b> Certain Smack APIs use
283     * this method to send plain stream elements.
284     * </p>
285     *
286     * @param nonza the Nonza to send.
287     * @throws NotConnectedException if the XMPP connection is not connected.
288     * @throws InterruptedException if the calling thread was interrupted.
289     */
290    void sendNonza(Nonza nonza) throws NotConnectedException, InterruptedException;
291
292    void sendNonzaNonBlocking(Nonza stanza) throws NotConnectedException, OutgoingQueueFullException;
293
294    /**
295     * Adds a connection listener to this connection that will be notified when
296     * the connection closes or fails.
297     *
298     * @param connectionListener a connection listener.
299     */
300    void addConnectionListener(ConnectionListener connectionListener);
301
302    /**
303     * Removes a connection listener from this connection.
304     *
305     * @param connectionListener a connection listener.
306     */
307    void removeConnectionListener(ConnectionListener connectionListener);
308
309    /**
310     * Send an IQ request and wait for the response.
311     *
312     * @param request the IQ request
313     * @param <I> the type of the expected result IQ.
314     * @return an IQ with type 'result'
315     * @throws NoResponseException if there was no response from the remote entity.
316     * @throws XMPPErrorException if there was an XMPP error returned.
317     * @throws NotConnectedException if the XMPP connection is not connected.
318     * @throws InterruptedException if the calling thread was interrupted.
319     * @since 4.3
320     */
321    @SuppressWarnings("TypeParameterUnusedInFormals")
322    <I extends IQ> I sendIqRequestAndWaitForResponse(IQ request)
323            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;
324
325    /**
326     * Creates a new stanza collector collecting IQ responses that are replies to the IQ <code>request</code>.
327     * Does also send the <code>request</code> IQ. The stanza filter for the collector is an
328     * {@link IQReplyFilter}, guaranteeing that stanza id and JID in the 'from' address have
329     * expected values.
330     *
331     * @param request the IQ request to filter responses from
332     * @return a new stanza collector.
333     * @throws NotConnectedException if the XMPP connection is not connected.
334     * @throws InterruptedException if the calling thread was interrupted.
335     */
336    StanzaCollector createStanzaCollectorAndSend(IQ request) throws NotConnectedException, InterruptedException;
337
338    /**
339     * Creates a new stanza collector for this connection. A stanza filter determines
340     * which stanzas will be accumulated by the collector. A StanzaCollector is
341     * more suitable to use than a {@link StanzaListener} when you need to wait for
342     * a specific result.
343     *
344     * @param stanzaFilter the stanza filter to use.
345     * @param stanza the stanza to send right after the collector got created
346     * @return a new stanza collector.
347     * @throws InterruptedException if the calling thread was interrupted.
348     * @throws NotConnectedException if the XMPP connection is not connected.
349     */
350    StanzaCollector createStanzaCollectorAndSend(StanzaFilter stanzaFilter, Stanza stanza)
351                    throws NotConnectedException, InterruptedException;
352
353    /**
354     * Creates a new stanza collector for this connection. A stanza filter
355     * determines which stanzas will be accumulated by the collector. A
356     * StanzaCollector is more suitable to use than a {@link StanzaListener}
357     * when you need to wait for a specific result.
358     * <p>
359     * <b>Note:</b> If you send a Stanza right after using this method, then
360     * consider using
361     * {@link #createStanzaCollectorAndSend(StanzaFilter, Stanza)} instead.
362     * Otherwise make sure cancel the StanzaCollector in every case, e.g. even
363     * if an exception is thrown, or otherwise you may leak the StanzaCollector.
364     * </p>
365     *
366     * @param stanzaFilter the stanza filter to use.
367     * @return a new stanza collector.
368     */
369    StanzaCollector createStanzaCollector(StanzaFilter stanzaFilter);
370
371    /**
372     * Create a new stanza collector with the given stanza collector configuration.
373     * <p>
374     * Please make sure to cancel the collector when it is no longer required. See also
375     * {@link #createStanzaCollector(StanzaFilter)}.
376     * </p>
377     *
378     * @param configuration the stanza collector configuration.
379     * @return a new stanza collector.
380     * @since 4.1
381     */
382    StanzaCollector createStanzaCollector(StanzaCollector.Configuration configuration);
383
384    /**
385     * Remove a stanza collector of this connection.
386     *
387     * @param collector a stanza collectors which was created for this connection.
388     */
389    void removeStanzaCollector(StanzaCollector collector);
390
391    /**
392     * Registers a stanza listener with this connection. The listener will be invoked when a (matching) incoming stanza
393     * is received. The stanza filter determines which stanzas will be delivered to the listener. It is guaranteed that
394     * the same listener will not be invoked concurrently and the order of invocation will reflect the order in
395     * which the stanzas have been received. If the same stanza listener is added again with a different filter, only
396     * the new filter will be used.
397     *
398     * @param stanzaListener the stanza listener to notify of new received stanzas.
399     * @param stanzaFilter the stanza filter to use.
400     * @since 4.4.0
401     */
402    void addStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter);
403
404    /**
405     * Removes a stanza listener for received stanzas from this connection.
406     *
407     * @param stanzaListener the stanza listener to remove.
408     * @return true if the stanza listener was removed.
409     * @since 4.4.0
410     */
411    boolean removeStanzaListener(StanzaListener stanzaListener);
412
413    /**
414     * Registers a <b>synchronous</b> stanza listener with this connection. A stanza listener will be invoked only when
415     * an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener. If
416     * the same stanza listener is added again with a different filter, only the new filter will be used.
417     * <p>
418     * <b>Important:</b> This stanza listeners will be called in the same <i>single</i> thread that processes all
419     * incoming stanzas. Only use this kind of stanza filter if it does not perform any XMPP activity that waits for a
420     * response. Consider using {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)} when possible, i.e. when
421     * the invocation order doesn't have to be the same as the order of the arriving stanzas. If the order of the
422     * arriving stanzas, consider using a {@link StanzaCollector} when possible.
423     * </p>
424     *
425     * @param stanzaListener the stanza listener to notify of new received stanzas.
426     * @param stanzaFilter the stanza filter to use.
427     * @since 4.1
428     */
429    void addSyncStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter);
430
431    /**
432     * Removes a stanza listener for received stanzas from this connection.
433     *
434     * @param stanzaListener the stanza listener to remove.
435     * @return true if the stanza listener was removed
436     * @since 4.1
437     */
438    boolean removeSyncStanzaListener(StanzaListener stanzaListener);
439
440    /**
441     * Registers an <b>asynchronous</b> stanza listener with this connection. A stanza listener will be invoked only
442     * when an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener.
443     * If the same stanza listener is added again with a different filter, only the new filter will be used.
444     * <p>
445     * Unlike {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)} stanza listeners added with this method will be
446     * invoked asynchronously in their own thread. Use this method if the order of the stanza listeners must not depend
447     * on the order how the stanzas where received.
448     * </p>
449     *
450     * @param stanzaListener the stanza listener to notify of new received stanzas.
451     * @param stanzaFilter the stanza filter to use.
452     * @since 4.1
453    */
454    void addAsyncStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter);
455
456    /**
457     * Removes an <b>asynchronous</b> stanza listener for received stanzas from this connection.
458     *
459     * @param stanzaListener the stanza listener to remove.
460     * @return true if the stanza listener was removed
461     * @since 4.1
462     */
463    boolean removeAsyncStanzaListener(StanzaListener stanzaListener);
464
465    /**
466     * Registers a stanza listener with this connection. The listener will be
467     * notified of every stanza that this connection sends. A stanza filter determines
468     * which stanzas will be delivered to the listener. Note that the thread
469     * that writes stanzas will be used to invoke the listeners. Therefore, each
470     * stanza listener should complete all operations quickly or use a different
471     * thread for processing.
472     *
473     * @param stanzaListener the stanza listener to notify of sent stanzas.
474     * @param stanzaFilter   the stanza filter to use.
475     */
476    void addStanzaSendingListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter);
477
478    /**
479     * Removes a stanza listener for sending stanzas from this connection.
480     *
481     * @param stanzaListener the stanza listener to remove.
482     */
483    void removeStanzaSendingListener(StanzaListener stanzaListener);
484
485    /**
486     * Registers a stanza interceptor with this connection. The interceptor will be
487     * invoked every time a stanza is about to be sent by this connection. Interceptors
488     * may modify the stanza to be sent. A stanza filter determines which stanzas
489     * will be delivered to the interceptor.
490     *
491     * <p>
492     * NOTE: For a similar functionality on incoming stanzas, see {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)}.
493     * </p>
494     *
495     * @param messageInterceptor the stanza interceptor to notify of stanzas about to be sent.
496     * @param messageFilter      the stanza filter to use.
497     */
498    void addMessageInterceptor(Consumer<MessageBuilder> messageInterceptor, Predicate<Message> messageFilter);
499
500    /**
501     * Removes a message interceptor.
502     *
503     * @param messageInterceptor the message interceptor to remove.
504     */
505    void removeMessageInterceptor(Consumer<MessageBuilder> messageInterceptor);
506
507    /**
508     * Registers a stanza interceptor with this connection. The interceptor will be
509     * invoked every time a stanza is about to be sent by this connection. Interceptors
510     * may modify the stanza to be sent. A stanza filter determines which stanzas
511     * will be delivered to the interceptor.
512     *
513     * <p>
514     * NOTE: For a similar functionality on incoming stanzas, see {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)}.
515     * </p>
516     *
517     * @param presenceInterceptor the stanza interceptor to notify of stanzas about to be sent.
518     * @param presenceFilter      the stanza filter to use.
519     */
520    void addPresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor, Predicate<Presence> presenceFilter);
521
522    /**
523     * Removes a presence interceptor.
524     *
525     * @param presenceInterceptor the stanza interceptor to remove.
526     */
527    void removePresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor);
528    /**
529     * Returns the current value of the reply timeout in milliseconds for request for this
530     * XMPPConnection instance.
531     *
532     * @return the reply timeout in milliseconds
533     */
534    long getReplyTimeout();
535
536    /**
537     * Set the stanza reply timeout in milliseconds. In most cases, Smack will throw a
538     * {@link NoResponseException} if no reply to a request was received within the timeout period.
539     *
540     * @param timeout for a reply in milliseconds
541     */
542    void setReplyTimeout(long timeout);
543
544    /**
545     * Get the connection counter of this XMPPConnection instance. Those can be used as ID to
546     * identify the connection, but beware that the ID may not be unique if you create more then
547     * <code>2*Integer.MAX_VALUE</code> instances as the counter could wrap.
548     *
549     * @return the connection counter of this XMPPConnection
550     */
551    int getConnectionCounter();
552
553    enum FromMode {
554        /**
555         * Leave the 'from' attribute unchanged. This is the behavior of Smack &lt; 4.0
556         */
557        UNCHANGED,
558        /**
559         * Omit the 'from' attribute. According to RFC 6120 8.1.2.1 1. XMPP servers "MUST (...)
560         * override the 'from' attribute specified by the client". It is therefore safe to specify
561         * FromMode.OMITTED here.
562         */
563        OMITTED,
564        /**
565         * Set the from to the clients full JID. This is usually not required.
566         */
567        USER
568    }
569
570    /**
571     * Set the FromMode for this connection instance. Defines how the 'from' attribute of outgoing
572     * stanzas should be populated by Smack.
573     *
574     * @param fromMode TODO javadoc me please
575     */
576    void setFromMode(FromMode fromMode);
577
578    /**
579     * Get the currently active FromMode.
580     *
581     * @return the currently active {@link FromMode}
582     */
583    FromMode getFromMode();
584
585    /**
586     * Get the feature stanza extensions for a given stream feature of the
587     * server, or <code>null</code> if the server doesn't support that feature.
588     *
589     * @param <F> {@link ExtensionElement} type of the feature.
590     * @param qname the qualified name of the XML element of feature.
591     * @return a stanza extensions of the feature or <code>null</code>
592     * @since 4.4
593     */
594    @SuppressWarnings("TypeParameterUnusedInFormals")
595    <F extends XmlElement> F getFeature(QName qname);
596
597    /**
598     * Get the feature stanza extensions for a given stream feature of the
599     * server, or <code>null</code> if the server doesn't support that feature.
600     *
601     * @param <F> {@link ExtensionElement} type of the feature.
602     * @param featureClass the class of the feature.
603     * @return a stanza extensions of the feature or <code>null</code>
604     * @since 4.4
605     */
606    default <F extends XmlElement> F getFeature(Class<F> featureClass) {
607        QName qname = XmppElementUtil.getQNameFor(featureClass);
608        return getFeature(qname);
609    }
610
611    /**
612     * Return true if the server supports the given stream feature.
613     *
614     * @param element TODO javadoc me please
615     * @param namespace TODO javadoc me please
616     * @return true if the server supports the stream feature.
617     */
618    default boolean hasFeature(String element, String namespace) {
619        QName qname = new QName(namespace, element);
620        return hasFeature(qname);
621    }
622
623    /**
624     * Return true if the server supports the given stream feature.
625     *
626     * @param qname the qualified name of the XML element of feature.
627     * @return true if the server supports the stream feature.
628     */
629    boolean hasFeature(QName qname);
630
631    /**
632     * Send an IQ request asynchronously. The connection's default reply timeout will be used.
633     *
634     * @param request the IQ request to send.
635     * @return a SmackFuture for the response.
636     */
637    SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request);
638
639    /**
640     * Send an IQ request asynchronously.
641     *
642     * @param request the IQ request to send.
643     * @param timeout the reply timeout in milliseconds.
644     * @return a SmackFuture for the response.
645     */
646    SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request, long timeout);
647
648    /**
649     * Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter. The
650     * connection's default reply timeout will be used.
651     *
652     * @param stanza the stanza to send.
653     * @param replyFilter the filter used for the response stanza.
654     * @param <S> the type of the stanza to send.
655     * @return a SmackFuture for the response.
656     */
657    <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, StanzaFilter replyFilter);
658
659    /**
660     * Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter.
661     *
662     * @param stanza the stanza to send.
663     * @param replyFilter the filter used for the response stanza.
664     * @param timeout the reply timeout in milliseconds.
665     * @param <S> the type of the stanza to send.
666     * @return a SmackFuture for the response.
667     */
668    <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, StanzaFilter replyFilter, long timeout);
669
670    /**
671     * Add a callback that is called exactly once and synchronously with the incoming stanza that matches the given
672     * stanza filter.
673     *
674     * @param callback the callback invoked once the stanza filter matches a stanza.
675     * @param stanzaFilter the filter to match stanzas or null to match all.
676     */
677    void addOneTimeSyncCallback(StanzaListener callback, StanzaFilter stanzaFilter);
678
679    /**
680     * Register an IQ request handler with this connection.
681     * <p>
682     * IQ request handler process incoming IQ requests, i.e. incoming IQ stanzas of type 'get' or 'set', and return a result.
683     * </p>
684     * @param iqRequestHandler the IQ request handler to register.
685     * @return the previously registered IQ request handler or null.
686     */
687    IQRequestHandler registerIQRequestHandler(IQRequestHandler iqRequestHandler);
688
689    /**
690     * Convenience method for {@link #unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type)}.
691     *
692     * @param iqRequestHandler TODO javadoc me please
693     * @return the previously registered IQ request handler or null.
694     */
695    IQRequestHandler unregisterIQRequestHandler(IQRequestHandler iqRequestHandler);
696
697    /**
698     * Unregister an IQ request handler with this connection.
699     *
700     * @param element the IQ element the IQ request handler is responsible for.
701     * @param namespace the IQ namespace the IQ request handler is responsible for.
702     * @param type the IQ type the IQ request handler is responsible for.
703     * @return the previously registered IQ request handler or null.
704     */
705    IQRequestHandler unregisterIQRequestHandler(String element, String namespace, IQ.Type type);
706
707    /**
708     * Returns the timestamp in milliseconds when the last stanza was received.
709     *
710     * @return the timestamp in milliseconds
711     */
712    long getLastStanzaReceived();
713}