File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Thu Aug 2 13:56:19 2012 UTC (11 years, 10 months ago) by misho
Branches: MAIN
CVS tags: sched2_7, SCHED2_6, HEAD
version 2.6

    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 "../inc/config.h"
   12: #include <aitsched.h>
   13: 
   14: intptr_t Kill;
   15: #ifdef AIO_SUPPORT
   16: struct iovec iv[3], wiv[3], riv[3];
   17: #endif
   18: 
   19: void *event(sched_task_t *arg)
   20: {
   21: 	printf("Event::\n");
   22: 	return NULL;
   23: }
   24: 
   25: void *eventlo(sched_task_t *arg)
   26: {
   27: 	printf("EventLOW::\n");
   28: 	return NULL;
   29: }
   30: 
   31: void *timer(sched_task_t *arg)
   32: {
   33: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   34: 	return NULL;
   35: }
   36: 
   37: void *r(sched_task_t *arg)
   38: {
   39: 	printf("read:: bytes\n");
   40: 	Kill++;
   41: 	return NULL;
   42: }
   43: 
   44: void *w(sched_task_t *arg)
   45: {
   46: 	printf("write::\n");
   47: 	return NULL;
   48: }
   49: 
   50: void *once(sched_task_t *arg)
   51: {
   52: 	printf("once::\n");
   53: 	return NULL;
   54: }
   55: 
   56: #ifdef AIO_SUPPORT
   57: void *aioread(sched_task_t *arg);
   58: void *aiowrite(sched_task_t *arg)
   59: {
   60: 	char *ole = malloc(BUFSIZ);
   61: 
   62: 	printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
   63: 			TASK_DATA(arg));
   64: 	free(TASK_DATA(arg));
   65: 
   66: 	memset(ole, 0, BUFSIZ);
   67: 	schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ - 1, -1);
   68: 	return NULL;
   69: }
   70: 
   71: void *aioread(sched_task_t *arg)
   72: {
   73: 	char *ole = malloc(BUFSIZ);
   74: 	int len;
   75: 
   76: 	printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
   77: 			(char*) TASK_DATA(arg));
   78: 
   79: 	if (TASK_ARG(arg)) {
   80: 		len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
   81: 		printf("sched Write len=%d %p\n", len, ole);
   82: 		schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole,  len, -1);
   83: 				
   84: 	}
   85: 	free(TASK_DATA(arg));
   86: 	return NULL;
   87: }
   88: 
   89: void *aiobulkread(sched_task_t *arg)
   90: {
   91: 	struct iovec *iv = TASK_DATA(arg);
   92: 	register int i;
   93: 
   94: 	printf("aioBULKread::\n");
   95: 	for (i = 0; i < 3; i++) {
   96: 		printf("%d) rlen[%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
   97: 		free(iv[i].iov_base);
   98: 	}
   99: 
  100: 	return NULL;
  101: }
  102: 
  103: void *aiobulkwrite(sched_task_t *arg)
  104: {
  105: 	struct iovec *iv = TASK_DATA(arg);
  106: 	register int i;
  107: 
  108: 	printf("aioBULKwrite::\n");
  109: 	for (i = 0; i < 3; i++) {
  110: 		printf("%d) wlen=%d\n", i, iv[i].iov_len);
  111: 		free(iv[i].iov_base);
  112: 	}
  113: 
  114: 	return NULL;
  115: }
  116: #endif
  117: 
  118: void sig(int s)
  119: {
  120: 	switch (s) {
  121: 		case SIGTERM:
  122: 			Kill++;
  123: 			break;
  124: 	}
  125: }
  126: 
  127: int
  128: main(int argc, char **argv)
  129: {
  130: 	sched_root_task_t *root;
  131: 	int f, fd;
  132: 	struct sockaddr_in sin;
  133: 	struct timespec ts = { 20, 0 };
  134: //	struct timespec p = { 0, 10000000 };
  135: 	struct sigaction sa;
  136: #ifdef AIO_SUPPORT
  137: 	char *ole = malloc(BUFSIZ);
  138: 	register int i;
  139: #endif
  140: 
  141: 	sa.sa_handler = sig;
  142: 	sigemptyset(&sa.sa_mask);
  143: 	sigaction(SIGTERM, &sa, NULL);
  144: 
  145: 	f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  146: 	if (f == -1)
  147: 		return 1;
  148: 	sin.sin_len = sizeof sin;
  149: 	sin.sin_family = AF_INET;
  150: 	sin.sin_port = htons(2345);
  151: 	sin.sin_addr.s_addr = INADDR_ANY;
  152: 	if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
  153: 		return 1;
  154: 
  155: 	fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
  156: 	if (fd == -1)
  157: 		return 1;
  158: 	printf("fd=%d\n", fd);
  159: 
  160: 	root = schedBegin();
  161: 	if (!root) {
  162: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  163: 		return 1;
  164: 	}
  165: 
  166: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  167: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  168: 		return 4;
  169: 	} else
  170: 		ts.tv_sec = 15;
  171: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  172: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  173: 		return 4;
  174: 	} else
  175: 		ts.tv_sec = 10;
  176: 
  177: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  178: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  179: 		return 2;
  180: 	}
  181: 
  182: 	if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
  183: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  184: 		return 3;
  185: 	}
  186: 
  187: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  188: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  189: 		return 4;
  190: 	}
  191: 
  192: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  193: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  194: 		return 5;
  195: 	}
  196: 
  197: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  198: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  199: 		return 6;
  200: 	}
  201: 
  202: #ifdef AIO_SUPPORT
  203: 	memset(ole, 0, BUFSIZ);
  204: 	if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
  205: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  206: 
  207: 
  208: 	iv[0].iov_len = 5;
  209: 	iv[1].iov_len = 2;
  210: 	iv[2].iov_len = 50;
  211: 	for (i = 0; i < 3; i++)
  212: 		iv[i].iov_base = malloc(iv[i].iov_len);
  213: 	if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
  214: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  215: 	fsync(fd);
  216: 	for (i = 0; i < 3; i++) {
  217: 		wiv[i].iov_len = 100;
  218: 		wiv[i].iov_base = malloc(wiv[i].iov_len);
  219: 	}
  220: 	strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
  221: 	wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
  222: 	strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
  223: 	wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
  224: 	strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
  225: 	wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
  226: 	if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
  227: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  228: 
  229: 	for (i = 0; i < 3; i++) {
  230: 		riv[i].iov_len = 5;
  231: 		riv[i].iov_base = malloc(riv[i].iov_len + 1);
  232: 		memset(riv[i].iov_base, 0, riv[i].iov_len + 1);
  233: 	}
  234: 	if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
  235: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  236: #endif
  237: 
  238: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  239: 
  240: //	schedPolling(root, &p, NULL);
  241: 	schedRun(root, &Kill);
  242: 	schedEnd(&root);
  243: 
  244: #ifdef AIO_SUPPORT
  245: 	for (i = 0; i < 3; i++)
  246: 		free(iv[i].iov_base);
  247: #endif
  248: 
  249: 	close(fd);
  250: 	close(f);
  251: 	return 0;
  252: }

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