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