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

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;
                     14: 
                     15: void *event(sched_task_t *arg)
                     16: {
                     17:        printf("Event::\n");
                     18:        return NULL;
                     19: }
                     20: 
                     21: void *eventlo(sched_task_t *arg)
                     22: {
                     23:        printf("EventLOW::\n");
                     24:        return NULL;
                     25: }
                     26: 
                     27: void *timer(sched_task_t *arg)
                     28: {
1.6       misho      29:        printf("Timer %p sec::\n", TASK_ARG(arg));
1.2       misho      30:        return NULL;
                     31: }
                     32: 
                     33: void *r(sched_task_t *arg)
                     34: {
1.6.8.1   misho      35:        printf("read:: bytes\n");
1.2       misho      36:        Kill++;
                     37:        return NULL;
                     38: }
                     39: 
                     40: void *w(sched_task_t *arg)
                     41: {
                     42:        printf("write::\n");
                     43:        return NULL;
                     44: }
                     45: 
                     46: void *once(sched_task_t *arg)
                     47: {
                     48:        printf("once::\n");
                     49:        return NULL;
                     50: }
                     51: 
1.6.8.2 ! misho      52: void *aioread(sched_task_t *arg);
1.6.8.1   misho      53: void *aiowrite(sched_task_t *arg)
                     54: {
1.6.8.2 ! misho      55:        char *ole = malloc(BUFSIZ);
        !            56: 
1.6.8.1   misho      57:        printf("AIO write[%d]:: %d bytes\n", TASK_FD(arg), (int) TASK_DATLEN(arg));
                     58:        free(TASK_DATA(arg));
1.6.8.2 ! misho      59: 
        !            60:        schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ);
1.6.8.1   misho      61:        return NULL;
                     62: }
                     63: 
                     64: void *aioread(sched_task_t *arg)
                     65: {
                     66:        char *ole = malloc(BUFSIZ);
                     67: 
1.6.8.2 ! misho      68:        printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
        !            69:                        (char*) TASK_DATA(arg));
1.6.8.1   misho      70: 
1.6.8.2 ! misho      71:        if (TASK_ARG(arg)) {
        !            72:                write((int) TASK_ARG(arg), TASK_DATA(arg), TASK_DATLEN(arg));
1.6.8.1   misho      73: 
1.6.8.2 ! misho      74:                schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole, 
        !            75:                                strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ));
        !            76:        }
        !            77:        free(TASK_DATA(arg));
1.6.8.1   misho      78:        return NULL;
                     79: }
                     80: 
1.5       misho      81: void sig(int s)
                     82: {
                     83:        switch (s) {
                     84:                case SIGTERM:
                     85:                        Kill++;
                     86:                        break;
                     87:        }
                     88: }
                     89: 
1.2       misho      90: int
                     91: main(int argc, char **argv)
                     92: {
                     93:        sched_root_task_t *root;
1.6.8.1   misho      94:        int f, fd;
1.2       misho      95:        struct sockaddr_in sin;
1.5       misho      96:        struct timespec ts = { 20, 0 };
                     97: //     struct timespec p = { 0, 10000000 };
                     98:        struct sigaction sa;
1.6.8.1   misho      99:        char *ole = malloc(BUFSIZ);
1.5       misho     100: 
                    101:        sa.sa_handler = sig;
                    102:        sigemptyset(&sa.sa_mask);
                    103:        sigaction(SIGTERM, &sa, NULL);
1.2       misho     104: 
                    105:        f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
                    106:        if (f == -1)
                    107:                return 1;
                    108:        sin.sin_len = sizeof sin;
                    109:        sin.sin_family = AF_INET;
                    110:        sin.sin_port = htons(2345);
                    111:        sin.sin_addr.s_addr = INADDR_ANY;
                    112:        if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
                    113:                return 1;
                    114: 
1.6.8.2 ! misho     115:        fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
1.6.8.1   misho     116:        if (fd == -1)
                    117:                return 1;
1.6.8.2 ! misho     118:        printf("fd=%d\n", fd);
1.6.8.1   misho     119: 
1.2       misho     120:        root = schedBegin();
                    121:        if (!root) {
                    122:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    123:                return 1;
                    124:        }
                    125: 
1.6       misho     126:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5       misho     127:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    128:                return 4;
                    129:        } else
                    130:                ts.tv_sec = 15;
1.6       misho     131:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5       misho     132:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    133:                return 4;
                    134:        } else
                    135:                ts.tv_sec = 10;
                    136: 
1.4       misho     137:        if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
1.2       misho     138:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    139:                return 2;
                    140:        }
                    141: 
1.4       misho     142:        if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
1.2       misho     143:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    144:                return 3;
                    145:        }
                    146: 
1.6       misho     147:        if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2       misho     148:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    149:                return 4;
                    150:        }
                    151: 
1.4       misho     152:        if (!schedRead(root, r, "rrr", f, NULL, 0)) {
1.2       misho     153:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    154:                return 5;
                    155:        }
                    156: 
1.4       misho     157:        if (!schedWrite(root, w, "www", f, NULL, 0)) {
1.2       misho     158:                printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    159:                return 6;
                    160:        }
                    161: 
1.6.8.1   misho     162:        if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ))
                    163:                printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
                    164: 
1.4       misho     165:        schedCallOnce(root, once, "000000", 42, NULL, 0);
1.2       misho     166: 
1.5       misho     167: //     schedPolling(root, &p, NULL);
1.2       misho     168:        schedRun(root, &Kill);
                    169:        schedEnd(&root);
                    170: 
1.6.8.1   misho     171:        close(fd);
1.2       misho     172:        close(f);
                    173:        return 0;
                    174: }

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