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