File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.6.8.4: download - view: text, annotated - select for diffs - revision graph
Wed Aug 1 22:20:49 2012 UTC (11 years, 11 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.6: preferred, unified
finish 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: 
   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: {
   29: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   30: 	return NULL;
   31: }
   32: 
   33: void *r(sched_task_t *arg)
   34: {
   35: 	printf("read:: bytes\n");
   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: 
   52: void *aioread(sched_task_t *arg);
   53: void *aiowrite(sched_task_t *arg)
   54: {
   55: 	char *ole = malloc(BUFSIZ);
   56: 
   57: 	printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
   58: 			TASK_DATA(arg));
   59: 	free(TASK_DATA(arg));
   60: 
   61: 	schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ);
   62: 	return NULL;
   63: }
   64: 
   65: void *aioread(sched_task_t *arg)
   66: {
   67: 	char *ole = malloc(BUFSIZ);
   68: 	int len;
   69: 
   70: 	printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg), 
   71: 			(char*) TASK_DATA(arg));
   72: 
   73: 	if (TASK_ARG(arg)) {
   74: //		write((int) TASK_ARG(arg), TASK_DATA(arg), TASK_DATLEN(arg));
   75: 
   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);
   79: 				
   80: 	}
   81: 	free(TASK_DATA(arg));
   82: 	return NULL;
   83: }
   84: 
   85: void sig(int s)
   86: {
   87: 	switch (s) {
   88: 		case SIGTERM:
   89: 			Kill++;
   90: 			break;
   91: 	}
   92: }
   93: 
   94: int
   95: main(int argc, char **argv)
   96: {
   97: 	sched_root_task_t *root;
   98: 	int f, fd;
   99: 	struct sockaddr_in sin;
  100: 	struct timespec ts = { 20, 0 };
  101: //	struct timespec p = { 0, 10000000 };
  102: 	struct sigaction sa;
  103: 	char *ole = malloc(BUFSIZ);
  104: 
  105: 	sa.sa_handler = sig;
  106: 	sigemptyset(&sa.sa_mask);
  107: 	sigaction(SIGTERM, &sa, NULL);
  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: 
  119: 	fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
  120: 	if (fd == -1)
  121: 		return 1;
  122: 	printf("fd=%d\n", fd);
  123: 
  124: 	root = schedBegin();
  125: 	if (!root) {
  126: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  127: 		return 1;
  128: 	}
  129: 
  130: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  131: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  132: 		return 4;
  133: 	} else
  134: 		ts.tv_sec = 15;
  135: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  136: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  137: 		return 4;
  138: 	} else
  139: 		ts.tv_sec = 10;
  140: 
  141: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  142: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  143: 		return 2;
  144: 	}
  145: 
  146: 	if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
  147: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  148: 		return 3;
  149: 	}
  150: 
  151: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  152: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  153: 		return 4;
  154: 	}
  155: 
  156: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  157: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  158: 		return 5;
  159: 	}
  160: 
  161: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  162: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  163: 		return 6;
  164: 	}
  165: 
  166: 	if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ))
  167: 		printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  168: 
  169: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  170: 
  171: //	schedPolling(root, &p, NULL);
  172: 	schedRun(root, &Kill);
  173: 	schedEnd(&root);
  174: 
  175: 	close(fd);
  176: 	close(f);
  177: 	return 0;
  178: }

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