PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/QXmppReconnectionManager.cpp

http://qxmpp.googlecode.com/
C++ | 101 lines | 66 code | 10 blank | 25 comment | 20 complexity | 9b64ea04e682ebb5e318c73c14af5edd MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * Copyright (C) 2008-2011 The QXmpp developers
  3. *
  4. * Author:
  5. * Manjeet Dahiya
  6. *
  7. * Source:
  8. * http://code.google.com/p/qxmpp
  9. *
  10. * This file is a part of QXmpp library.
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. */
  23. #include "QXmppReconnectionManager.h"
  24. #include "QXmppClient.h"
  25. #include "QXmppLogger.h"
  26. #include "QXmppUtils.h"
  27. QXmppReconnectionManager::QXmppReconnectionManager(QXmppClient* client) :
  28. QObject(client),
  29. m_receivedConflict(false),
  30. m_reconnectionTries(0),
  31. m_timer(this),
  32. m_client(client)
  33. {
  34. m_timer.setSingleShot(true);
  35. bool check = connect(&m_timer, SIGNAL(timeout()), SLOT(reconnect()));
  36. Q_ASSERT(check);
  37. Q_UNUSED(check);
  38. }
  39. void QXmppReconnectionManager::connected()
  40. {
  41. m_receivedConflict = false;
  42. m_reconnectionTries = 0;
  43. }
  44. void QXmppReconnectionManager::error(QXmppClient::Error error)
  45. {
  46. if(m_client && error == QXmppClient::XmppStreamError)
  47. {
  48. // if we receive a resource conflict, inhibit reconnection
  49. if(m_client->xmppStreamError() == QXmppStanza::Error::Conflict)
  50. m_receivedConflict = true;
  51. }
  52. else if(m_client && error == QXmppClient::SocketError && !m_receivedConflict)
  53. {
  54. int time = getNextReconnectingInTime();
  55. // time is in sec
  56. m_timer.start(time*1000);
  57. emit reconnectingIn(time);
  58. }
  59. else if (m_client && error == QXmppClient::KeepAliveError)
  60. {
  61. // if we got a keepalive error, reconnect in one second
  62. m_timer.start(1000);
  63. }
  64. }
  65. int QXmppReconnectionManager::getNextReconnectingInTime()
  66. {
  67. int reconnectingIn;
  68. if(m_reconnectionTries < 5)
  69. reconnectingIn = 10;
  70. else if(m_reconnectionTries < 10)
  71. reconnectingIn = 20;
  72. else if(m_reconnectionTries < 15)
  73. reconnectingIn = 40;
  74. else
  75. reconnectingIn = 60;
  76. return reconnectingIn;
  77. }
  78. void QXmppReconnectionManager::reconnect()
  79. {
  80. if(m_client)
  81. {
  82. emit reconnectingNow();
  83. m_client->connectToServer(m_client->configuration(), m_client->clientPresence());
  84. }
  85. }
  86. void QXmppReconnectionManager::cancelReconnection()
  87. {
  88. m_timer.stop();
  89. m_receivedConflict = false;
  90. m_reconnectionTries = 0;
  91. }