File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.6.8.9: download - view: text, annotated - select for diffs - revision graph
Thu Aug 2 13:02:46 2012 UTC (12 years ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.6: preferred, unified
patch for systems without AIO

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <string.h>
    4: #include <unistd.h>
    5: #include <fcntl.h>
    6: #include <signal.h>
    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: #ifdef EVFILT_LIO
   15: struct iovec iv[3], wiv[3], riv[3];
   16: #endif
   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: {
   32: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   33: 	return NULL;
   34: }
   35: 
   36: void *r(sched_task_t *arg)
   37: {
   38: 	printf("read:: bytes\n");
   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: 
   55: #ifdef EVFILT_LIO
   56: void *aioread(sched_task_t *arg);
   57: void *aiowrite(sched_task_t *arg)
   58: {
   59: 	char *ole = malloc(BUFSIZ);
   60: 
   61: 	printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
   62: 			TASK_DATA(arg));
   63: 	free(TASK_DATA(arg));
   64: 
   65: 	memset(ole, 0, BUFSIZ);
   66: 	schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ - 1, -1);
   67: 	return NULL;
   68: }
   69: 
   70: void *aioread(sched_task_t *arg)
   71: {
   72: 	char *ole = malloc(BUFSIZ);
   73: 	int len;
   74: 
   75: 	printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
   76: 			(char*) TASK_DATA(arg));
   77: 
   78: 	if (TASK_ARG(arg)) {
   79: 		len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
   80: 		printf("sched Write len=%d %p\n", len, ole);
   81: 		schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole,  len, -1);
   82: 				
   83: 	}
   84: 	free(TASK_DATA(arg));
   85: 	return NULL;
   86: }
   87: 
   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++) {
   95: 		printf("%d) rlen[%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
   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++) {
  109: 		printf("%d) wlen=%d\n", i, iv[i].iov_len);
  110: 		free(iv[i].iov_base);
  111: 	}
  112: 
  113: 	return NULL;
  114: }
  115: #endif
  116: 
  117: void sig(int s)
  118: {
  119: 	switch (s) {
  120: 		case SIGTERM:
  121: 			Kill++;
  122: 			break;
  123: 	}
  124: }
  125: 
  126: int
  127: main(int argc, char **argv)
  128: {
  129: 	sched_root_task_t *root;
  130: 	int f, fd;
  131: 	struct sockaddr_in sin;
  132: 	struct timespec ts = { 20, 0 };
  133: //	struct timespec p = { 0, 10000000 };
  134: 	struct sigaction sa;
  135: #ifdef EVFILT_LIO
  136: 	char *ole = malloc(BUFSIZ);
  137: 	register int i;
  138: #endif
  139: 
  140: 	sa.sa_handler = sig;
  141: 	sigemptyset(&sa.sa_mask);
  142: 	sigaction(SIGTERM, &sa, NULL);
  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: 
  154: 	fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
  155: 	if (fd == -1)
  156: 		return 1;
  157: 	printf("fd=%d\n", fd);
  158: 
  159: 	root = schedBegin();
  160: 	if (!root) {
  161: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  162: 		return 1;
  163: 	}
  164: 
  165: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  166: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  167: 		return 4;
  168: 	} else
  169: 		ts.tv_sec = 15;
  170: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  171: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  172: 		return 4;
  173: 	} else
  174: 		ts.tv_sec = 10;
  175: 
  176: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  177: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  178: 		return 2;
  179: 	}
  180: 
  181: 	if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
  182: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  183: 		return 3;
  184: 	}
  185: 
  186: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  187: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  188: 		return 4;
  189: 	}
  190: 
  191: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  192: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  193: 		return 5;
  194: 	}
  195: 
  196: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  197: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  198: 		return 6;
  199: 	}
  200: 
  201: #ifdef EVFILT_LIO
  202: 	memset(ole, 0, BUFSIZ);
  203: 	if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
  204: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  205: 
  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);
  212: 	if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
  213: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  214: 	fsync(fd);
  215: 	for (i = 0; i < 3; i++) {
  216: 		wiv[i].iov_len = 100;
  217: 		wiv[i].iov_base = malloc(wiv[i].iov_len);
  218: 	}
  219: 	strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
  220: 	wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
  221: 	strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
  222: 	wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
  223: 	strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
  224: 	wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
  225: 	if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
  226: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  227: 
  228: 	for (i = 0; i < 3; i++) {
  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);
  232: 	}
  233: 	if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
  234: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  235: #endif
  236: 
  237: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  238: 
  239: //	schedPolling(root, &p, NULL);
  240: 	schedRun(root, &Kill);
  241: 	schedEnd(&root);
  242: 
  243: #ifdef EVFILT_LIO
  244: 	for (i = 0; i < 3; i++)
  245: 		free(iv[i].iov_base);
  246: #endif
  247: 
  248: 	close(fd);
  249: 	close(f);
  250: 	return 0;
  251: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>