bool run(struct grid *grid)
{
  char entree[50];
  char **tmp;
  char *ent;
  int i;

  print_game(grid);
  my_memreset(entree, 50);
  ent = readLine();
  if (my_strcmp(my_strcpy(entree, ent), "") != 0) {
    free(ent);
    tmp = my_str_to_wordtab(entree);
    if (choise(grid, tmp) == true)
      return (true);
    for (i = 0; tmp[i][0] != '\0'; i++)
        free(tmp[i]);
    free(tmp);
  }
  else
    my_putstr("Wrong command \n");
  return (is_won(grid));
}
void Lieutenant::mainLoop(){
	fd_set fdset;
  	int timetic = -1;
  	int timeticround = 40;
    while (round <= f+1) {    /*need to continue to next round f*/  
	//cout << "Iterate" << endl;
	struct timeval timeout={1,0}; 
	FD_ZERO(&fdset);
	FD_SET(msg.sockfd, &fdset);

	select(msg.sockfd+1, &fdset, NULL, NULL, &timeout);
	if (FD_ISSET(msg.sockfd, &fdset)) {   
	   // cout << "receive message" << endl;
	    void *p = NULL;
	    int type = msg.recvMessage(p);
	    /*insert the messages received in this round(discard messages not for this round), 
				must be a way to sort the messages */
		/*Send ACK*/
	    if(type == BYZANTINE){
	    	ByzantineMessage* rc_byzmsg = (ByzantineMessage*)p;
	    	
				
			cout << rc_byzmsg->round << "my round is " << round << endl;
			if(rc_byzmsg->round == round){
				int nu_ids = (rc_byzmsg->size - sizeof(ByzantineMessage)) / sizeof(uint32_t);
				cout << "received a byzantine msg in main loop from " << rc_byzmsg->ids[nu_ids - 1] <<endl; 
				//cout << "received a byzantine msg in main loop from " << byznode->ids[byznode->nu_ids-1] <<endl; 
				if(!set.ContainV(rc_byzmsg)){
					ByztMsgNode* byznode = set.Addv(rc_byzmsg);
					cout << "set is: "<< endl;
					set.printV(); 
				}
				makeAck(rc_byzmsg->round);
				cout << "preparing send ack" << endl;
				//cout << "num of ids " << byznode->nu_ids << endl;
				//cout << "receiving from node "<<  << endl;
				msg.sendMessage(ACK, (void*)ack, rc_byzmsg->ids[nu_ids-1]);
				cout << "ack sent" << endl;
				// can compare the ip to see if are equal
				
  		   		
  		   	}
	
		}
		else if(type == ACK){
			Ack* rc_ack = (Ack*)p;
			//int round = ntohl(rc_ack->round);
			cout << "received a ack in main loop in " << round << " round" << endl;
			if(rc_ack->size == sizeof(Ack) && rc_ack->round == round){
				//set_bymsg(round);
				/*tell send this one has be received*/
				cout << "the received ip is "<< msg.s << endl;
				// to do   int recvid = retid(msg.s);
				// cout << 
				int recvid = msg.getidbyIp(msg.s);
				if(recvid == -1){ cout << "id error \n" << endl; }
				ByztMsgNode * byztnode = set.Getv();
				for(int i = 0; i < byztnode->needsendcount; ++i){
					if(recvid == byztnode->sendlist[i].id){
						byztnode->sendlist[i].issent = true;
						break;
					}
				}
			}
		}
		else{
			cout << "receiving error"<< endl;
		}
	    
	}
	timetic = (timetic + 1) % 3;
	/*forword (to all others who should receive) messages in the last round received. if not contine*/
	if(timetic == 0){
		cout << "in round " << round << endl;
		ByztMsgNode * byztnode = set.Getv();
		if(byztnode && byztnode->round == round - 1){
			cout << "in round " << round  <<  " gonna send"<< endl;
			int sendtally = 0;
			for(int i = 0; i < byztnode->needsendcount; ++i){
				if(byztnode->sendlist[i].issent == false){
					sendtally++;
					makeByzantineMessage(byztnode,round, myid);
					msg.sendMessage(BYZANTINE, (void*)byzmsg, byztnode->sendlist[i].id); 
					cout << "byzmsg send to " << byztnode->sendlist[i].id << endl;  
				}
			}
			if(sendtally == 0 && timeticround < 0){
				cout << "set tag to false" << endl;
				byztnode->tag = false;
				round++;
				timetic = -1;
				timeticround = 18; 
				continue;
			}
		}
	}
	timeticround--;
	if(timeticround < 0){
		ByztMsgNode *byztnode = set.Getv();
		if(byztnode && byztnode->round != round){
			cout << "set tag to false" << endl;
			byztnode->tag = false;
		}	
		round++;
		timetic = -1;
		timeticround = 18;	
	}
		
	/*all messages in the last round had be succedly forwarded and
		 all messages should be received in this round has received enter next round*/
   }
	choise();
	// cout << "final table is: "<< endl;
	//printmsgtable();
	//cout << "done in round " << round << endl;
}
Exemple #3
0
int contact()
{
main://system("cls");    /* ************Main menu ***********************  */
    printf("\n\t **** Welcome to a0 contact Manager ****");
    printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new   Contact\n\t\t[2] List all Contacts\n\t\t[3] Search for contact\n\t\t[4] Edit  a Contact\n\t\t[5] Delete a Contact\n\t\t[0] Exit\n\t\t=================\n\t\t");
    printf("Enter the choice:");
    scanf("%d",&ch);
    switch(ch)
    {
    case 0:
        printf("\n\n\t\tAre you sure u want to exit?");
        break;
    /* *********************add new contacts************  */
    case 1: //system("cls");
        fp=fopen("contact.dll","a");
        for (;;)
        {
            fflush(stdin);
            printf("To exit type exit/EXIt  NAME:");
            scanf("%s",&list.name);
            if(strcmp(list.name,"exit") == 0 || strcmp(list.name,"EXIT")==0)
            {
                break;
            }
            //if(strcmp (list.name , o) == 0)
            //break;
            fflush(stdin);
            printf("Phone:");
            scanf("%ld",&list.ph);
            fflush(stdin);
            printf("address:");
            scanf("%s",&list.add);
            fflush(stdin);
            printf("email address:");
            scanf("%s",&list.email);
            gets(list.email);
            fflush(stdin);
            printf("\n");
            fwrite(&list,sizeof(list),1,fp);
        }
        fclose(fp);
        break;
    /* *********************list of contacts*************************  */
    case 2://system("cls");
        printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t==== ============================\n\nName\t\tPhone No\t    Address\t\tE-mail ad.\n=== ==============================================================\n\n");
        for(i=97; i<=122; i=i+1)
        {
            fp=fopen("contact.dll","r");
            fflush(stdin);
            found=0;
            while(fread(&list,sizeof(list),1,fp)==1)
            {
                if(list.name[0]==i || list.name[0]==i-32)
                {
                    printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t\n",list.name,list.ph,list.add,list.email);
                    found++;
                }
            }
            if(found!=0)
            {
                printf("=========================================================== [%c]- (%d)\n\n",i-32,found);
                //getch();
            }
            fclose(fp);
        }
        break;
    /* *******************search contacts**********************  */
    case 3://system("cls");
        do
        {
            found=0;
            printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name of  contact to search:");
            fflush(stdin);
            scanf("%s",&query);
            l=strlen(query);
            fp=fopen("contact.dll","r");
            //system("cls");
            printf("\n\n..::Search result for '%s' \n====================================== ==========\n",query);
            while(fread(&list,sizeof(list),1,fp)==1)
            {
                for(i=0; i<=l; i++)
                    name[i]=list.name[i];
                name[l]='\0';
                if(strcmp(name,query)==0)
                {
                    printf("\n..::Name\t: %s\n..::Phone\t: %ld\n..::Address\t: %s\n..::Email address: %s \n",list.name,list.ph,list.add,list.email);
                    found++;
                    if (found%4==0)
                    {
                        printf("..::Press any key to continue...");
                        //getch();
                    }
                }
            }
            if(found==0)
                printf("\n..::No match found!");
            else
                printf("\n..::%d match(s) found!",found);
            fclose(fp);
            printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t");
            scanf("%d",&ch);
        } while(ch==1);
        break;
    /* *********************edit contacts************************/
    case 4://system("cls");
        fp=fopen("contact.dll","r");
        ft=fopen("temp.dat","w");
        fflush(stdin);
        printf("..::Edit contact\n===============================\n\n\t..::Enter the  name of contactedit:");
        scanf("%s",name);
        while(fread(&list,sizeof(list),1,fp)==1) {
            if(strcmp(name,list.name)!=0)
                fwrite(&list,sizeof(list),1,ft);
        }
        fflush(stdin);
        printf("\n\n..::Editing '%s'\n\n",name);
        printf("..::Name(Use identical):");
        scanf("%s",&list.name);
        fflush(stdin);
        printf("..::Phone:");
        scanf("%ld",&list.ph);
        fflush(stdin);
        printf("..::address:");
        scanf("%s",&list.add);
        fflush(stdin);
        printf("..::email address:");
        scanf("%s",&list.email);
        gets(list.email);
        printf("\n");
        fwrite(&list,sizeof(list),1,ft);
        fclose(fp);
        fclose(ft);
        remove("contact.dll");
        rename("temp.dat","contact.dll");
        break;
    /* ********************delete contacts**********************/
    case 5:/*system("cls");*/
        fflush(stdin);
        printf("\n\n\t..::DELETE A CONTACT\n\t==========================\n\t..::Enter  the name of contact to delete:");
        scanf("%s",&name);
        fp=fopen("contact.dll","r");
        ft=fopen("temp.dat","w");
        while(fread(&list,sizeof(list),1,fp)!=0)
            if (strcmp(name,list.name)!=0)
                fwrite(&list,sizeof(list),1,ft);
        fclose(fp);
        fclose(ft);
        remove("contact.dll");
        rename("temp.dat","contact.dll");
        break;
    default:
        printf("Invalid choice");
        break;
    }
    printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n");
    scanf("%d",&ch);
    switch (ch) {
    case 1:
        goto main;
    case 0:
        choise();
    default:
        printf("Invalid choice");
        break;
    }
    return 0;
}