refactor TimedReconnect and AQ_Reconnect to make more the same

This commit is contained in:
Douglas Clowes
2013-06-14 15:33:11 +10:00
parent aaeaff692e
commit 784372503c

View File

@@ -182,46 +182,46 @@ static int TimedReconnect(void *cntx, int mode)
* 0: The request is still in progress * 0: The request is still in progress
* +1: The request succeeded * +1: The request succeeded
*/ */
if (iRet < 0) { if (iRet <= 0) {
snprintf(line, 132, "Failed reconnect on AsyncQueue '%s'", if (iRet < 0) {
self->queue_name); snprintf(line, 132, "Failed reconnect on AsyncQueue '%s'",
SICSLogWrite(line, eStatus); self->queue_name);
NetWatchSetMode(self->nw_ctx, 0); SICSLogWrite(line, eStatus);
/* implement an exponential backoff within limits */ /* Timer for retry */
self->retryTimer = 2 * self->retryTimer; NetWatchSetMode(self->nw_ctx, 0);
if (self->retryTimer < 125) /* implement an exponential backoff within limits */
self->retryTimer = 125; self->retryTimer = 2 * self->retryTimer;
if (self->retryTimer > 16000) if (self->retryTimer < 125)
self->retryTimer = 16000; self->retryTimer = 125;
NetWatchRegisterTimer(&self->nw_tmr, self->retryTimer, TimedReconnect, if (self->retryTimer > 16000)
self); self->retryTimer = 16000;
SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncWaiting\n", NetWatchRegisterTimer(&self->nw_tmr, self->retryTimer,
self->queue_name, __func__, state_name(self->state)); TimedReconnect, self);
self->state = eAsyncWaiting; SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncWaiting\n",
} else if (iRet == 0) { self->queue_name, __func__, state_name(self->state));
snprintf(line, 132, "Inprogress reconnect on AsyncQueue '%s'", self->state = eAsyncWaiting;
self->queue_name); } else {
NetWatchSetMode(self->nw_ctx, nwatch_write); NetWatchSetMode(self->nw_ctx, nwatch_write);
SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncConnecting\n", SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncConnecting\n",
self->queue_name, __func__, state_name(self->state)); self->queue_name, __func__, state_name(self->state));
self->state = eAsyncConnecting; self->state = eAsyncConnecting;
} else { /* await reconnect result in MyCallback */
NetWatchSetMode(self->nw_ctx, nwatch_read); }
SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncConnected\n", return 1;
self->queue_name, __func__, state_name(self->state));
self->state = eAsyncConnected;
snprintf(line, 132, "Reconnect on AsyncQueue '%s'", self->queue_name);
SICSLogWrite(line, eStatus);
AQ_Notify(self, AQU_RECONNECT);
} }
NetWatchSetMode(self->nw_ctx, nwatch_read);
SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncConnected\n",
self->queue_name, __func__, state_name(self->state));
self->state = eAsyncConnected;
snprintf(line, 132, "Reconnect on AsyncQueue '%s'", self->queue_name);
SICSLogWrite(line, eStatus);
AQ_Notify(self, AQU_RECONNECT);
return 1; return 1;
} }
static int AQ_Reconnect(pAsyncQueue self) static int AQ_Reconnect(pAsyncQueue self)
{ {
int iRet; int iRet;
int sock;
int flag = 1;
char line[132]; char line[132];
if (self->state != eAsyncConnected) if (self->state != eAsyncConnected)
@@ -251,6 +251,7 @@ static int AQ_Reconnect(pAsyncQueue self)
if (iRet < 0) { if (iRet < 0) {
/* Timer for retry */ /* Timer for retry */
NetWatchSetMode(self->nw_ctx, 0); NetWatchSetMode(self->nw_ctx, 0);
/* implement an exponential backoff within limits */
self->retryTimer = 125; /* initial delay */ self->retryTimer = 125; /* initial delay */
NetWatchRegisterTimer(&self->nw_tmr, self->retryTimer, NetWatchRegisterTimer(&self->nw_tmr, self->retryTimer,
TimedReconnect, self); TimedReconnect, self);
@@ -262,7 +263,7 @@ static int AQ_Reconnect(pAsyncQueue self)
SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncConnecting\n", SICSLogPrintf(eStatus, "In %s:%s: state %s => eAsyncConnecting\n",
self->queue_name, __func__, state_name(self->state)); self->queue_name, __func__, state_name(self->state));
self->state = eAsyncConnecting; self->state = eAsyncConnecting;
/* TODO await reconnect result */ /* await reconnect result in MyCallback */
} }
return iRet; return iRet;
} }