Fixed ticket #91: timer to re-subscribe buddy's presence when subscription failed

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1311 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/include/pjsua-lib/pjsua_internal.h b/pjsip/include/pjsua-lib/pjsua_internal.h
index fae345e..a2195d5 100644
--- a/pjsip/include/pjsua-lib/pjsua_internal.h
+++ b/pjsip/include/pjsua-lib/pjsua_internal.h
@@ -206,6 +206,9 @@
     unsigned		 buddy_cnt;		    /**< Buddy count.	*/
     pjsua_buddy		 buddy[PJSUA_MAX_BUDDIES];  /**< Buddy array.	*/
 
+    /* Presence: */
+    pj_timer_entry	 pres_timer;/**< Presence refresh timer.	*/
+
     /* Media: */
     pjsua_media_config   media_cfg; /**< Media config.			*/
     pjmedia_endpt	*med_endpt; /**< Media endpoint.		*/
diff --git a/pjsip/src/pjsua-lib/pjsua_pres.c b/pjsip/src/pjsua-lib/pjsua_pres.c
index 364d33d..4809e9a 100644
--- a/pjsip/src/pjsua-lib/pjsua_pres.c
+++ b/pjsip/src/pjsua-lib/pjsua_pres.c
@@ -22,6 +22,10 @@
 
 #define THIS_FILE   "pjsua_pres.c"
 
+#ifndef PJSUA_PRES_TIMER
+#   define PJSUA_PRES_TIMER	120
+#endif
+
 
 /*
  * Get total number of buddies.
@@ -1133,6 +1137,25 @@
     }
 }
 
+/* Timer callback to re-create client subscription */
+static void pres_timer_cb(pj_timer_heap_t *th,
+			  pj_timer_entry *entry)
+{
+    pj_time_val delay = { PJSUA_PRES_TIMER, 0 };
+
+    PJ_UNUSED_ARG(th);
+
+    PJSUA_LOCK();
+
+    entry->id = PJ_FALSE;
+    refresh_client_subscriptions();
+
+    pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay);
+    entry->id = PJ_TRUE;
+
+    PJSUA_UNLOCK();
+}
+
 
 /*
  * Init presence
@@ -1161,7 +1184,17 @@
  */
 pj_status_t pjsua_pres_start(void)
 {
-    /* Nothing to do (is it?) */
+    /* Start presence timer to re-subscribe to buddy's presence when
+     * subscription has failed.
+     */
+    if (pjsua_var.pres_timer.id == PJ_FALSE) {
+	pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0};
+
+	pjsua_var.pres_timer.cb = &pres_timer_cb;
+	pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer,
+				   &pres_interval);
+    }
+
     return PJ_SUCCESS;
 }
 
@@ -1189,6 +1222,11 @@
 {
     unsigned i;
 
+    if (pjsua_var.pres_timer.id != 0) {
+	pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer);
+	pjsua_var.pres_timer.id = PJ_FALSE;
+    }
+
     for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
 	if (!pjsua_var.acc[i].valid)
 	    continue;