static void printCount() { PUTMEMO("Sessions canceled", itoa(_sessionsCanceled(0))); PUTMEMO("Blocks received", itoa(_blocksReceived(0))); PUTMEMO("Bytes received", itoa(_bytesReceived(0))); fflush(stdout); }
static void printCount() { signal(SIGALRM, printCount); PUTMEMO("Bundles received", itoa(_bundleCount(0))); fflush(stdout); alarm(5); }
int bpcounter(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10) { char *ownEid = (char *) a1; int maxCount = a2; #else int main(int argc, char **argv) { char *ownEid = (argc > 1 ? argv[1] : NULL); int maxCount = (argc > 2 ? atoi(argv[2]) : 0); #endif BpSAP sap; Sdr sdr; BpDelivery dlv; int stop = 0; time_t startTime = 0; int bytesReceived; int bundlesReceived = 0; time_t endTime; long interval; if (ownEid == NULL) { PUTS("Usage: bpcounter <own endpoint ID> [<max count>]"); return 0; } if (maxCount < 1) { maxCount = 2000000000; } if (bp_attach() < 0) { putErrmsg("Can't attach to BP.", NULL); return 0; } if (bp_open(ownEid, &sap) < 0) { putErrmsg("Can't open own endpoint.", ownEid); return 0; } oK(_bpsap(&sap)); sdr = bp_get_sdr(); bundlesReceived = 0; bytesReceived = 0; isignal(SIGALRM, printCount); alarm(5); isignal(SIGINT, handleQuit); while (_running(NULL)) { if (bp_receive(sap, &dlv, BP_BLOCKING) < 0) { putErrmsg("bpcounter bundle reception failed.", NULL); oK(_running(&stop)); continue; } if (dlv.result == BpPayloadPresent) { if ((bundlesReceived = _bundleCount(1)) == 1) { startTime = time(NULL); } bytesReceived += zco_length(sdr, dlv.adu); } bp_release_delivery(&dlv, 1); if (bundlesReceived == maxCount) { oK(_running(&stop)); } } if (bundlesReceived > 0) { endTime = time(NULL); interval = endTime - startTime; PUTMEMO("Time (seconds)", itoa(interval)); if (interval > 0) { PUTMEMO("Throughput (bytes per second)", itoa(bytesReceived / interval)); } } bp_close(sap); PUTMEMO("Stopping bpcounter; bundles received", itoa(bundlesReceived)); bp_detach(); return 0; }
int main(int argc, char **argv) #endif { char *wmspace; int wmid; PsmPartition wm = NULL; PsmMgtOutcome outcome; PsmAddress testlist; sm_SemId semaphore; int cycleNbr = 1; char fileName[256]; int outputFile; PsmAddress lineListElt; PsmAddress lineAddress; char *line; if (sm_ipc_init() < 0) { return 0; } if (sm_ShmAttach(0x1108, 10000000, &wmspace, &wmid) < 0) { PERROR("can't attach to shared memory"); return 0; } if (psm_manage(wmspace, 10000000, "file2sm", &wm, &outcome) < 0 || outcome == Refused) { PUTS("can't manage shared memory"); return 0; } testlist = psm_get_root(wm); if (testlist == 0) { testlist = sm_list_create(wm); if (testlist == 0) { PUTS("can't create shared memory list"); return 0; } psm_set_root(wm, testlist); } semaphore = sm_SemCreate(0x1101, SM_SEM_FIFO); if (semaphore < 0) { PUTS("can't create semaphore"); return 0; } PUTMEMO("Working on cycle", utoa(cycleNbr)); isprintf(fileName, sizeof fileName, "file_copy_%d", cycleNbr); outputFile = iopen(fileName, O_WRONLY | O_APPEND, 0666); if (outputFile < 0) { PERROR("can't open output file"); return 0; } while (1) { while (sm_list_length(wm, testlist) == 0) { sm_SemTake(semaphore); /* Wait for line. */ } lineListElt = sm_list_first(wm, testlist); lineAddress = sm_list_data(wm, lineListElt); line = psp(wm, lineAddress); /* Process text of line. */ if (strcmp(line, "*** End of the file ***\n") == 0) { /* Close file, open next one. */ close(outputFile); cycleNbr++; PUTMEMO("Working on cycle", utoa(cycleNbr)); isprintf(fileName, sizeof fileName, "file_copy_%d", cycleNbr); outputFile = iopen(fileName, O_WRONLY | O_APPEND, 0666); if (outputFile < 0) { PERROR("Can't open output file"); return 0; } } else /* Just write line to output file. */ { if (iputs(outputFile, line) < 0) { close(outputFile); PERROR("Can't write to output file"); return 0; } } /* Delete line from shared memory list. */ psm_free(wm, lineAddress); CHKZERO(sm_list_delete(wm, lineListElt, (SmListDeleteFn) NULL, NULL) == 0); } }