Advertisement
AbraaoAllysson

semThread

Apr 12th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <fstream>
  4. #include <string.h>
  5. #include <dirent.h>
  6. #include <sstream>
  7. #include <vector>
  8. #include <thread>
  9. /*g++ -std=c++11 -pthread main.cpp */
  10. using namespace std;
  11.  
  12.  
  13. volatile int lock = 0;
  14. int SIZE =0;
  15. volatile int *contThread;
  16. int indice = -1;
  17.  
  18. void write(vector<string> filenames);
  19. void readFile(string filename);
  20. void* readFiles(char directory[], vector<string> *filenames);
  21.  
  22.  
  23.  
  24. class Requisition
  25. {
  26. string strGet_m;
  27. string strReq_m;
  28. public:
  29. Requisition(string strGet, string strReq)
  30. {
  31. this->strGet_m = strGet;
  32. this->strReq_m = strReq;
  33. }
  34. string getStrGet()
  35. {
  36. return strGet_m;
  37. }
  38. string getStrReq()
  39. {
  40. return strReq_m;
  41. }
  42. };
  43. /*void readReqs()
  44. {
  45. ifstream myFile(filename);
  46. cout << "\n\n\n*****************\n\n\n"<<endl;
  47. if(!myFile.is_open())
  48. {
  49. cout << "\n\n Unable to open file \n\n"<<endl;
  50. exit(0);
  51.  
  52. }
  53.  
  54. else
  55. {
  56. myFile.seekg(0,myFile.end);
  57. long size = myFile.tellg();
  58. myFile.seekg (0);
  59. char* buffer = new char[size];
  60. myFile.read(buffer,size);
  61. myFile.close();
  62. cout << "size is: " << size << " bytes.\n"<<endl;
  63. //writeReqs(filename,size);
  64. }
  65. } */
  66.  
  67. void readAnsw(string filename, string reqname)
  68. {
  69.  
  70. ifstream myFile(("/home/abraao/abraao/LP2/trabalho_2-Sublime/files/" +filename), ifstream::binary);
  71. cout << "\n\n\n*****************\n\n\n"<<endl;
  72. reqname.replace((reqname.size()-4), reqname.length(), "");
  73. // cout << filename << " " << reqname <<endl;
  74.  
  75. if(!myFile.is_open())
  76. {
  77. cout << "\n\n Unable to open file \n\n"<<endl;
  78. exit(0);
  79.  
  80. }
  81.  
  82. else
  83. {
  84.  
  85.  
  86. /* granvando na pasta*/
  87. string arqAll;
  88.  
  89. ofstream myfileWrite(( "/home/abraao/abraao/LP2/trabalho_2-Sublime/answ/" + reqname + ".ans") );
  90. while ( !myFile.eof() )
  91. {
  92. getline (myFile, arqAll);
  93. myfileWrite << arqAll;
  94. myfileWrite << "\n";
  95. // cout<<"ddfkdskjjdskfkjds" <<endl;
  96. }
  97.  
  98.  
  99. myFile.close();
  100. SIZE += myfileWrite.tellp();
  101. cout<< SIZE <<endl;
  102. /*int size = myfileWrite.tellp();
  103. cout << "size is: " << size << " bytes.\n"<<endl; */
  104. myfileWrite.close();
  105.  
  106.  
  107. //cout << "size is: " << size << " bytes.\n"<<endl;
  108. //write(string filename);
  109. }
  110.  
  111. }
  112.  
  113.  
  114. void* readFiles(char directory[], vector<string> *filenames)
  115. {
  116.  
  117. DIR *dir = NULL;
  118. struct dirent *drnt = NULL;
  119. string *filesArr, str;
  120. int aux = 0;
  121. int tamanho;
  122. dir = opendir(directory);
  123. char *diretorio = NULL;
  124. if(dir)
  125. {
  126.  
  127. while(drnt = readdir(dir)){aux++;}
  128. filesArr = new string[aux+1];
  129. aux = 0;
  130. seekdir(dir, 0);
  131. int tamanho;
  132. while(drnt = readdir(dir))
  133. {
  134.  
  135. str = (string)directory + "/" + (string)drnt->d_name;
  136. diretorio = (char *) malloc(sizeof(char)*30);
  137. strcpy(diretorio,drnt->d_name);
  138. tamanho = strlen(diretorio);
  139. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='t')&&(diretorio[tamanho-2]=='x')&&(diretorio[tamanho-1]=='t'))
  140. {
  141. filenames->push_back( (string)drnt->d_name );
  142.  
  143. }
  144. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='j')&&(diretorio[tamanho-2]=='p')&&(diretorio[tamanho-1]=='g'))
  145. {
  146. filenames->push_back( (string)drnt->d_name );
  147.  
  148. }
  149. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='p')&&(diretorio[tamanho-2]=='n')&&(diretorio[tamanho-1]=='g'))
  150. {
  151. filenames->push_back( (string)drnt->d_name );
  152.  
  153. }
  154. if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='r')&&(diretorio[tamanho-2]=='e')&&(diretorio[tamanho-1]=='q'))
  155. {
  156. filenames->push_back( (string)drnt->d_name );
  157.  
  158. }
  159.  
  160.  
  161.  
  162. }
  163.  
  164. }
  165. else
  166. {
  167.  
  168. cout << "Não foi possível abrir essa pasta " << directory << "\n";
  169.  
  170. }
  171.  
  172.  
  173. closedir(dir);
  174.  
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. vector <Requisition> readReqs(vector<string> filenamesReqs, vector<string> filenames)
  182. {
  183.  
  184. vector <Requisition> vR;
  185. string strGet,strReq,strFile;
  186. int i=0;
  187. for (int i = 0; i < filenamesReqs.size(); ++i)
  188. {
  189. strFile = "/home/abraao/abraao/LP2/trabalho_2-Sublime/reqs/" + filenamesReqs[i];
  190. //cout<< strFile <<endl;
  191. ifstream myfile(strFile);
  192. while (getline( myfile, strGet, ' ') )
  193. {
  194.  
  195. getline ( myfile, strReq);
  196. //cout << "GET: " << strGet <<endl;;
  197. // getline(myfile, strReq) ;
  198. //cout << "requisition " << strGet <<" ---"<< strReq << " FIM"<<endl;
  199. vR.push_back(Requisition(strGet,strReq ));
  200.  
  201. }
  202. myfile.close();
  203.  
  204. }
  205.  
  206. return vR;
  207.  
  208.  
  209.  
  210. }
  211.  
  212. void _unlock()
  213. {
  214. __sync_synchronize();
  215. lock = 0;
  216. }
  217.  
  218. void _lock()
  219. {
  220. while(__sync_lock_test_and_set(&lock, 1));
  221. }
  222.  
  223.  
  224. void *monitor(volatile int *contThr, int tam)
  225. {
  226.  
  227. for(int i = 0; i < tam; i++)
  228. {
  229.  
  230. while(contThr[i] != 1);
  231.  
  232. }
  233.  
  234. for(int i = 0; i < tam; i++)
  235. {
  236.  
  237. contThr[i] = 0;
  238.  
  239. }
  240.  
  241. }
  242.  
  243. /*void* openFile(std::vector<Requisition> vR, vector<string> filenames,vector<string> filenamesReqs, int i)
  244. {
  245.  
  246. // _lock();
  247.  
  248. for (int j = 0; j < filenames.size(); ++j)
  249. {
  250. if(vR[i].getStrGet()=="GET")
  251. {
  252. if(vR[i].getStrReq()== filenames[j])
  253. {
  254. //cout<< "i= " <<i << "j= " << j <<vR[i].getStrGet()<< "FIM" <<endl;
  255. //cout<< "i= " <<i << "j= " << j <<vR[i].getStrReq()<< "FIM" <<endl;
  256. readAnsw( filenames[j], filenamesReqs[i]);
  257. break;
  258. }
  259. }
  260.  
  261. else
  262. {
  263. cout<< i << " " << j <<" ERROR BAD COMMAND" <<endl;
  264. break;
  265. }
  266.  
  267. }
  268.  
  269.  
  270. indice++;
  271. // _unlock();
  272.  
  273. // __sync_fetch_and_add(&contThread[indice], 1);
  274. cout<<" SY " << i << " - " <<indice<<" "<< (int)contThread[indice] <<endl;
  275. //while(contThread[indice] == 1);
  276. cout<<" MERDAA "<<"valor = " << indice <<endl;
  277. }*/
  278.  
  279.  
  280. void openFile(std::vector<Requisition> vR, vector<string> filenames,vector<string> filenamesReqs)
  281. {
  282.  
  283.  
  284. for (int i = 0; i < vR.size(); ++i)
  285. {
  286. for (int j = 0; j < filenames.size(); ++j)
  287. {
  288. if(vR[i].getStrGet()=="GET")
  289. {
  290.  
  291. cout<<filenames[j] <<" "<<vR[i].getStrReq() <<endl;
  292.  
  293. if(filenames[j] == vR[i].getStrReq())
  294. {
  295.  
  296. cout<< "i= " <<i << "j= " << j <<vR[i].getStrGet()<< "FIM" <<endl;
  297. cout<< "i= " <<i << "j= " << j <<vR[i].getStrReq()<< "FIM" <<endl;
  298.  
  299. readAnsw( filenames[j], filenamesReqs[i]);
  300. break;
  301. }
  302.  
  303. }
  304.  
  305. else
  306. {
  307. cout <<" ERROR BAD COMMAND" <<endl;
  308. break;
  309. }
  310. }
  311. }
  312.  
  313.  
  314. lock = 0;
  315.  
  316. }
  317.  
  318.  
  319.  
  320. int main(int argc, char const *argv[])
  321. {
  322. /* code */
  323. //readFile("/home/abraao/abraao/LP2/Trabalho2/files/Homem-de-Ferro-1920x1080.jpg");
  324. char files[] = "files";
  325. vector<string> filenames,filenamesReqs;
  326. readFiles(files,&filenames);
  327. /*for (int i = 0; i <filenames.size(); ++i)
  328. {
  329. cout<< filenames[i] <<endl;
  330. } */
  331. char reqs[] = "reqs";
  332. readFiles(reqs,&filenamesReqs);
  333. /*for (int i = 0; i <filenamesReqs.size(); ++i)
  334. {
  335. cout <<filenamesReqs[i] <<endl;
  336. }*/
  337. //cout <<"\n********************"<<endl;
  338. vector<Requisition> vR = readReqs(filenamesReqs, filenames);
  339. /*for (int i = 0; i <vR.size(); ++i)
  340. {
  341. cout << vR[i].getStrGet() << " " <<vR[i].getStrReq()<< " dasas" << endl;
  342. }*/
  343.  
  344. //openFile(vR, filenames,filenamesReqs);
  345. //cout << vR.size();
  346. //contThread = new int[vR.size()];
  347.  
  348. // thread *threadReqs = new thread[vR.size()];
  349. openFile(vR,filenames,filenamesReqs);
  350. cout<< "\n Numeros de Bytes processado " << SIZE << endl;
  351. //for (int i = 0; i < vR.size(); ++i)
  352. //{
  353. //cout<< i <<" = "<< vR.size() << endl;
  354. //threadReqs[i] = thread(openFile, vR,filenames,filenamesReqs, i);
  355. // threadReqs[i].join();
  356.  
  357. //}
  358.  
  359. //thread threadMonitor(monitor, contThread , vR.size() );
  360. //threadMonitor.join();
  361. //for(int j = 0; j < vR.size(); j++)
  362. //{
  363.  
  364. // threadReqs[j].join();
  365. //
  366.  
  367.  
  368. //openFile(vR,filenames);
  369.  
  370.  
  371. //readReqs(filename[0]);
  372. // Constructs the new thread and runs it. Does not block execution.
  373. //vector<thread> vT1;
  374. //vT1.push_back( thread(r, files, &filenames) );
  375. // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
  376. //vT1[0].join();
  377.  
  378.  
  379.  
  380. return 0;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement