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