Example #1
0
  int main(int argc, char *argv[])
{ /*
  Open the pseudo terminal argv[1] or exit in case of error
  */
  pt_ch= open(argv[1],O_WRONLY);
  if(pt_ch<0){
    printf("Could not open pseudo terminal %s\n", argv[1]);
    exit(1);
  }

  char buff[READ]; //contains the message send from the client
  char *cmd[ARGMAX];//contains the comand to execute
  struct prodcons * b = &buffer;
  
  /*
  Server, buffer, thread initialization
  */
  int s = myServerSocket(atoi(argv[2]));
  init(&buffer); 
  pthread_create(&printer, NULL, print, 0);

  /*
  Infinite cicle to handle client requests
  */
  while(1) {
    int sc = myAcceptServerSocket(s);
    myReadSocket( sc, buff, READ );

    if(makeargv( buff, cmd)>1 ){ 
      if((b->writepos + 1) % BUFFER_SIZE == b->readpos) {
        myFree(buff);
        sprintf(buff, "b");

        myWriteSocket( sc, buff ,sizeof(char));
      }                            
      else if(producer(cmd[1])==1) { 
        myFree(buff);
        sprintf(buff, "t");

        myWriteSocket( sc, buff ,sizeof(char));
      } 
      else {
        myFree(buff);
        sprintf(buff, "f");

        myWriteSocket( sc, buff,sizeof(char));
      }                            
    }
    /*else if(makeargv( buff, cmd)==1 ){

      if((b->writepos + 1) % BUFFER_SIZE == b->readpos) {
       myFree(buff);
       sprintf(buff, "f");

       myWriteSocket( sc, buff,sizeof(char));
      }
      else{
        myFree(buff);
        sprintf(buff, "%d\n", b->writepos);

        myWriteSocket( sc, buff,strlen(buff));
      }*/

      else if(makeargv( buff, cmd)==1 ){
        int status = (((b->writepos - b->readpos) + BUFFER_SIZE) % BUFFER_SIZE);

        myFree(buff);
        sprintf(buff, "%d\n", status);

        myWriteSocket( sc, buff,strlen(buff));
      
   }
 }	


 void * retval;
  /* Wait until producer and consumer finish. */
 pthread_join(printer, &retval);
 return 0;
}
Example #2
0
int main(int argc, char* argv[]) {

	int port = DEFAULT_PORT; 
	char* calendarName = DEFAULT_CALENDAR_NAME;
	int create = 0;  

	// Process command line options
	char c;	
	while ((c = getopt (argc, argv, "cp:n:")) != -1)
		switch (c) {
    		case 'c':
      			create = 1;
       			break;
       		
	      	case 'p':
    	    	port = atoi(optarg);
        		break;
        
	        case 'n':
    	    	calendarName = optarg;
        		break;
        		
      		default:
        		print_server_usage(argv[0]);
				return 1;
     	}
 
 	// Create or open calendar database	
 	if (create) {
 		if (new_calendar_with_name(calendarName) == -1) {
      			printf("Error creating calendar %s: %s\n", calendarName, calendar_error_msg());
      			print_server_usage(argv[0]);
      			return 1;
      	}
    }
    else {
 		if (calendar_open_with_name(calendarName) == -1) {
      		printf("Error opening calendar %s: %s\n", calendarName, calendar_error_msg());
      		print_server_usage(argv[0]);
      		return 1;
    	}
    }
 
 
    // Creating the socket to receive incoming connections
	server_socket = myServerSocket(port);
    if (server_socket == -1) {
    	perror("");
    	return -1;
  	}
           		
    // Client connection handling loop
    while (1) {
		int client = myAcceptServerSocket(server_socket);    
		if (client == -1) 
    		perror("Error accepting client");
    	else {
    		// NOT IMPLEMENTED - START
    		pthread_t t;
    		pthread_create(&t, NULL, handleClient, (void*)(&client));
    	}
    		// NOT IMPLEMENTED - END

    		//handleClient(client); // handle the client
	}
    
    return 0;
}