summaryrefslogtreecommitdiff
path: root/science/gsystem/files/patch-network-reconnect.diff
blob: 9129a620b0dae498a9ff46d9388b8c66926d2671 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
Index: src/worldengine/GXmlDataController.cpp
===================================================================
--- src/worldengine/GXmlDataController.cpp	(revision 547)
+++ src/worldengine/GXmlDataController.cpp	(working copy)
@@ -1306,6 +1306,14 @@
 
 void GXmlDataController::sendUserMessage(QString message, QString destination)
 {
+  //@todo: this doesn't work???
+//   XMPP::Jid destjid;
+//   destjid.set(destination);
+//   if (!destjid.isValid())
+//   {
+//     qWarning(QString("Destination %1 not a valid JID, not sending message!").arg(destination));
+//     return;
+//   }
   QDomDocument d;
   QDomElement e = d.createElement("message");
   d.appendChild(e);
Index: src/worldengine/GXmppNetwork.cpp
===================================================================
--- src/worldengine/GXmppNetwork.cpp	(revision 547)
+++ src/worldengine/GXmppNetwork.cpp	(working copy)
@@ -94,19 +94,22 @@
 : GXmlNetwork(parent,name),
   Active(FALSE),
   Connected(FALSE),
+  Connecting(FALSE),
   XmppLayerCreated(FALSE),
   XmppConnector(NULL),
   Tls(NULL),
   TlsHandler(NULL),
   Stream(NULL),
   NetworkId(full_jid),
-  MessageQueue(new GXmppMessageQueue())
+  MessageQueue(new GXmppMessageQueue()),
+  StayConnected(FALSE)
 {
   connect(qApp,SIGNAL(aboutToQuit()),this,SLOT(closeNetwork()));
 
-  QTimer* heartbeat_timer = new QTimer(this,"heartbeat timer");
-  connect(heartbeat_timer,SIGNAL(timeout()),this,SLOT(sendHeartbeat()));
-  heartbeat_timer->start(55000);
+//   QTimer* heartbeat_timer = new QTimer(this,"heartbeat timer");
+//   connect(heartbeat_timer,SIGNAL(timeout()),this,SLOT(sendHeartbeat()));
+//   heartbeat_timer->start(55000);
+  // see XMPP::ClientStream::setNoopTime() which is now used
 }
 
 GXmppNetwork::~GXmppNetwork()
@@ -123,8 +126,9 @@
 {
   if (XmppLayerCreated)
   {
-    qWarning("XMPP Layer already created!");
-    return true;
+    qWarning("XMPP Layer already created! Destroying it first...");
+    this->deleteXmppLayer(true);
+    qWarning("XMPP Layer now destroyed");
   }
     
   //XMPP related setup
@@ -241,6 +245,11 @@
     XmppConnector=NULL;
   }
   
+  this->Connected = false;
+  this->Connecting = false;
+  this->Active = false;
+  this->XmppLayerCreated = false;
+  
   return true;
 }
 
@@ -296,6 +305,14 @@
 
 bool GXmppNetwork::initNetwork()
 {
+  if (this->Connecting)
+  {
+    qWarning("Already in the process of connecting the network");
+    return false;
+  }
+  
+  this->Connecting = true;
+  
   QMutexLocker lock(this);
   if (!this->createXmppLayer())
   {
@@ -332,7 +349,7 @@
     Tls->setCertificateStore(certStore);
   }
   
-//   Stream->setNoopTime(55000); // every 55 seconds
+  Stream->setNoopTime(55000); // every 55 seconds
   qDebug("Connecting XMPP network with JID " + this->getNetworkId());
   qDebug("Warning: if you get a segmentation fault next, this probably means");
   qDebug("         that the server you want to connect to (the domain part");
@@ -344,15 +361,22 @@
   return true;
 }
 
+void GXmppNetwork::reconnectNetwork()
+{
+  QTimer::singleShot(100,this,SLOT(initNetwork()));
+}
+
 bool GXmppNetwork::closeNetwork()
 {
   QMutexLocker lock(this);
+  this->StayConnected = false;
   if (this->Connected)
   {
     qDebug("Disconnecting XMPP Stream...");
     this->Stream->close();
     this->Connected = false;
     this->Active = false; // correct place?
+    this->Connecting = false;
     return true;
   }
   else
@@ -379,6 +403,15 @@
     qWarning("Destination is empty, not sending message!");
     return false;
   }
+  
+  //@todo this doesn't seem to be functional(??)
+  XMPP::Jid destjid(destination);
+  if (!destjid.isValid())
+  {
+    qWarning(QString("Destination %1 not a valid JID, not sending message!").arg(destination));
+    return false;
+  }
+  
 //   qDebug(QString("type range for user events: %1 to %2").arg(QString::number(QEvent::User)).arg(QString::number(QEvent::MaxUser)));
   QDomDocument d;
   QDomElement e = d.createElement("message");
@@ -386,15 +419,24 @@
   e.setAttribute("to",destination);
   e.appendChild(d.importNode(data,"true"));
   
-  if (!this->isSubscribed(destination))
+  if (!this->isSubscribed(destination) || !this->isConnected())
   {
-    qWarning(QString("Delaying sending of message, destination %1 is not yet available").arg(destination));
+    if (!this->isConnected() && this->StayConnected)
+    {
+      qWarning("Network is not connected! Reconnecting...");
+      this->reconnectNetwork();
+    }
+    else
+    {
+      qWarning(QString("Delaying sending of message, destination %1 is not yet available").arg(destination));
+      this->makeDestinationAvailable(destination);
+    }
     this->MessageQueue->add(destination,d.toString());
-    this->makeDestinationAvailable(destination);
   }
   else
   {
-    QApplication::postEvent(this,new QNetworkSendEvent(d.toString()));
+    this->send(d.toString());
+//     QApplication::postEvent(this,new QNetworkSendEvent(d.toString()));
   }
 
   return true;
@@ -455,7 +497,7 @@
 
 bool GXmppNetwork::isConnected()
 {
-  if (this->Active && this->Connected && Stream->isAuthenticated())
+  if (this->Active && this->Connected && Stream->isAuthenticated() && Stream->isActive())
     return true;
   else
     return false;
@@ -505,7 +547,9 @@
 
 void GXmppNetwork::clientStreamConnected()
 {
+  this->Connecting = false;
   this->Connected = true;
+  this->StayConnected = true;
   qDebug("XMPP Stream connected");
 }
 
@@ -553,7 +597,12 @@
 
 void GXmppNetwork::clientStreamConnectionClosed()
 {
-  qDebug("XMPP Stream connection closed");
+  qDebug("XMPP Stream connection closed.");
+  if (this->StayConnected)
+  {
+    qDebug("Using timer for delayed recreation of the XMPP layer...");
+    this->reconnectNetwork();
+  }
 }
 
 void GXmppNetwork::clientStreamDelayedCloseFinished()
@@ -765,7 +814,7 @@
   }
   else if(err == XMPP::ClientStream::ErrProtocol)
   {
-    s = "conntest: XMPP protocol error";
+    s = "XMPP protocol error";
   }
   else if(err == XMPP::ClientStream::ErrStream)
   {
@@ -870,6 +919,12 @@
     s = "broken security layer (SASL)";
 
   qWarning(s);
+  
+  if (!this->isConnected() && this->StayConnected)
+  {
+    qWarning("XMPP Stream got disconnected, reconnecting...");
+    QTimer::singleShot(100,this,SLOT(initNetwork()));
+  }
 }
 
 //END XMPP slots and signals for network management //
Index: src/worldengine/GXmppNetwork.h
===================================================================
--- src/worldengine/GXmppNetwork.h	(revision 547)
+++ src/worldengine/GXmppNetwork.h	(working copy)
@@ -81,6 +81,11 @@
   bool Connected;
   
   /**
+   * Currently connecting?
+   */
+  bool Connecting;
+  
+  /**
    * XMPP related objects created.
    */
   bool XmppLayerCreated;
@@ -130,6 +135,14 @@
    */
   GXmppMessageQueue* MessageQueue;
   
+  /**
+   * Set to true if the network should stay connected.
+   * In this case reinitializing the network will be done
+   * automatically if the network got disconnected.
+   * closeNetwork() sets this to false before shutting down.
+   */
+  bool StayConnected;
+  
   protected slots:
   
   /**
@@ -208,6 +221,13 @@
    * Initialization.
    */
   virtual bool initNetwork();
+  
+  /**
+   * Issues a single shot timer to call initNetwork()
+   * Useful when wanting to reconnect but being in the middle
+   * of something (that is, in a slot)
+   */
+  virtual void reconnectNetwork();
    
   /**
    * Shuts down the network.