File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.6.8.6: download - view: text, annotated - select for diffs - revision graph
Thu Aug 2 12:08:44 2012 UTC (11 years, 11 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.6: preferred, unified
fix return rw length to iovec

    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: 	schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ);
   63: 	return NULL;
   64: }
   65: 
   66: void *aioread(sched_task_t *arg)
   67: {
   68: 	char *ole = malloc(BUFSIZ);
   69: 	int len;
   70: 
   71: 	printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
   72: 			(char*) TASK_DATA(arg));
   73: 
   74: 	if (TASK_ARG(arg)) {
   75: //		write((int) TASK_ARG(arg), TASK_DATA(arg), TASK_DATLEN(arg));
   76: 
   77: 		len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
   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: 				
   81: 	}
   82: 	free(TASK_DATA(arg));
   83: 	return NULL;
   84: }
   85: 
   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++) {
   93: 		printf("%d) rlen[%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
   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++) {
  107: 		printf("%d) wlen=%d\n", i, iv[i].iov_len);
  108: 		free(iv[i].iov_base);
  109: 	}
  110: 
  111: 	return NULL;
  112: }
  113: 
  114: void sig(int s)
  115: {
  116: 	switch (s) {
  117: 		case SIGTERM:
  118: 			Kill++;
  119: 			break;
  120: 	}
  121: }
  122: 
  123: int
  124: main(int argc, char **argv)
  125: {
  126: 	sched_root_task_t *root;
  127: 	int f, fd;
  128: 	struct sockaddr_in sin;
  129: 	struct timespec ts = { 20, 0 };
  130: //	struct timespec p = { 0, 10000000 };
  131: 	struct sigaction sa;
  132: 	char *ole = malloc(BUFSIZ);
  133: 	register int i;
  134: 
  135: 	sa.sa_handler = sig;
  136: 	sigemptyset(&sa.sa_mask);
  137: 	sigaction(SIGTERM, &sa, NULL);
  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: 
  149: 	fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
  150: 	if (fd == -1)
  151: 		return 1;
  152: 	printf("fd=%d\n", fd);
  153: 
  154: 	root = schedBegin();
  155: 	if (!root) {
  156: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  157: 		return 1;
  158: 	}
  159: 
  160: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  161: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  162: 		return 4;
  163: 	} else
  164: 		ts.tv_sec = 15;
  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 = 10;
  170: 
  171: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  172: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  173: 		return 2;
  174: 	}
  175: 
  176: 	if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
  177: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  178: 		return 3;
  179: 	}
  180: 
  181: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  182: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  183: 		return 4;
  184: 	}
  185: 
  186: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  187: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  188: 		return 5;
  189: 	}
  190: 
  191: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  192: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  193: 		return 6;
  194: 	}
  195: 
  196: 	if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ))
  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(iv[i].iov_len);
  211: 	}
  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))
  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>