]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/network/serv_networkclient.c
Citadel Networker: after calling sub-writers we need to rewind the state.
[citadel.git] / citadel / modules / network / serv_networkclient.c
index 82b3920a03890f0d9af4461add425c0c6c49f022..d3736240771f873cd80349780651214a79ebdc96 100644 (file)
@@ -185,6 +185,8 @@ eNextState NWC_ReadGreeting(AsyncNetworker *NW)
        extract_token (connected_to, ChrPtr(NW->IO.IOBuf), 1, ' ', sizeof connected_to);
        if (strcmp(connected_to, ChrPtr(NW->node)) != 0)
        {
+               if (NW->IO.ErrMsg == NULL)
+                       NW->IO.ErrMsg = NewStrBuf();
                StrBufPrintf(NW->IO.ErrMsg,
                             "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
                             connected_to, ChrPtr(NW->node));
@@ -216,6 +218,8 @@ eNextState NWC_ReadAuthReply(AsyncNetworker *NW)
        }
        else
        {
+               if (NW->IO.ErrMsg == NULL)
+                       NW->IO.ErrMsg = NewStrBuf();
                StrBufPrintf(NW->IO.ErrMsg,
                             "Connected to node \"%s\" but my secret wasn't accurate.",
                             ChrPtr(NW->node));
@@ -343,6 +347,7 @@ eNextState NWC_ReadREADState(AsyncNetworker *NW)
 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
 eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
 {
+       eNextState rc;
        AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
        if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
@@ -360,7 +365,9 @@ eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
                }
        
                unlink(ChrPtr(NW->tempFileName));
-               return NWC_DispatchWriteDone(&NW->IO);
+               rc = NWC_DispatchWriteDone(&NW->IO);
+               NW->State --;
+               return rc;
        }
        else {
                NW->State --;
@@ -371,6 +378,7 @@ eNextState NWC_ReadREADBlob(AsyncNetworker *NW)
 
 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
 {
+       eNextState rc;
        AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
        if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
@@ -388,7 +396,9 @@ eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
                }
        
                unlink(ChrPtr(NW->tempFileName));
-               return NWC_DispatchWriteDone(&NW->IO);
+               rc = NWC_DispatchWriteDone(&NW->IO);
+               NW->State --;
+               return rc;
        }
        else {
                NW->State --;
@@ -437,6 +447,7 @@ eNextState NWC_SendNUOP(AsyncNetworker *NW)
                NW->State = eQUIT;
                rc = NWC_SendQUIT(NW);
                NWC_DBG_SEND();
+               return rc;
        }
 
        if (fstat(fd, &statbuf) == -1) {
@@ -497,19 +508,21 @@ eNextState NWC_SendBlobDone(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
        eNextState rc;
-       if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
+       if (IO->IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
        {
                NW->State ++;
 
-               FDIOBufferDelete(&NW->IO.IOB);
-               rc =  NWC_DispatchWriteDone(&NW->IO);
+               FDIOBufferDelete(&IO->IOB);
+               rc =  NWC_DispatchWriteDone(IO);
                NW->State --;
                return rc;
        }
        else {
                NW->State --;
-               NW->IO.IOB.ChunkSendRemain = NW->IO.IOB.ChunkSize;
-               return NWC_DispatchWriteDone(&NW->IO);
+               IO->IOB.ChunkSendRemain = IO->IOB.ChunkSize;
+               rc = NWC_DispatchWriteDone(IO);
+               NW->State --;
+               return rc;
        }
 }
 
@@ -787,30 +800,39 @@ eNextState NWC_Terminate(AsyncIO *IO)
 
 eNextState NWC_Timeout(AsyncIO *IO)
 {
+       AsyncNetworker *NW = IO->Data;
        EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
 
+       if (NW->IO.ErrMsg == NULL)
+               NW->IO.ErrMsg = NewStrBuf();
+       StrBufPrintf(NW->IO.ErrMsg, "Timeout while talking to %s \r\n", ChrPtr(NW->host));
        return NWC_FailNetworkConnection(IO);
 }
 eNextState NWC_ConnFail(AsyncIO *IO)
 {
-///    AsyncNetworker *NW = IO->Data;
+       AsyncNetworker *NW = IO->Data;
 
        EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
-////   StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
+       if (NW->IO.ErrMsg == NULL)
+               NW->IO.ErrMsg = NewStrBuf();
+       StrBufPrintf(NW->IO.ErrMsg, "failed to connect %s \r\n", ChrPtr(NW->host));
+
        return NWC_FailNetworkConnection(IO);
 }
 eNextState NWC_DNSFail(AsyncIO *IO)
 {
-///    AsyncNetworker *NW = IO->Data;
+       AsyncNetworker *NW = IO->Data;
 
        EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
-////   StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State])); todo
+       if (NW->IO.ErrMsg == NULL)
+               NW->IO.ErrMsg = NewStrBuf();
+       StrBufPrintf(NW->IO.ErrMsg, "failed to look up %s \r\n", ChrPtr(NW->host));
+
        return NWC_FailNetworkConnection(IO);
 }
 eNextState NWC_Shutdown(AsyncIO *IO)
 {
        EVN_syslog(LOG_DEBUG, "%s\n", __FUNCTION__);
-////   pop3aggr *pMsg = IO->Data;
 
        FinalizeNetworker(IO);
        return eAbort;
@@ -836,11 +858,9 @@ eNextState nwc_connect_ip(AsyncIO *IO)
 static int NetworkerCount = 0;
 void RunNetworker(AsyncNetworker *NW)
 {
-       AsyncIO *IO = &NW->IO;
-
        NW->n = NetworkerCount++;
        network_talking_to(SKEY(NW->node), NTT_ADD);
-       EVN_syslog(LOG_DEBUG, "network: polling <%s>\n", ChrPtr(NW->node));
+       syslog(LOG_DEBUG, "NW[%s][%ld]: polling\n", ChrPtr(NW->node), NW->n);
        ParseURL(&NW->IO.ConnectMe, NW->Url, 504);
 
        InitIOStruct(&NW->IO,
@@ -934,7 +954,9 @@ void network_poll_other_citadel_nodes(int full_poll, char *working_ignetcfg)
                                        }
                                }
                        }
-                       if (poll && (StrLength (NW->host) > 0) && (!strcmp(ChrPtr(NW->host), "0.0.0.0")))
+                       if (poll && 
+                           (StrLength(NW->host) > 0) && 
+                           strcmp("0.0.0.0", ChrPtr(NW->host)))
                        {
                                NW->Url = NewStrBufPlain(NULL, StrLength(Line));
                                StrBufPrintf(NW->Url, "citadel://:%s@%s:%s",