Пример #1
0
void manage_clients(int server_socket, client_data * * client_list)
{ int i, room_number = 0;
  struct pollfd open_room[max_users + 1];
  // create poll field for each client
  for (i = 0; i < max_users; i += 1)
  { open_room[i].fd = 0;
    open_room[i].events = 0;
    open_room[i].revents = 0; }
  //create poll field for server
  open_room[max_users].fd = server_socket;
  open_room[max_users].events = POLLRDNORM;
  open_room[max_users].revents = 0;
  while (1)
  { // indicates wait 200ms between calls to poll
    int p = poll(open_room, max_users + 1, 200);
    if (p < 0)
    { perror("poll error");
      usleep(100000);
      continue; }
    if (open_room[max_users].revents && POLLRDNORM)
    { int room_socket = accept_client_connection(server_socket);
      if (room_number < max_users)
      { open_room[room_number].fd = room_socket;
        open_room[room_number].events = POLLRDNORM || POLLERR;
        open_room[room_number].revents = 0;
        print_online_users(room_socket, client_list);
        get_user_initials(room_socket, room_number, client_list);
        room_number += 1; } }
    for (i = 0; i < max_users; i += 1)
    { if (open_room[i].revents && POLLRDNORM)
      { printf("\nreceived data from %d\n", i);
        int s = send_outgoing_message(open_room[i].fd, i, client_list);
        if (!s)
        { print_user_logout(open_room[i].fd, i, client_list);
          close(open_room[i].fd);
          open_room[i].fd = 0;
          open_room[i].events = 0;
          open_room[i].revents = 0;
          client_list[i]->room_socket = 0; } } }
  }
}
int main (int n_args, char ** args) {
	cout << "******* Image storage server *******" << endl << endl;
	
	if (!check_server_arguments (n_args, args)) {
		cerr << "#SERVER > ERROR - Invalid argument" << endl;
		exit(-1);
	}
	if (!socket_initialization_server (&listen_socket, SP_port, (int) BACKLOG_QUEUE)) {
		cerr << "#SERVER > ERROR - Can't create a listen socket correctly" << endl;
		exit(-1);
	}
	
	
	// Registering services
	store_image = new Store_image ("store_image", SP_address, SP_port);
	get_image = new Get_image ("get_image", SP_address, SP_port);
	get_list = new Get_list ("get_list", SP_address, SP_port);
	
	cout << "#SERVER > Registering image_storage server" << endl;
	if (!register_service_provider (SP_address, SP_port)) cerr << "#SERVER > " << SPACER << "ERROR - Can't register the server" << endl;
	else {
		cout << "#SERVER > Registering store_image service" << endl;
		if (!register_service (store_image->get_description())) cerr << "#SERVER > " << SPACER << "ERROR - Can't register the store_image service" << endl;
		
		cout << "#SERVER > Registering get_image service" << endl;
		if (!register_service (get_image->get_description())) cerr << "#SERVER > " << SPACER << "ERROR - Can't register the get_image service" << endl;
	
		cout << "#SERVER > Registering get_list service" << endl;
		if (!register_service (get_list->get_description())) cerr << "#SERVER > " << SPACER << "ERROR - Can't register the get_list service" << endl;
	}
	
	// Initializing mutex
	pthread_mutex_init (&mutex_1, NULL);
	pthread_mutex_init (&mutex_2, NULL);
	pthread_mutex_init (&mutex_thread_free, NULL);
	pthread_cond_init (&cond_thread_free, NULL);
	readers_count = 0;
	
	// Creating threads
	pthread_t thread_ID;
	
	for (int i = 0; i < N_THREADS; i++) {
		if (pthread_create(&thread_ID, NULL, thread_body, (void*) i) != 0) {
			cerr << "#SERVER > ERROR - Can't create a service thread" << endl;
			exit(-1);			
		}
		thread[i].set_ID (thread_ID);
	}
	if (pthread_create(&thread_ID, NULL, control_thread_body, NULL) != 0)  {
		cerr << "#SERVER > ERROR - Can't create the control thread" << endl;
		exit(-1);			
	}
	control_thread.set_ID (thread_ID);
	
	
	cout << "\n#SERVER > Waiting for connections" << endl;
	while (control_thread.is_active()) {
		int client_socket = accept_client_connection (listen_socket);
		if (client_socket == -1)  {
			if (control_thread.is_active()) cerr << "#SERVER > ERROR - Can't accept the connection with the client" << endl;
			continue;
		}
		
		while (!assign_execution_thread (client_socket)) pthread_cond_wait (&cond_thread_free, &mutex_thread_free);
	}

	
	// Closing sockets and threads
	for (int i = 0; i < N_THREADS; i++) {
		thread[i].thread_exit();
		pthread_join(thread[i].get_ID(), NULL);
	}
	pthread_join(control_thread.get_ID(), NULL);
	
	shutdown(listen_socket, SHUT_RDWR);
	close(listen_socket);
	
	
	// Unregistering services - Only unregister_service_provider is needed
	cout << "#SERVER > Unregistering store_image service" << endl;
	if (!unregister_service (store_image->get_description())) cerr << "#SERVER > " << SPACER << "ERROR - Can't unregister the store_image service" << endl;
	delete (store_image);
	
	cout << "#SERVER > Unregistering get_image service" << endl;
	if (!unregister_service (get_image->get_description())) cerr << "#SERVER > " << SPACER << "ERROR - Can't unregister the get_image service" << endl;
	delete (get_image);
	
	cout << "#SERVER > Unregistering get_list service" << endl;
	if (!unregister_service (get_list->get_description())) cerr << "#SERVER > " << SPACER << "ERROR - Can't unregister the get_list service" << endl;
	delete (get_list);
	
	cout << "#SERVER > Unregistering image_storage server" << endl;
	if (!unregister_service_provider (SP_address, SP_port)) cerr << "#SERVER > " << SPACER << "ERROR - Can't unregister the server" << endl;
	
	
	cout << endl << "#SERVER > Server closed" << endl;
	cout << endl << "***********************" << endl;
	exit(0);
}
Пример #3
0
static void *acceptor_thread_main(void *p)
{
    while (true)
        accept_client_connection();
    return NULL;
}
main ()
{
    int port;
    std::vector<Sat*> sat_tracking;

    /********************************************************************************/
    /************************************** PIPE ***********************************/
    /********************************************************************************/
	int ttt; //PIPE
	char *pipe = "/tmp/satObPIPE";

	mkfifo(pipe,0666);

    /********************************************************************************/
    /********************************** SEMAPHORE CAPTURE **************************/
    /********************************************************************************/
    key_t key = ftok ("/bin/ls", 1990);
    if (key == (key_t)-1){
        std::cout << "Fatal error in shared memory. Can not create key.\n";
        return -1;
    }
    int sem_id = semget (key,1,0600 | IPC_CREAT);
    if (sem_id == -1){
        std::cout << "Fatal error in shared memory. Can not create/open semaphore.\n";
        return -1;
    }
	semctl(sem_id,0,SETVAL,1);
    /********************************************************************************/
    /********************************** SEMAPHORE FREQUENCY ************************/
    /********************************************************************************/
    key_t keyf = ftok ("/bin/ls", 1995);
	if (keyf == (key_t)-1)
	{
		std::cout << "Fatal error in shared memory. Can not create key.\n";
        return -1;
	}
    int sem_idf = semget (keyf,1,0600 | IPC_CREAT);
	if (sem_idf == -1)
	{
		std::cout << "Fatal error in shared memory. Can not create/open semaphore.\n";
        return -1;
	}
	semctl(sem_idf,0,SETVAL,1);

    /********************************************************************************/
    /********************************** SHARED MEMORY ******************************/
    /********************************************************************************/
	key_t memory_key = ftok ("/bin/ls", 1968);
	if (memory_key == -1)
	{
        std::cout << "Fatal error in shared memory. Can not create key.\n";
        return -1;
	}
	int memory_id = shmget (memory_key, sizeof(int), 0777 | IPC_CREAT);
	if (memory_id == -1)
	{
        std::cout << "Fatal error in shared memory. Can not create shared memory.\n";
        return -1;
	}
	int* memory = (int *)shmat(memory_id, (char *)0, 0);
	if (memory == NULL)
	{
        std::cout << "Fatal error in shared memory. Can not create shared memory.\n";
        return -1;
	}
	*memory=0;
    /********************************************************************************/
    /********************************** CONFIGURATION ******************************/
    /********************************************************************************/
    char result[PATH_MAX];
    ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
    std::string path( result, (count > 0) ? count : 0 );
    path=path.substr(0,path.length()-7);
    path.insert(path.length(),"/default.ini");

    boost::property_tree::ptree conf;
    boost::property_tree::ini_parser::read_ini(path, conf);
    port=std::atoi(conf.get<std::string>("CONNECTION.Port").c_str());

    /********************************************************************************/
    /********************************** CONNECTION *********************************/
    /********************************************************************************/
	int socket_server;
	int socket_client;

	socket_server = open_inet_socket(port);
	if (socket_server == -1)
	{
		printf ("Can not open server socket.\n");
		exit (-1);
	}


    /********************************************************************************/
    /********************************** SATELLITES RECEPTION ***********************/
    /********************************************************************************/
    while(true){
        std::string satellite_number;
        std::string frequency;
        char str;
        time_t timer;

        socket_client = accept_client_connection (socket_server);
        if (socket_server == -1)
        {
            printf ("Can not open client socket.\n");
            exit (-1);
        }
        for(int i=1;i>0;++i){
            read_socket (socket_client, &str, 1);
            if(str=='|'){
                i=-1;
            }
            else{
                satellite_number=satellite_number.insert(satellite_number.size(),1,str);
            }
        }

        for(int i=1;i>0;++i){
            read_socket (socket_client, &str, 1);
            if(str=='&'){
                i=-1;
            }
            else{
                frequency=frequency.insert(frequency.size(),1,str);
            }
        }
        close (socket_client);

        /********************************************************************************/
        /********************************** SAVE SATELLITES TRACKED ********************/
        /********************************************************************************/
        bool exist_sat=false;
        //If satellite is having tracking only change (or not) the frequency and date
        for(int i = 0; (i< sat_tracking.size()) && (exist_sat==false); i++)
        {
            if(std::atoi(satellite_number.c_str())==sat_tracking[i]->satellite_number ){
                exist_sat=true;
            }
        }

        if(exist_sat==false){
            Sat* p_sat=new Sat();
            p_sat->satellite_number=std::atoi(satellite_number.c_str());
            p_sat->frequency=std::atof(frequency.c_str());
            std::time(&timer);
            p_sat->time=timer;

            /********************************************************************************/
            /********************************** SAT-OBSERVER INSTANCE **********************/
            /********************************************************************************/

            //Create a sat-observer instance.
            int child = fork();
            switch(child){
                case -1:
                    std::cout << "Error: Can not create sat-observer instance.\n";
                break;
                //Child process: sat-observer
                case 0:
                    char result[PATH_MAX];
                    ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
                    std::string path( result, (count > 0) ? count : 0 );
                    path=path.substr(0,path.length()-6);
                    path.insert(path.length(),"sat-observer ");
                    path.insert(path.length(),satellite_number);
                    path.insert(path.length()," ");
                    path.insert(path.length(),frequency);
                    system(path.c_str());
                break;
            }
            p_sat->proccess_id=child;
            sat_tracking.push_back(p_sat);
        }

        for(int i = 0; i < sat_tracking.size(); i++)
        {
            char* dt = ctime(&(sat_tracking[i]->time));
            std::cout << "Satellite number: " << sat_tracking[i]->satellite_number << " || Frequency: " << sat_tracking[i]->frequency << " || " << dt <<  "\n";
        }
        std::cout << "\n\n\n";
    }


	close (socket_client);
	close (socket_server);
}