static void check_sel_downx2(struct sel *sel)
{
	struct cstat *c1;
	struct cstat *c2;
	struct cstat *c3;
	fail_unless(sel!=NULL);
	fail_unless(sel->page==PAGE_CLIENT_LIST);
	get_clients(sel, &c1, &c2, &c3);

	fail_unless(sel->client==c3);
}
static void check_sel(struct sel *sel)
{
	struct cstat *c1;
	struct cstat *c2;
	struct cstat *c3;
	fail_unless(sel!=NULL);
	fail_unless(sel->page==PAGE_CLIENT_LIST);
	get_clients(sel, &c1, &c2, &c3);

	fail_unless(sel->client==c1);
	fail_unless(sel->backup==c1->bu);
	fail_unless(sel->backup->bno==1);
}
Beispiel #3
0
int			main(int ac, char **av)
{
  t_serv		serv;
  struct sockaddr_in	sin;
  struct sockaddr_in	csin;

  if (ac != 2)
    {
      write(2, "Usage : ./serveur port\n", 24);
      exit(EXIT_FAILURE);
    }
  serv.sin = &sin;
  serv.csin = &csin;
  serv.port = atoi(av[1]);
  init_serv(&serv, getprotobyname("TCP"));
  init_struct(&serv);
  get_clients(&serv);
  xclose(serv.s);
  return (EXIT_SUCCESS);
}
Beispiel #4
0
int main(int argc, char* argv[]){
	int i, out, num_threads, queue_max;
	char *dummy, *client_path, *out_path, *log_path, *client, *full_msg;
	srand(time(NULL));
	pthread_attr_t attr;
	num_threads = 5;
	queue_max = 7;
	full_msg = "INFO: Queue full. Waiting to add more clients.\n";

	//Incorporate command line arguments
	switch(argc){
		case 3:
			break;

		case 4:
			num_threads = (int)strtol(argv[3], &dummy, 10);
			break;

		case 5:
			queue_max = (int)strtol(argv[4], &dummy, 10);
			num_threads = (int)strtol(argv[3], &dummy, 10);
			break;

		default:
			fprintf(stderr, "ERROR: Invalid number of arguments\n");
			exit(0);
			break;
	}
	client_path = argv[1];
	out_path = argv[2];

	//Create log.txt
	log_path = malloc(sizeof(char) * strlen(out_path) + 9);
	strcpy(log_path, out_path);
	strcat(log_path, "/log.txt");
    if(open(log_path, O_CREAT | O_WRONLY | O_TRUNC, OPEN_2) == -1){
    	fprintf(stderr, "ERROR: open() failure\n");
    }

    //Load all clients from clients.txt
	all_clients = get_clients(client_path);
	//Fill the buffer to the queue_max limit
	buffer = malloc(sizeof(queue));
	i = 0;
	while(i < queue_max && size(all_clients) != 0){
		client = extract(all_clients);
		insert(buffer, client);
		i++;
	}

	//Begin threading
  	pthread_t threads[num_threads];
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	
	for (i = 0; i < num_threads; i++){
		pthread_create(&threads[i], &attr, (void*)&process_clients, out_path);
	}

	//The main thread tries to refill the buffer queue
	while(1){
		//If all_clients is empty, there are none left to move to the buffer
		if(size(all_clients) == 0){
			break;
		}

		pthread_mutex_lock(&mutex1);
		if(size(buffer) >= queue_max){
			//If the buffer is full, keep waiting
			pthread_mutex_unlock(&mutex1);
			out = open(log_path, O_CREAT | O_WRONLY | O_APPEND, OPEN_2);
			if(out == -1){
				fprintf(stderr, "ERROR: open() failure\n");
			}
			pthread_mutex_lock(&mutex2);
			write(out, full_msg, strlen(full_msg));
			close(out);
			pthread_mutex_unlock(&mutex2);
			usleep(rand()%100);
			continue;
		}
		else{ 
			//Refill the buffer queue to capacity
			while(size(buffer) < queue_max && size(all_clients) != 0){
				client = extract(all_clients);
				insert(buffer, client);
			}
			pthread_mutex_unlock(&mutex1);
			usleep(rand()%100);
			continue;
		}
	}

	//Wait for the child threads
	for(i = 0; i < count; i++){
		pthread_join(threads[i], NULL);
	}

	pthread_exit(NULL);
}