void negotiator(void * ptr) { int *new = (int*) ptr; int newsockfd = *new; int panning = 1; char fileName[256]; char buff[256]; int noOfBytes = 0; bzero(fileName, 256); linkedList *panList = searchWorker(socketList, newsockfd); while ((noOfBytes = Receive(newsockfd, fileName, sizeof(fileName))) > 0) { if (strcmp(fileName, "EXIT") == 0) { break; } //fileName[noOfBytes] = 0; fprintf(stderr, "\n%s\n", fileName); //pthread_mutex_lock(&mutex); unsigned int size = getSize(fileName); //pthread_mutex_unlock(&mutex); sprintf(buff, "%d", size); fprintf(stderr, "%d", size); if (doSend(panning) == 1) { Send(newsockfd, buff, strlen(buff)); char* s = (char*) malloc(size * sizeof(char) + 1); //pthread_mutex_lock(&mutex); readFile(s, fileName); //pthread_mutex_unlock(&mutex); fprintf(stderr, "\n %ld \n", strlen(s)); int i, j, temp; i = 0; j = strlen(s) - 1; while (i < j) { temp = s[i]; s[i++] = s[j]; s[j--] = temp; } Send(newsockfd, s, size); free(s); panning = panList->data.priority; } //send the file length as 0 else{ sprintf(buff, "%d", 0); Send(newsockfd, buff, strlen(buff)); panning = panList->data.priority; } } Close(newsockfd); }
int main(int argc, char * argv[]) { boost::thread_group worker_group; std::vector<boost::exception_ptr> index_errors; CmdLineOptions user_options; boost::shared_ptr<BoundedQueue<std::string>> FilesToIndex( new BoundedQueue<std::string>(MAX_QUEUE_FILES) ); try { /* get exceptions from threads launched or FileIndexer_exception */ get_CmdLineOptions(argc,argv,std::move(user_options)); /* Launch search thread */ boost::exception_ptr search_error; boost::thread searchWorker(searchPath, user_options, FilesToIndex, boost::ref(search_error)); /* Launch worker threads */ bool exception_thrown = false; int i; for (i = 0; ((i < user_options.N) && !exception_thrown); ++i) { boost::exception_ptr index_error; FileWorker _w(i, user_options.debug); try { worker_group.create_thread(boost::bind(&FileWorker::run, &_w, FilesToIndex, boost::ref(index_error))); } catch (boost::thread_resource_error const& e) { exception_thrown = true; std::cout << "Couldn't launch as many threads as requested: " << e.what() << std::endl; std::cout << "Continuing with " << i << " threads\n"; // don't push index_error continue; } catch (std::exception const& e) { std::cout << "Thread " << i << " thew exception: " << e.what() << std::endl; // need to tear down because we dont know what caused this cleanupWorkers(std::move(worker_group),std::move(index_errors)); return EXIT_ERROR; } index_errors.push_back(index_error); } /* Wait for workers to drain FilesToIndex */ searchWorker.join(); if( search_error ) boost::rethrow_exception(search_error); if (user_options.debug > 1) std::cout << "search worker has joined" << std::endl; cleanupWorkers(std::move(worker_group),std::move(index_errors)); if (user_options.debug > 1) std::cout << "index workers have all joined" << std::endl; /* print the resulting main index */ /* all done */ } /* --- main try block --- */ catch(const recursive_directory_iterator_error &e) { // std::cout << std::endl // << e.what() << std::endl; cleanupWorkers(std::move(worker_group),std::move(index_errors)); return EXIT_ERROR; } catch(std::exception const& e) { std::cout << std::endl << e.what() << std::endl; cleanupWorkers(std::move(worker_group),std::move(index_errors)); return EXIT_ERROR; } /* --- main catch block -- */ return EXIT_SUCCESS; } /* --- int main() --- */