1package eu.siacs.conversations.xmpp;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.OutputStream;
6import java.math.BigInteger;
7import java.net.Socket;
8import java.net.UnknownHostException;
9import java.security.KeyManagementException;
10import java.security.KeyStore;
11import java.security.KeyStoreException;
12import java.security.MessageDigest;
13import java.security.NoSuchAlgorithmException;
14import java.security.SecureRandom;
15import java.security.cert.CertPathValidatorException;
16import java.security.cert.CertificateException;
17import java.security.cert.X509Certificate;
18import java.util.HashSet;
19import java.util.Hashtable;
20import java.util.List;
21
22import javax.net.ssl.SSLContext;
23import javax.net.ssl.SSLSocket;
24import javax.net.ssl.SSLSocketFactory;
25import javax.net.ssl.TrustManager;
26import javax.net.ssl.TrustManagerFactory;
27import javax.net.ssl.X509TrustManager;
28
29import org.json.JSONException;
30import org.xmlpull.v1.XmlPullParserException;
31
32import android.os.Bundle;
33import android.os.PowerManager;
34import android.os.SystemClock;
35import android.util.Log;
36import eu.siacs.conversations.entities.Account;
37import eu.siacs.conversations.utils.CryptoHelper;
38import eu.siacs.conversations.utils.DNSHelper;
39import eu.siacs.conversations.xml.Element;
40import eu.siacs.conversations.xml.Tag;
41import eu.siacs.conversations.xml.TagWriter;
42import eu.siacs.conversations.xml.XmlReader;
43import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
44import eu.siacs.conversations.xmpp.stanzas.IqPacket;
45import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
46import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
47import eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket;
48import eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket;
49import eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket;
50import eu.siacs.conversations.xmpp.stanzas.streammgmt.ResumePacket;
51
52public class XmppConnection implements Runnable {
53
54 protected Account account;
55 private static final String LOGTAG = "xmppService";
56
57 private PowerManager.WakeLock wakeLock;
58
59 private SecureRandom random = new SecureRandom();
60
61 private Socket socket;
62 private XmlReader tagReader;
63 private TagWriter tagWriter;
64
65 private boolean shouldBind = true;
66 private boolean shouldAuthenticate = true;
67 private Element streamFeatures;
68 private HashSet<String> discoFeatures = new HashSet<String>();
69
70 private String streamId = null;
71
72 private int stanzasReceived = 0;
73 private int stanzasSent = 0;
74
75 public long lastPaketReceived = 0;
76 public long lastPingSent = 0;
77 public long lastConnect = 0;
78 public long lastSessionStarted = 0;
79
80 private static final int PACKET_IQ = 0;
81 private static final int PACKET_MESSAGE = 1;
82 private static final int PACKET_PRESENCE = 2;
83
84 private Hashtable<String, PacketReceived> packetCallbacks = new Hashtable<String, PacketReceived>();
85 private OnPresencePacketReceived presenceListener = null;
86 private OnIqPacketReceived unregisteredIqListener = null;
87 private OnMessagePacketReceived messageListener = null;
88 private OnStatusChanged statusListener = null;
89 private OnTLSExceptionReceived tlsListener;
90
91 public XmppConnection(Account account, PowerManager pm) {
92 this.account = account;
93 wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
94 "XmppConnection");
95 tagReader = new XmlReader(wakeLock);
96 tagWriter = new TagWriter();
97 }
98
99 protected void changeStatus(int nextStatus) {
100 if (account.getStatus() != nextStatus) {
101 account.setStatus(nextStatus);
102 if (statusListener != null) {
103 statusListener.onStatusChanged(account);
104 }
105 }
106 }
107
108 protected void connect() {
109 Log.d(LOGTAG,account.getJid()+ ": connecting");
110 lastConnect = SystemClock.elapsedRealtime();
111 try {
112 shouldAuthenticate = shouldBind = !account.isOptionSet(Account.OPTION_REGISTER);
113 tagReader = new XmlReader(wakeLock);
114 tagWriter = new TagWriter();
115 packetCallbacks.clear();
116 this.changeStatus(Account.STATUS_CONNECTING);
117 Bundle namePort = DNSHelper.getSRVRecord(account.getServer());
118 String srvRecordServer = namePort.getString("name");
119 int srvRecordPort = namePort.getInt("port");
120 if (srvRecordServer != null) {
121 Log.d(LOGTAG, account.getJid() + ": using values from dns "
122 + srvRecordServer + ":" + srvRecordPort);
123 socket = new Socket(srvRecordServer, srvRecordPort);
124 } else {
125 socket = new Socket(account.getServer(), 5222);
126 }
127 OutputStream out = socket.getOutputStream();
128 tagWriter.setOutputStream(out);
129 InputStream in = socket.getInputStream();
130 tagReader.setInputStream(in);
131 tagWriter.beginDocument();
132 sendStartStream();
133 Tag nextTag;
134 while ((nextTag = tagReader.readTag()) != null) {
135 if (nextTag.isStart("stream")) {
136 processStream(nextTag);
137 break;
138 } else {
139 Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName());
140 return;
141 }
142 }
143 if (socket.isConnected()) {
144 socket.close();
145 }
146 } catch (UnknownHostException e) {
147 this.changeStatus(Account.STATUS_SERVER_NOT_FOUND);
148 if (wakeLock.isHeld()) {
149 wakeLock.release();
150 }
151 return;
152 } catch (IOException e) {
153 if (account.getStatus() != Account.STATUS_TLS_ERROR) {
154 this.changeStatus(Account.STATUS_OFFLINE);
155 }
156 if (wakeLock.isHeld()) {
157 wakeLock.release();
158 }
159 return;
160 } catch (XmlPullParserException e) {
161 this.changeStatus(Account.STATUS_OFFLINE);
162 Log.d(LOGTAG, "xml exception " + e.getMessage());
163 if (wakeLock.isHeld()) {
164 wakeLock.release();
165 }
166 return;
167 }
168
169 }
170
171 @Override
172 public void run() {
173 connect();
174 }
175
176 private void processStream(Tag currentTag) throws XmlPullParserException,
177 IOException {
178 Tag nextTag = tagReader.readTag();
179 while ((nextTag != null) && (!nextTag.isEnd("stream"))) {
180 if (nextTag.isStart("error")) {
181 processStreamError(nextTag);
182 } else if (nextTag.isStart("features")) {
183 processStreamFeatures(nextTag);
184 if ((streamFeatures.getChildren().size() == 1)
185 && (streamFeatures.hasChild("starttls"))
186 && (!account.isOptionSet(Account.OPTION_USETLS))) {
187 changeStatus(Account.STATUS_SERVER_REQUIRES_TLS);
188 }
189 if (account.isOptionSet(Account.OPTION_REGISTER)) {
190 Log.d(LOGTAG,account.getJid()+": trying to register");
191 }
192 } else if (nextTag.isStart("proceed")) {
193 switchOverToTls(nextTag);
194 } else if (nextTag.isStart("success")) {
195 Log.d(LOGTAG, account.getJid()
196 + ": logged in");
197 tagReader.readTag();
198 tagReader.reset();
199 sendStartStream();
200 processStream(tagReader.readTag());
201 break;
202 } else if (nextTag.isStart("failure")) {
203 tagReader.readElement(nextTag);
204 changeStatus(Account.STATUS_UNAUTHORIZED);
205 } else if (nextTag.isStart("enabled")) {
206 this.stanzasSent = 0;
207 Element enabled = tagReader.readElement(nextTag);
208 if ("true".equals(enabled.getAttribute("resume"))) {
209 this.streamId = enabled.getAttribute("id");
210 Log.d(LOGTAG,account.getJid()+": stream managment enabled (resumable)");
211 } else {
212 Log.d(LOGTAG,account.getJid()+": stream managment enabled");
213 }
214 this.lastSessionStarted = SystemClock.elapsedRealtime();
215 this.stanzasReceived = 0;
216 RequestPacket r = new RequestPacket();
217 tagWriter.writeStanzaAsync(r);
218 } else if (nextTag.isStart("resumed")) {
219 tagReader.readElement(nextTag);
220 changeStatus(Account.STATUS_ONLINE);
221 Log.d(LOGTAG,account.getJid()+": session resumed");
222 } else if (nextTag.isStart("r")) {
223 tagReader.readElement(nextTag);
224 AckPacket ack = new AckPacket(this.stanzasReceived);
225 //Log.d(LOGTAG,ack.toString());
226 tagWriter.writeStanzaAsync(ack);
227 } else if (nextTag.isStart("a")) {
228 Element ack = tagReader.readElement(nextTag);
229 lastPaketReceived = SystemClock.elapsedRealtime();
230 int serverSequence = Integer.parseInt(ack.getAttribute("h"));
231 if (serverSequence>this.stanzasSent) {
232 this.stanzasSent = serverSequence;
233 }
234 //Log.d(LOGTAG,"server ack"+ack.toString()+" ("+this.stanzasSent+")");
235 } else if (nextTag.isStart("failed")) {
236 tagReader.readElement(nextTag);
237 Log.d(LOGTAG,account.getJid()+": resumption failed");
238 streamId = null;
239 if (account.getStatus() != Account.STATUS_ONLINE) {
240 sendBindRequest();
241 }
242 } else if (nextTag.isStart("iq")) {
243 processIq(nextTag);
244 } else if (nextTag.isStart("message")) {
245 processMessage(nextTag);
246 } else if (nextTag.isStart("presence")) {
247 processPresence(nextTag);
248 } else {
249 Log.d(LOGTAG, "found unexpected tag: " + nextTag.getName()
250 + " as child of " + currentTag.getName());
251 }
252 nextTag = tagReader.readTag();
253 }
254 if (account.getStatus() == Account.STATUS_ONLINE) {
255 account.setStatus(Account.STATUS_OFFLINE);
256 if (statusListener != null) {
257 statusListener.onStatusChanged(account);
258 }
259 }
260 }
261
262 private Element processPacket(Tag currentTag, int packetType)
263 throws XmlPullParserException, IOException {
264 Element element;
265 switch (packetType) {
266 case PACKET_IQ:
267 element = new IqPacket();
268 break;
269 case PACKET_MESSAGE:
270 element = new MessagePacket();
271 break;
272 case PACKET_PRESENCE:
273 element = new PresencePacket();
274 break;
275 default:
276 return null;
277 }
278 element.setAttributes(currentTag.getAttributes());
279 Tag nextTag = tagReader.readTag();
280 while (!nextTag.isEnd(element.getName())) {
281 if (!nextTag.isNo()) {
282 Element child = tagReader.readElement(nextTag);
283 element.addChild(child);
284 }
285 nextTag = tagReader.readTag();
286 }
287 ++stanzasReceived;
288 lastPaketReceived = SystemClock.elapsedRealtime();
289 return element;
290 }
291
292 private void processIq(Tag currentTag) throws XmlPullParserException,
293 IOException {
294 IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
295 if (packetCallbacks.containsKey(packet.getId())) {
296 if (packetCallbacks.get(packet.getId()) instanceof OnIqPacketReceived) {
297 ((OnIqPacketReceived) packetCallbacks.get(packet.getId()))
298 .onIqPacketReceived(account, packet);
299 }
300
301 packetCallbacks.remove(packet.getId());
302 } else if (this.unregisteredIqListener != null) {
303 this.unregisteredIqListener.onIqPacketReceived(account, packet);
304 }
305 }
306
307 private void processMessage(Tag currentTag) throws XmlPullParserException,
308 IOException {
309 MessagePacket packet = (MessagePacket) processPacket(currentTag,
310 PACKET_MESSAGE);
311 String id = packet.getAttribute("id");
312 if ((id != null) && (packetCallbacks.containsKey(id))) {
313 if (packetCallbacks.get(id) instanceof OnMessagePacketReceived) {
314 ((OnMessagePacketReceived) packetCallbacks.get(id))
315 .onMessagePacketReceived(account, packet);
316 }
317 packetCallbacks.remove(id);
318 } else if (this.messageListener != null) {
319 this.messageListener.onMessagePacketReceived(account, packet);
320 }
321 }
322
323 private void processPresence(Tag currentTag) throws XmlPullParserException,
324 IOException {
325 PresencePacket packet = (PresencePacket) processPacket(currentTag,
326 PACKET_PRESENCE);
327 String id = packet.getAttribute("id");
328 if ((id != null) && (packetCallbacks.containsKey(id))) {
329 if (packetCallbacks.get(id) instanceof OnPresencePacketReceived) {
330 ((OnPresencePacketReceived) packetCallbacks.get(id))
331 .onPresencePacketReceived(account, packet);
332 }
333 packetCallbacks.remove(id);
334 } else if (this.presenceListener != null) {
335 this.presenceListener.onPresencePacketReceived(account, packet);
336 }
337 }
338
339 private void sendStartTLS() throws IOException {
340 Tag startTLS = Tag.empty("starttls");
341 startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls");
342 tagWriter.writeTag(startTLS);
343 }
344
345 private void switchOverToTls(Tag currentTag) throws XmlPullParserException,
346 IOException {
347 Tag nextTag = tagReader.readTag(); // should be proceed end tag
348 try {
349 SSLContext sc = SSLContext.getInstance("TLS");
350 TrustManagerFactory tmf = TrustManagerFactory
351 .getInstance(TrustManagerFactory.getDefaultAlgorithm());
352 // Initialise the TMF as you normally would, for example:
353 // tmf.in
354 try {
355 tmf.init((KeyStore) null);
356 } catch (KeyStoreException e1) {
357 // TODO Auto-generated catch block
358 e1.printStackTrace();
359 }
360
361 TrustManager[] trustManagers = tmf.getTrustManagers();
362 final X509TrustManager origTrustmanager = (X509TrustManager) trustManagers[0];
363
364 TrustManager[] wrappedTrustManagers = new TrustManager[] { new X509TrustManager() {
365
366 @Override
367 public void checkClientTrusted(X509Certificate[] chain,
368 String authType) throws CertificateException {
369 origTrustmanager.checkClientTrusted(chain, authType);
370 }
371
372 @Override
373 public void checkServerTrusted(X509Certificate[] chain,
374 String authType) throws CertificateException {
375 try {
376 origTrustmanager.checkServerTrusted(chain, authType);
377 } catch (CertificateException e) {
378 if (e.getCause() instanceof CertPathValidatorException) {
379 String sha;
380 try {
381 MessageDigest sha1 = MessageDigest.getInstance("SHA1");
382 sha1.update(chain[0].getEncoded());
383 sha = CryptoHelper.bytesToHex(sha1.digest());
384 if (!sha.equals(account.getSSLFingerprint())) {
385 changeStatus(Account.STATUS_TLS_ERROR);
386 if (tlsListener!=null) {
387 tlsListener.onTLSExceptionReceived(sha,account);
388 }
389 throw new CertificateException();
390 }
391 } catch (NoSuchAlgorithmException e1) {
392 // TODO Auto-generated catch block
393 e1.printStackTrace();
394 }
395 } else {
396 throw new CertificateException();
397 }
398 }
399 }
400
401 @Override
402 public X509Certificate[] getAcceptedIssuers() {
403 return origTrustmanager.getAcceptedIssuers();
404 }
405
406 } };
407 sc.init(null, wrappedTrustManagers, null);
408 SSLSocketFactory factory = sc.getSocketFactory();
409 SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,
410 socket.getInetAddress().getHostAddress(), socket.getPort(),
411 true);
412 tagReader.setInputStream(sslSocket.getInputStream());
413 tagWriter.setOutputStream(sslSocket.getOutputStream());
414 sendStartStream();
415 Log.d(LOGTAG,account.getJid()+": TLS connection established");
416 processStream(tagReader.readTag());
417 sslSocket.close();
418 } catch (NoSuchAlgorithmException e1) {
419 // TODO Auto-generated catch block
420 e1.printStackTrace();
421 } catch (KeyManagementException e) {
422 // TODO Auto-generated catch block
423 e.printStackTrace();
424 }
425 }
426
427 private void sendSaslAuth() throws IOException, XmlPullParserException {
428 String saslString = CryptoHelper.saslPlain(account.getUsername(),
429 account.getPassword());
430 Element auth = new Element("auth");
431 auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
432 auth.setAttribute("mechanism", "PLAIN");
433 auth.setContent(saslString);
434 tagWriter.writeElement(auth);
435 }
436
437 private void processStreamFeatures(Tag currentTag)
438 throws XmlPullParserException, IOException {
439 this.streamFeatures = tagReader.readElement(currentTag);
440 if (this.streamFeatures.hasChild("starttls")
441 && account.isOptionSet(Account.OPTION_USETLS)) {
442 sendStartTLS();
443 } else if (this.streamFeatures.hasChild("mechanisms")
444 && shouldAuthenticate) {
445 sendSaslAuth();
446 } else if (this.streamFeatures.hasChild("sm") && streamId != null) {
447 Log.d(LOGTAG,"found old stream id. trying to remuse");
448 ResumePacket resume = new ResumePacket(this.streamId,stanzasReceived);
449 this.tagWriter.writeStanzaAsync(resume);
450 } else if (this.streamFeatures.hasChild("bind") && shouldBind) {
451 sendBindRequest();
452 if (this.streamFeatures.hasChild("session")) {
453 Log.d(LOGTAG,"sending session");
454 IqPacket startSession = new IqPacket(IqPacket.TYPE_SET);
455 Element session = new Element("session");
456 session.setAttribute("xmlns",
457 "urn:ietf:params:xml:ns:xmpp-session");
458 session.setContent("");
459 startSession.addChild(session);
460 this.sendIqPacket(startSession, null);
461 }
462 }
463 }
464
465 private void sendInitialPresence() {
466 PresencePacket packet = new PresencePacket();
467 packet.setAttribute("from", account.getFullJid());
468 if (account.getKeys().has("pgp_signature")) {
469 try {
470 String signature = account.getKeys().getString("pgp_signature");
471 Element status = new Element("status");
472 status.setContent("online");
473 packet.addChild(status);
474 Element x = new Element("x");
475 x.setAttribute("xmlns", "jabber:x:signed");
476 x.setContent(signature);
477 packet.addChild(x);
478 } catch (JSONException e) {
479 //
480 }
481 }
482 this.sendPresencePacket(packet);
483 }
484
485 private void sendBindRequest() throws IOException {
486 IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
487 Element bind = new Element("bind");
488 bind.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-bind");
489 Element resource = new Element("resource");
490 resource.setContent("Conversations");
491 bind.addChild(resource);
492 iq.addChild(bind);
493 this.sendIqPacket(iq, new OnIqPacketReceived() {
494 @Override
495 public void onIqPacketReceived(Account account, IqPacket packet) {
496 String resource = packet.findChild("bind").findChild("jid")
497 .getContent().split("/")[1];
498 account.setResource(resource);
499 account.setStatus(Account.STATUS_ONLINE);
500 if (streamFeatures.hasChild("sm")) {
501 EnablePacket enable = new EnablePacket();
502 tagWriter.writeStanzaAsync(enable);
503 }
504 sendInitialPresence();
505 sendServiceDiscovery();
506 if (statusListener != null) {
507 statusListener.onStatusChanged(account);
508 }
509 }
510 });
511 }
512
513 private void sendServiceDiscovery() {
514 IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
515 iq.setAttribute("to", account.getServer());
516 Element query = new Element("query");
517 query.setAttribute("xmlns", "http://jabber.org/protocol/disco#info");
518 iq.addChild(query);
519 this.sendIqPacket(iq, new OnIqPacketReceived() {
520
521 @Override
522 public void onIqPacketReceived(Account account, IqPacket packet) {
523 if (packet.hasChild("query")) {
524 List<Element> elements = packet.findChild("query")
525 .getChildren();
526 for (int i = 0; i < elements.size(); ++i) {
527 if (elements.get(i).getName().equals("feature")) {
528 discoFeatures.add(elements.get(i).getAttribute(
529 "var"));
530 }
531 }
532 }
533 if (discoFeatures.contains("urn:xmpp:carbons:2")) {
534 sendEnableCarbons();
535 }
536 }
537 });
538 }
539
540 private void sendEnableCarbons() {
541 IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
542 Element enable = new Element("enable");
543 enable.setAttribute("xmlns", "urn:xmpp:carbons:2");
544 iq.addChild(enable);
545 this.sendIqPacket(iq, new OnIqPacketReceived() {
546
547 @Override
548 public void onIqPacketReceived(Account account, IqPacket packet) {
549 if (!packet.hasChild("error")) {
550 Log.d(LOGTAG, account.getJid()
551 + ": successfully enabled carbons");
552 } else {
553 Log.d(LOGTAG, account.getJid()
554 + ": error enableing carbons " + packet.toString());
555 }
556 }
557 });
558 }
559
560 private void processStreamError(Tag currentTag) {
561 Log.d(LOGTAG, "processStreamError");
562 }
563
564 private void sendStartStream() throws IOException {
565 Tag stream = Tag.start("stream:stream");
566 stream.setAttribute("from", account.getJid());
567 stream.setAttribute("to", account.getServer());
568 stream.setAttribute("version", "1.0");
569 stream.setAttribute("xml:lang", "en");
570 stream.setAttribute("xmlns", "jabber:client");
571 stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
572 tagWriter.writeTag(stream);
573 }
574
575 private String nextRandomId() {
576 return new BigInteger(50, random).toString(32);
577 }
578
579 public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) {
580 String id = nextRandomId();
581 packet.setAttribute("id", id);
582 this.sendPacket(packet, callback);
583 }
584
585 public void sendMessagePacket(MessagePacket packet) {
586 this.sendPacket(packet, null);
587 }
588
589 public void sendMessagePacket(MessagePacket packet,
590 OnMessagePacketReceived callback) {
591 this.sendPacket(packet, callback);
592 }
593
594 public void sendPresencePacket(PresencePacket packet) {
595 this.sendPacket(packet, null);
596 }
597
598 public void sendPresencePacket(PresencePacket packet,
599 OnPresencePacketReceived callback) {
600 this.sendPacket(packet, callback);
601 }
602
603 private synchronized void sendPacket(final AbstractStanza packet, PacketReceived callback) {
604 // TODO dont increment stanza count if packet = request packet or ack;
605 ++stanzasSent;
606 tagWriter.writeStanzaAsync(packet);
607 if (callback != null) {
608 if (packet.getId()==null) {
609 packet.setId(nextRandomId());
610 }
611 packetCallbacks.put(packet.getId(), callback);
612 }
613 }
614
615 public void sendPing() {
616 if (streamFeatures.hasChild("sm")) {
617 Log.d(LOGTAG,account.getJid()+": sending r as ping");
618 tagWriter.writeStanzaAsync(new RequestPacket());
619 } else {
620 Log.d(LOGTAG,account.getJid()+": sending iq as ping");
621 IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
622 Element ping = new Element("ping");
623 iq.setAttribute("from",account.getFullJid());
624 ping.setAttribute("xmlns", "urn:xmpp:ping");
625 iq.addChild(ping);
626 this.sendIqPacket(iq, null);
627 }
628 }
629
630 public void setOnMessagePacketReceivedListener(
631 OnMessagePacketReceived listener) {
632 this.messageListener = listener;
633 }
634
635 public void setOnUnregisteredIqPacketReceivedListener(
636 OnIqPacketReceived listener) {
637 this.unregisteredIqListener = listener;
638 }
639
640 public void setOnPresencePacketReceivedListener(
641 OnPresencePacketReceived listener) {
642 this.presenceListener = listener;
643 }
644
645 public void setOnStatusChangedListener(OnStatusChanged listener) {
646 this.statusListener = listener;
647 }
648
649 public void setOnTLSExceptionReceivedListener(OnTLSExceptionReceived listener) {
650 this.tlsListener = listener;
651 }
652
653 public void disconnect(boolean force) {
654 Log.d(LOGTAG,"disconnecting");
655 try {
656 if (force) {
657 socket.close();
658 return;
659 }
660 tagWriter.finish();
661 while(!tagWriter.finished()) {
662 //Log.d(LOGTAG,"not yet finished");
663 Thread.sleep(100);
664 }
665 tagWriter.writeTag(Tag.end("stream:stream"));
666 } catch (IOException e) {
667 Log.d(LOGTAG,"io exception during disconnect");
668 } catch (InterruptedException e) {
669 Log.d(LOGTAG,"interupted while waiting for disconnect");
670 }
671 }
672
673 public boolean hasFeatureRosterManagment() {
674 if (this.streamFeatures==null) {
675 return false;
676 } else {
677 return this.streamFeatures.hasChild("ver");
678 }
679 }
680
681 public void r() {
682 this.tagWriter.writeStanzaAsync(new RequestPacket());
683 }
684
685 public int getReceivedStanzas() {
686 return this.stanzasReceived;
687 }
688
689 public int getSentStanzas() {
690 return this.stanzasSent;
691 }
692}