Annotation of libaitsched/example/test.c, revision 1.6.8.5

1.2       misho       1: #include <stdio.h>
1.6.8.1   misho       2: #include <stdlib.h>
                      3: #include <string.h>
1.2       misho       4: #include <unistd.h>
                      5: #include <fcntl.h>
1.5       misho       6: #include <signal.h>
1.2       misho       7: #include <sys/types.h>
                      8: #include <sys/stat.h>
                      9: #include <sys/socket.h>
                     10: #include <netinet/in.h>
                     11: #include <aitsched.h>
                     12: 
                     13: intptr_t Kill;
1.6.8.5 ! misho      14: struct iovec iv[3];
1.2       misho      15: 
                     16: void *event(sched_task_t *arg)
                     17: {
                     18:        printf("Event::\n");
                     19:        return NULL;
                     20: }
                     21: 
                     22: void *eventlo(sched_task_t *arg)
                     23: {
                     24:        printf("EventLOW::\n");
                     25:        return NULL;
                     26: }
                     27: 
                     28: void *timer(sched_task_t *arg)
                     29: {
1.6       misho      30:        printf("Timer %p sec::\n", TASK_ARG(arg));
1.2       misho      31:        return NULL;
                     32: }
                     33: 
                     34: void *r(sched_task_t *arg)
                     35: {
1.6.8.1   misho      36:        printf("read:: bytes\n");
1.2       misho      37:        Kill++;
                     38:        return NULL;
                     39: }
                     40: 
                     41: void *w(sched_task_t *arg)
                     42: {
                     43:        printf("write::\n");
                     44:        return NULL;
                     45: }
                     46: 
                     47: void *once(sched_task_t *arg)
                     48: {
                     49:        printf("once::\n");
                     50:        return NULL;
                     51: }
                     52: 
1.6.8.2   misho      53: void *aioread(sched_task_t *arg);
1.6.8.1   misho      54: void *aiowrite(sched_task_t *arg)
                     55: {
1.6.8.2   misho      56:        char *ole = malloc(BUFSIZ);
                     57: 
1.6.8.4   misho      58:        printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
                     59:                        TASK_DATA(arg));
1.6.8.1   misho      60:        free(TASK_DATA(arg));
1.6.8.2   misho      61: 
                     62:        schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ);
1.6.8.1   misho      63:        return NULL;
                     64: }
                     65: 
                     66: void *aioread(sched_task_t *arg)
                     67: {
                     68:        char *ole = malloc(BUFSIZ);
1.6.8.3   misho      69:        int len;
1.6.8.1   misho      70: 
1.6.8.2   misho      71:        printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
                     72:                        (char*) TASK_DATA(arg));
1.6.8.1   misho      73: 
1.6.8.2   misho      74:        if (TASK_ARG(arg)) {
1.6.8.4   misho      75: //             write((int) TASK_ARG(arg), TASK_DATA(arg), TASK_DATLEN(arg));
1.6.8.1   misho      76: 
1.6.8.4   misho      77:                len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
1.6.8.3   misho      78:                printf("sched Write len=%d %p\n", len, ole);
                     79:                schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole,  len);
                     80:                                
1.6.8.2   misho      81:        }
                     82:        free(TASK_DATA(arg));
1.6.8.1   misho      83:        return NULL;
                     84: }
                     85: 
1.6.8.5 ! misho      86: void *aiobulkread(sched_task_t *arg)
        !            87: {
        !            88:        struct iovec *iv = TASK_DATA(arg);
        !            89:        register int i;
        !            90: 
        !            91:        printf("aioBULKread::\n");
        !            92:        for (i = 0; i < 3; i++) {
        !            93:                printf("%d) [%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
        !            94:                free(iv[i].iov_base);
        !            95:        }
        !            96: 
        !            97:        return NULL;
        !            98: }
        !            99: 
        !           100: void *aiobulkwrite(sched_task_t *arg)
        !           101: {
        !           102:        struct iovec *iv = TASK_DATA(arg);
        !           103:        register int i;
        !           104: 
        !           105:        printf("aioBULKwrite::\n");
        !           106:        for (i = 0; i < 3; i++) {
        !           107:                printf("%d) %d\n", i, iv[i].iov_len);
        !           108:                free(iv[i].iov_base);
        !           109:        }
        !           110: 
        !           111:        return NULL;
        !           112: }
        !           113: 
1.5       misho     114: void sig(int s)
                    115: {
                    116:        switch (s) {
                    117:                case SIGTERM:
                    118:                        Kill++;
                    119:                        break;
                    120:        }
                    121: }
                    122: 
1.2       misho     123: int
                    124: main(int argc, char **argv)
                    125: {
                    126:        sched_root_task_t *root;
1.6.8.1   misho     127:        int f, fd;
1.2       misho     128:        struct sockaddr_in sin;
1.5       misho     129:        struct timespec ts = { 20, 0 };
                    130: //     struct timespec p = { 0, 10000000 };
                    131:        struct sigaction sa;
1.6.8.1   misho     132:        char *ole = malloc(BUFSIZ);
1.6.8.5 ! misho     133:        register int i;
1.5       misho     134: 
                    135:        sa.sa_handler = sig;
                    136:        sigemptyset(&sa.sa_mask);
                    137:        sigaction(SIGTERM, &sa, NULL);
1.2       misho     138: 
                    139:        f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
                    140:        if (f == -1)
                    141:                return 1;
                    142:        sin.sin_len = sizeof sin;
                    143:        sin.sin_family = AF_INET;
                    144:        sin.sin_port = htons(2345);
                    145:        sin.sin_addr.s_addr = INADDR_ANY;
                    146:        if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
                    147:                return 1;
                    148: 
1.6.8.2   misho     149:        fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
1.6.8.1   misho     150:        if (fd == -1)
                    151:                return 1;
1.6.8.2   misho     152:        printf("fd=%d\n", fd);
1.6.8.1   misho     153: 
1.2       misho     154:        root = schedBegin();
                    155:        if (!root) {
                    156:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    157:                return 1;
                    158:        }
                    159: 
1.6       misho     160:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5       misho     161:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    162:                return 4;
                    163:        } else
                    164:                ts.tv_sec = 15;
1.6       misho     165:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5       misho     166:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    167:                return 4;
                    168:        } else
                    169:                ts.tv_sec = 10;
                    170: 
1.4       misho     171:        if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
1.2       misho     172:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    173:                return 2;
                    174:        }
                    175: 
1.4       misho     176:        if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
1.2       misho     177:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    178:                return 3;
                    179:        }
                    180: 
1.6       misho     181:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     182:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    183:                return 4;
                    184:        }
                    185: 
1.4       misho     186:        if (!schedRead(root, r, "rrr", f, NULL, 0)) {
1.2       misho     187:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    188:                return 5;
                    189:        }
                    190: 
1.4       misho     191:        if (!schedWrite(root, w, "www", f, NULL, 0)) {
1.2       misho     192:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    193:                return 6;
                    194:        }
                    195: 
1.6.8.1   misho     196:        if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ))
                    197:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    198: 
1.6.8.5 ! misho     199: 
        !           200:        lseek(fd, 0, SEEK_SET);
        !           201:        iv[0].iov_len = 5;
        !           202:        iv[1].iov_len = 2;
        !           203:        iv[2].iov_len = 50;
        !           204:        for (i = 0; i < 3; i++)
        !           205:                iv[i].iov_base = malloc(iv[i].iov_len);
        !           206:        if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3))
        !           207:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           208: 
        !           209:        for (i = 0; i < 3; i++) {
        !           210:                iv[i].iov_len = 100;
        !           211:                iv[i].iov_base = malloc(iv[i].iov_len);
        !           212:        }
        !           213:        strlcpy(iv[0].iov_base, "aaa\n", iv[0].iov_len);
        !           214:        strlcpy(iv[1].iov_base, "bbbbb\n", iv[1].iov_len);
        !           215:        strlcpy(iv[2].iov_base, "zzz\n", iv[2].iov_len);
        !           216:        if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, iv, 3))
        !           217:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           218: 
        !           219:        lseek(fd, 0, SEEK_SET);
        !           220:        for (i = 0; i < 3; i++) {
        !           221:                iv[i].iov_len = 4;
        !           222:                iv[i].iov_base = malloc(iv[i].iov_len);
        !           223:        }
        !           224:        if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3))
        !           225:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           226: 
1.4       misho     227:        schedCallOnce(root, once, "000000", 42, NULL, 0);
1.2       misho     228: 
1.5       misho     229: //     schedPolling(root, &p, NULL);
1.2       misho     230:        schedRun(root, &Kill);
                    231:        schedEnd(&root);
                    232: 
1.6.8.5 ! misho     233:        for (i = 0; i < 3; i++)
        !           234:                free(iv[i].iov_base);
        !           235: 
1.6.8.1   misho     236:        close(fd);
1.2       misho     237:        close(f);
                    238:        return 0;
                    239: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>