root/test/event/event-test.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fifo_read
  2. main

   1 /*
   2  * Compile with:
   3  * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
   4  */
   5 
   6 #ifdef HAVE_SYS_TYPES_H
   7 #include <sys/types.h>
   8 #endif
   9 #include <sys/stat.h>
  10 #ifndef WIN32
  11 #ifdef HAVE_SYS_QUEUE_H
  12 #include <sys/queue.h>
  13 #endif
  14 #ifdef HAVE_UNISTD_H
  15 #include <unistd.h>
  16 #endif
  17 #ifdef HAVE_SYS_TIME_H
  18 #include <sys/time.h>
  19 #endif
  20 #else
  21 #include <windows.h>
  22 #endif
  23 #include <fcntl.h>
  24 #include <stdlib.h>
  25 #include <stdio.h>
  26 #include <string.h>
  27 #include <errno.h>
  28 #include <unistd.h>
  29 
  30 #include <opal/mca/event/event.h>
  31 #include "opal/runtime/opal.h"
  32 
  33 static void
  34 fifo_read(int fd, short event, void *arg)
  35 {
  36         char buf[255];
  37         int len;
  38         opal_event_t *ev = arg;
  39 #ifdef WIN32
  40         DWORD dwBytesRead;
  41 #endif
  42 
  43         /* Reschedule this event */
  44         opal_event.add(ev, NULL);
  45 
  46         fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
  47                 fd, event, arg);
  48 #ifdef WIN32
  49         len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
  50 
  51         /* Check for end of file. */
  52         if(len && dwBytesRead == 0) {
  53                 fprintf(stderr, "End Of File");
  54                 opal_event.del(ev);
  55                 return;
  56         }
  57 
  58         buf[dwBytesRead + 1] = '\0';
  59 #else
  60         len = read(fd, buf, sizeof(buf) - 1);
  61 
  62         if (len == -1) {
  63                 perror("read");
  64                 return;
  65         } else if (len == 0) {
  66                 fprintf(stderr, "Connection closed\n");
  67                 return;
  68         }
  69 
  70         buf[len] = '\0';
  71 #endif
  72         fprintf(stdout, "Read: %s\n", buf);
  73 }
  74 
  75 int
  76 main (int argc, char **argv)
  77 {
  78         opal_event_t evfifo;
  79 #ifdef WIN32
  80         HANDLE socket;
  81         // Open a file.
  82         socket = CreateFile("test.txt",     // open File
  83                         GENERIC_READ,                 // open for reading
  84                         0,                            // do not share
  85                         NULL,                         // no security
  86                         OPEN_EXISTING,                // existing file only
  87                         FILE_ATTRIBUTE_NORMAL,        // normal file
  88                         NULL);                        // no attr. template
  89 
  90         if(socket == INVALID_HANDLE_VALUE)
  91                 return 1;
  92 
  93 #else
  94         struct stat st;
  95         char *fifo = "event.fifo";
  96         int socket;
  97 
  98         if (lstat (fifo, &st) == 0) {
  99                 if ((st.st_mode & S_IFMT) == S_IFREG) {
 100                         errno = EEXIST;
 101                         perror("lstat");
 102                         exit (1);
 103                 }
 104         }
 105 
 106         unlink (fifo);
 107         if (mkfifo (fifo, 0600) == -1) {
 108                 perror("mkfifo");
 109                 exit (1);
 110         }
 111 
 112         /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
 113 #ifdef __linux
 114         socket = open (fifo, O_RDWR | O_NONBLOCK, 0);
 115 #else
 116         socket = open (fifo, O_RDONLY | O_NONBLOCK, 0);
 117 #endif
 118 
 119         if (socket == -1) {
 120                 perror("open");
 121                 exit (1);
 122         }
 123 
 124         fprintf(stderr, "Write data to %s\n", fifo);
 125 #endif
 126         /* Initialize the event library */
 127         opal_init();
 128 
 129         /* Initialize one event */
 130 #ifdef WIN32
 131         opal_event.set(opal_event_base, &evfifo, (int)socket, OPAL_EV_READ, fifo_read, &evfifo);
 132 #else
 133         opal_event.set(opal_event_base, &evfifo, socket, OPAL_EV_READ, fifo_read, &evfifo);
 134 #endif
 135 
 136         /* Add it to the active events, without a timeout */
 137         opal_event.add(&evfifo, NULL);
 138 
 139         opal_event.dispatch(opal_event_base);
 140 #ifdef WIN32
 141         CloseHandle(socket);
 142 #endif
 143         return (0);
 144 }
 145 

/* [<][>][^][v][top][bottom][index][help] */