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