static void AddGameToList(HWND List, map<string, string> Headers)
{
  if (List != NULL)
  {
    int Index = ListView_GetItemCount(List);
    /* Add a new item to the list view */
    LVITEM ItemInfo;
    ItemInfo.mask = 0;
    ItemInfo.iItem = Index;
    ItemInfo.iSubItem = 0;
    ListView_InsertItem(List,&ItemInfo);
    /* Set the item's other attributes */
    ListView_SetItemText(List,Index,0,(CHAR*)Headers["Date"].c_str());
    ListView_SetItemText(List,Index,1,(CHAR*)Headers["White"].c_str());
    ListView_SetItemText(List,Index,2,(CHAR*)Headers["Black"].c_str());
    char* Str = timeformat(atoi(Headers["WhiteTime"].c_str()));
    ListView_SetItemText(List,Index,3,(CHAR*)Str);
    delete[] Str;
    Str = timeformat(atoi(Headers["BlackTime"].c_str()));
    ListView_SetItemText(List,Index,4,(CHAR*)Str);
    delete[] Str;
    ListView_SetItemText(List,Index,5,(CHAR*)Headers["Round"].c_str());
    ListView_SetItemText(List,Index,6,(CHAR*)Headers["Result"].c_str());
  }
}
	/**
 	 * \brief change the state
	 */
	void	changeState(FocusState state,
			const Ice::Current& /* current */) {
		std::cout << timeformat("%H:%M:%S ", time(NULL));
		std::cout << "new state: ";
		std::cout << focusingstate2string(state);
		std::cout << std::endl;
	}
Exemple #3
0
void PlayerPanel::DrawPlayerInformation(HDC DC, int X, int Y, int Width, int Height)
{
  if (Game != NULL)
  {
    const ChessPlayer* Player = Game->GetPlayer(Color);
    SetBkMode(DC,TRANSPARENT);
    SetTextColor(DC, GetSysColor(COLOR_BTNTEXT));
    /* Highlight the active player */
    if (Game->GetActivePlayer() == Color && Game->GetState() != Undefined)
    {
      HBRUSH OldBrush = (HBRUSH)SelectObject(DC,CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT)));
      HPEN OldPen = (HPEN)SelectObject(DC,CreatePen(PS_SOLID,1,GetSysColor(COLOR_HIGHLIGHT)));
      Rectangle(DC,X,Y,X+Width,Y+Height);
      DeleteObject(SelectObject(DC,OldPen));
      DeleteObject(SelectObject(DC,OldBrush));
      SetTextColor(DC,GetSysColor(COLOR_HIGHLIGHTTEXT));
    }
    if (Game->GetState() >= Started)
    {
      /* Draw the player's time */
      SIZE S;
      HFONT OldFont = (HFONT)SelectObject(DC,EasyCreateFont(DC,DefaultSystemFont,8,0));
      char* Str = timeformat(Player->MoveTime/1000);
      GetTextExtentPoint32(DC,Str,strlen(Str),&S);
      TextOut(DC,X+4,Height-S.cy-2,Str,strlen(Str));
      delete[] Str;
      DeleteObject(SelectObject(DC,OldFont));
    }
    /* Draw the player's status */
    string Text;
    if (IsReady)
      Text = "Ready";
    else if (Game->GetActivePlayer() == Color)
    {
      if (Game->IsPlayerChecked() && Game->IsPlayerMated())
        Text = "Checkmate";
      else if (Game->IsPlayerMated())
        Text = "Stalemate";
      else if (Game->IsPlayerChecked())
        Text = "Check";
    }
    if (Text.length() > 0)
    {
      SIZE S;
      HFONT OldFont = (HFONT)SelectObject(DC,EasyCreateFont(DC,DefaultSystemFont,8,fsBold));
      GetTextExtentPoint32(DC,Text.c_str(),Text.length(),&S);
      TextOut(DC,X+Width-S.cx-4,Height-S.cy-2,Text.c_str(),Text.length());
      DeleteObject(SelectObject(DC,OldFont));
    }
    /* Clean up */
    SetBkMode(DC,OPAQUE);
  }
}
Exemple #4
0
char* createQID(char* SID, struct tm *t) {
	char *QID = malloc(sizeof(char)* (23 + 1));
	sprintf(QID,"%s%s", SID, timeformat(t));
	return QID;
}
Exemple #5
0
int main(int argc, char **argv) {

	int fd_tcp, fd_ecp, newfd, clientlen, serverlen, ret, questionnaire_number, nbytes, nleft, size, score=0;
	
	struct sockaddr_in serveraddr, clientaddr;
	struct hostent *hostptr;
	
	unsigned short int TESport = TES_PORT, ECPport = ECP_PORT;
	
	ssize_t rcvd_bytes, send_bytes, read_file_bytes, send_bytes_data, AQT_bytes, AQS_bytes;

	char *SID, *QID, dtime[18+1], *data, filename[12+1], *correctanswers, *ptr1, ECPname[MAX_BUF], recv_str[MAX_BUF], send_str[MAX_BUF];
		 SID = (char*)malloc(sizeof(char) * (5 + 1));
		 QID = (char*)malloc(sizeof(char) * (23 + 1));
		 
		 
	char usage[] = "usage: ./TES [-p TESport] [-n ECPname] [-e ECPport]";

	time_t rawtime;
  	struct tm * current_time;
  	time (&rawtime);
  	current_time = localtime (&rawtime);
 	
	pid_t pid;
	void(*old_handler)(int);//interrupt handler

							/* Avoid zombies when child processes die. */
	if ((old_handler = signal(SIGCHLD, SIG_IGN)) == SIG_ERR) exit(1);
	
	if (signal(SIGALRM, handler_alrm) == SIG_ERR) { 
		perror("Error in signal");
		exit(1); 
	}
	

	if (argc % 2 != 1 || argc > 7) {
		printf("error: Incorrect number of arguments.\n%s\n", usage);
		return 1;
	}

	if (argc == 3) {
		if (!strcmp(argv[1], "-p")) TESport = atoi(argv[2]);
		else if (!strcmp(argv[1], "-n")) strcpy(ECPname, argv[2]);
		else if (!strcmp(argv[1], "-e")) ECPport = atoi(argv[2]);
		else {
			printf("error: Invalid option.\n%s\n", usage);
			return 1;
		}
	}

	if (argc == 5) {
		if (!strcmp(argv[1], "-p")) {
			TESport = atoi(argv[2]);
			if (!strcmp(argv[3], "-n")) strcpy(ECPname, argv[4]);
			else if (!strcmp(argv[3], "-e")) ECPport = atoi(argv[4]);
			else {
				printf("error: Invalid option.\n%s\n", usage);
				return 1;
			}
		}
		if (!strcmp(argv[1], "-n")) {
			strcpy(ECPname, argv[2]);
			if (!strcmp(argv[3], "-e")) ECPport = atoi(argv[4]);
			else {
				printf("error: Invalid option.\n%s\n", usage);
				return 1;
			}
		}
		else {
			printf("error: Invalid option.\n%s\n", usage);
			return 1;
		}
	}

	if (argc == 7) {
		if (!strcmp(argv[1], "-p")) {
			TESport = atoi(argv[2]);
			if (!strcmp(argv[3], "-n")) {
				strcpy(ECPname, argv[4]);
				if (!strcmp(argv[1], "-e")) ECPport = atoi(argv[2]);
				else {
					printf("error: Invalid option.\n%s\n", usage);
					return 1;
				}
			}else {
				printf("error: Invalid option.\n%s\n", usage);
				return 1;
			}
		}else {
			printf("error: Invalid option.\n%s\n", usage);
			return 1;
		}
	}


	if ((fd_tcp = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("Error creating tcp socket.\n");
		exit(1);
	}
	if ((fd_ecp = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
		perror("Error creating ecp socket.\n");
		exit(1);
	}
	memset((void*)&serveraddr, (int)'\0', sizeof(serveraddr));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
	serveraddr.sin_port = htons((u_short)TESport);
	if (bind(fd_tcp, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) == -1) {
		perror("Error: bind().\n");
		exit(1);
	}
	if (listen(fd_tcp, 5) == -1){
		perror("Error: listen().\n");
		exit(1);
	}

	while (1) {

		clientlen = sizeof(clientaddr);

		do newfd = accept(fd_tcp, (struct sockaddr*)&clientaddr, &clientlen);//wait for a connection
		while (newfd == -1 && errno == EINTR);
		if (newfd == -1){
			perror("Error: accept().\n");
			exit(1);
		}

								/* Create a child process for each new connection. */
		if ((pid = fork()) == -1) {
			perror("Error: fork().\n");
			exit(1);
		}
		else if (pid == 0) { //child process
			close(fd_tcp);
			printf("Sent by [%s :%hu]\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
									
								/* USER-TES Protocol (in TCP) */
		
			ptr1 = readTCPclient(newfd);
			printf("\nReceived message:«%s»\n", ptr1);
		
			//we can use parseString() from functions.c...
			char* arr[8];
			char * ch;
			ch = strtok(strdup(ptr1), " ");
			int i = 0;
			while (ch != NULL) {
				arr[i] = ch;
				i++;
				ch = strtok(NULL, " ");
			}

			/* USER-TES: RQT SID
			TES-USER: AQT QID time size data */

			if (strcmp(arr[0], "RQT") == 0 && atoi(arr[1]) < 100000 && atoi(arr[1]) > 9999) {
				
				strcpy(SID, arr[1]);
			
				questionnaire_number = rand() % (NUM_OF_FILES + 1);//choose randomly 1 of available questionnaires
				sprintf(filename, "T%dQF%d.pdf", TES_NUMBER, questionnaire_number);
				
				sprintf(dtime, "%s",timeformat(deadline(current_time)));
				//generate QID: TES_NUMBER+questionnaire_number+deadline
				sprintf(QID, "%d_%d_%s",  TES_NUMBER, questionnaire_number, timeformat1(deadline(current_time))); 

				FILE *handler = fopen(filename, "rb");
				if (handler == NULL){
					perror("Error: fopen().\n");
					exit(1);
				}

				fseek(handler, 0, SEEK_END);
				size = ftell(handler);
				rewind(handler);

				//allocate a string that can hold it all
				data = (char*)malloc(sizeof(char) * (size + 1));
				//read it all in one operation
				read_file_bytes = fread(data, sizeof(char), size, handler);

				if (size != read_file_bytes) {
					free(data);
					perror("Error: fread().\n");
					exit(1);
				}

				//each message ends with the character '\n'
				AQT_bytes = sprintf(send_str, "AQT %s %s %d ", QID, dtime, size);

				ptr1 = send_str;

				/* write() may write a smaller number of bytes than solicited */
				nleft = AQT_bytes + read_file_bytes; // '\0' is not transmitted

				while (nleft > 0) {
					if (nleft > read_file_bytes) {
						send_bytes = write(newfd, ptr1, AQT_bytes);
						if (send_bytes <= 0) {
							perror("Error: write().\n");
							exit(1);
						}
						nleft -= send_bytes;
						ptr1 += send_bytes;
					}
					else {
						send_bytes_data = write(newfd, data, nleft);
						if (send_bytes_data <= 0) {
							perror("Error: data write().\n");
							exit(1);
						}
						nleft -= send_bytes_data;
						data += send_bytes_data;
					}
					
				}
				send_bytes_data = write(newfd, "\n", 1);
				if (send_bytes_data <= 0) {
					perror("Error: data write().\n");
					exit(1);
				}

				close (newfd); exit(0);
			}
			
			else if (strcmp(arr[0], "RQS") == 0 ) {
			  	
			  if (atoi(arr[1]) > 99999 ||  atoi(arr[1]) < 10000 || sizeof(arr[2]) > 24){
				  if ((send_bytes = write(newfd, "-2\n", 4)) <= 0)exit(1);
				  close(newfd); exit(1);
			  }
			  
			  strcpy(SID, arr[1]);
			  strcpy(QID, arr[2]);
			
			   if (checkdeadline(arr[2],current_time) == -1){
				  if ((send_bytes = write(newfd, "-1\n", 4)) <= 0)exit(1);
				  close(newfd); exit(1);
				  }
			  
			  
			  for (i = 0; i < 5; i++) {	  
				  if (strcmp(arr[i + 3], "A") == 0 ||
					  strcmp(arr[i + 3], "B") == 0 ||
					  strcmp(arr[i + 3], "C") == 0 ||
					  strcmp(arr[i + 3], "D") == 0 ||
					  strcmp(arr[i + 3], "N") == 0)continue;
				  else {
					  printf("\n----->invalid answers\n");
					  if ((send_bytes = write(newfd, "ERR\n", ERR_SIZE)) <= 0)exit(1);
				  }
			  }
			  
			  FILE *a = fopen(giveanswersfilename(QID), "r");
			  if (a == NULL) {
				  perror("Error: fopen().\n");
				  exit(1);
			  }
			  fseek(a, 0, SEEK_END);
			  size = ftell(a);
			  rewind(a);


			  //allocate a string that can hold it all
			  correctanswers = (char*)malloc(sizeof(char) * (size));
			  //read it all in one operation
			  if (fread(correctanswers, sizeof(char), size, a) <= 0) {
				  perror("Error: fread().\n");
				  exit(1);
			  }
			  

			  for (i = 0; i < 5; i++) {
				  if (*correctanswers == arr[i + 3][0]) {
					  score += 20;
					  correctanswers+=3;
				  }
			  }
			  
			  AQS_bytes = sprintf(send_str, "AQS %s %d\n", QID, score);


			  ptr1 = send_str;
			  nbytes = AQS_bytes; // '\0' is not transmitted

			  /* write() may write a smaller number of bytes than solicited */
			  nleft = nbytes;
			  while (nleft > 0) {
				  send_bytes = write(newfd, ptr1, nleft);
				  if (send_bytes <= 0) exit(1); //error
				  nleft -= send_bytes;
				  ptr1 += send_bytes;
			  }
			  printf("TEST AQS sent : «%s»", send_str);
			  close(newfd);
			  
			  /* TES-ECP Protocol (in UDP) */
				
			  memset((void*)&serveraddr, (int)'\0', sizeof(serveraddr));
			  serveraddr.sin_family = AF_INET;
			 if ((hostptr = gethostbyname(ECPname)) == NULL) exit(1);
				printf("%s", ECPname);
			 serveraddr.sin_addr.s_addr = ((struct in_addr*)(hostptr->h_addr_list[0]))->s_addr; //ECP server IP address
			 printf("%s", hostptr->h_name);
			  serveraddr.sin_port = htons((u_short)ECPport);
			 
			  serverlen = sizeof(serveraddr);

			  /* TES-ECP: IQR SID QID topic_name score
			  ECP-TES: AWI QID */

			  sprintf(send_str, "IQR %s %s %s %d\n", SID, QID, TOPIC_NAME, score);
				printf("%s\n",send_str);

			  send_bytes = sendto(fd_ecp, send_str, strlen(send_str), 0, (struct sockaddr*)&serveraddr, serverlen);
			  if (send_bytes == -1)exit(1);//error
			 
			  alarm(10);//generates the SIGALRM signal when the specified time has expired

			  rcvd_bytes = recvfrom(fd_ecp, recv_str, strlen(recv_str), 0, (struct sockaddr*)&clientaddr, &clientlen);
			  if (rcvd_bytes == -1)exit(1);//error
			 else alarm(0); //cancel currently active alarm

			  char *array[8];
			  char *c = strtok(recv_str, " ");
			  int j = 0;
			  while (c != NULL) {
				  array[j] = c;
				  j++;
				  c = strtok(NULL, " ");
			  }
			  if (strcmp(array[0], "AWI") == 0 && strcmp(array[1], QID) == 0) {
				  close(fd_ecp);
				  exit(0);
			  }
			
			  else {
				  send_bytes = sendto(fd_ecp, "ERR\n", ERR_SIZE, 0, (struct sockaddr*)&serveraddr, serverlen);
				  if (send_bytes == -1)exit(1);//error
				  exit(1);
			  }
		    }
		      
		      else {
			printf("\n----->invalid answers\n");
			send_bytes = write(newfd, "ERR\n", ERR_SIZE);
			if (send_bytes <= 0)exit(1);
				close(newfd); exit(1);
		     }
			
		}
		
		//parent process
		do ret = close(newfd);
		while (ret == -1 && errno == EINTR);
		if (ret == -1)exit(1);//error
	}
}
	/**
	 * \brief add a point
	 */
	void	addPoint(const FocusPoint& point,
			const Ice::Current& /* current */) {
		std::cout << timeformat("%H:%M:%S ", time(NULL));
		std::cout << point.position << ": " << point.value;
		std::cout << std::endl;
	}