void* resolver(void* outputfp) { /* there is not SBUFSIZE for the resolver becuase hostname is set equil to * the value popped from the queue, which is a *void. hostname[SBUFSIZE] * would throw an error because the string popped from the queue could be * longer in length than SBUFSIZE. Therefor we have the hostname be a *char. */ char* hostname; /* Array to store the IP address string. It is long enought to store the * address which is INET6_ADDRSTRLEN long. */ char firstipstr[INET6_ADDRSTRLEN]; // all of the threads will keep getting IP addresses untill the queue is empty. // If the queue while(!queue_is_empty(&inputQueue) || requester_running) { hostname = queue_pop(&inputQueue); // popping a NULL from the queue means that the queue is empty. if(hostname == NULL) return EXIT_SUCCESS; if(dnslookup(hostname, firstipstr, sizeof(firstipstr)) == UTIL_FAILURE){ fprintf(stderr, "dnslookup error: %s\n", hostname); strncpy(firstipstr, "", sizeof(firstipstr)); } fprintf(outputfp, "%s,%s\n", hostname, firstipstr); free(hostname); } return EXIT_SUCCESS; }
int main(int argc, char* argv[]){ /* Local Vars */ FILE* inputfp = NULL; FILE* outputfp = NULL; char hostname[SBUFSIZE]; char errorstr[SBUFSIZE]; char firstipstr[INET6_ADDRSTRLEN]; int i; /* Check Arguments */ if(argc < MINARGS){ fprintf(stderr, "Not enough arguments: %d\n", (argc - 1)); fprintf(stderr, "Usage:\n %s %s\n", argv[0], USAGE); return EXIT_FAILURE; } /* Open Output File */ outputfp = fopen(argv[(argc-1)], "w"); if(!outputfp){ perror("Error Opening Output File"); return EXIT_FAILURE; } /* Loop Through Input Files */ for(i=1; i<(argc-1); i++){ /* Open Input File */ inputfp = fopen(argv[i], "r"); if(!inputfp){ sprintf(errorstr, "Error Opening Input File: %s", argv[i]); perror(errorstr); break; } /* Read File and Process*/ while(fscanf(inputfp, INPUTFS, hostname) > 0){ /* Lookup hostname and get IP string */ if(dnslookup(hostname, firstipstr, sizeof(firstipstr)) == UTIL_FAILURE){ fprintf(stderr, "dnslookup error: %s\n", hostname); strncpy(firstipstr, "", sizeof(firstipstr)); } /* Write to Output File */ fprintf(outputfp, "%s,%s\n", hostname, firstipstr); } /* Close Input File */ fclose(inputfp); } /* Close Output File */ fclose(outputfp); return EXIT_SUCCESS; }
void* ResThreads(void* empty) { char* outhostname; //hostname you pop off the queue char firstipstring[INET6_ADDRSTRLEN]; //while the search is incomplete or the queue is not empty while (!queue_is_empty(requesterQ) || !SearchComplete) { //if queue is empty wait while (queue_is_empty(requesterQ)){ if(SearchComplete) { free(outhostname); return 0; } } //if there is an item in the queue, pop pthread_mutex_lock(&Q_lock);//lock to have exclusion outhostname = queue_pop(requesterQ); //hostname pthread_mutex_unlock(&Q_lock); //Lookup hostname and get IP string if(dnslookup(outhostname, firstipstring, sizeof(firstipstring)) == UTIL_FAILURE) { fprintf(stderr, "dnslookup error: %s\n", outhostname); strncpy(firstipstring, "", sizeof(firstipstring)); } //Write to output file pthread_mutex_lock( &Out_lock ); //Lock the outputfile so you only put one thing at a time in it fprintf(outputfp, "%s,%s\n", outhostname, firstipstring); free(outhostname); pthread_mutex_unlock( &Out_lock ); } return 0; }
f_node::f_node (chordID i, str h, unsigned short p) : ID (i), host (h), port (p), vnode_num (0), fingers (NULL), predecessor (NULL), successors (NULL), selected (true), highlight (false) { draw = check_get_state (); dnslookup (); };
// f_node functions f_node::f_node (const chord_node &n) : ID (n.x), host (n.r.hostname), hostname(""), port (n.r.port), vnode_num (n.vnode_num), fingers (NULL), predecessor (NULL), successors (NULL), selected (true), highlight (false) { draw = check_get_state (); for (u_int i = 0; i < n.coords.size (); i++) coords.push_back (n.coords[i]); dnslookup (); }
void* OutputThread (void* p) { thread_resolve_arg_t* args = (thread_resolve_arg_t*) p; // need to pop items off of queue while true // queue in use while (1) { // lock queue mutex pthread_mutex_lock(args->mutex_queue); // pop item off of queue char* hostnamepop = queue_pop(args->rqueue); // unlock queue mutex pthread_mutex_unlock(args->mutex_queue); // check that queue is non-empty as looks up hostname and frees // it if (hostnamepop) { char hostname[SBUFSIZE]; sprintf(hostname, "%s", hostnamepop); free(hostnamepop); /* Lookup hostname and get IP string */ char firstipstr[INET6_ADDRSTRLEN]; if (dnslookup(hostname, firstipstr, sizeof(firstipstr)) == UTIL_FAILURE){ fprintf(stderr, "dnslookup error: %s\n", hostname); strncpy(firstipstr, "", sizeof(firstipstr)); } // lock output file mutex pthread_mutex_lock(args->mutex_ofile); // write output file fprintf(args->outputfp, "%s,%s\n", hostname, firstipstr); // unlock the output file pthread_mutex_unlock(args->mutex_ofile); // check for empty queue otherwise, no hostname // make sure reader threads are done } // if queue is still in use // there are still jobs in request queue to do else if (!request_queue_finished) { // sleep for random time between 0 and 100 microseconds usleep(rand() % 100); } else { // otherwise exit out break; } } return NULL; }
void* resolver(void* myFile){ // Handle filename char* file_name = (char*) myFile; FILE *file = NULL; int domains_popped = 0; char firstipstr[MAX_IP_LENGTH]; char* hostname; int loop = 1; // Flag for the while loop // Open file to append only file = fopen(file_name, "a"); if(!file){ // Check for error fprintf(stderr, "Error Opening Output File: %s\n", file_name); return NULL; } while(loop){ sem_wait(&_queue); // Check if queue has domain names to be resolved if(!queue_is_empty(&_q)){ hostname = queue_pop(&_q); domains_popped++; // Resolve hostname and output error if not correct if(dnslookup(hostname, firstipstr, sizeof(firstipstr))== UTIL_FAILURE){ fprintf(stderr, "dnslookup error: %s\n", hostname); // Error message strncpy(firstipstr, "", sizeof(firstipstr)); } // Write output to file fprintf(file, "%s,%s\n", hostname, firstipstr); free(hostname); // Release hostname memory } else{ sem_wait(&_requesterThreads); if (_num_closed == 0){ // If all input files have been closed loop = 0; // Exit the loop } sem_post(&_requesterThreads); } sem_post(&_queue); } fclose(file); // Close output file printf("Resolver thread has processed %d hostnames.\n", domains_popped); return NULL; }
void* Consumer(void* args){ consumer_args* arg_c = args; char hostname[MAX_NAME_LENGTH]; char strings[MAX_NAME_LENGTH]; while(1){ /* if producer thread is runing and queue is not empty */ if(!queue_is_empty(arg_c->q)){ /*We can strart to manipulate critical section */ pthread_mutex_lock(arg_c->consume_mutex); &hostname = (char *)queue_pop(arg_c->q); /* Lookup hostname and get IP string */ if(dnslookup(hostname, firstipstr, sizeof(firstipstr)) == UTIL_FAILURE){ fprintf(stderr, "dnslookup error: %s\n", hostname); strncpy(firstipstr, "", sizeof(firstipstr)); } /* Write */ strcpy(strings,hostname); strcat(strings,","); strcat(strings,firstipstr); fprintf(arg_c->file, "%s\n", strings); pthread_mutex_unlock(arg_c->produce_mutex); } if((!control)&&(queue_is_empty(arg_c->q))){ return NULL; } } return NULL; }
void* resolve_domain(void* params) { char *hostname; char firstipstr[INET6_ADDRSTRLEN]; int queue_is_not_empty = 0; //set queue to be empty unless we hear otherwise int continue_looping = 0; //don't loop by default pthread_mutex_lock(&mutex_for_queue); queue_is_not_empty = !queue_is_empty(&q); //check to see if the queue is empty - must lock for this behavior pthread_mutex_unlock(&mutex_for_queue); pthread_mutex_lock(&mutex_for_all_files_read); continue_looping = (!all_files_read)||(queue_is_not_empty); //all files are not read or the queue is not empty pthread_mutex_unlock(&mutex_for_all_files_read); while(continue_looping) { pthread_mutex_lock(&mutex_for_queue); if((hostname = (char*)queue_pop(&q)) == NULL) //pop off the hostname into the queue { //if it errored out //fprintf(stderr, "ERROR: queue_pop failed with value: %s\n", hostname); pthread_mutex_unlock(&mutex_for_queue); //unlock the queue for others to use } else { //if it didn't error out, if something actually did pop off the queue pthread_mutex_unlock(&mutex_for_queue); // Lookup hostname and get IP string if(dnslookup(hostname, firstipstr, sizeof(firstipstr)) == UTIL_FAILURE) { fprintf(stderr, "dnslookup error: %s\n", hostname); strncpy(firstipstr, "", sizeof(firstipstr)); //copy a NULL value to the IP string } //WRITE RESULT TO THE OUTPUT FILE pthread_mutex_lock(&mutex_for_output); //lock the output file fprintf(outputfp, "%s,%s\n", hostname, firstipstr); pthread_mutex_unlock(&mutex_for_output); //unlock the output file free(hostname); //free this chunk of memory from the queue if the pop was successful hostname = NULL; } //check to see if the while loop should continue pthread_mutex_lock(&mutex_for_queue); queue_is_not_empty = !queue_is_empty(&q); //check to see if the queue is empty - must lock for this behavior pthread_mutex_unlock(&mutex_for_queue); pthread_mutex_lock(&mutex_for_all_files_read); continue_looping = (!all_files_read)||(queue_is_not_empty); //all files are not read or the queue is not empty pthread_mutex_unlock(&mutex_for_all_files_read); } pthread_mutex_lock(&mutex_for_queue); //lock the queue if(queue_is_empty(&q)) //this is where we should end up after the while loop { //fprintf(stderr, "Queue is empty\n"); pthread_mutex_unlock(&mutex_for_queue); //unlock the queue for others to use pthread_exit(NULL); //Exit, Returning NULL return NULL; } else //this is where we error out if there's stuff in the queue - we shouldn't get here based on the while loop { fprintf(stderr, "Queue is NOT empty - THIS IS A PROBLEM\n"); pthread_mutex_unlock(&mutex_for_queue); //unlock the queue for others to use pthread_exit(EXIT_FAILURE); //Exit, RETURN AN ERROR CODE return EXIT_FAILURE; } //this should never be reached!!! - it's here JIC pthread_mutex_unlock(&mutex_for_queue); //unlock the queue for others to use /* Exit, Returning NULL*/ pthread_exit(NULL); return NULL; }