File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Mon May 14 12:09:12 2012 UTC (12 years, 1 month ago) by misho
Branches: MAIN
CVS tags: sched2_2, sched2_1, sched1_6, SCHED2_1, SCHED2_0, SCHED1_5, HEAD
version 1.5

    1: #include <stdio.h>
    2: #include <unistd.h>
    3: #include <fcntl.h>
    4: #include <signal.h>
    5: #include <sys/types.h>
    6: #include <sys/stat.h>
    7: #include <sys/socket.h>
    8: #include <netinet/in.h>
    9: #include <aitsched.h>
   10: 
   11: intptr_t Kill;
   12: 
   13: void *event(sched_task_t *arg)
   14: {
   15: 	printf("Event::\n");
   16: 	return NULL;
   17: }
   18: 
   19: void *eventlo(sched_task_t *arg)
   20: {
   21: 	printf("EventLOW::\n");
   22: 	return NULL;
   23: }
   24: 
   25: void *timer(sched_task_t *arg)
   26: {
   27: 	printf("Timer %ld sec::\n", (intptr_t) TASK_ARG(arg));
   28: 	return NULL;
   29: }
   30: 
   31: void *r(sched_task_t *arg)
   32: {
   33: 	printf("read::\n");
   34: 	Kill++;
   35: 	return NULL;
   36: }
   37: 
   38: void *w(sched_task_t *arg)
   39: {
   40: 	printf("write::\n");
   41: 	return NULL;
   42: }
   43: 
   44: void *once(sched_task_t *arg)
   45: {
   46: 	printf("once::\n");
   47: 	return NULL;
   48: }
   49: 
   50: void sig(int s)
   51: {
   52: 	switch (s) {
   53: 		case SIGTERM:
   54: 			Kill++;
   55: 			break;
   56: 	}
   57: }
   58: 
   59: int
   60: main(int argc, char **argv)
   61: {
   62: 	sched_root_task_t *root;
   63: 	int f;
   64: 	struct sockaddr_in sin;
   65: 	struct timespec ts = { 20, 0 };
   66: //	struct timespec p = { 0, 10000000 };
   67: 	struct sigaction sa;
   68: 
   69: 	sa.sa_handler = sig;
   70: 	sigemptyset(&sa.sa_mask);
   71: 	sigaction(SIGTERM, &sa, NULL);
   72: 
   73: 	f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
   74: 	if (f == -1)
   75: 		return 1;
   76: 	sin.sin_len = sizeof sin;
   77: 	sin.sin_family = AF_INET;
   78: 	sin.sin_port = htons(2345);
   79: 	sin.sin_addr.s_addr = INADDR_ANY;
   80: 	if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
   81: 		return 1;
   82: 
   83: 	root = schedBegin();
   84: 	if (!root) {
   85: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
   86: 		return 1;
   87: 	}
   88: 
   89: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
   90: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
   91: 		return 4;
   92: 	} else
   93: 		ts.tv_sec = 15;
   94: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
   95: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
   96: 		return 4;
   97: 	} else
   98: 		ts.tv_sec = 10;
   99: 
  100: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  101: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  102: 		return 2;
  103: 	}
  104: 
  105: 	if (!schedEventLo(root, eventlo, "piuk", 1111, NULL, 0)) {
  106: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  107: 		return 3;
  108: 	}
  109: 
  110: 	if (!schedTimer(root, timer, (void*) ts.tv_sec, ts, NULL, 0)) {
  111: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  112: 		return 4;
  113: 	}
  114: 
  115: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  116: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  117: 		return 5;
  118: 	}
  119: 
  120: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  121: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  122: 		return 6;
  123: 	}
  124: 
  125: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  126: 
  127: //	schedPolling(root, &p, NULL);
  128: 	schedRun(root, &Kill);
  129: 	schedEnd(&root);
  130: 
  131: 	close(f);
  132: 	return 0;
  133: }

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