Beispiel #1
0
/**
 * The function to handle incoming client requests
 *    socket - the client socket descriptor
 *
 * The function returns to client:
 *    -1, if an error occurred in the processing of the request
 *    the id of the event, if ADD or REMOVE were successfully executed 
 *    the size of the list of events + the list itself, if a LIST* operation was successfully executed 
 */
void* handleClient(void* s) {
	int socket = *(int*) s;
	int nBytes;
	EventOp event_op;
	
	printf("Handling new client at socket %d.\n", socket);
	
	while ((nBytes = myReadSocket(socket, (char *) &event_op, sizeof(EventOp))) > 0) {
		
		int reply;
		switch (event_op.type) {
		
		case ADD: {
			pthread_mutex_lock(&lock); // NOT IMPLEMENTED

			printf("Handling ADD request from %s \n", event_op.user);
		
			Event event;
			nBytes = myReadSocket(socket, (char *) &event, sizeof(Event));
			strcpy(user(&event), event_op.user);
			
			if (nBytes == -1) 
				reply = -1;
			else {
				if (calendar_add(&event) == -1)
					reply = -1;
				else
					reply = event.id;
			}	
			myWriteSocket(socket, (char *) &reply, sizeof(int));
			pthread_mutex_unlock(&lock); // NOT IMPLEMENTED
			break;
		}
			 
		case REMOVE: {
			pthread_mutex_lock(&lock); //NOT IMPLEMENTED

			printf("Handling REMOVE request from %s\n", event_op.user);
	
			// NOT IMPLEMENTED - START

			if (calendar_remove(event_op.id) == -1)
					reply = -1;
				else
					reply = event_op.id;

			myWriteSocket(socket, (char *) &reply, sizeof(int));

			pthread_mutex_unlock(&lock);

			// NOT IMPLEMENTED - END

			break;
		}	
			
		case LIST: {
			pthread_mutex_lock(&lock); // NOT IMPLEMENTED

			printf("Handling LIST request from %s\n", event_op.user);
		
			// NOT IMPLEMENTED - START

			EventList* eventList = calendar_list_event(event_op.id);
			if (eventList == NULL) {
				int reply = -1; 
				myWriteSocket(socket, (char *) &reply, sizeof(int));
			}
			else
			{
				myWriteSocket(socket, (char *) &eventList->nEvents, sizeof(int));
				if (eventList->nEvents)
					myWriteSocket(socket, (char *) eventList->events, sizeof(Event) * eventList->nEvents);
				destroy_event_list(eventList);
			}

			pthread_mutex_unlock(&lock); // NOT IMPLEMENTED
			// NOT IMPLEMENTED - END

			break;	
		}
		
		case LIST_ALL: {
			pthread_mutex_lock(&lock); // NOT IMPLEMENTED

			printf("Handling LIST ALL request from %s \n", event_op.user);
		
			EventList* eventList = calendar_list();
			if (eventList == NULL) {
				int reply = -1; 
				myWriteSocket(socket, (char *) &reply, sizeof(int));
			}
			else {	
				myWriteSocket(socket, (char *) &eventList->nEvents, sizeof(int));
				if (eventList->nEvents)
					myWriteSocket(socket, (char *) eventList->events, sizeof(Event) * eventList->nEvents);
				destroy_event_list(eventList);
			}

			pthread_mutex_unlock(&lock); // NOT IMPLEMENTED
			break;	
		}
		
		case LIST_ONGOING_EVENTS: {
			printf("Handling LIST_ONGOING_EVENTS request from %s \n", event_op.user);	
			
			time_t t = time(NULL);
			struct tm *tm_struct = localtime(&t);
			EventList* eventList = calendar_list_ongoing(
				tm_struct->tm_mday, tm_struct->tm_mon+1, tm_struct->tm_year+1900, 
				tm_struct->tm_hour*100 + tm_struct->tm_min);
	
			if (eventList == NULL) {
				int reply = 0;  // 0 is ignored on the client side
				myWriteSocket(socket, (char *) &reply, sizeof(int));
			}
			else {		
				myWriteSocket(socket, (char *) &eventList->nEvents, sizeof(int));
				if (eventList->nEvents)
					myWriteSocket(socket, (char *) eventList->events, sizeof(Event) * eventList->nEvents);
				destroy_event_list(eventList);
			}
			break;
		}
		
		default:
			printf ("Internal error: Unknown request type %d.\n", event_op.type);	
		}	
	}

	close(socket);
	printf("Closed client connection.\n");
	
	if (nBytes == -1) 
		perror("");

	return NULL;
}
Beispiel #2
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;
}
/*
 * Handle a request issued from the command line.
 * It sends the request to the server and waits for the reply
 *    request - the request information
 */
int communicate_event_request(Request* request) {

	int res = myWriteSocket(request->socket, (char *) &request->event_op, sizeof(EventOp));
	if (res == -1)
		return -1;
	
	if (operation(request) == ADD) {	
		myWriteSocket(request->socket, (char *) &request->event, sizeof(Event));
		if (res == -1)
			return -1;
	}
	
	
	int reply;
	res = myReadSocket(request->socket, (char *) &reply, sizeof(int));
	
	if (res == -1)
		return -1;

	if (reply == -1) 
			printf ("Error: Could not perform operation.\n");
	
	else if (reply > 0) { 			
		if (is_list(request)) {
			EventList* eventList = new_event_list(reply);	
			res = myReadSocket(request->socket, (char *) eventList->events, 
							eventList->nEvents * sizeof(Event));
			if (res == -1) 	
				return -1;

			if (operation(request) == LIST_ONGOING_EVENTS) {
				// NOT IMPLEMENTED - START
				printf("\n******************\n*  NOTIFICATION  *\n******************\n");
				print_event_list(eventList);

				// NOT IMPLEMENTED - END		
				return 0;
			}
			else 
				print_event_list(eventList);	
	
			destroy_event_list(eventList);	
		}
		else {
			switch (operation(request)) {
		
				case ADD:
					printf ("Added event number %d\n", reply);
					break;
					
				case REMOVE:
					
					printf("Removed event number %d\n", reply);
					
					break;
						
				default:	
					printf ("Internal Error - client handling operation %d\n", (-reply));
			}
		}
	}
			
	return 0;
}