File: test55.c

package info (click to toggle)
libtpl 1.5-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 4,284 kB
  • ctags: 748
  • sloc: sh: 10,059; ansic: 5,644; perl: 1,062; makefile: 119; cpp: 32
file content (91 lines) | stat: -rwxr-xr-x 2,487 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include "tpl.h"

#define DEBUG 0

extern tpl_hook_t tpl_hook;
int num_tpls = 0, sum_tpls = 0;

int tpl_cb(void *tpl, size_t tpllen, void*data) {
    int i;
    tpl_node *tn;

    tpl_hook.oops = printf;

    if (DEBUG) printf("obtained tpl of length %d\n", (int)tpllen);
    tn = tpl_map("A(i)", &i);
    tpl_load(tn, TPL_MEM, tpl, tpllen);
    num_tpls++;
    while (tpl_unpack(tn,1) > 0) sum_tpls += i;
    tpl_free(tn); 
    /* this next line is a hack to test the callback's ability
     * to abort further tpl processing by returning < 0 */
    if (num_tpls == 1) return -1;
    return 0;
    
}

int main() {
    FILE *f1,*f2;
    int fdflags,fd,fd1,fd2;
    int selrc, maxfd;
    tpl_gather_t *gs1=NULL,*gs2=NULL,**gs;
    struct timeval tv;
    fd_set rset;


    f1 = popen("cat test26_0.tpl;sleep 1; cat test26_1.tpl", "r");
    fd1 = fileno(f1); 
    fdflags = fcntl(fd1, F_GETFL, 0);
    fcntl( fd1, F_SETFL, fdflags | O_NONBLOCK);

    f2 = popen("cat test26_2.tpl;sleep 1; cat test26_3.tpl", "r");
    fd2 = fileno(f2); 
    fdflags = fcntl(fd2, F_GETFL, 0);
    fcntl( fd2, F_SETFL, fdflags | O_NONBLOCK);

    while (1) {
        FD_ZERO( &rset );
        if (fd1 >= 0) FD_SET( fd1, &rset );
        if (fd2 >= 0) FD_SET( fd2, &rset );

        if (fd1 == -1 && fd2 == -1) {
            printf("%d tpls gathered.\n",num_tpls);
            printf("%d is their sum.\n",sum_tpls);
            return(0);
        }

        maxfd=0;
        if (fd1>maxfd) maxfd = fd1;
        if (fd2>maxfd) maxfd = fd2;

        tv.tv_sec = 5;
        tv.tv_usec = 0;

        selrc = select(maxfd+1, &rset, NULL, NULL, &tv );
        if (selrc == -1) {
           perror("select()");
        } else if (selrc) {
            for(fd=0;fd<maxfd+1;fd++) {
                if ( FD_ISSET(fd, &rset) ) {
                    if (DEBUG) printf("fd %d readable\n", fd);
                    gs = (fd1 == fd) ? &gs1 : &gs2;
                    if (tpl_gather(TPL_GATHER_NONBLOCKING,fd,gs,tpl_cb,NULL) <= 0) {
                        if (fd1 == fd) {pclose(f1); fd1 = -1; }
                        if (fd2 == fd) {pclose(f2); fd2 = -1; }
                    } else {
                        if (DEBUG) printf("tpl_gather >0\n");
                    }
                }
            }
        } else {
            if (DEBUG) printf("timeout\n");  
        }
    }
    return(0);
}