예제 #1
0
static void dispatchSubCmd(int16 cnId, NDB *ndb)
{
	char *pCnt;
	char *pNam;
	char fileNam[9];
	long fileLen;
	char cmd;
	static int havCnt, havDat;

	cmd = ndb->ndata[0];	/* get copies because we are going to throw ndb away */
	pCnt = strtok(ndb->ndata+1, " ");
	pNam = strtok(NULL, "\n");
	strncpy(fileNam, pNam, 8);
	fileNam[8] = '\0';
	fileLen = atol(pCnt);		/* get file length */

	KRfree(ndb->ptr); KRfree(ndb);	/* not needed any more */

	switch (cmd) {
	case '\1':		/* Abort jobs */
		output(cnId, "\1", 1);	/* unimplemented */
		break;

	case '\2':		/* Receive control file */
		output(cnId, "\0", 1);	/* acknowledge */
		dumpFile(cnId, fileNam, fileLen);
		output(cnId, "\0", 1);	/* success */
		havCnt=1;
		if (havDat) {
			printFile(fileNam);
			havDat=0;
			havCnt=0;
		}
		break;

	case '\3':		/* Receive data file */
		output(cnId, "\0", 1);	/* acknowledge */
		dumpFile(cnId, fileNam, fileLen);
		output(cnId, "\0", 1);	/* success */
		havDat=1;
		if (havCnt) {
			printFile(fileNam);
			havDat=0;
			havCnt=0;
		}
		break;

	default:
		output(cnId, "\1", 1);		/* error */
        uiPrintf(uiH, uiPrERR, "dispatchSubCmd|funny deamon rec subcmd");
		break;
	}


}	/* dispatchSubCmd */
예제 #2
0
int sendAndReceive(BYTE tcpNotUdp, DWORD blockSize, int handle, BYTE getBlockNotNdb)
{
    //----------
    // send
    int res;
    DWORD now, endTime;
    DWORD kbs = 1 + (blockSize / 1024);
    
    endTime = getTicks() + (kbs * 200);         // for each kB of data give 1 second to transfer
    
    while(1) {              // try to send
        if(tcpNotUdp) {
            res = TCP_send(handle, wBuf, blockSize);
        } else {
            res = UDP_send(handle, wBuf, blockSize);
        }

        if(res != E_OBUFFULL) {
            break;
        }
        
        now = getTicks();
        if(now >= endTime) {        // timeout? 
            out_result_error_string(0, blockSize, "send() timeout");
            return 0;
        }
    }
    
    if(res != E_NORMAL) { 
        out_result_error_string(0, res, "send() failed");
        return 0;
    }
    
    //----------
    // wait
    endTime = getTicks() + (kbs * 200);         // for each kB of data give 1 second to transfer
    
    memset(rBuf, 0, blockSize);
    BYTE *pBuf  = rBuf;
    DWORD toGet = blockSize;
    
    while(toGet > 0) {
        now = getTicks();
        if(now >= endTime) {                    // timeout?
            out_result_error_string(0, blockSize, "CNbyte_count() timeout");
            return 0;
        }

        res = CNbyte_count(handle);                     // find out how many bytes are waiting

        if(res > 0) {                                   // something waiting? read it
            if(getBlockNotNdb) {                        // retrieve using CNget_block?
                res = CNget_block(handle, pBuf, res);
                
                if(res > 0) {
                    pBuf  += res;
                    toGet -= res;
                } else if(res != E_NODATA && res < 0) {        // if it's some error, and that error is not E_NODATA, fail
                    out_result_error_string(0, blockSize, "CNget_block() failed");
                    return 0;
                }
            } else {                                    // retrieve using CNget_NDB
                NDB *ndb = CNget_NDB(handle);

                if(ndb) {                               // if something retrieved
                    memcpy(pBuf, ndb->ndata, ndb->len); // copy in the data

                    pBuf += ndb->len;                     // it was this many bytes
                    toGet -= ndb->len;

                    KRfree (ndb->ptr);                  // free the ram
                    KRfree (ndb);
                }
            }
        }
    }
    
    //----------
    // data are valid? 
    res = memcmp(rBuf, wBuf, blockSize);
    
    if(res != 0) {
        out_result_string(0, "Received data mismatch");
        return 0;
    }

    //-------
    // if came here, everything is OK
    return 1;
}
예제 #3
0
void  do_some_work()

{
   NDB     *ndb;
   time_t  timeout;
   int     handle, message[2], response, ready;

   if ((handle = UDP_open (0, IP_DIALER_PORT)) < 0) {
        form_alert (1, no_udp);
        return;
      }

   for (;;) {

        timeout = time (NULL) + TIMEOUT;

        do {
             evnt_timer (200, 0);

             if ((ndb = CNget_NDB (handle)) != NULL) {
                  switch (* (int16 *) ndb->ndata) {
                     case IP_DIAL_REQUEST :
                       sprintf (alert, query, ndb->ndata + 4, * (int16 *) (ndb->ndata + 2));
                       if (form_alert (1, alert) == 1)
                            message[0] = IP_DIAL_DONE,  message[1] = 0;
                         else {
                            message[0] = IP_DIAL_ERROR;
                            response = 0;
                            do {
                                 sprintf (alert, number, response);
                                 ready = 0;
                                 switch (form_alert (1, alert)) {
                                    case 1 :   response --;   break;
                                    case 2 :   ready = 1;     break;
                                    case 3 :   response ++;   break;
                                    }
                              } while (! ready);
                            message[1] = response;
                          }
                       break;
                     case IP_DIAL_HANGUP :
                       form_alert (1, hangup);
                       message[0] = IP_DIAL_DONE;
                       message[1] = 0;
                       break;
                     }
                  KRfree (ndb->ptr);
                  KRfree (ndb);
                  UDP_send (handle, (char *) message, 4);
                  UDP_close (handle);

                  if ((handle = UDP_open (0, IP_DIALER_PORT)) < 0) {
                       form_alert (1, no_udp);
                       return;
                     }
                }
          } while (time (NULL) < timeout);

        if (form_alert (1, proceed) != 1)   break;
      }

   UDP_close (handle);
 }
예제 #4
0
static void dispatchD(int16 cnId, NDB* ndb)
{
	strtok(ndb->ndata, "\n");	/* LF to '\0' */

	switch (ndb->ndata[0]) {
	case '\1':		/* Print any waiting jobs */
		KRfree(ndb->ptr); KRfree(ndb);
		break;		/* unimplemented */

	case '\2': {		/* Receive a printer job */
			/* look for this printer in default.cfg */
			int rc = lookupQueue(ndb->ndata+1);
			KRfree(ndb->ptr); KRfree(ndb);	/* must be done exactly here! */
			if (rc==0) {
				output(cnId, "\0", 1);	/* know this printer */
				setSpoolPath();
				recvJob(cnId);
			} else {
				output(cnId, "\1", 1);	/* unknown */
			}
		}
		break;

	case '\3':		/* Send queue state (short) */
		KRfree(ndb->ptr); KRfree(ndb);
		break;		/* unimplemented */

	case '\4':		/* Send queue state (long) */
		KRfree(ndb->ptr); KRfree(ndb);
		break;		/* unimplemented */

	case '\5':		/* Remove jobs */
		KRfree(ndb->ptr); KRfree(ndb);
		break;		/* unimplemented */

	default:
		KRfree(ndb->ptr); KRfree(ndb);
        uiPrintf(uiH, uiPrERR, "dispatchD|funny daemon cmd");
		break;
	}

}	/* end of dispatchD */
예제 #5
0
static void dumpFile(int16 cnId, char fileNam[], long fileLen)
{
	int eFlag;
	char *eM;
	long accuLen;
	int fh;
	int16 nInQueue;
	clock_t tQuit;

#if 0
uiPrintf(uiH, "   %s|L: %ld|N: %s", pCnt, fileLen, fileNam);
#endif
	if ( (fh=Fcreate(fileNam, 0)) < 0 ) {
		uiPrintf(uiH, uiPrERR, "dumpFile|cannot create file");
		return;
	}


	for (eFlag=1,accuLen=0; eFlag; ) {
		tQuit = clock() + LTIMEOUT * CLK_TCK;	/* time to quit at */

		while ( (nInQueue = CNbyte_count(cnId)) == 0 || nInQueue == E_NODATA) {
			if (clock() > tQuit) {
		        eM="timed out"; goto errExit;
			}
			uiYield(uiH, YIELDMS);		/* wait till something arrives */
		} 
	
	
		if (nInQueue > 0) {
			NDB* ndb;
	
			if ( (ndb = CNget_NDB(cnId)) != NULL ) {
				accuLen += ndb->len;
#if 0
uiPrintf(uiH, "al: %ld", accuLen);
#endif
				if (accuLen == fileLen+1) {		/* this happens at the end */
					if (Fwrite(fh, ndb->len-1, ndb->ndata) <0) {
						eM="cannot write 1"; goto errExit;
					}
					if (ndb->ndata[ndb->len-1] != '\0') {
						eM="trailing 0 ?"; goto errExit;
					}
					eFlag=0;						/* normal end */
				} else {
					if (accuLen > fileLen+1) {	/* this should never happen */
						eM="prot.mismatch"; goto errExit;
					} else {
						if (Fwrite(fh, ndb->len, ndb->ndata) <0) {
							eM="cannot write 2"; goto errExit;
						}
					}
				}
				KRfree(ndb->ptr); KRfree(ndb);	/* throw ndb away */

			} else {
		        eM="get_NDB"; goto errExit;
			}	/* if..get_NDB successful */

		} else {
			eM=get_err_text(nInQueue); goto errExit;
		}	/* if..something in Queue */

	}	/* while..more blocks to read */


	Fclose(fh);
	return;

errExit:
	uiPrintf(uiH, uiPrERR, "dumpFile|%s", eM);
	Fclose(fh);
}	/* dumpFile */