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 <I extends IQ> I sendIqRequestAndWaitForResponse(IQ request) 322 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException; 323 324 /** 325 * Creates a new stanza collector collecting IQ responses that are replies to the IQ <code>request</code>. 326 * Does also send the <code>request</code> IQ. The stanza filter for the collector is an 327 * {@link IQReplyFilter}, guaranteeing that stanza id and JID in the 'from' address have 328 * expected values. 329 * 330 * @param request the IQ request to filter responses from 331 * @return a new stanza collector. 332 * @throws NotConnectedException if the XMPP connection is not connected. 333 * @throws InterruptedException if the calling thread was interrupted. 334 */ 335 StanzaCollector createStanzaCollectorAndSend(IQ request) throws NotConnectedException, InterruptedException; 336 337 /** 338 * Creates a new stanza collector for this connection. A stanza filter determines 339 * which stanzas will be accumulated by the collector. A StanzaCollector is 340 * more suitable to use than a {@link StanzaListener} when you need to wait for 341 * a specific result. 342 * 343 * @param stanzaFilter the stanza filter to use. 344 * @param stanza the stanza to send right after the collector got created 345 * @return a new stanza collector. 346 * @throws InterruptedException if the calling thread was interrupted. 347 * @throws NotConnectedException if the XMPP connection is not connected. 348 */ 349 StanzaCollector createStanzaCollectorAndSend(StanzaFilter stanzaFilter, Stanza stanza) 350 throws NotConnectedException, InterruptedException; 351 352 /** 353 * Creates a new stanza collector for this connection. A stanza filter 354 * determines which stanzas will be accumulated by the collector. A 355 * StanzaCollector is more suitable to use than a {@link StanzaListener} 356 * when you need to wait for a specific result. 357 * <p> 358 * <b>Note:</b> If you send a Stanza right after using this method, then 359 * consider using 360 * {@link #createStanzaCollectorAndSend(StanzaFilter, Stanza)} instead. 361 * Otherwise make sure cancel the StanzaCollector in every case, e.g. even 362 * if an exception is thrown, or otherwise you may leak the StanzaCollector. 363 * </p> 364 * 365 * @param stanzaFilter the stanza filter to use. 366 * @return a new stanza collector. 367 */ 368 StanzaCollector createStanzaCollector(StanzaFilter stanzaFilter); 369 370 /** 371 * Create a new stanza collector with the given stanza collector configuration. 372 * <p> 373 * Please make sure to cancel the collector when it is no longer required. See also 374 * {@link #createStanzaCollector(StanzaFilter)}. 375 * </p> 376 * 377 * @param configuration the stanza collector configuration. 378 * @return a new stanza collector. 379 * @since 4.1 380 */ 381 StanzaCollector createStanzaCollector(StanzaCollector.Configuration configuration); 382 383 /** 384 * Remove a stanza collector of this connection. 385 * 386 * @param collector a stanza collectors which was created for this connection. 387 */ 388 void removeStanzaCollector(StanzaCollector collector); 389 390 /** 391 * Registers a stanza listener with this connection. The listener will be invoked when a (matching) incoming stanza 392 * is received. The stanza filter determines which stanzas will be delivered to the listener. It is guaranteed that 393 * the same listener will not be invoked concurrently and the order of invocation will reflect the order in 394 * which the stanzas have been received. If the same stanza listener is added again with a different filter, only 395 * the new filter will be used. 396 * 397 * @param stanzaListener the stanza listener to notify of new received stanzas. 398 * @param stanzaFilter the stanza filter to use. 399 * @since 4.4.0 400 */ 401 void addStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter); 402 403 /** 404 * Removes a stanza listener for received stanzas from this connection. 405 * 406 * @param stanzaListener the stanza listener to remove. 407 * @return true if the stanza listener was removed. 408 * @since 4.4.0 409 */ 410 boolean removeStanzaListener(StanzaListener stanzaListener); 411 412 /** 413 * Registers a <b>synchronous</b> stanza listener with this connection. A stanza listener will be invoked only when 414 * an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener. If 415 * the same stanza listener is added again with a different filter, only the new filter will be used. 416 * <p> 417 * <b>Important:</b> This stanza listeners will be called in the same <i>single</i> thread that processes all 418 * incoming stanzas. Only use this kind of stanza filter if it does not perform any XMPP activity that waits for a 419 * response. Consider using {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)} when possible, i.e. when 420 * the invocation order doesn't have to be the same as the order of the arriving stanzas. If the order of the 421 * arriving stanzas, consider using a {@link StanzaCollector} when possible. 422 * </p> 423 * 424 * @param stanzaListener the stanza listener to notify of new received stanzas. 425 * @param stanzaFilter the stanza filter to use. 426 * @since 4.1 427 */ 428 void addSyncStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter); 429 430 /** 431 * Removes a stanza listener for received stanzas from this connection. 432 * 433 * @param stanzaListener the stanza listener to remove. 434 * @return true if the stanza listener was removed 435 * @since 4.1 436 */ 437 boolean removeSyncStanzaListener(StanzaListener stanzaListener); 438 439 /** 440 * Registers an <b>asynchronous</b> stanza listener with this connection. A stanza listener will be invoked only 441 * when an incoming stanza is received. A stanza filter determines which stanzas will be delivered to the listener. 442 * If the same stanza listener is added again with a different filter, only the new filter will be used. 443 * <p> 444 * Unlike {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)} stanza listeners added with this method will be 445 * invoked asynchronously in their own thread. Use this method if the order of the stanza listeners must not depend 446 * on the order how the stanzas where received. 447 * </p> 448 * 449 * @param stanzaListener the stanza listener to notify of new received stanzas. 450 * @param stanzaFilter the stanza filter to use. 451 * @since 4.1 452 */ 453 void addAsyncStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter); 454 455 /** 456 * Removes an <b>asynchronous</b> stanza listener for received stanzas from this connection. 457 * 458 * @param stanzaListener the stanza listener to remove. 459 * @return true if the stanza listener was removed 460 * @since 4.1 461 */ 462 boolean removeAsyncStanzaListener(StanzaListener stanzaListener); 463 464 /** 465 * Registers a stanza listener with this connection. The listener will be 466 * notified of every stanza that this connection sends. A stanza filter determines 467 * which stanzas will be delivered to the listener. Note that the thread 468 * that writes stanzas will be used to invoke the listeners. Therefore, each 469 * stanza listener should complete all operations quickly or use a different 470 * thread for processing. 471 * 472 * @param stanzaListener the stanza listener to notify of sent stanzas. 473 * @param stanzaFilter the stanza filter to use. 474 */ 475 void addStanzaSendingListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter); 476 477 /** 478 * Removes a stanza listener for sending stanzas from this connection. 479 * 480 * @param stanzaListener the stanza listener to remove. 481 */ 482 void removeStanzaSendingListener(StanzaListener stanzaListener); 483 484 /** 485 * Registers a stanza interceptor with this connection. The interceptor will be 486 * invoked every time a stanza is about to be sent by this connection. Interceptors 487 * may modify the stanza to be sent. A stanza filter determines which stanzas 488 * will be delivered to the interceptor. 489 * 490 * <p> 491 * NOTE: For a similar functionality on incoming stanzas, see {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)}. 492 * </p> 493 * 494 * @param messageInterceptor the stanza interceptor to notify of stanzas about to be sent. 495 * @param messageFilter the stanza filter to use. 496 */ 497 void addMessageInterceptor(Consumer<MessageBuilder> messageInterceptor, Predicate<Message> messageFilter); 498 499 /** 500 * Removes a message interceptor. 501 * 502 * @param messageInterceptor the message interceptor to remove. 503 */ 504 void removeMessageInterceptor(Consumer<MessageBuilder> messageInterceptor); 505 506 /** 507 * Registers a stanza interceptor with this connection. The interceptor will be 508 * invoked every time a stanza is about to be sent by this connection. Interceptors 509 * may modify the stanza to be sent. A stanza filter determines which stanzas 510 * will be delivered to the interceptor. 511 * 512 * <p> 513 * NOTE: For a similar functionality on incoming stanzas, see {@link #addAsyncStanzaListener(StanzaListener, StanzaFilter)}. 514 * </p> 515 * 516 * @param presenceInterceptor the stanza interceptor to notify of stanzas about to be sent. 517 * @param presenceFilter the stanza filter to use. 518 */ 519 void addPresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor, Predicate<Presence> presenceFilter); 520 521 /** 522 * Removes a presence interceptor. 523 * 524 * @param presenceInterceptor the stanza interceptor to remove. 525 */ 526 void removePresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor); 527 /** 528 * Returns the current value of the reply timeout in milliseconds for request for this 529 * XMPPConnection instance. 530 * 531 * @return the reply timeout in milliseconds 532 */ 533 long getReplyTimeout(); 534 535 /** 536 * Set the stanza reply timeout in milliseconds. In most cases, Smack will throw a 537 * {@link NoResponseException} if no reply to a request was received within the timeout period. 538 * 539 * @param timeout for a reply in milliseconds 540 */ 541 void setReplyTimeout(long timeout); 542 543 /** 544 * Get the connection counter of this XMPPConnection instance. Those can be used as ID to 545 * identify the connection, but beware that the ID may not be unique if you create more then 546 * <code>2*Integer.MAX_VALUE</code> instances as the counter could wrap. 547 * 548 * @return the connection counter of this XMPPConnection 549 */ 550 int getConnectionCounter(); 551 552 enum FromMode { 553 /** 554 * Leave the 'from' attribute unchanged. This is the behavior of Smack < 4.0 555 */ 556 UNCHANGED, 557 /** 558 * Omit the 'from' attribute. According to RFC 6120 8.1.2.1 1. XMPP servers "MUST (...) 559 * override the 'from' attribute specified by the client". It is therefore safe to specify 560 * FromMode.OMITTED here. 561 */ 562 OMITTED, 563 /** 564 * Set the from to the clients full JID. This is usually not required. 565 */ 566 USER 567 } 568 569 /** 570 * Set the FromMode for this connection instance. Defines how the 'from' attribute of outgoing 571 * stanzas should be populated by Smack. 572 * 573 * @param fromMode TODO javadoc me please 574 */ 575 void setFromMode(FromMode fromMode); 576 577 /** 578 * Get the currently active FromMode. 579 * 580 * @return the currently active {@link FromMode} 581 */ 582 FromMode getFromMode(); 583 584 /** 585 * Get the feature stanza extensions for a given stream feature of the 586 * server, or <code>null</code> if the server doesn't support that feature. 587 * 588 * @param <F> {@link ExtensionElement} type of the feature. 589 * @param qname the qualified name of the XML element of feature. 590 * @return a stanza extensions of the feature or <code>null</code> 591 * @since 4.4 592 */ 593 <F extends XmlElement> F getFeature(QName qname); 594 595 /** 596 * Get the feature stanza extensions for a given stream feature of the 597 * server, or <code>null</code> if the server doesn't support that feature. 598 * 599 * @param <F> {@link ExtensionElement} type of the feature. 600 * @param featureClass the class of the feature. 601 * @return a stanza extensions of the feature or <code>null</code> 602 * @since 4.4 603 */ 604 default <F extends XmlElement> F getFeature(Class<F> featureClass) { 605 QName qname = XmppElementUtil.getQNameFor(featureClass); 606 return getFeature(qname); 607 } 608 609 /** 610 * Return true if the server supports the given stream feature. 611 * 612 * @param element TODO javadoc me please 613 * @param namespace TODO javadoc me please 614 * @return true if the server supports the stream feature. 615 */ 616 default boolean hasFeature(String element, String namespace) { 617 QName qname = new QName(namespace, element); 618 return hasFeature(qname); 619 } 620 621 /** 622 * Return true if the server supports the given stream feature. 623 * 624 * @param qname the qualified name of the XML element of feature. 625 * @return true if the server supports the stream feature. 626 */ 627 boolean hasFeature(QName qname); 628 629 /** 630 * Send an IQ request asynchronously. The connection's default reply timeout will be used. 631 * 632 * @param request the IQ request to send. 633 * @return a SmackFuture for the response. 634 */ 635 SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request); 636 637 /** 638 * Send an IQ request asynchronously. 639 * 640 * @param request the IQ request to send. 641 * @param timeout the reply timeout in milliseconds. 642 * @return a SmackFuture for the response. 643 */ 644 SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request, long timeout); 645 646 /** 647 * Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter. The 648 * connection's default reply timeout will be used. 649 * 650 * @param stanza the stanza to send. 651 * @param replyFilter the filter used for the response stanza. 652 * @param <S> the type of the stanza to send. 653 * @return a SmackFuture for the response. 654 */ 655 <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, StanzaFilter replyFilter); 656 657 /** 658 * Send a stanza asynchronously, waiting for exactly one response stanza using the given reply filter. 659 * 660 * @param stanza the stanza to send. 661 * @param replyFilter the filter used for the response stanza. 662 * @param timeout the reply timeout in milliseconds. 663 * @param <S> the type of the stanza to send. 664 * @return a SmackFuture for the response. 665 */ 666 <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, StanzaFilter replyFilter, long timeout); 667 668 /** 669 * Add a callback that is called exactly once and synchronously with the incoming stanza that matches the given 670 * stanza filter. 671 * 672 * @param callback the callback invoked once the stanza filter matches a stanza. 673 * @param stanzaFilter the filter to match stanzas or null to match all. 674 */ 675 void addOneTimeSyncCallback(StanzaListener callback, StanzaFilter stanzaFilter); 676 677 /** 678 * Register an IQ request handler with this connection. 679 * <p> 680 * IQ request handler process incoming IQ requests, i.e. incoming IQ stanzas of type 'get' or 'set', and return a result. 681 * </p> 682 * @param iqRequestHandler the IQ request handler to register. 683 * @return the previously registered IQ request handler or null. 684 */ 685 IQRequestHandler registerIQRequestHandler(IQRequestHandler iqRequestHandler); 686 687 /** 688 * Convenience method for {@link #unregisterIQRequestHandler(String, String, org.jivesoftware.smack.packet.IQ.Type)}. 689 * 690 * @param iqRequestHandler TODO javadoc me please 691 * @return the previously registered IQ request handler or null. 692 */ 693 IQRequestHandler unregisterIQRequestHandler(IQRequestHandler iqRequestHandler); 694 695 /** 696 * Unregister an IQ request handler with this connection. 697 * 698 * @param element the IQ element the IQ request handler is responsible for. 699 * @param namespace the IQ namespace the IQ request handler is responsible for. 700 * @param type the IQ type the IQ request handler is responsible for. 701 * @return the previously registered IQ request handler or null. 702 */ 703 IQRequestHandler unregisterIQRequestHandler(String element, String namespace, IQ.Type type); 704 705 /** 706 * Returns the timestamp in milliseconds when the last stanza was received. 707 * 708 * @return the timestamp in milliseconds 709 */ 710 long getLastStanzaReceived(); 711}