test_write_pipe.cpp

00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 #include "config.h"
00023 
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <fcntl.h>
00027 #include <assert.h>
00028 #include <stdio.h>
00029 #include <unistd.h>
00030 #include <errno.h>
00031 #include <string.h>
00032 #include <assert.h>
00033 
00034 #define PIPE_NAME "/tmp/test_write_pipe"
00035 #define FILE_NAME "test_file.txt"
00036 #define SIZE 1000
00037 
00038 int main()
00039 {
00040   // create named pipe
00041   mkfifo (PIPE_NAME, S_IRUSR);
00042   
00043 #ifdef DEBUG
00044   puts ("Named pipe created.");
00045 #endif
00046   
00047   // open named pipe
00048   int fd_pipe = open (PIPE_NAME, O_RDONLY, S_IRUSR);
00049   FILE* fd_file = fopen (FILE_NAME, "r");
00050   
00051   if (fd_pipe == -1 || fd_file == 0) {
00052 #ifdef DEBUG
00053     printf ("Error opening pipe. fd_pipe: %d errno: %d\n", fd_pipe, errno);
00054 #endif
00055     return -1;
00056   }
00057   
00058   fseek (fd_file, 0, SEEK_END);
00059   int fsize = ftell (fd_file);
00060   fseek (fd_file, 0, SEEK_SET);
00061   
00062 #ifdef DEBUG
00063   printf ("fsize: %d\n", fsize);
00064 #endif
00065   
00066   //puts("Named pipe opened sucessfully.");
00067   
00068   while (1) {
00069     char buf_pipe[SIZE], buf_file[SIZE];
00070     int bytes_read_pipe = read (fd_pipe, buf_pipe, sizeof (buf_pipe));
00071     
00072     int bytes_read_file = fread (buf_file, 1, bytes_read_pipe, fd_file);
00073     fsize -= bytes_read_file;
00074     
00075     // error
00076     if (memcmp (buf_file, buf_pipe, bytes_read_pipe) || bytes_read_pipe != bytes_read_file) {
00077 #ifdef DEBUG
00078       puts ("dont match");
00079 #endif
00080       return -1;
00081     }
00082     
00083     // transmission is over
00084     if (bytes_read_pipe == 0) {
00085 #ifdef DEBUG
00086       puts ("transmission is over");
00087 #endif
00088       if (fsize == 0) {// read every single byte from the file
00089         return 0;
00090       }
00091 #ifdef DEBUG
00092       puts ("bytes not read");
00093 #endif
00094       return -1; // there were more bytes to be received
00095     }
00096 #ifdef DEBUG
00097     for (int i = 0; i < bytes_read_pipe; i++)
00098       putchar (buf_pipe[i]);
00099     putchar ('\n');
00100 #endif
00101   }
00102   
00103   close (fd_pipe);
00104   fclose (fd_file);
00105   return 0;
00106 }
00107 
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3