File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.6.8.8: download - view: text, annotated - select for diffs - revision graph
Thu Aug 2 12:25:38 2012 UTC (11 years, 11 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.6: preferred, unified
finish LIO/AIO UT

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

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