//this functions parses the topology information stored in topology.dat //returns the cost of the direct link between the two given nodes //if no direct link between the two given nodes, INFINITE_COST is returned unsigned int topology_getCost(int fromNodeID, int toNodeID) { if(fromNodeID == toNodeID){ return 0; } FILE* fh; fh = fopen("../topology/topology.dat","r"); if(fh == NULL){ fprintf(stderr,"Cannot open topology.dat\n"); exit(-1); } int nodeId1,nodeId2; int i; char hostname1[32],hostname2[32]; int cost; char localname[32]; gethostname(localname,sizeof(localname)); for(i = 0; !feof(fh);i++){ strcpy(hostname1,"vacant line"); strcpy(hostname2,"vacant line"); fscanf(fh,"%s %s %d",hostname1,hostname2,&cost); nodeId1 = topology_getNodeIDfromname(hostname1); nodeId2 = topology_getNodeIDfromname(hostname2); if((nodeId1 == fromNodeID && nodeId2 == toNodeID) || (nodeId1 == toNodeID && nodeId2 == fromNodeID)){ fclose(fh); return cost; } } fclose(fh); return INFINITE_COST; }
//this functions parses the topology information stored in topology.dat //returns the number of neighbors int topology_getNbrNum() { int myNodeID; int nbrNumber = 0; //get my node ID and topology.dat if ((myNodeID = topology_getMyNodeID()) < 0) return -1; char *fileName = "../topology/topology.dat"; FILE *pFile = fopen(fileName, "r"); if (pFile == NULL) { perror("Error getting file in get NbrNum.\n"); return -1; } char buffer[200]; char host1[100]; char host2[200]; int num, h1, h2; while (fgets(buffer, sizeof(buffer), pFile)){ sscanf(buffer, "%s %s %d\n", host1, host2, &num); if ((h1 = topology_getNodeIDfromname(host1)) < 0 || (h2 = topology_getNodeIDfromname(host2)) < 0){ printf("Error. host1 = %s, h1 = %d, host2 = %s, h2 = %d\n", host1, h1, host2, h2); return -1; } if (h1 == myNodeID || h2 == myNodeID) nbrNumber++; memset(buffer, 0, sizeof(char)*200); memset(host1, 0, sizeof(char)*100); memset(host2, 0, sizeof(char)*100); } fclose(pFile); return nbrNumber; }
//这个函数解析保存在文件topology.dat中的拓扑信息. //返回指定两个节点之间的直接链路代价. //如果指定两个节点之间没有直接链路, 返回INFINITE_COST. unsigned int topology_getCost(int fromNodeID, int toNodeID) { if (fromNodeID == toNodeID) { return 0; } FILE *fp; if ((fp = fopen("../topology/topology.dat", "r")) == NULL) { printf("cannot open topology.dat!\n"); exit(0); } int NodeID1, NodeID2; int i; char hostname1[32], hostname2[32]; int cost; char localname[32]; gethostname(localname, sizeof(localname)); for(i = 0; !feof(fp); ++i) { strcpy(hostname1, "vacant line"); strcpy(hostname2, "vacant line");//to prevent the vacant line fscanf(fp, "%s %s %d", hostname1, hostname2, &cost); NodeID1 = topology_getNodeIDfromname(hostname1); NodeID2 = topology_getNodeIDfromname(hostname2); //printf("%s, %s, %d\n", hostname1, hostname2, cost); if ((NodeID1 == fromNodeID && NodeID2 == toNodeID) || (NodeID1 == toNodeID && NodeID2 == fromNodeID)) { fclose(fp); return cost; } } fclose(fp); return INFINITE_COST; }
//这个函数解析保存在文件topology.dat中的拓扑信息. //返回一个动态分配的数组, 它包含所有邻居的节点ID. int* topology_getNbrArray(){ char buf[MAX_LENGTH]; char *p,*q; FILE *file = fopen("../topology/topology.dat", "r"); if(file == NULL){ return -1; } char hostname[MAX_LENGTH]; gethostname(hostname, MAX_LENGTH); int num = topology_getNbrNum(); int *nodeID = (int *)malloc(sizeof(int)*num); memset(nodeID, 0, sizeof(int)*num); int i = 0; while( fgets(buf, MAX_LENGTH, file) ){ p = strtok(buf, "\t\b\n\v\r "); q = strtok(NULL, "\t\b\n\v\r "); if(strcmp(p,hostname)==0) nodeID[i++] = topology_getNodeIDfromname(q); if(strcmp(q,hostname)==0) nodeID[i++] = topology_getNodeIDfromname(p); p = strtok(NULL,"\t\b\n\v\r "); } fclose(file); return nodeID; }
void topology_analysis() { char host1[20], host2[20]; int cost, host1ID, host2ID; in_addr_t host1IP, host2IP; int node[20], nbr[20]; in_addr_t nip[20]; int myNodeID = topology_getMyNodeID(); FILE *pFile = fopen("../topology/topology.dat", "r"); if(pFile == NULL) perror("Error opening topology"); while(fscanf(pFile,"%s %s %d", host1, host2, &cost) > 0){ host1ID = topology_getNodeIDfromname(host1); host1IP = topology_getNodeIPfromname(host1); host2ID = topology_getNodeIDfromname(host2); host2IP = topology_getNodeIPfromname(host2); if(host1ID == myNodeID){ nbr[nbrNum] = host2ID; nip[nbrNum] = host2IP; nbrNum++; if(host2ID < myNodeID) lessNbr++; } else if(host2ID == myNodeID){ nbr[nbrNum] = host1ID; nip[nbrNum] = host1IP; nbrNum++; if(host1ID < myNodeID) lessNbr++; } if(searchInArray(node, host1ID, nodeNum) == -1){ node[nodeNum] = host1ID; nodeNum++; } if(searchInArray(node, host2ID, nodeNum) == -1){ node[nodeNum] = host2ID; nodeNum++; } } int i = 0; nbrIDArray = (int *)malloc(nbrNum * sizeof (int)); for(i = 0; i < nbrNum; i++) nbrIDArray[i] = nbr[i]; nbrIPArray = (in_addr_t *)malloc(nbrNum * sizeof (in_addr_t)); for(i = 0; i < nbrNum; i++) nbrIPArray[i] = nip[i]; nodeArray = (int *)malloc(nodeNum * sizeof (int)); for(i = 0; i < nodeNum; i++) nodeArray[i] = node[i]; fclose(pFile); }
/* * Place the number of neighbours in the area pointered by nbrNum * Return an malloced array of neighbors' nodeID * On error, return NULL */ int *read_Nbr(int *num) { //printf("In read_Nbr\n"); int myId = topology_getMyNodeID(); FILE *f = open_dat(); int nbrNum = 0; int nbrArr[MAX_NODE_NUM]; char left_node[NODE_NAME_LEN], right_node[NODE_NAME_LEN]; int cost = 0; fscanf(f, "%s%s%d", left_node, right_node, &cost); // first entry while (!feof(f)) { int left_nodeId = topology_getNodeIDfromname(left_node); int right_nodeId = topology_getNodeIDfromname(right_node); int nbrId = -1; // check whether there's my neighbor if (left_nodeId == myId) nbrId = right_nodeId; else if (right_nodeId == myId) nbrId = left_nodeId; // check whether the neighbor appeared int i = 0; for(i = 0; i < nbrNum; i ++) { if (nbrArr[i] == nbrId) break; } if(i >= nbrNum && nbrId != -1) { // a new neighbor nbrArr[nbrNum ++] = nbrId; } fscanf(f, "%s%s%d", left_node, right_node, &cost); // next entry or EOF } *num = nbrNum; // malloc an array to places the neiboId int *arr = (int *)malloc(sizeof(int) * nbrNum); if (arr == NULL) { printf("malloc error!\n"); return NULL; } // move the neiboIds int i = 0; for(i = 0; i < nbrNum; i ++) { arr[i] = nbrArr[i]; } fclose(f); return arr; }
/* * Place the number of all nodes in the area pointered by nbrNum * Return an malloced array of all nodes' nodeID * On error, return NULL */ int *read_Nodes(int *num) { FILE *f = open_dat(); int nodesNum = 0; int nodeArr[MAX_NODE_NUM]; char left_node[NODE_NAME_LEN], right_node[NODE_NAME_LEN]; int cost = 0; fscanf(f, "%s%s%d", left_node, right_node, &cost); // first entry while (!feof(f)) { int left_nodeId = topology_getNodeIDfromname(left_node); int right_nodeId = topology_getNodeIDfromname(right_node); // check whether the left node appeared int left_appeared = 0; // false int right_appeared = 0; // false int i = 0; for(i = 0; i < nodesNum; i ++) { if (nodeArr[i] == left_nodeId) left_appeared = 1; // true if (nodeArr[i] == right_nodeId) right_appeared = 1; // true } if (left_appeared == 0) { // a new node nodeArr[nodesNum ++] = left_nodeId; } if (right_appeared == 0) { // a new node nodeArr[nodesNum ++] = right_nodeId; } fscanf(f, "%s%s%d", left_node, right_node, &cost); // next entry } *num = nodesNum; // malloc an array to places the nodeId int *arr = (int *)malloc(sizeof(int) * nodesNum); if (arr == NULL) { printf("malloc error!\n"); return NULL; } // move the nodeIds int i = 0; for(i = 0; i < nodesNum; i ++) { arr[i] = nodeArr[i]; } fclose(f); return arr; }
//这个函数返回本机的节点ID //如果不能获取本机的节点ID, 返回-1. int topology_getMyNodeID() { char name[32]; gethostname(name,sizeof(name)); //printf("hostname:%s \n",name); return topology_getNodeIDfromname(name); }
//returns neighbor names as dynamically allocated char** array entries int getNeighborNames(char **nbrNames, int numNeighbors){ int myNodeID; int nbrNumber = 0; //get my node ID and the # of lines in topology.dat if ((myNodeID = topology_getMyNodeID()) < 0) return -1; char *fileName = "../topology/topology.dat"; FILE *pFile = fopen(fileName, "r"); if (pFile == NULL) { perror("Error getting file in getNeighborNames.\n"); return -1; } char buffer[200]; char host1[100]; char host2[200]; int num, h1, h2; while (fgets(buffer, sizeof(buffer), pFile)){ sscanf(buffer, "%s %s %d\n", host1, host2, &num); if ((h1 = topology_getNodeIDfromname(host1)) < 0 || (h2 = topology_getNodeIDfromname(host2)) < 0){ printf("Error. host1 = %s, h1 = %d, host2 = %s, h2 = %d\n", host1, h1, host2, h2); return -1; } if (h1 == myNodeID ) { //then host2 is neighbor nbrNames[nbrNumber] = malloc(sizeof(char)*100); memset(nbrNames[nbrNumber], 0, sizeof(char)*100); memcpy(nbrNames[nbrNumber], host2, 100); nbrNumber++; } else if (h2 == myNodeID) { //then host1 is neighbor nbrNames[nbrNumber] = malloc(sizeof(char)*100); memset(nbrNames[nbrNumber], 0, sizeof(char)*100); memcpy(nbrNames[nbrNumber], host1, 100); nbrNumber++; } memset(buffer, 0, sizeof(char)*200); memset(host1, 0, sizeof(char)*100); memset(host2, 0, sizeof(char)*100); } fclose(pFile); if (nbrNumber == numNeighbors) return 1; else return -1; }
//this function returns my node ID //if my node ID can't be retrieved, return -1 int topology_getMyNodeID() { char hostname[1024]; hostname[1023] = '\0'; gethostname(hostname, 1023); //printf("My hostname is %s.\n", hostname); return topology_getNodeIDfromname(hostname); }
//返回指定两个节点之间的直接链路代价. //如果指定两个节点之间没有直接链路, 返回INFINITE_COST. unsigned int topology_getCost(int fromNodeID, int toNodeID) { char host1[20], host2[20]; int cost, host1ID, host2ID; FILE *pFile = fopen("../topology/topology.dat", "r"); if(pFile == NULL) perror("Error opening topology"); while(fscanf(pFile,"%s %s %d", host1, host2, &cost) > 0){ host1ID = topology_getNodeIDfromname(host1); host2ID = topology_getNodeIDfromname(host2); if((host1ID == fromNodeID && host2ID == toNodeID)|| (host1ID == toNodeID && host2ID == fromNodeID)) return cost; } fclose(pFile); return INFINITE_COST; }
//这个函数解析保存在文件topology.dat中的拓扑信息. //返回指定两个节点之间的直接链路代价. //如果指定两个节点之间没有直接链路, 返回INFINITE_COST. unsigned int topology_getCost(int fromNodeID, int toNodeID){ FILE *file = fopen("../topology/topology.dat", "r"); if(file == NULL) return -1; char *p,*q; char buf[MAX_LENGTH]; while( fgets(buf, MAX_LENGTH, file) ){ p = strtok(buf, "\t\b\n\v\r "); q = strtok(NULL, "\t\b\n\v\r "); if(topology_getNodeIDfromname(p) == fromNodeID && topology_getNodeIDfromname(q) == toNodeID) { fclose(file); p=strtok(NULL,"\t\b\n\v\r "); return atoi(p); } p = strtok(NULL,"\t\b\n\v\r "); } fclose(file); return INFINITE_COST; }
//这个函数解析保存在文件topology.dat中的拓扑信息. //返回指定两个节点之间的直接链路代价. //如果指定两个节点之间没有直接链路, 返回INFINITE_COST. unsigned int topology_getCost(int fromNodeID, int toNodeID) { int ret_cost = INFINITE_COST; FILE *f = open_dat(); char left_node[NODE_NAME_LEN], right_node[NODE_NAME_LEN]; int cost = 0; fscanf(f, "%s%s%d", left_node, right_node, &cost); // first entry while (!feof(f)) { int left_nodeId = topology_getNodeIDfromname(left_node); int right_nodeId = topology_getNodeIDfromname(right_node); // check if (left_nodeId == fromNodeID && right_nodeId == toNodeID) { ret_cost = cost; break; } fscanf(f, "%s%s%d", left_node, right_node, &cost); // next entry or EOF } fclose(f); return ret_cost; }
//this functions parses the topology information stored in topology.dat //returns the cost of the direct link between the two given nodes //if no direct link between the two given nodes, INFINITE_COST is returned unsigned int topology_getCost(int fromNodeID, int toNodeID) { if (fromNodeID == toNodeID) { return 0; } //get topology.dat char *fileName = "../topology/topology.dat"; FILE *pFile = fopen(fileName, "r"); if (pFile == NULL) { perror("Error getting file in getCost.\n"); return -1; } char buffer[200]; char host1[100]; char host2[200]; int h1, h2; unsigned int num; while (fgets(buffer, sizeof(buffer), pFile)){ sscanf(buffer, "%s %s %u\n", host1, host2, &num); if ((h1 = topology_getNodeIDfromname(host1)) < 0 || (h2 = topology_getNodeIDfromname(host2)) < 0){ printf("Error. host1 = %s, h1 = %d, host2 = %s, h2 = %d\n", host1, h1, host2, h2); return -1; } if ((h1 == fromNodeID && h2 == toNodeID) || (h2 == fromNodeID && h1 == toNodeID)) { //printf("Cost between %d and %d is %u.\n", h1, h2, num); fclose(pFile); return num; } memset(buffer, 0, sizeof(char)*200); memset(host1, 0, sizeof(char)*100); memset(host2, 0, sizeof(char)*100); } fclose(pFile); return INFINITE_COST; }
//This function first creates a neighbor table dynamically. It then parses the topology/topology.dat file // and fill the nodeID and nodeIP fields in all the entries, initialize conn field as -1 . //return the created neighbor table nbr_entry_t* nt_create() { //get number of neighbors int nbrNumber; if ((nbrNumber = topology_getNbrNum()) < 0){ perror("Couldn't get neighbor number.\n"); return NULL; } //malloc neighbor table nbr_entry_t *nbrTable = malloc(sizeof(nbr_entry_t)*nbrNumber); memset(nbrTable, 0, sizeof(nbr_entry_t)*nbrNumber); //get names of neighbors char **nbrNames = malloc(sizeof(char *)* nbrNumber); memset(nbrNames, 0, sizeof(char *)*nbrNumber); if (getNeighborNames(nbrNames, nbrNumber) < 0) { printf("Error getting neighbor names.\n"); return NULL; } //populate neighbor table for (int i = 0; i < nbrNumber; i++){ //get node ID from hostname if ((nbrTable[i].nodeID = topology_getNodeIDfromname(nbrNames[i])) < 0) { perror("Couldn't get nodeID from name whilst populating neighbor table.\n"); return NULL; } //get IP from hostname struct hostent *he; if ((he = gethostbyname(nbrNames[i])) == NULL) { perror("Couldn't get IP from name whilst populating neighbor table.\n"); return NULL; } nbrTable[i].nodeIP = *(in_addr_t *)he->h_addr_list[0]; //conn initialized as -1 nbrTable[i].conn = -1; free(nbrNames[i]); } free(nbrNames); return nbrTable; }
//this functions parses the topology information stored in topology.dat //returns a dynamically allocated array which contains all the neighbors'IDs int* topology_getNbrArray() { int myNodeID; int nbrNumber = 0; //get my node ID and the # of lines in topology.dat if ((myNodeID = topology_getMyNodeID()) < 0) { printf("Couldn't get my node ID in getNbrArray.\n"); return NULL; } int numNodes; if ((numNodes = topology_getNbrNum()) <= 0) { printf("Error getting total number of neighbors from getNbrArray.\n"); return NULL; } int *nodeIDs = malloc(sizeof(int)*numNodes); memset(nodeIDs, 0, sizeof(int)* numNodes); int i; for (i = 0; i < numNodes; i++) { nodeIDs[i] = -1; } //get topology.dat char *fileName = "../topology/topology.dat"; FILE *pFile = fopen(fileName, "r"); if (pFile == NULL) { perror("Error getting file in getNbrArray.\n"); return NULL; } char buffer[200]; char host1[100]; char host2[200]; int h1, h2; unsigned int num; while (fgets(buffer, sizeof(buffer), pFile)){ sscanf(buffer, "%s %s %u\n", host1, host2, &num); if ((h1 = topology_getNodeIDfromname(host1)) < 0 || (h2 = topology_getNodeIDfromname(host2)) < 0){ printf("Error. host1 = %s, h1 = %d, host2 = %s, h2 = %d\n", host1, h1, host2, h2); return NULL; } if (h1 == myNodeID) { nodeIDs[nbrNumber] = h2; nbrNumber++; } if (h2 == myNodeID) { nodeIDs[nbrNumber] = h1; nbrNumber++; } memset(buffer, 0, sizeof(char)*200); memset(host1, 0, sizeof(char)*100); memset(host2, 0, sizeof(char)*100); } /*printf("Returning neighbor node array with nodes: "); for (i = 0; i < numNodes; i++) { printf("%d ", nodeIDs[i]); } printf("\n");*/ fclose(pFile); return nodeIDs; }
//this functions parses the topology information stored in topology.dat //returns a dynamically allocated array which contains all the nodes' IDs in the overlay network int* topology_getNodeArray() { int numNodes; if ((numNodes = topology_getNodeNum()) <= 0) { printf("Error getting total number of nodes from getNodeArray.\n"); return NULL; } int *nodeIDs = malloc(sizeof(int)*numNodes); memset(nodeIDs, 0, sizeof(int)* numNodes); int i; for (i = 0; i < numNodes; i++) { nodeIDs[i] = -1; } //get topology.dat char *fileName = "../topology/topology.dat"; FILE *pFile = fopen(fileName, "r"); if (pFile == NULL) { perror("Error getting file in getNodeArray.\n"); return NULL; } char buffer[200]; char host1[100]; char host2[200]; int h1, h2; unsigned int num; while (fgets(buffer, sizeof(buffer), pFile)){ sscanf(buffer, "%s %s %u\n", host1, host2, &num); if ((h1 = topology_getNodeIDfromname(host1)) < 0 || (h2 = topology_getNodeIDfromname(host2)) < 0){ printf("Error. host1 = %s, h1 = %d, host2 = %s, h2 = %d\n", host1, h1, host2, h2); return NULL; } int j = 0; while (nodeIDs[j] != -1) { if (h1 == nodeIDs[j]) { j = -1; break; } j++; } if (j != -1 && j < MAX_NODE_NUM) { nodeIDs[j] = h1; } j = 0; while (nodeIDs[j] != -1) { if (h2 == nodeIDs[j]) { j = -1; break; } j++; } if (j != -1 && j < MAX_NODE_NUM) { nodeIDs[j] = h2; } memset(buffer, 0, sizeof(char)*200); memset(host1, 0, sizeof(char)*100); memset(host2, 0, sizeof(char)*100); } //printf("Topology.c returning node array with nodes: "); //for (i = 0; i < numNodes; i++) { // printf("%d ", nodeIDs[i]); //} //printf("\n"); fclose(pFile); return nodeIDs; }
//this functions parses the topology information stored in topology.dat //returns the number of total nodes in the overlay int topology_getNodeNum() { int nodeIDs[MAX_NODE_NUM]; memset(nodeIDs, 0, sizeof(int)* MAX_NODE_NUM); for (int i = 0; i < MAX_NODE_NUM; i++) { nodeIDs[i] = -1; } //get topology.dat char *fileName = "../topology/topology.dat"; FILE *pFile = fopen(fileName, "r"); if (pFile == NULL) { perror("Error getting file in getNodeNum.\n"); return -1; } char buffer[200]; char host1[100]; char host2[200]; int h1, h2; unsigned int num; while (fgets(buffer, sizeof(buffer), pFile)){ sscanf(buffer, "%s %s %u\n", host1, host2, &num); if ((h1 = topology_getNodeIDfromname(host1)) < 0 || (h2 = topology_getNodeIDfromname(host2)) < 0){ printf("Error. host1 = %s, h1 = %d, host2 = %s, h2 = %d\n", host1, h1, host2, h2); return -1; } int j = 0; while (nodeIDs[j] != -1) { if (h1 == nodeIDs[j]) { j = -1; break; } j++; } if (j != -1 && j < MAX_NODE_NUM) { nodeIDs[j] = h1; } j = 0; while (nodeIDs[j] != -1) { if (h2 == nodeIDs[j]) { j = -1; break; } j++; } if (j != -1 && j < MAX_NODE_NUM) { nodeIDs[j] = h2; } memset(buffer, 0, sizeof(char)*200); memset(host1, 0, sizeof(char)*100); memset(host2, 0, sizeof(char)*100); } int count = 0; while (nodeIDs[count] != -1) { count++; } fclose(pFile); return count; }
int main() { //用于丢包率的随机数种子 srand(time(NULL)); //连接到SIP进程并获得TCP套接字描述符 int sip_conn = connectToSIP(); if(sip_conn<0) { printf("can not connect to the local SIP process\n"); } //初始化STCP服务器 stcp_server_init(sip_conn); //启动seghandler线程 pthread_t seghandle_thread; int rc; rc = pthread_create(&seghandle_thread, NULL, seghandler, &sip_conn); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } //在端口SERVERPORT1上创建STCP服务器套接字 int sockfd= stcp_server_sock(SERVERPORT1); if(sockfd<0) { printf("can't create stcp server\n"); exit(1); } //监听并接受来自STCP客户端的连接 stcp_server_accept(sockfd); char hostname[50]; printf("Enter server name to connect:"); scanf("%s",hostname); int server_nodeID = topology_getNodeIDfromname(hostname); if(server_nodeID == -1) { printf("host name error!\n"); exit(1); } else { printf("connecting to node %d\n",server_nodeID); } //在端口87上创建STCP客户端套接字, 并连接到STCP服务器端口88 int sockfd2 = stcp_client_sock(CLIENTPORT1); if(sockfd2<0) { printf("fail to create stcp client sock"); exit(1); } if(stcp_client_connect(sockfd2,server_nodeID,SERVERPORT1)<0) { printf("fail to connect to stcp server\n"); exit(1); } char buf1[6]; //接收来自第一个连接的字符串 int i; for(i=0;i<5;i++) { stcp_server_recv(sockfd,buf1,6); printf("recv string: %s from connection 1\n",buf1); } //通过第二个连接发送字符串 char mydata2[7] = "byebye"; for(i=0;i<5;i++){ stcp_client_send(sockfd2, mydata2, 7); printf("send string:%s to connection 2\n",mydata2); } sleep(WAITTIME); //关闭STCP服务器 if(stcp_server_close(sockfd)<0) { printf("can't destroy stcp server\n"); exit(1); } sleep(WAITTIME); //关闭连接 if(stcp_client_disconnect(sockfd2)<0) { printf("fail to disconnect from stcp server\n"); exit(1); } //断开与SIP进程之间的连接 disconnectToSIP(sip_conn); }
//这个函数返回本机的节点ID //如果不能获取本机的节点ID, 返回-1. int topology_getMyNodeID(){ char hostname[MAX_LENGTH]; gethostname(hostname, MAX_LENGTH); return topology_getNodeIDfromname(hostname); }
int main(int argc, char **argv) { //用于丢包率的随机数种子 srand(time(NULL)); //连接到SIP进程并获得TCP套接字描述符 int sip_conn = connectToSIP(); if(sip_conn<0) { printf("fail to connect to the local SIP process\n"); exit(1); } //初始化stcp客户端 stcp_client_init(sip_conn); sleep(STARTDELAY); char hostname[50]; printf("Enter server name to connect:"); scanf("%s",hostname); int server_nodeID = topology_getNodeIDfromname(hostname); if(server_nodeID == -1) { printf("host name error!\n"); exit(1); } else { printf("connecting to node %d\n",server_nodeID); } int number; printf("Enter client number to connect:"); scanf("%d",&number); if(number ==1 ){ sockfd = stcp_client_sock(CLIENTPORT1); if(sockfd<0) { printf("fail to create stcp client sock"); exit(1); } if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT1)<0) { printf("fail to connect to stcp server\n"); exit(1); } printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT1,SERVERPORT1); } else if(number ==2 ){ sockfd = stcp_client_sock(CLIENTPORT2); if(sockfd<0) { printf("fail to create stcp client sock"); exit(1); } if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT2)<0) { printf("fail to connect to stcp server\n"); exit(1); } printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT2,SERVERPORT2); } else if(number ==3 ){ sockfd = stcp_client_sock(CLIENTPORT3); if(sockfd<0) { printf("fail to create stcp client sock"); exit(1); } if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT3)<0) { printf("fail to connect to stcp server\n"); exit(1); } printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT3,SERVERPORT3); } else { printf("client number error!\n"); exit(1); } pthread_t msgthd,mainthd; pthread_attr_t attr; pthread_mutex_init(&wait_mutex,NULL); pthread_cond_init(&wait_cond,NULL); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); memset(&send_msg,0,YY_MSG_HEADER_LENGTH+1024); memset(&rec_msg,0,YY_MSG_HEADER_LENGTH+1024); send_msg.protocol_name[0]='Y'; send_msg.protocol_name[1]='Y'; send_msg.protocol_name[2]='M'; send_msg.protocol_name[3]='G'; bool login=false; system("clear"); printf("Welcome to NJUCS Instant Messenger Demo Program!\nPlease input Your user name to login(The name's length shouled be less than 20 letters;The name only contains letters and digits!)\n(c)cls,(#)exit\n"); send_msg.service = 0X01; while(!login) { //main menu char a; scanf("%c",&a); gets(buffer); if(strcmp(buffer,"#")==0) break; else if(strcmp(buffer,"c")==0) { system("clear"); printf("Welcome to NJUCS Instant Messenger Demo Program!\nPlease input Your user name to login(The name's length shouled be less than 20 letters;The name only contains letters and digits!)\n(c)cls,(#)exit\n"); continue; } else if(strlen(buffer)>20) { printf("The inputed name is too long!input again:\n"); continue; } else { bool illegal=false; int i=0; for(i;i<strlen(buffer);i++) { if(!(('a'<=buffer[i]&& 'z'>=buffer[i]) ||('A'<=buffer[i] && 'Z' >=buffer[i])||('0'<=buffer[i] && '9'>=buffer[i])) ) { printf("The input is illegal!input again:\n"); illegal=true; break; } } if(illegal) continue; } strcpy(send_msg.send_usr,buffer); /*send the message to log in!*/ int len = YY_MSG_HEADER_LENGTH; stcp_client_send(sockfd,&len,sizeof(int)); stcp_client_send(sockfd,&send_msg,len); stcp_client_recv(sockfd,&len,sizeof(int)); stcp_client_recv(sockfd,&rec_msg,len); switch(rec_msg.service) { case 0x11:/*the user is aleady exist*/ printf("Sorry, The user %s already exists!input another:\n",rec_msg.send_usr);break; case 0x12:/*log in success!*/ strncpy(usrname,buffer,20); if(pthread_create(&mainthd, &attr, handle_main, NULL )!= 0) { perror("Thread creation problem"); exit(-1); } if(pthread_create(&msgthd, &attr, handle_msg, NULL)!= 0) { perror("Thread creation problem"); exit(-1); } pthread_join(mainthd,NULL); pthread_join(msgthd,NULL); login=true; break; default: printf("message error!error code:0x%02x",rec_msg.service);break; } } pthread_attr_destroy(&attr); pthread_mutex_destroy(&wait_mutex); pthread_cond_destroy(&wait_cond); pthread_exit(NULL); disconnectToSIP(sip_conn); }
//read topology.dat void getTopoData() { FILE *pFile; pFile = fopen("../topology/topology.dat","r"); if(pFile == NULL) { printf("open file error\n"); exit(-1); } char buf[128]; int lineCount=0; while (fgets(buf,sizeof(buf),pFile)!= NULL) { lineCount++; } if(lineCount<=0) { printf("no topology in file\n"); exit(-1); } //init topo data list allIdList=malloc(lineCount*2*sizeof(int)); nbIdList=malloc(lineCount*sizeof(int)); nbIpList=malloc(lineCount*sizeof(in_addr_t)); costList=malloc(lineCount*sizeof(int)); //get data from file char host1[32],host2[32]; int cost; int myId=topology_getMyNodeID(); nbCount=0; fseek(pFile,0,SEEK_SET); while(fscanf(pFile,"%s %s %d", host1, host2, &cost) > 0) { int id1=topology_getNodeIDfromname(host1); int id2=topology_getNodeIDfromname(host2); int i=0; for(;i<allCount;i++) { if(allIdList[i]==id1)break; } if(i==allCount)allIdList[allCount++]=id1; i=0; for(;i<allCount;i++) { if(allIdList[i]==id2)break; } if(i==allCount)allIdList[allCount++]=id2; //neighbor if(id1==myId) { nbIdList[nbCount]=id2; nbIpList[nbCount]=topology_getNodeIPfromname(host2); costList[nbCount]=cost; if(id2>id1)bigCount++; else if(id1>id2)smallCount++; nbCount++; } else if(id2==myId) { nbIdList[nbCount]=id1; nbIpList[nbCount]=topology_getNodeIPfromname(host1); costList[nbCount]=cost; if(id2>id1)smallCount++; else if(id1>id2)bigCount++; nbCount++; } } }
//这个函数返回本机的节点ID //如果不能获取本机的节点ID, 返回-1. int topology_getMyNodeID() { char localhostname[30]; gethostname(localhostname, 30); return topology_getNodeIDfromname(localhostname); }
int main() { //用于丢包率的随机数种子 srand(time(NULL)); //连接到SIP进程并获得TCP套接字描述符 int sip_conn = connectToSIP(); if(sip_conn<0) { printf("fail to connect to the local SIP process\n"); exit(1); } //初始化stcp客户端 stcp_client_init(sip_conn); sleep(STARTDELAY); char hostname[50]; printf("Enter server name to connect:"); scanf("%s",hostname); int server_nodeID = topology_getNodeIDfromname(hostname); if(server_nodeID == -1) { printf("host name error!\n"); exit(1); } else { printf("connecting to node %d\n",server_nodeID); } //在端口87上创建STCP客户端套接字, 并连接到STCP服务器端口88. int sockfd = stcp_client_sock(CLIENTPORT1); if(sockfd<0) { printf("fail to create stcp client sock"); exit(1); } if(stcp_client_connect(sockfd,server_nodeID,SERVERPORT1)<0) { printf("fail to connect to stcp server\n"); exit(1); } printf("client connected to server, client port:%d, server port %d\n",CLIENTPORT1,SERVERPORT1); //获取sendthis.txt文件长度, 创建缓冲区并读取文件中的数据 FILE *f; f = fopen("sendthis.txt","r"); assert(f!=NULL); fseek(f,0,SEEK_END); int fileLen = ftell(f); fseek(f,0,SEEK_SET); char *buffer = (char*)malloc(fileLen); fread(buffer,fileLen,1,f); fclose(f); //首先发送文件长度, 然后发送整个文件. stcp_client_send(sockfd,&fileLen,sizeof(int)); stcp_client_send(sockfd, buffer, fileLen); free(buffer); //等待一段时间, 然后关闭连接. sleep(WAITTIME); if(stcp_client_disconnect(sockfd)<0) { printf("fail to disconnect from stcp server\n"); exit(1); } if(stcp_client_close(sockfd)<0) { printf("fail to close stcp client\n"); exit(1); } //断开与SIP进程之间的连接 disconnectToSIP(sip_conn); }
//这个函数首先动态创建一个邻居表. 然后解析文件topology/topology.dat, 填充所有条目中的nodeID和nodeIP字段, 将conn字段初始化为-1. //返回创建的邻居表. nbr_entry_t* nt_create() { //printf("-------------nt_create------------\n"); nbr_entry_t *result = (nbr_entry_t *)malloc(sizeof(nbr_entry_t) * 3); int myNode = topology_getMyNodeID(); FILE *fp; fp = fopen("/home/b101220023/lab13/topology/topology.dat", "r"); if (fp == NULL) { printf("Cann't open file topology.dat\n"); return 0; } char buffer[100]; int count = 0; while (fgets(buffer, 99, fp) != NULL) { //printf("count is %d\n",count); char *node1 = (char *)malloc(11); char *node2 = (char *)malloc(11); memcpy(node1, buffer, 10); memcpy(node2, buffer + 11, 10); node1[10] = 0; node2[10] = 0; //printf("node1 is %s, node2 is %s\n", node1, node2); int node1ID = topology_getNodeIDfromname(node1); int node2ID = topology_getNodeIDfromname(node2); //printf("node1Id is %d, node2ID is %d\n", node1ID, node2ID); if (node1ID == myNode ) // 添加邻居节点信息 { result[count].nodeID = node2ID; if (node2ID == 185) { result[count].nodeIP = inet_addr("114.212.190.185"); } else if (node2ID == 186) { result[count].nodeIP = inet_addr("114.212.190.186"); } else if (node2ID == 187) { result[count].nodeIP = inet_addr("114.212.190.187"); } else if (node2ID == 188) { result[count].nodeIP = inet_addr("114.212.190.188"); } result[count].conn = -1; count ++; } else if (node2ID == myNode) { result[count].nodeID = node1ID; if (node1ID == 185) { result[count].nodeIP = inet_addr("114.212.190.185"); } else if (node1ID == 186) { result[count].nodeIP = inet_addr("114.212.190.186"); } else if (node1ID == 187) { result[count].nodeIP = inet_addr("114.212.190.187"); } else if (node1ID == 188) { result[count].nodeIP = inet_addr("114.212.190.188"); } result[count].conn = -1; count ++; } } fclose(fp); //printf("count is %d\n", count); return result; }