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