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