#include #include #include #include #include #include #include #include #include #include "tools.h" char* sID[]={"peer1","peer2","peer3","peer4","provider1","provider2"}; char PeerIDchecked[4];//check how many peers are already registered char ProviderIDchecked[2];//check how many providers are already checked. #define MYPORT 3081 // the port users will be connecting to #define MAXBUFLEN 100 /********************************************* void CheckPeerID(char* buf) func: check the peer id. para: buf, the whole message received from peers. *********************************************/ void CheckPeerID(char* buf) { int i; char tmpStr[8]; getStrSegment(buf, tmpStr,1);// get the peer name for(i=0;i<4;i++){ if(strcmp(sID[i],tmpStr) == 0){ PeerIDchecked[i] = 0x1; printf("Peer%d registration is done successfully!\n",i); } } } /******************************************** int PEERs_All_Registered(void) func: check if all peers are registered. return: if all peers are registered, return 1, else return 0 *********************************************/ int PEERs_All_Registered(void) { int i; for(i=0;i<4;i++){ if(PeerIDchecked[i] == '\0') return 0; } printf("Registration of peers completed!Run the content_providers!\n"); return 1; } /******************************************** void CheckProviderID(char* buf) func: check the provider id. para: buf, the whole message received from providers. *********************************************/ void CheckProviderID(char* buf) { int i,j; char tmpStr[8]; getStrSegment(buf, tmpStr,1);//get the provider's name for(i=4;i<6;i++){ if(strcmp(sID[i],tmpStr) == 0){ printf("incoming message from content_provider%d\n",i-3); printf("Content_provider%d ",i-3); if(ProviderLoginProcedure(buf)){ printf("logged in successfully!\n"); ProviderIDchecked[i-4] = 0x1; } else printf("logged in unsuccessfully!\n"); } } } /******************************************** int ProviderLoginProcedure(char* buf) func: check the username and password of the provider para: buf, the whole message received from providers. return: if authentication is sueccess return 1, else return 0 ********************************************/ int ProviderLoginProcedure(char* buf) { int i; char tmpStr[8]; char tmpStr2[9]; getStrSegment(buf, tmpStr,2);// get the provider's username getStrSegment(buf, tmpStr2,3);// get the password if(strcmp("provider",tmpStr) == 0){// check if they are the same if(strcmp("spring07",tmpStr2) == 0){ return 1; } } return 0; } int Providers_All_Registered(void) { int i; for(i=0;i<2;i++){ if(ProviderIDchecked[i] == '\0') return 0; } printf("Registration of providers completed\n"); return 1; } void init(void) { memset(PeerIDchecked, '\0',4); memset(ProviderIDchecked, '\0',2); } int main(void) { int sockfd; int provoderAccepted = 0; struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information struct sockaddr_in provider_addr[2]; // connector's address information struct sockaddr_in Peer_TCP_addr; // connector's address information socklen_t addr_len; int numbytes; char buf[MAXBUFLEN]; unsigned short peer_PortNumber; FILE *fp; int i; init(); /*Create file "directory.txt"*/ fp = fopen("directory.txt", "w"); /*Open a UDP socket */ if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = INADDR_ANY; //using local host IP address memset(&(my_addr.sin_zero), '\0', 8); /*binds the UDP socket*/ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } addr_len = sizeof(struct sockaddr); /*waiting for all peers to register*/ while(!PEERs_All_Registered()){ if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } buf[numbytes] = '\0'; /*Writing peer info to "directory.txt"*/ fprintf(fp,"%s\n",buf); CheckPeerID( buf); } fclose(fp); /*waiting for all peers to register*/ while(!Providers_All_Registered()){ if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } /*store the provider information*/ memcpy(&provider_addr[provoderAccepted],&their_addr,sizeof(struct sockaddr_in)); provoderAccepted++; buf[numbytes] = '\0'; CheckProviderID( buf); } /*send the file name to all providers*/ for(i=0;i<2;i++){ if ((numbytes = sendto(sockfd, "bootstrap_server directory.txt", strlen("bootstrap_server directory.txt"), 0, (struct sockaddr *)&provider_addr[i], sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } //*get message from provider, the following code is used to contol the order of providers if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } } close(sockfd); return 0; }