Пример #1
0
void main(int argc, char **argv) {
    struct priority_queue *p;
    int i, tmp1, tmp2;
    
    p = pq_new(20);

    for (i = 0; i < 20; i++) {
        tmp1 = rand() % 100;
        tmp2 = rand() % 100;

        printf("Inserting %d from %d\n",tmp1, tmp2);
        pq_insert(p, tmp1, tmp2);
    }

    for (i = 0; i < 20; i++) {
        pq_extract(p, &tmp1, &tmp2);
        printf("Valor: %d from %d\n", tmp1, tmp2);
    }
    pq_free(p);

    p = pq_new(3);
    pq_insert(p, -13, 0);
    pq_insert(p, -12, 0);
    pq_insert(p, -9, 0);
    pq_extract(p, &tmp1, &tmp2);
    printf("Valor: %d from %d\n", tmp1, tmp2);
    pq_extract(p, &tmp1, &tmp2);
    printf("Valor: %d from %d\n", tmp1, tmp2);
    pq_extract(p, &tmp1, &tmp2);
        printf("Valor: %d from %d\n", tmp1, tmp2);

}
Пример #2
0
int main() {
    PQ_PTR pq = pq_create(10, 0);

    pq_insert(pq, 0, 5);
    pq_insert(pq, 1, 10);
    pq_insert(pq, 2, -3);
    pq_insert(pq, 3, 2);
    pq_insert(pq, 4, 20);
    pq_insert(pq, 5, -80);
    pq_insert(pq, 6, -40);
    pq_insert(pq, 7, 33);
    pq_insert(pq, 8, 120);
    pq_insert(pq, 9, 7);

    printf("size of queue: %d\n", pq_size(pq));

    double p;
    int id;

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_remove_by_id(pq, 2);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_free(pq);

    return 0;
}
Пример #3
0
int main(int argc, char **argv) {
    unsigned int exp_universe, big, exp_n, n, n_1, i, j, m, repeats, alpha;
    struct priority_queue *pq, *pq1, *pq2;
    unsigned int *elems;
    unsigned int idx;
    struct timespec before, after;

    if (argc < 6) {
        printf("Usage : run_once exp_universe exp_n percentage filename repetitions big\n");
        printf("Example : run_once 11 5 16 test_file 2 1 will run a test with:\n \t2^16 integers in the range [0,...2^11 - 1]\n\tOne of the structures will hold approximately 16 percent of the elements; the remaining amount will be in the other.\n\tThe test will be repeated (without any change in the input integers) 2 times.\n\tThe test is big so times will be measured in ms.\n");
        exit(1);
    }

    big = atoi(argv[6]);
    exp_universe = atoi(argv[1]); 
    exp_n = atoi(argv[2]); 
    alpha = atoi(argv[3]); 
    repeats = atoi(argv[5]);
    n = 1 << exp_n;

    n_1 = (alpha * n) / 100; 

    elems = gen_instance(argv[4], n, exp_universe);


    for (m = 0; m < repeats; m++) {

    pq1 = pq_new(n_1, exp_universe);
    pq2 = pq_new(n - n_1, exp_universe);


    for (i = 0; i < n_1; i++) {
        pq_insert(pq1, elems[i]);
    }
    for (; i < n; i++) {
        pq_insert(pq2, elems[i]);
    }

    clock_gettime(CLOCK_MONOTONIC, &before);
    pq = pq_merge(pq1, pq2);
    clock_gettime(CLOCK_MONOTONIC, &after);

    if (big) {
        printf("merge: %lldms\n", timespec_diff_ms(after, before));
    } else {
        printf("merge: %lldns\n", timespec_diff_ns(after, before));
    }

    pq_free(pq);
    }
    free(elems);
}
Пример #4
0
int main()
{
    prio_q* pq = calloc(1,sizeof(prio_q));
    printf("%u elements inserted.\n", pq->n);
    pq_insert(pq, 5);
    pq_insert(pq, 50);
    pq_insert(pq, 60);
    pq_insert(pq, 70);
    pq_insert(pq, 80);
    pq_insert(pq, 90);
    pq_insert(pq, 342);
    pq_insert(pq, 3);
    printf("%u elements inserted.\n", pq->n);
    pq_print_tree(pq);

    printf("Trying to dequeue..\n");
    int foo = pq_dequeue(pq);
    printf("Got %d..\n", foo);
    pq_print_tree(pq);

    printf("Trying to run through..\n");
    pq_run(pq);
    int a[5] = {4543,-33,4,54,2};
    printf("Original: ");
    int j = 0;
    for(; j < 5; ++j) printf("%d ", a[j]);
    printf("\n");
    heapsort(a, 5);
    printf("Heapsorted: ");
    for(j=0; j < 5; ++j) printf("%d ", a[j]);
    printf("\n");
    free(pq);
    return 0;
}
Пример #5
0
void kruskal()
{
	int i, e;
	pq_init();
	find_init();

	//1. 모든 정점을 pq삽입한다.
	// 우선순위 큐가 가중치가 가장 낮은 것이 루트가 되도록 정렬
	for (i = 0; i < E; i++)
		pq_insert(i);

	while (!pq_empty())
	{
		e = pq_remove();
		//2. 분리집합인지 확인한다.
		//   edge[e]의 양쪽 정점이 같은 집합에 속해 있는지 확인 한 후
		//   같은 집합이 아니면 최소 신장 트리에 추가
		//   집합을 합친다.
		if (find_set(edge[e].v1, edge[e].v2))
		{
			printf("%c %c\n", int2name(edge[e].v1), int2name(edge[e].v2));
			cost += edge[e].weight;
		}
	}
	printf("최소비용 : %d\n", cost);
}
Пример #6
0
Файл: k9.c Проект: hpersh/k9
void
rdy_insert(struct k9_task *task)
{
  if (task == idle_task)  return;

  pq_insert(rdy_pq, task->u->ready->pq_node);
}
Пример #7
0
static void
sender_insertProducts(void)
{
    pqueue* pq;
    int     status = pq_open(UP7_PQ_PATHNAME, 0, &pq);

    CU_ASSERT_EQUAL_FATAL(status, 0);

    product        prod;
    prod_info*     info = &prod.info;
    char           ident[80];
    void*          data = NULL;
    unsigned short xsubi[3] = {(unsigned short)1234567890,
                               (unsigned short)9876543210,
                               (unsigned short)1029384756
                              };
    info->feedtype = EXP;
    info->ident = ident;
    info->origin = "localhost";
    (void)memset(info->signature, 0, sizeof(info->signature));

    for (int i = 0; i < NUM_PRODS; i++) {
        const unsigned size = MAX_PROD_SIZE*erand48(xsubi) + 0.5;
        const ssize_t  nbytes = snprintf(ident, sizeof(ident), "%d", i);

        CU_ASSERT_TRUE_FATAL(nbytes >= 0 && nbytes < sizeof(ident));
        status = set_timestamp(&info->arrival);
        CU_ASSERT_EQUAL_FATAL(status, 0);
        info->seqno = i;
        uint32_t signet = htonl(i);
        (void)memcpy(info->signature+sizeof(signaturet)-sizeof(signet), &signet,
                     sizeof(signet));
        info->sz = size;

        data = realloc(data, size);
        CU_ASSERT_PTR_NOT_NULL(data);
        prod.data = data;

        status = pq_insert(pq, &prod);
        CU_ASSERT_EQUAL_FATAL(status, 0);
        char buf[LDM_INFO_MAX];
        LOG_ADD1("Inserted: prodInfo=\"%s\"",
                 s_prod_info(buf, sizeof(buf), info, 1));
        log_log(LOG_INFO);

        struct timespec duration;
        duration.tv_sec = 0;
        duration.tv_nsec = 5000000; // 5 ms
        status = nanosleep(&duration, NULL);
        CU_ASSERT_EQUAL_FATAL(status, 0);
    }

    free(data);
    status = pq_close(pq);
    CU_ASSERT_EQUAL_FATAL(status, 0);
}
Пример #8
0
int main(int argc, char *argv[])
{
    pq_init(5);
    assert(pq_empty());

    pq_insert(1);
    pq_insert(2);
    pq_insert(3);
    pq_insert(4);
    pq_insert(5);
    pq_show(); nl();

    int i;
    for (i = 0; i < 5; i++)
    {
        pq_delmax();
        pq_show(); nl();
    }

    return 0;
}
Пример #9
0
static void push(graph_t *graph, int source, int sink, int weight, void *data)
{
    push_data_t *d;
    pq_elem_t e;

    d = data;

    e.distance = d->dist[source] + weight;
    e.source = source;
    e.sink = sink;

    pq_insert(d->pq, &e);
}
Пример #10
0
Файл: k9.c Проект: hpersh/k9
static void
tmout_insert(struct k9_task * const task, unsigned tmout)
{
  struct list *p;

  if (tmout == K9_TMOUT_FOREVER) {
    pq_node_init(task->u->blocked->tmout->pq_node);

    return;
  }
  
  task->u->blocked->tmout->deadline = cur_ticks + tmout;
  
  pq_insert(tmout_pq, task->u->blocked->tmout->pq_node);
}
Пример #11
0
// Sort a list
void heapsort(int s[], int i)
{
    prio_q* pq = calloc(1,sizeof(prio_q));
    int j;
    // Insert them all in a heap
    for(j=0; j<i; ++j)
    {
        pq_insert(pq, s[j]);
    }
    pq_print_tree(pq);
    // Now replace them in the original array
    for(j=0; j<i; ++j)
    {
        s[j] = pq_dequeue(pq);
    }
    free(pq);
}
Пример #12
0
static char *pq_min_test()
{
    int i;
    int test_data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    struct pq *queue = pq_create(int_min_cmp);

    for (i = 0; i < 10; i++) {
        pq_insert(queue, &test_data[i]);
    }

    for (i = 0; i < 10; i++) {
        int val = *((int *)pq_remove(queue));
        mu_assert("values removed out of order\n", val == test_data[i]);
    }

    pq_destroy(queue);

    return 0;
}
Пример #13
0
int main(void)
{
    int n = 5;
    pq_t pq = pq_init(n);
    int i;

    for (i = 0; i < n; i++) {
        int item = rand() % 100;
        printf("%3d ", item);
        pq_insert(pq, item);
    }

    putchar('\n');

    /*pq_print(pq);*/
    while (!pq_empty(pq)) {
        printf("%3d ", pq_delmax(pq));
    }

    putchar('\n');
    pq_finalize(&pq);
    return 0;
}
Пример #14
0
int main(
        int ac,
        char *av[]
)
{
        const char* const       pqfname = getQueuePath();
        const char* const progname = ubasename(av[0]);
        int useProductID = FALSE;
        int signatureFromId = FALSE;
        char *productID = NULL;
        int multipleFiles = FALSE;
        char identifier[KEYSIZE];
        int status;
        int seq_start = 0;
        enum ExitCode {
            exit_success = 0,   /* all files inserted successfully */
            exit_system = 1,    /* operating-system failure */
            exit_pq_open = 2,   /* couldn't open product-queue */
            exit_infile = 3,    /* couldn't process input file */
            exit_dup = 4,       /* input-file already in product-queue */
            exit_md5 = 6        /* couldn't initialize MD5 processing */
        } exitCode = exit_success;

#if !USE_MMAP
        pqeIndex = PQE_NONE;
#endif

        {
            extern int optind;
            extern int opterr;
            extern char *optarg;
            int ch;

            (void) openulog(progname, LOG_NOTIME, LOG_LDM, "-");
            (void) setulogmask(LOG_UPTO(LOG_NOTICE));

            opterr = 0; /* Suppress getopt(3) error messages */

            while ((ch = getopt(ac, av, ":ivxl:q:f:s:p:")) != EOF)
                    switch (ch) {
                    case 'i':
                            signatureFromId = 1;
                            break;
                    case 'v':
                            (void) setulogmask(getulogmask() | LOG_MASK(LOG_INFO));
                            break;
                    case 'x':
                            (void) setulogmask(getulogmask() | LOG_MASK(LOG_DEBUG));
                            break;
                    case 'l':
                            openulog(progname, ulog_get_options(), LOG_LDM, optarg);
                            break;
                    case 'q':
                            setQueuePath(optarg);
                            break;
                    case 's':
                            seq_start = atoi(optarg);
                            break;
                    case 'f':
                            feedtype = atofeedtypet(optarg);
                            if(feedtype == NONE)
                            {
                                fprintf(stderr, "Unknown feedtype \"%s\"\n", optarg);
                                    usage(progname);
                            }
                            break;
                    case 'p':
                            useProductID = TRUE;
                            productID = optarg;
                            break;
                    case ':': {
                        LOG_ADD1("Option \"-%c\" requires an operand", optopt);
                        usage(progname);
                    }
                    /* no break */
                    default:
                        LOG_ADD1("Unknown option: \"%c\"", optopt);
                        usage(progname);
                        /* no break */
                    }

            ac -= optind; av += optind ;

            if(ac < 1) usage(progname);
            }

        /*
         * register exit handler
         */
        if(atexit(cleanup) != 0)
        {
                serror("atexit");
                exit(exit_system);
        }

        /*
         * set up signal handlers
         */
        set_sigactions();

        /*
         * who am i, anyway
         */
        (void) strncpy(myname, ghostname(), sizeof(myname));
        myname[sizeof(myname)-1] = 0;

        /*
         * open the product queue
         */
        if(status = pq_open(pqfname, PQ_DEFAULT, &pq))
        {
                if (PQ_CORRUPT == status) {
                    uerror("The product-queue \"%s\" is inconsistent\n",
                            pqfname);
                }
                else {
                    uerror("pq_open: \"%s\" failed: %s",
                            pqfname, status > 0 ? strerror(status) :
                                            "Internal error");
                }
                exit(exit_pq_open);
        }


        {
        char *filename;
        int fd;
        struct stat statb;
        product prod;
        MD5_CTX *md5ctxp = NULL;

        /*
         * Allocate an MD5 context
         */
        md5ctxp = new_MD5_CTX();
        if(md5ctxp == NULL)
        {
                serror("new_md5_CTX failed");
                exit(exit_md5);
        }


        /* These members are constant over the loop. */
        prod.info.origin = myname;
        prod.info.feedtype = feedtype;

        if (ac > 1) {
          multipleFiles = TRUE;
        }

        for(prod.info.seqno = seq_start ; ac > 0 ;
                         av++, ac--, prod.info.seqno++)
        {
                filename = *av;

                fd = open(filename, O_RDONLY, 0);
                if(fd == -1)
                {
                        serror("open: %s", filename);
                        exitCode = exit_infile;
                        continue;
                }

                if( fstat(fd, &statb) == -1) 
                {
                        serror("fstat: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                /* Determine what to use for product identifier */
                if (useProductID) 
                  {
                    if (multipleFiles) 
                      {
                        sprintf(identifier,"%s.%d", productID, prod.info.seqno);
                        prod.info.ident = identifier;
                      }
                    else
                      prod.info.ident = productID;
                   }
                else
                    prod.info.ident = filename;
                
                prod.info.sz = statb.st_size;
                prod.data = NULL;

                /* These members, and seqno, vary over the loop. */
                status = set_timestamp(&prod.info.arrival);
                if(status != ENOERR) {
                        serror("set_timestamp: %s, filename");
                        exitCode = exit_infile;
                        continue;
                }

#if USE_MMAP
                prod.data = mmap(0, prod.info.sz,
                        PROT_READ, MAP_PRIVATE, fd, 0);
                if(prod.data == NULL)
                {
                        serror("mmap: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                status = 
                    signatureFromId
                        ? mm_md5(md5ctxp, prod.info.ident,
                            strlen(prod.info.ident), prod.info.signature)
                        : mm_md5(md5ctxp, prod.data, prod.info.sz,
                            prod.info.signature);

                (void)exitIfDone(1);

                if (status != 0) {
                    serror("mm_md5: %s", filename);
                    (void) munmap(prod.data, prod.info.sz);
                    (void) close(fd);
                    exitCode = exit_infile;
                    continue;
                }

                /* These members, and seqno, vary over the loop. */
                status = set_timestamp(&prod.info.arrival);
                if(status != ENOERR) {
                        serror("set_timestamp: %s, filename");
                        exitCode = exit_infile;
                        continue;
                }

                /*
                 * Do the deed
                 */
                status = pq_insert(pq, &prod);

                switch (status) {
                case ENOERR:
                    /* no error */
                    if(ulogIsVerbose())
                        uinfo("%s", s_prod_info(NULL, 0, &prod.info,
                            ulogIsDebug())) ;
                    break;
                case PQUEUE_DUP:
                    uerror("Product already in queue: %s",
                        s_prod_info(NULL, 0, &prod.info, 1));
                    exitCode = exit_dup;
                    break;
                case PQUEUE_BIG:
                    uerror("Product too big for queue: %s",
                        s_prod_info(NULL, 0, &prod.info, 1));
                    exitCode = exit_infile;
                    break;
                case ENOMEM:
                    uerror("queue full?");
                    exitCode = exit_system;
                    break;  
                case EINTR:
#if defined(EDEADLOCK) && EDEADLOCK != EDEADLK
                case EDEADLOCK:
                    /*FALLTHROUGH*/
#endif
                case EDEADLK:
                    /* TODO: retry ? */
                    /*FALLTHROUGH*/
                default:
                    uerror("pq_insert: %s", status > 0
                        ? strerror(status) : "Internal error");
                    break;
                }

                (void) munmap(prod.data, prod.info.sz);
#else // USE_MMAP above; !USE_MMAP below
                status = 
                    signatureFromId
                        ? mm_md5(md5ctxp, prod.info.ident,
                            strlen(prod.info.ident), prod.info.signature)
                        : fd_md5(md5ctxp, fd, statb.st_size,
                            prod.info.signature);

                (void)exitIfDone(1);

                if (status != 0) {
                        serror("xx_md5: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                if(lseek(fd, 0, SEEK_SET) == (off_t)-1)
                {
                        serror("rewind: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                pqeIndex = PQE_NONE;
                status = pqe_new(pq, &prod.info, &prod.data, &pqeIndex);

                if(status != ENOERR) {
                    serror("pqe_new: %s", filename);
                    exitCode = exit_infile;
                }
                else {
                    ssize_t     nread = read(fd, prod.data, prod.info.sz);

                    (void)exitIfDone(1);

                    if (nread != prod.info.sz) {
                        serror("read %s %u", filename, prod.info.sz);
                        status = EIO;
                    }
                    else {
                        status = pqe_insert(pq, pqeIndex);
                        pqeIndex = PQE_NONE;

                        switch (status) {
                        case ENOERR:
                            /* no error */
                            if(ulogIsVerbose())
                                uinfo("%s", s_prod_info(NULL, 0, &prod.info,
                                    ulogIsDebug())) ;
                            break;
                        case PQUEUE_DUP:
                            uerror("Product already in queue: %s",
                                s_prod_info(NULL, 0, &prod.info, 1));
                            exitCode = exit_dup;
                            break;
                        case ENOMEM:
                            uerror("queue full?");
                            break;  
                        case EINTR:
#if defined(EDEADLOCK) && EDEADLOCK != EDEADLK
                        case EDEADLOCK:
                            /*FALLTHROUGH*/
#endif
                        case EDEADLK:
                            /* TODO: retry ? */
                            /*FALLTHROUGH*/
                        default:
                            uerror("pq_insert: %s", status > 0
                                ? strerror(status) : "Internal error");
                        }
                    }                   /* data read into `pqeIndex` region */

                    if (status != ENOERR) {
                        (void)pqe_discard(pq, pqeIndex);
                        pqeIndex = PQE_NONE;
                    }
                }                       /* `pqeIndex` region allocated */

#endif
                (void) close(fd);
        }                               /* input-file loop */

        free_MD5_CTX(md5ctxp);  
        }                               /* code block */

        exit(exitCode);
}
Пример #15
0
static int process_file(void){

  struct stat statb;
  int ccb_offset = g.opt_ccbsize;
  int status = 0;
  product prod;
  ssize_t nread;
  ssize_t filedata_size;
  unsigned char *p;
  int n;
  
  status = pq_open(g.opt_pqfname, PQ_DEFAULT, &g.pq);
  if(status != 0){
    if(status == PQ_CORRUPT)
      errx(1, "The product queue %s is inconsistent.", g.opt_pqfname);
    else 
      err(1, "pq_open");
  }

  prod.info.origin = g.opt_origin;
  prod.info.feedtype = g.opt_feedtype;
  set_timestamp(&prod.info.arrival);
  prod.info.seqno = g.seq;
  prod.info.ident = g.opt_prodid;
  if(prod.info.ident == NULL)
    prod.info.ident = g.input_fname;

  /*
   * This should not happen because it has been checked in option processing.
   */
  if(prod.info.ident == NULL)
    errx(1, "No prodid specified.");

  if(g.input_fname == NULL){
    g.fd = STDIN_FILENO;
    filedata_size = g.opt_filesize;
  }else {
    g.fd = open(g.input_fname, O_RDONLY, 0);
    if(g.fd == -1)
      err(1, "open");
	
    if(g.opt_filesize == 0){
      if(fstat(g.fd, &statb) == -1)
	err(1, "fstat");
      
      filedata_size = statb.st_size;
    }else
      filedata_size = g.opt_filesize;
  }

  if(g.opt_noccb == 0){
    if(filedata_size <= g.opt_ccbsize)
      input_file_errx(1, "No data in inputfile");
    else
      filedata_size -= g.opt_ccbsize;
  }else
    ccb_offset = 0;

  prod.info.sz = filedata_size;
  if(g.opt_gempak == 1){
    prod.info.sz += GMPK_HEADER_SIZE + GMPK_TRAILER_SIZE;
  }

  if(prod.info.sz <= STATIC_POOL_SIZE)
    prod.data = (void*)static_pool;
  else
    prod.data = malloc(prod.info.sz);

  if(prod.data == NULL)
    err(1, "malloc");

  if(ccb_offset != 0){
    if(lseek(g.fd, ccb_offset, SEEK_SET) == -1)
      input_file_err(1, "lseek error in inputfile");
  }

  p = (unsigned char*)prod.data;

  if(g.opt_gempak == 1){
    n = sprintf(gmpk_header_str, gmpk_header_fmt, (int)(g.seq % 1000));
    if(n != GMPK_HEADER_SIZE)
      errx(1, "gmpk_header_fmt format error.");

    memcpy(p, gmpk_header_str, GMPK_HEADER_SIZE);
    p += GMPK_HEADER_SIZE;
  }

  /*
   * Use readn when nbsp2ldm is opened as a pipe in tcl.
   */
  nread = readn(g.fd, p, filedata_size);
  if(nread != filedata_size)
    input_file_err(1, "read() error");

  if((g.opt_seq_str != NULL) && (g.opt_md5seq == 1))
    status = calc_md5((unsigned char*)g.opt_seq_str, strlen(g.opt_seq_str),
		      prod.info.signature);
  else
    status = calc_md5(p, filedata_size, prod.info.signature);

  if (status != 0)
    errx(1, "calc_md5");

  if(g.opt_gempak == 1){
    p += filedata_size;
    memcpy(p, gmpk_trailer_str, GMPK_TRAILER_SIZE);
  }

  status = pq_insert(g.pq, &prod);    
  if(status == PQUEUE_DUP){
    status = 0;
    warnx("Product already in queue: %s.", prod.info.ident);
  }else if(status != 0)
    errx(1, "pq_insert: %d", status);

  return(status);
}
int main( int argc, char** argv )
{
    uint64_t i;

    // pointers for casting
    pq_op_create *op_create;
    pq_op_destroy *op_destroy;
    pq_op_clear *op_clear;
    pq_op_get_key *op_get_key;
    pq_op_get_item *op_get_item;
    pq_op_get_size *op_get_size;
    pq_op_insert *op_insert;
    pq_op_find_min *op_find_min;
    pq_op_delete *op_delete;
    pq_op_delete_min *op_delete_min;
    pq_op_decrease_key *op_decrease_key;
    //pq_op_meld *op_meld;
    pq_op_empty *op_empty;

    // temp dummies for readability
    pq_type *q;//, *r;
    pq_node_type *n;

    if( argc < 2 )
        exit( -1 );

    int trace_file = open( argv[1], O_RDONLY );
    if( trace_file < 0 )
    {
        fprintf( stderr, "Could not open file.\n" );
        return -1;
    }

    pq_trace_header header;
    pq_trace_read_header( trace_file, &header );
    close( trace_file );

    //printf("Header: (%llu,%lu,%lu)\n",header.op_count,header.pq_ids,
    //    header.node_ids);

    pq_op_blank *ops = calloc( MIN( header.op_count, CHUNK_SIZE ),
        sizeof( pq_op_blank ) );
    pq_type **pq_index = calloc( header.pq_ids, sizeof( pq_type* ) );
    pq_node_type **node_index = calloc( header.node_ids,
        sizeof( pq_node_type* ) );
    if( ops == NULL || pq_index == NULL || node_index == NULL )
    {
        fprintf( stderr, "Calloc fail.\n" );
        return -1;
    }

#ifdef USE_QUAKE
    mem_capacities[0] = header.node_ids << 2;
#else
    mem_capacities[0] = header.node_ids;
#endif

#ifdef USE_EAGER
    mem_map *map = mm_create( mem_types, mem_sizes, mem_capacities );
#else
    mem_map *map = mm_create( mem_types, mem_sizes );
#endif

    uint64_t op_remaining, op_chunk;
    int status;
    struct timeval t0, t1;
    uint32_t iterations = 0;
    uint32_t total_time = 0;
    key_type k;
    //pq_node_type *min;

#ifndef CACHEGRIND
    while( iterations < 5 || total_time < PQ_MIN_USEC )
    {
        mm_clear( map );
        iterations++;
#endif

        trace_file = open( argv[1], O_RDONLY );
        if( trace_file < 0 )
        {
            fprintf( stderr, "Could not open file.\n" );
            return -1;
        }
        pq_trace_read_header( trace_file, &header );
        op_remaining = header.op_count;

        while( op_remaining > 0 )
        {
            op_chunk = MIN( CHUNK_SIZE, op_remaining );
            op_remaining -= op_chunk;

            for( i = 0; i < op_chunk; i++ )
            {
                status = pq_trace_read_op( trace_file, ops + i );
                if( status == -1 )
                {
                    fprintf( stderr, "Invalid operation!" );
                    return -1;
                }
            }

#ifndef CACHEGRIND
            gettimeofday(&t0, NULL);
#endif

            for( i = 0; i < op_chunk; i++ )
            {
                switch( ops[i].code )
                {
                    case PQ_OP_CREATE:
                        op_create = (pq_op_create*) ( ops + i );
                        //printf("pq_create(%d)\n", op_create->pq_id);
                        pq_index[op_create->pq_id] = pq_create( map );
                        break;
                    case PQ_OP_DESTROY:
                        op_destroy = (pq_op_destroy*) ( ops + i );
                        //printf("pq_destroy(%d)\n", op_destroy->pq_id);
                        q = pq_index[op_destroy->pq_id];
                        pq_destroy( q );
                        pq_index[op_destroy->pq_id] = NULL;
                        break;
                    case PQ_OP_CLEAR:
                        op_clear = (pq_op_clear*) ( ops + i );
                        //printf("pq_clear(%d)\n", op_clear->pq_id );
                        q = pq_index[op_clear->pq_id];
                        pq_clear( q );
                        break;
                    case PQ_OP_GET_KEY:
                        op_get_key = (pq_op_get_key*) ( ops + i );
                        //printf("pq_get_key(%d,%d)\n", op_get_key->pq_id,
                        //    op_get_key->node_id );
                        q = pq_index[op_get_key->pq_id];
                        n = node_index[op_get_key->node_id];
                        pq_get_key( q, n );
                        break;
                    case PQ_OP_GET_ITEM:
                        op_get_item = (pq_op_get_item*) ( ops + i );
                        //printf("pq_get_item(%d,%d)\n", op_get_item->pq_id,
                        //    op_get_item->node_id);
                        q = pq_index[op_get_item->pq_id];
                        n = node_index[op_get_item->node_id];
                        pq_get_item( q, n );
                        break;
                    case PQ_OP_GET_SIZE:
                        op_get_size = (pq_op_get_size*) ( ops + i );
                        //printf("pq_get_size(%d)\n", op_get_size->pq_id);
                        q = pq_index[op_get_size->pq_id];
                        pq_get_size( q );
                        break;
                    case PQ_OP_INSERT:
                        op_insert = (pq_op_insert*) ( ops + i );
                        //printf("pq_insert(%d,%d,%llu,%d)\n", op_insert->pq_id,
                        //    op_insert->node_id, op_insert->key, op_insert->item );
                        q = pq_index[op_insert->pq_id];
                        node_index[op_insert->node_id] = pq_insert( q,
                            op_insert->item, op_insert->key );
                        break;
                    case PQ_OP_FIND_MIN:
                        op_find_min = (pq_op_find_min*) ( ops + i );
                        //printf("pq_find_min(%d)\n", op_find_min->pq_id );
                        q = pq_index[op_find_min->pq_id];
                        pq_find_min( q );
                        break;
                    case PQ_OP_DELETE:
                        op_delete = (pq_op_delete*) ( ops + i );
                        //printf("pq_delete(%d,%d)\n", op_delete->pq_id,
                        //    op_delete->node_id );
                        q = pq_index[op_delete->pq_id];
                        n = node_index[op_delete->node_id];
                        pq_delete( q, n );
                        break;
                    case PQ_OP_DELETE_MIN:
                        op_delete_min = (pq_op_delete_min*) ( ops + i );
                        //printf("pq_delete_min(%d)\n", op_delete_min->pq_id);
                        q = pq_index[op_delete_min->pq_id];
                        //min = pq_find_min( q );
                        k = pq_delete_min( q );
#ifdef CACHEGRIND
                        if( argc > 2 )
                            printf("%llu\n",k);
#endif
                        break;
                    case PQ_OP_DECREASE_KEY:
                        op_decrease_key = (pq_op_decrease_key*) ( ops + i );
                        //printf("pq_decrease_key(%d,%d,%llu)\n", op_decrease_key->pq_id,
                        //    op_decrease_key->node_id, op_decrease_key->key);
                        q = pq_index[op_decrease_key->pq_id];
                        n = node_index[op_decrease_key->node_id];
                        pq_decrease_key( q, n, op_decrease_key->key );
                        break;
                    /*case PQ_OP_MELD:
                        printf("Meld.\n");
                        op_meld = (pq_op_meld*) ( ops + i );
                        q = pq_index[op_meld->pq_src1_id];
                        r = pq_index[op_meld->pq_src2_id];
                        pq_index[op_meld->pq_dst_id] = pq_meld( q, r );
                        break;*/
                    case PQ_OP_EMPTY:
                        op_empty = (pq_op_empty*) ( ops + i );
                        //printf("pq_empty(%d)\n", op_empty->pq_id);
                        q = pq_index[op_empty->pq_id];
                        pq_empty( q );
                        break;
                    default:
                        break;
                }
                //verify_queue( pq_index[0], header.node_ids );
            }

#ifndef CACHEGRIND
            gettimeofday(&t1, NULL);
            total_time += (t1.tv_sec - t0.tv_sec) * 1000000 +
                (t1.tv_usec - t0.tv_usec);
#endif
        }

        close( trace_file );
#ifndef CACHEGRIND
    }
#endif

    for( i = 0; i < header.pq_ids; i++ )
    {
        if( pq_index[i] != NULL )
            pq_destroy( pq_index[i] );
    }

    mm_destroy( map );
    free( pq_index );
    free( node_index );
    free( ops );

#ifndef CACHEGRIND
    printf( "%d\n", total_time / iterations );
#endif

    return 0;
}
Пример #17
0
static int process_file(void) {
  /*
   * Process a file given in the command line or its data from stdin
   */
  struct stat statb;
  int ccb_offset = g.opt_ccbsize;
  int status = 0;
  product prod;
  ssize_t nread;
  ssize_t filedata_size;
  unsigned char *p;
  int n;

#ifdef TEST
  test_print();
  return(0);
#endif
  
  prod.info.origin = g.opt_origin;
  prod.info.feedtype = g.opt_feedtype;
  set_timestamp(&prod.info.arrival);
  prod.info.seqno = g.seq;
  prod.info.ident = g.opt_prodid;
  if(prod.info.ident == NULL)
    prod.info.ident = g.input_fname;

  /*
   * This should not happen because it has been checked in option processing.
   */
  if(prod.info.ident == NULL)
    log_errx(1, "No prodid specified.");

  if(g.input_fname == NULL){
    g.fd = STDIN_FILENO;
    filedata_size = g.opt_filesize;
  } else {
    g.fd = open(g.input_fname, O_RDONLY, 0);
    if(g.fd == -1)
      input_file_err(1, "Error from open()");
	
    if(g.opt_filesize == 0){
      if(fstat(g.fd, &statb) == -1)
	input_file_err(1, "Error from fstat()");
      
      filedata_size = statb.st_size;
    }else
      filedata_size = g.opt_filesize;
  }

  if(g.opt_noccb == 0){
    if(filedata_size <= g.opt_ccbsize)
      input_file_errx(1, "No data in inputfile");
    else
      filedata_size -= g.opt_ccbsize;
  }else
    ccb_offset = 0;

  prod.info.sz = filedata_size;
  if(g.opt_gempak == 1){
    prod.info.sz += GMPK_HEADER_SIZE + GMPK_TRAILER_SIZE;
  }

  if(prod.info.sz <= STATIC_POOL_SIZE)
    prod.data = (void*)static_pool;
  else if(prod.info.sz <= g.dynamic_pool_size)
    prod.data = g.dynamic_pool;
  else {
    prod.data = malloc(prod.info.sz);
    if(prod.data == NULL)
      log_err(1, "Error frrom malloc()");

    if(g.dynamic_pool != NULL)
      free(g.dynamic_pool);
    
    g.dynamic_pool = prod.data;
    g.dynamic_pool_size = prod.info.sz;
  }

  if(ccb_offset != 0){
    if(lseek(g.fd, ccb_offset, SEEK_SET) == -1)
      input_file_err(1, "Error from lseek()");
  }

  p = (unsigned char*)prod.data;

  if(g.opt_gempak == 1){
    n = sprintf(gmpk_header_str, gmpk_header_fmt, (int)(g.seq % 1000));
    if(n != GMPK_HEADER_SIZE)
      errx(1, "gmpk_header_fmt format error.");

    memcpy(p, gmpk_header_str, GMPK_HEADER_SIZE);
    p += GMPK_HEADER_SIZE;
  }

  /*
   * Use readn when nbsp2ldm is opened as a pipe in tcl.
   */
  nread = readn(g.fd, p, filedata_size);
  if(nread != filedata_size)
    input_file_err(1, "Error from read()");

  if((g.opt_md5seq == 1) && (g.opt_seq_str != NULL))
    status = calc_md5((unsigned char*)g.opt_seq_str,
		      strlen(g.opt_seq_str),
		      prod.info.signature);
  else
    status = calc_md5(p, filedata_size, prod.info.signature);

  if (status != 0)
    log_errx(1, "Error from calc_md5()");

  if(g.opt_gempak == 1){
    p += filedata_size;
    memcpy(p, gmpk_trailer_str, GMPK_TRAILER_SIZE);
  }

  status = pq_insert(g.pq, &prod);    
  if(status == PQUEUE_DUP){
    status = 0;
    log_warnx("Product already in queue: %s.", prod.info.ident);
  }else if(status != 0)
    log_errx(1, "Error from pq_insert: %d", status);
  else if(g.opt_verbose == 1)
    log_info("Inserted %s", prod.info.ident);

  if((g.fd != -1) && (g.fd != STDIN_FILENO)){
    close(g.fd);
    g.fd = -1;
  }

  resetdefaults();    /* reset the option parameters for the next file */
  
  return(status != 0 ? 1 : 0);
}
Пример #18
0
/*
@function main
*/
int main(int argc, char** argv)
{
	/*test pq*/
	struct tnode* p = NULL;
	struct tnode* lc, *rc;
	int NCHAR = 256; /*number of characters*/
	int i = 0;
	unsigned char c = 0;
	int usedAsciiKind = 0;

	float totalCount = 0.f;
	int count[256] = { 0 };
	float freq[256] = { 0.f };

	char INPUT_FILE[256];
	char CODE_FILE[256];
	char OUTPUT_FILE[256];

	FILE* fin = NULL;
	FILE* fout = NULL;
	FILE* fcode = NULL;

	/*zero out code*/
	memset(code, 0, sizeof(code));

	if (argc == 1) {
		printf("USAGE : %s file_to_compress", argv[0]);
		return 0;
	}
	else {
		strcpy_s(INPUT_FILE, _countof(INPUT_FILE), argv[1]);
		strcpy_s(OUTPUT_FILE, _countof(OUTPUT_FILE), argv[1]);
		strcat_s(OUTPUT_FILE, _countof(OUTPUT_FILE), ".huff");
		strcpy_s(CODE_FILE, _countof(CODE_FILE), argv[1]);
		strcat_s(CODE_FILE, _countof(CODE_FILE), ".code");
	}

	fopen_s(&fin, INPUT_FILE, "rb");
	fseek(fin, 0, SEEK_END);
	streamLength = ftell(fin);
	rewind(fin);
	//일단 빈도 통계를 내야 하므로 처음부터 끝까지 읽어가며 각 아스키 캐릭터들의 수를 센다
	//streamLength = 0;
	while (1) {
		if (feof(fin))
			break;
		fread_s(&c, sizeof(unsigned char), sizeof(unsigned char), 1, fin);
		
		totalCount += 1.;
		count[c]++;
		//streamLength++;
	}
	fclose(fin);

	qhead = NULL;
	/*initialize with freq*/
	for (i = 0; i<NCHAR; i++)
	{
		if (count[i] > 0) {
			pq_insert(talloc(i, count[i]));
			usedAsciiKind++;
		}
	}

	/*build tree*/
	for (i = 0; i < (usedAsciiKind - 1); i++)
	{
		lc = pq_pop();
		rc = pq_pop();
		/*create parent*/
		p = talloc(0, lc->appear + rc->appear);
		/*set parent link*/
		lc->parent = rc->parent = p;
		/*set child link*/
		p->right = rc; p->left = lc;
		/*make it non-leaf*/
		p->isleaf = 0;
		/*add the new node to the queue*/
		pq_insert(p);
	}

	/*get root*/
	root = pq_pop();

	/*build code*/
	generate_code(root, 0);

	/*output code*/
	fopen_s(&fout, CODE_FILE, "wb");
	//dump_code(fout);
	dump_code_bin(fout);
	fclose(fout);

	/*encode a sample string*/
	fopen_s(&fin, INPUT_FILE, "rb");
	fopen_s(&fout, OUTPUT_FILE, "wb");
	encodeBin(fin, fout);
	fclose(fin);
	fclose(fout);
	/*TODO: clear resources*/
	freetree(root);
	freequeue(qhead);

	return 0;
}
Пример #19
0
int
main (int ac, char *av[])
{
  char *progname = av[0];
  int status;
  int seq_start = 0;
  stat_info *sinfo, *shead = NULL, *slast = NULL;
  int statusoff=0;

  /*
   * Set up error logging
   */
    if (log_init(progname)) {
        log_syserr("Couldn't initialize logging module");
        exit(1);
    }

  const char* pqfname = getQueuePath();

  /*
   * Check the environment for some options.
   * May be overridden by command line switches below.
   */
  {
    const char *ldmpqfname = getenv ("LDMPQFNAME");
    if (ldmpqfname != NULL)
      pqfname = ldmpqfname;
  }

  {
    extern int optind;
    extern int opterr;
    extern char *optarg;
    int ch;

    opterr = 1;

    while ((ch = getopt (ac, av, "vxl:q:f:s:S")) != EOF)
      switch (ch)
	{
	case 'v':
          if (!log_is_enabled_info)
            (void)log_set_level(LOG_LEVEL_INFO);
	  break;
	case 'x':
          (void)log_set_level(LOG_LEVEL_DEBUG);
	  break;
	case 'l':
          if (log_set_destination(optarg)) {
              log_syserr("Couldn't set logging destination to \"%s\"",
                      optarg);
              exit(1);
          }
	  break;
	case 'q':
	  pqfname = optarg;
	  break;
	case 's':
	  seq_start = atoi (optarg);
	  break;
	case 'f':
	  feedtype = atofeedtypet (optarg);
	  if (feedtype == NONE)
	    {
	      fprintf (stderr, "Unknown feedtype \"%s\"\n", optarg);
	      usage (progname);
	    }
	  break;
	case 'S':
	     statusoff=1;
	     break;
	case '?':
	  usage (progname);
	  break;
	}

    setQueuePath(pqfname);

    ac -= optind;
    av += optind;

    if (ac < 1)
      usage (progname);
  }

  /*
   * register exit handler
   */
  if (atexit (cleanup) != 0)
    {
      log_syserr ("atexit");
      exit (1);
    }

  /*
   * set up signal handlers
   */
  set_sigactions ();

  /*
   * who am i, anyway
   */
  (void) strcpy (myname, ghostname ());

  /*
   * open the product queue
   */
  if ((status = pq_open (pqfname, PQ_DEFAULT, &pq)))
    {
      if (status > 0) {
          log_add_syserr("\"%s\" failed", pqfname);
          log_flush_error();
      }
      else {
          log_error_q("\"%s\" failed: %s", pqfname, "Internal error");
      }
      exit (2);
    }


  {
    char *filename;
    int fd;
    struct stat statb;
    product prod;
    unsigned char *prodmmap;
    MD5_CTX *md5ctxp = NULL;
    int gversion;

    /*
     * Allocate an MD5 context
     */
    md5ctxp = new_MD5_CTX ();
    if (md5ctxp == NULL)
      {
	log_syserr ("new_md5_CTX failed");
	exit (6);
      }

    /* These members are constant over the loop. */
    prod.info.origin = myname;
    prod.info.feedtype = feedtype;

    prod.info.seqno = seq_start;

    /*
     * Open the file to be inserted and process
     */
    while (ac > 0)
      {
        long insert_sum = 0;
	long sinfo_cnt = 0;
        long stat_size = 0;

	filename = *av;
	av++;
	ac--;

	log_notice_q ("open and memorymap %s\0", filename);

	fd = open (filename, O_RDONLY, 0);
	if (fd == -1)
	  {
	    log_syserr ("open: %s", filename);
	    continue;
	  }

	if (fstat (fd, &statb) == -1)
	  {
	    log_syserr ("fstat: %s", filename);
	    (void) close (fd);
	    continue;
	  }

	if ((prodmmap = (unsigned char *) mmap (0, statb.st_size,
				       PROT_READ, MAP_PRIVATE, fd,
				       0)) == MAP_FAILED)
	  {
	    log_syserr ("allocation failed");
	  }
	else
	  {
	    int GRIBDONE = 0;
	    off_t griboff = 0;
	    size_t griblen = 0;
	    log_notice_q ("%ld bytes memory mapped\0", (long) statb.st_size);

	    while (!GRIBDONE)
	      {
		log_debug("griboff %d\0", (int) griboff);
		/* get offset of next grib product */
		status =
		  get_grib_info (prodmmap, statb.st_size, &griboff, &griblen,
				 &gversion);

		switch (status)
		  {
		  case 0:
		    prod.data = prodmmap + griboff;
		    prod.info.sz = griblen;

		    /*
		     * revised MD5 calculation...using filename
		     * to allow duplicate products in different files.
		     */
		    MD5Init (md5ctxp);
  		    MD5Update (md5ctxp, (void *)filename, strlen(filename));
  		    /*MD5Update (md5ctxp, (void *)prod.data, prod.info.sz);*/
		    if ( prod.info.sz > 10000 )
  		       MD5Update (md5ctxp, (void *)prod.data, 10000);
		    else
  		       MD5Update (md5ctxp, (void *)prod.data, prod.info.sz);
  		    MD5Final (prod.info.signature, md5ctxp);

		    /*if (mm_md5 (md5ctxp, prod.data, prod.info.sz,
				prod.info.signature) != 0)
		      {
			log_error_q ("could not compute MD5\0");
		      }
		    else
		      { */
			prod.info.ident = (char *) malloc (KEYSIZE + 1);
			get_gribname (gversion, prod.data, prod.info.sz,
				      filename, prod.info.seqno,
				      prod.info.ident);
			/*
			 * Do the deed
			 */
			status = set_timestamp (&prod.info.arrival);
			if (status != ENOERR)
			  {
			    log_add_syserr("could not set timestamp");
                            log_flush_error();
			  }
			/*
			 * Insert the product
			 */
			status = pq_insert (pq, &prod);
			log_info_q ("%d %s\0", status, prod.info.ident);
                
			if ( status == ENOERR )
			   insert_sum += prod.info.sz;

			if (! statusoff )
			  {
			  /*
			   * Log this status
			   */
			    sinfo_cnt++;
			    sinfo = (stat_info *)malloc(sizeof(stat_info));
                   	    sinfo->insertstatus = status;
			    sinfo->prodname = (char *)malloc(strlen(prod.info.ident)+1);
                   	    strcpy(sinfo->prodname, prod.info.ident);
                   	    sinfo->seqno = prod.info.seqno;
                   	    sinfo->prodsz = prod.info.sz;
                   	    sinfo->next = NULL;
                   	    stat_size += strlen(sinfo->prodname);
                   	    if(shead == NULL)
			      {
                      	        shead = sinfo;
                      	        slast = sinfo;
                   	      }
                   	    else 
			      {
                      	        slast->next = sinfo;
                      	        slast = sinfo;
                   	      }
			  }
		      /*}*/
		    griboff += griblen;
		    prod.info.seqno++;
		    break;
		  case -1:
		    GRIBDONE = 1;
		    break;
		  case -2:
		    log_error_q ("truncated grib file at: %d", prod.info.seqno);
		    GRIBDONE = 1;
		    break;
		  case -7:
		    log_error_q ("End sequence 7777 not found where expected: %d",
			    prod.info.seqno);
		    griboff += griblen;
		    log_error_q("resume looking at %d\0",griboff);
		    break;
		  default:
		    log_error_q ("unknown error %d\0", status);
		    griboff += griblen;
		    if (griboff >= statb.st_size)
		      GRIBDONE = 1;
		    break;
		  }

		if (griboff >= statb.st_size)
		  GRIBDONE = 1;
	      }


	    log_notice_q ("munmap\0");
	    (void) munmap ((void *)prodmmap, statb.st_size);

	    if ( stat_size != 0 )
	    /*
	     * Add a status message to product queue
	     */
	      {
		char *statusmess;
		log_notice_q("stats_size %ld %ld\0",stat_size,sinfo_cnt);

		statusmess = calloc((30 * sinfo_cnt) + stat_size +
                        strlen(filename) + 128, sizeof(char));
                if(statusmess == NULL) 
		  {
              	    log_syserr("could not malloc status message %ld\0",
              	            stat_size);
           	  }
           	else
		  {
		    char tmpprod[512];
                    sinfo = shead; slast = NULL;

                    status = set_timestamp(&prod.info.arrival);
                    /* ctime ends with \n\0" */
                    sprintf(statusmess,"%s complete (%ld bytes) at %sInserted %ld of %ld\n",
                       filename,(long)statb.st_size, ctime(&prod.info.arrival.tv_sec),
                       insert_sum,(long)statb.st_size);

                    while(sinfo != NULL) 
		      {
                        sprintf(tmpprod,"%3d %5d %8d %s\n",sinfo->insertstatus,
                            sinfo->seqno,sinfo->prodsz,sinfo->prodname);
                        strcat(statusmess,tmpprod);

                        slast = sinfo;
                 	sinfo = sinfo->next;

                        free(slast->prodname);
                    	free(slast);

                      }

		    shead = NULL;

                    sprintf(tmpprod,".status.%s %06d",filename, prod.info.seqno);
                    prod.info.ident = tmpprod;
                    prod.data = statusmess;
                    prod.info.sz = strlen(statusmess);
                    status = mm_md5(md5ctxp, prod.data, prod.info.sz, prod.info.signature);
                    status = set_timestamp(&prod.info.arrival);
                    status = pq_insert(pq, &prod);
                    if(log_is_enabled_info)
                        log_info_q("%s", s_prod_info(NULL, 0, &prod.info,
                                log_is_enabled_debug)) ;
                    free(statusmess);
		    prod.info.seqno++;
		  }
	      }
	  }

	(void) close (fd);
      }
  }

exit(0);
}
Пример #20
0
/*
 * Arguments:
 *      arrival Data-product creation-time.  IGNORED.  The creation-time
 *              will be set by this function according to the system clock
 *              just prior to inserting the data-product into the product-
 *              queue.
 *      seqno   Sequence number.
 *      ident   Product-identifier.
 *      len     Size of data-portion of data-product in bytes.
 *      buf     Pointer to data-portion of data-product.
 */
void
toClients(timestampt arrival,
        unsigned seqno,
        const char *ident,
        unsigned len,
        const char *buf)
{
        static struct product prod;
        int status;
        char *result;

        MD5Init(md5ctxp);
        /*
         * If user has not disabled skipLeadingCtlString with -5 option,
         * and the product contains the correct leading control string for
         * a wmo product, then skip that control string in calculating the 
         * checksum.
         */
        if (skipLeadingCtlString && ((result = wmo_prod(buf)) != 0))
          {
          MD5Update(md5ctxp, (const unsigned char *)result, len-(result-buf));
#if DEBUG
          log_info("WMO prod: Skipping %d chars\n", result-buf);
#endif
          }
        else  /* calculate checksum on entire product */         
        {
          MD5Update(md5ctxp, (const unsigned char *)buf, len);
#if DEBUG
          log_info("not a WMO Prod\n");
#endif
        }
        MD5Final((unsigned char*)prod.info.signature, md5ctxp);

        prod.info.origin = myname;
        prod.info.feedtype = feedtype;
        prod.info.seqno = seqno;
        prod.info.ident = (char *)ident; /* cast away const */
        prod.info.sz = len;
        prod.data = (void *)buf; /* cast away const */

        if(((strncmp(prod.info.ident,"SDUS2",5) == 0) ||
            (strncmp(prod.info.ident,"SDUS3",5) == 0) ||
            (strncmp(prod.info.ident,"SDUS5",5) == 0) ||
            (strncmp(prod.info.ident,"SDUS7",5) == 0)) && (useNex == 1))
           {
           prod.info.feedtype = NEXRAD;
           }

        if(log_is_enabled_info)
                log_info("%s", s_prod_info(NULL, 0, &prod.info,
                        log_is_enabled_debug));

        if(pq == NULL)          /* if we are "feedtest", do nothing else */
                return;

        set_timestamp(&prod.info.arrival);

        status = pq_insert(pq, &prod);
        if(status == ENOERR)
                return; /* Normal return */

        /* else */
        if(status == PQUEUE_DUP)
        {
                ndups++;
                log_info("Product already in queue");
                return;
        }

        /* else, error */
        if (status > 0) {
            log_errno(status, "pq_insert");
        }
        else {
            log_error("pq_insert: Internal error");
        }
        exit(1); /* ??? */
}
Пример #21
0
int main(
        int ac,
        char *av[]
)
{
        char *progname = av[0];
        char *logfname;
        int useProductID = FALSE;
        int signatureFromId = FALSE;
        char *productID = NULL;
        int multipleFiles = FALSE;
        char identifier[KEYSIZE];
        int status;
        int seq_start = 0;
        enum ExitCode {
            exit_success = 0,
            exit_system = 1,    /* operating-system failure */
            exit_pq_open = 2,   /* couldn't open product-queue */
            exit_infile = 3,    /* couldn't process input file */
            exit_md5 = 6        /* couldn't initialize MD5 processing */
        } exitCode = exit_success;

        logfname = "-";

        /*
         * Check the environment for some options.
         * May be overridden by command line switches below.
         */
        {
                const char *ldmpqfname = getenv("LDMPQFNAME");
                if(ldmpqfname != NULL)
                        pqfname = ldmpqfname;
        }

        {
        extern int optind;
        extern int opterr;
        extern char *optarg;
        int ch;
        int logmask = (LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) |
            LOG_MASK(LOG_NOTICE));

        opterr = 1;

        while ((ch = getopt(ac, av, "ivxl:q:f:s:p:")) != EOF)
                switch (ch) {
                case 'i':
                        signatureFromId = 1;
                        break;
                case 'v':
                        logmask |= LOG_MASK(LOG_INFO);
                        break;
                case 'x':
                        logmask |= LOG_MASK(LOG_DEBUG);
                        break;
                case 'l':
                        logfname = optarg;
                        break;
                case 'q':
                        pqfname = optarg;
                        break;
                case 's':
                        seq_start = atoi(optarg);
                        break;
                case 'f':
                        feedtype = atofeedtypet(optarg);
                        if(feedtype == NONE)
                        {
                            fprintf(stderr, "Unknown feedtype \"%s\"\n", optarg);
                                usage(progname);        
                        }
                        break;
                case 'p':
                        useProductID = TRUE;
                        productID = optarg;
                        break;
                case '?':
                        usage(progname);
                        break;
                }

        ac -= optind; av += optind ;

        if(ac < 1) usage(progname);
        (void) setulogmask(logmask);
        }

        /*
         * Set up error logging
         */
        (void) openulog(ubasename(progname), LOG_NOTIME, LOG_LDM, logfname);

        /*
         * register exit handler
         */
        if(atexit(cleanup) != 0)
        {
                serror("atexit");
                exit(exit_system);
        }

        /*
         * set up signal handlers
         */
        set_sigactions();

        /*
         * who am i, anyway
         */
        (void) strcpy(myname, ghostname());

        /*
         * open the product queue
         */
        if(status = pq_open(pqfname, PQ_DEFAULT, &pq))
        {
                if (PQ_CORRUPT == status) {
                    uerror("The product-queue \"%s\" is inconsistent\n",
                            pqfname);
                }
                else {
                    uerror("pq_open: \"%s\" failed: %s",
                            pqfname, status > 0 ? strerror(status) :
                                            "Internal error");
                }
                exit(exit_pq_open);
        }


        {
        char *filename;
        int fd;
        struct stat statb;
        product prod;
        MD5_CTX *md5ctxp = NULL;

        /*
         * Allocate an MD5 context
         */
        md5ctxp = new_MD5_CTX();
        if(md5ctxp == NULL)
        {
                serror("new_md5_CTX failed");
                exit(exit_md5);
        }


        /* These members are constant over the loop. */
        prod.info.origin = myname;
        prod.info.feedtype = feedtype;

        if (ac > 1) {
          multipleFiles = TRUE;
        }

        for(prod.info.seqno = seq_start ; ac > 0 ;
                         av++, ac--, prod.info.seqno++)
        {
                filename = *av;

                fd = open(filename, O_RDONLY, 0);
                if(fd == -1)
                {
                        serror("open: %s", filename);
                        exitCode = exit_infile;
                        continue;
                }

                if( fstat(fd, &statb) == -1) 
                {
                        serror("fstat: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                /* Determine what to use for product identifier */
                if (useProductID) 
                  {
                    if (multipleFiles) 
                      {
                        sprintf(identifier,"%s.%d", productID, prod.info.seqno);
                        prod.info.ident = identifier;
                      }
                    else
                      prod.info.ident = productID;
                   }
                else
                    prod.info.ident = filename;
                
                prod.info.sz = statb.st_size;
                prod.data = NULL;

#ifndef NO_MMAP
                prod.data = mmap(0, prod.info.sz,
                        PROT_READ, MAP_PRIVATE, fd, 0);
                if(prod.data == NULL)
                {
                        serror("mmap: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                status = 
                    signatureFromId
                        ? mm_md5(md5ctxp, prod.info.ident,
                            strlen(prod.info.ident), prod.info.signature)
                        : mm_md5(md5ctxp, prod.data, prod.info.sz,
                            prod.info.signature);

                (void)exitIfDone(1);

                if (status != 0) {
                    serror("mm_md5: %s", filename);
                    (void) munmap(prod.data, prod.info.sz);
                    (void) close(fd);
                    exitCode = exit_infile;
                    continue;
                }

                /* These members, and seqno, vary over the loop. */
                status = set_timestamp(&prod.info.arrival);
                if(status != ENOERR) {
                        serror("set_timestamp: %s, filename");
                        exitCode = exit_infile;
                        continue;
                }

                /*
                 * Do the deed
                 */
                status = pq_insert(pq, &prod);

                switch (status) {
                case ENOERR:
                    /* no error */
                    if(ulogIsVerbose())
                        uinfo("%s", s_prod_info(NULL, 0, &prod.info,
                            ulogIsDebug())) ;
                    break;
                case PQUEUE_DUP:
                    uerror("Product already in queue: %s",
                        s_prod_info(NULL, 0, &prod.info, 1));
                    break;
                case ENOMEM:
                    uerror("queue full?");
                    break;  
                case EINTR:
#if defined(EDEADLOCK) && EDEADLOCK != EDEADLK
                case EDEADLOCK:
                    /*FALLTHROUGH*/
#endif
                case EDEADLK:
                    /* TODO: retry ? */
                    /*FALLTHROUGH*/
                default:
                    uerror("pq_insert: %s", status > 0
                        ? strerror(status) : "Internal error");
                    break;
                }

                (void) munmap(prod.data, prod.info.sz);
#else /*!NO_MMAP*/
                status = 
                    signatureFromId
                        ? mm_md5(md5ctxp, prod.info.ident,
                            strlen(prod.info.ident), prod.info.signature)
                        : fd_md5(md5ctxp, fd, statb.st_size,
                            prod.info.signature);

                (void)exitIfDone(1);

                if (status != 0) {
                        serror("fd_md5: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                if(lseek(fd, 0, SEEK_SET) == (off_t)-1)
                {
                        serror("rewind: %s", filename);
                        (void) close(fd);
                        exitCode = exit_infile;
                        continue;
                }

                index = PQE_NONE;
                status = pqe_new(pq, &prod.info, &prod.data, &index);

                if(status != ENOERR) {
                    serror("pqe_new: %s", filename);
                    exitCode = exit_infile;
                }
                else {
                    ssize_t     nread = read(fd, prod.data, prod.info.sz);

                    (void)exitIfDone(1);

                    if (nread != prod.info.sz) {
                        serror("read %s %u", filename, prod.info.sz);
                        status = EIO;
                    }
                    else {
                        status = pqe_insert(pq, index);
                        index = PQE_NONE;

                        switch (status) {
                        case ENOERR:
                            /* no error */
                            if(ulogIsVerbose())
                                uinfo("%s", s_prod_info(NULL, 0, &prod.info,
                                    ulogIsDebug())) ;
                            break;
                        case PQUEUE_DUP:
                            uerror("Product already in queue: %s",
                                s_prod_info(NULL, 0, &prod.info, 1));
                            break;
                        case ENOMEM:
                            uerror("queue full?");
                            break;  
                        case EINTR:
#if defined(EDEADLOCK) && EDEADLOCK != EDEADLK
                        case EDEADLOCK:
                            /*FALLTHROUGH*/
#endif
                        case EDEADLK:
                            /* TODO: retry ? */
                            /*FALLTHROUGH*/
                        default:
                            uerror("pq_insert: %s", status > 0
                                ? strerror(status) : "Internal error");
                        }
                    }                   /* data read into "index" region */

                    if (status != ENOERR) {
                        (void)pqe_discard(pq, index);
                        index = PQE_NONE;
                    }
                }                       /* "index" region allocated */

#endif /*!NO_MMAP*/
                (void) close(fd);
        }                               /* input-file loop */

        free_MD5_CTX(md5ctxp);  
        }                               /* code block */

        exit(exitCode);
}
Пример #22
0
/*
    @function main
*/
int main()
{
    /*test pq*/
    struct tnode* p=NULL;
    struct tnode* lc,*rc;
    float freq[]={0.01,0.04,0.05,0.11,0.19,0.20,0.4};
    int   NCHAR=7; /*number of characters*/
    int i=0;
    const char *CODE_FILE="code.txt";
    const char *OUT_FILE="encoded.txt";
    FILE* fout=NULL;
	/*zero out code*/
    memset(code,0,sizeof(code));

	/*testing queue*/
    pq_insert(talloc('a', 0.1));
    pq_insert(talloc('b', 0.2));
    pq_insert(talloc('c', 0.15));
    pq_insert(talloc('g', 0.3));
    pq_insert(talloc('x', 0.002));
    		
    /*making sure it pops in the right order*/
	puts("making sure it pops in the right order");
	while((p=pq_pop()))
    {
	printf("iam free \n");

	if(p != NULL)
	{
            free(p);
	}
	
    }
	
    qhead=NULL;
    /*initialize with freq*/
    for(i=0;i<NCHAR;i++)
    {
        pq_insert(talloc('a'+i,freq[i]));
    } 
    /*build tree*/
    for(i=0;i<NCHAR-1;i++)
    {
        lc=pq_pop();
        
        rc=pq_pop();
        
        /*create parent*/
        p=talloc(0,lc->freq+rc->freq);
        /*set parent link*/
        lc->parent=rc->parent=p;
        /*set child link*/
        p->right=rc; p->left=lc;
		/*make it non-leaf*/
		p->isleaf=0;
        /*add the new node to the queue*/
        pq_insert(p);
    }
    /*get root*/
     root=pq_pop();
	/*build code*/
	generate_code(root,0);
	/*output code*/
	puts("code:");
	fout=fopen(CODE_FILE,"w");
	dump_code(stdout);
	dump_code(fout);
	fclose(fout);

	/*encode a sample string*/
	puts("orginal:abba cafe bad");
	fout=fopen(OUT_FILE,"w");
	encode("abba cafe bad",stdout);
	encode("abba cafe bad",fout);
	fclose(fout);

	free(root);
	free(lc);
	free(rc);
	return 0;


}