Advertisement
rnort

SPO-5-NIX-FINAL

Dec 20th, 2012
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <dlfcn.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <aio.h>
  7. #include <dirent.h>
  8. #include <fcntl.h>
  9. #include <pthread.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <iostream>
  13. #include <vector>
  14. #include <string>
  15. #include <errno.h>
  16.  
  17. #define BUFFER_SIZE 1024
  18.  
  19. void makeFileList(char*);
  20. void* reader(void*);
  21. void* writer(void*);
  22. void clearBuffer();
  23. void createThread( pthread_t* , void*(*function)(void*) , void* );
  24. void* openLibrary(char* libPath);
  25. int validateArguments(int argCount, char* appName);
  26. void createMutexes();
  27. void initReadCb(int fid);
  28. void initWriteCb(int fid);
  29. // Extern functions from .so library
  30. int (*ARead)(struct aiocb*);
  31. int (*AWrite)(struct aiocb*);
  32. pthread_mutex_t r_mutex, w_mutex;
  33. std::vector<std::string> FileList;
  34. char buffer[BUFFER_SIZE];
  35. int isReadingEnded = 0;
  36.  
  37. struct aiocb read_cb, write_cb;
  38. int read_len = 0, write_len = 0;
  39.  
  40.  
  41. int main(int argc, char** argv)
  42. {
  43.     if (validateArguments(argc, argv[0]) != 1)
  44.     {
  45.         return(EXIT_FAILURE);
  46.     }
  47.     void* library = openLibrary(argv[1]);
  48.     createMutexes();   
  49.     makeFileList(argv[2]);
  50.     pthread_mutex_lock(&w_mutex);
  51.     pthread_t pwriter, preader;
  52.     createThread( &pwriter, &writer, (void*)argv[3] );
  53.     createThread( &preader, &reader, (void*)argv[2] );
  54.     pthread_join (preader, 0);
  55.     pthread_join (pwriter, 0);
  56.     pthread_mutex_destroy(&r_mutex);
  57.     pthread_mutex_destroy(&w_mutex);
  58.     dlclose(library);      
  59.     puts(" - END - ");
  60.     return 0;        
  61. }
  62.  
  63.  
  64. int validateArguments(int argCount, char* appName)
  65. {
  66.     if ( argCount != 4 )
  67.     {
  68.         printf("\n %s [library_path] [input_folder_path] [output_file_path]\n", appName );
  69.         return 0;
  70.     }
  71.     return 1;
  72. }
  73.  
  74. void createMutexes()
  75. {
  76.     pthread_mutex_init(&r_mutex, NULL);
  77.     pthread_mutex_init(&w_mutex, NULL);
  78. }
  79.  
  80. void createThread( pthread_t* id, void*(*function)(void*) , void* args )
  81. {
  82.     int result = pthread_create(id ,NULL, function, args);
  83. }
  84.  
  85. void* openLibrary(char* libPath)
  86. {
  87.     void *lid = dlopen(libPath, RTLD_LAZY);
  88.     ARead = dlsym(lid, "Read");
  89.     AWrite = dlsym(lid, "Write");
  90.     return lid;
  91. }
  92.  
  93. void makeFileList(char* inputDir)
  94. {
  95.     DIR *dp;
  96.     struct dirent *dirp;
  97.     if((dp = opendir(inputDir)) == NULL)
  98.     {
  99.         std::cout << "Error(" << strerror(errno) << ") opening " << inputDir << std::endl;
  100.         return errno;
  101.     }
  102.     while ((dirp = readdir(dp)) != NULL)
  103.     {
  104.         FileList.push_back( std::string(inputDir) + std::string(dirp->d_name));
  105.         if ( opendir(FileList.back().c_str()) != NULL )
  106.         {
  107.             FileList.pop_back();
  108.         }
  109.     }
  110.     closedir(dp);
  111. }
  112.  
  113. void* reader(void *pp)
  114. {
  115.     char* currentFilePath = NULL;
  116.     for( int i = 0, file_count = FileList.size(); i < file_count; ++i)
  117.     {
  118.         currentFilePath = FileList[i].c_str();
  119.         int fid = open(currentFilePath, O_RDONLY);
  120.         if(fid == -1)
  121.         {
  122.             continue;
  123.         }
  124.         clearBuffer();
  125.         initReadCb(fid);       
  126.         do
  127.         {
  128.             pthread_mutex_lock(&r_mutex);
  129.             printf("Reading %s\n", currentFilePath);
  130.             read_len = ARead(&read_cb);
  131.             read_cb.aio_offset += read_len;
  132.             pthread_mutex_unlock(&w_mutex);
  133.         }while(read_len == BUFFER_SIZE);
  134.         close(fid);
  135.     }
  136.     isReadingEnded = 1;
  137.     pthread_mutex_unlock(&r_mutex);
  138. }
  139.  
  140. void* writer(void* arg)
  141. {
  142.     char* outputFile = (char*)arg;
  143.     int fid = open(outputFile, O_WRONLY | O_CREAT | O_APPEND | O_TRUNC);
  144.     if(fid == -1)
  145.     {
  146.         return;
  147.     }  
  148.     initWriteCb(fid);
  149.     while(!isReadingEnded)
  150.     {
  151.         pthread_mutex_lock(&w_mutex);
  152.         write_cb.aio_nbytes = read_len;
  153.         printf("%ld bytes writed\n", AWrite(&write_cb));
  154.         pthread_mutex_unlock(&r_mutex);
  155.     }
  156.     close(fid);
  157. }
  158.  
  159. void clearBuffer()
  160. {
  161.     bzero(buffer, BUFFER_SIZE-1);
  162. }
  163.  
  164. void initReadCb(int fid)
  165. {
  166.     read_cb.aio_fildes = fid;
  167.     read_cb.aio_offset = 0;
  168.     read_cb.aio_buf = &buffer;
  169.     read_cb.aio_nbytes = BUFFER_SIZE;
  170.     read_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
  171. }
  172. void initWriteCb(int fid)
  173. {
  174.     write_cb.aio_fildes = fid;
  175.     write_cb.aio_offset = 0;
  176.     write_cb.aio_buf = &buffer;
  177.     write_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement