void fnPackets() {

	cse4589_print_and_log("%d\n", packetCounter);
	packetCounter = 0;

//	return 1;
}
void fnDisplay() {
	/*
	 * Declaration of variables Start
	 */
	int i;
	/*
	 * Declaration of variables End
	 */
	for (i = 1; i <= numberOfServers; i++) {
		cse4589_print_and_log("%-15d%-15d%-15d\n", routingDetails[i].toServerID,
				routingDetails[i].fromServerID, routingDetails[i].totalCost);
	}

//	return 1;
}
Exemplo n.º 3
0
void Console::print(std::string str) {
    if (chat_curs_y >= LINES - CMD_WINDOW_HEIGHT) {
        chat_curs_y = CHAT_WINDOW_STARTY;
        clearchat();
    }
    mvwprintw(chat_window, chat_curs_y, CHAT_WINDOW_STARTX,
              str.c_str()); // Print string to chat window
    wrefresh(chat_window);  // Refresh chat window
    cse4589_print_and_log(str.c_str());
    int nlines = std::count(str.begin(), str.end(), '\n');
    if (nlines == 0) {
        chat_curs_y += 1;
    } else {
        chat_curs_y += nlines;
    }
}
Exemplo n.º 4
0
/**
 * Determines the command to be executed by parsing the user input
 * @param String entered by the user
 */
void comm_process(char *comm_line){

    char *comm_args[257];                    //Stores tokenized arguments of the input command
    char temp[999];                         //Create a copy of entered command line value
    int i=0,j;                                //Counter
    unsigned int arguments=0;               //Stores no of command line arguments


    //Set all elements of temp array to '\0'
    memset((char *)&temp,'\0',sizeof(temp));

    //Set all elements of char array to '\0'
    memset((char *)&comm_args,'\0',sizeof(comm_args));

    //Copy input string
    strcpy(temp,comm_line);

    //Extract command from the user entered string
    comm_args[0]=strtok(comm_line," ");

    //printf("\nDEBUG: comm_args[0] is : %s",comm_args[0]);

    //Extract arguments
    while(comm_args[i]){

        comm_args[++i]=strtok(NULL," ");

        //printf("\nDEBUG: comm_args[%D] : %s",i,comm_args[i]);

        if(i==5 || comm_args[i]==NULL)
        {
            arguments=i-1;
            break;
        }
        //vprintf("\nDEBUG: comm_args[%d] is :%s",i,comm_args[i]);
    }

	comm_line=temp;

    //Common commands
    if (strncmp("AUTHOR",comm_args[0],sizeof("AUTHOR")-1)==0 && arguments==0)
    {

        success(comm_args[0]);
        cse4589_print_and_log("I, alizisha, have read and understood the course academic integrity policy.\n");
        end_(comm_args[0]);

    }
    else if( strncmp("PORT",comm_args[0],sizeof("PORT")-1)==0 && arguments==0 && isLoggedIn==TRUE)
    {

        if(my_port>=0 && my_port<=65536){
            success(comm_args[0]);
            cse4589_print_and_log("PORT:%d\n", my_port);
        }else{
            error_(comm_args[0]);
        }

        end_(comm_args[0]);
    }
    else if( strncmp("IP",comm_args[0],sizeof("IP")-1)==0 && arguments==0 && isLoggedIn==TRUE)
    {

        if(getPublicIP()>=0){
           success(comm_args[0]);
           cse4589_print_and_log("IP:%s\n", my_ip);
        }else{
            error_(comm_args[0]);
        }
        end_(comm_args[0]);
    }
    else if( strncmp("LIST",comm_args[0],sizeof("LIST")-1)==0 && arguments==0 && isLoggedIn==TRUE)
    {
        char *ip_addr=(char *)calloc(INET_ADDRSTRLEN+1,sizeof(char));
        char *host_name=(char *)calloc(50,sizeof(char));

        int list_id;

        if(mode==CLIENT)
        {
            success(comm_args[0]);
            for(i=0,list_id=1;i<=3;i++)
            {
                if(clients[i].port!=NOT_CONNECTED)
                {
                    //printf("\nDEBUG: 1");
                    memset(host_name,'\0',50);
                    memset(ip_addr,'\0',INET_ADDRSTRLEN+1);
                    //printf("\nDEBUG: 2");
                    //printf("\nIP before ntop is: %d",clients[i].ip);
                    inet_ntop(AF_INET,&(clients[i].ip),ip_addr,INET_ADDRSTRLEN);
                    //printf("\nDEBUG: 3");
                    get_name(host_name,clients[i].ip);
                    //printf("\nDEBUG: 4");
                    cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", list_id++, host_name, ip_addr, clients[i].port);
                    //printf("\nDEBUG: 5");
                }
            }
        }else if(mode==SERVER)
        {
            success(comm_args[0]);
            //memset(host_name,'\0',50);
            //memset(ip_addr,'\0',INET_ADDRSTRLEN+1);

            /*

            for(i=0,j=1;i<=3;i++,j++)
                printf("\n%d\tIP: %d\tPort: %d",j,client_list[i].nodeinfo.ip,client_list[i].nodeinfo.port);

            fflush(stdout);*/

            for(i=0,list_id=1;i<=3;i++)
            {
                if(client_list[i].isOnline==TRUE)
                {
                    memset(host_name,'\0',50);
                    memset(ip_addr,'\0',INET_ADDRSTRLEN+1);
                    //printf("\nDEBUG: 2");
                    inet_ntop(AF_INET,&(client_list[i].nodeinfo.ip),ip_addr,INET_ADDRSTRLEN);
                    //printf("\nDEBUG: 3");
                    get_name(host_name,client_list[i].nodeinfo.ip);
                    //printf("\nDEBUG: 4");
                    cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", list_id++, host_name, ip_addr, client_list[i].nodeinfo.port);
                }
            }

            /*

            printf("\nAfter listing!");
            for(i=0,j=1;i<=3;i++,j++)
                printf("\n%d\tIP: %d\tPort: %d",j,client_list[i].nodeinfo.ip,client_list[i].nodeinfo.port);

            fflush(stdout);
            */
        }else
        {
            error_(comm_args[0]);
        }

        free(ip_addr);
        free(host_name);

        end_(comm_args[0]);

    }
    else if( strncmp("EXIT",comm_args[0],sizeof("EXIT")-1)==0 && arguments==0)
    {
        //Insert method to logout

        success(comm_args[0]);
        end_(comm_args[0]);

        exit(0);
    }
    //Server Commands
    else if( strncmp("STATISTICS",comm_args[0],sizeof("STATISTICS")-1)==0 && arguments==0 && mode==SERVER)
    {
        success(comm_args[0]);
        serv_stats();
        end_(comm_args[0]);

    }
    else if( strncmp("BLOCKED",comm_args[0],sizeof("BLOCKED")-1)==0 && arguments==1 && mode==SERVER)
    {
        command_blocked(comm_args[1]);
        end_(comm_args[0]);
    }
    //Client Commands
    else if( strncmp("BLOCK",comm_args[0],sizeof("BLOCK")-1)==0 && arguments==1 && mode==CLIENT && isLoggedIn==TRUE
        && strncmp("BLOCKED",comm_args[0],sizeof("BLOCKED")-1)!=0)
    {
        //printf("\nDEBUG: Extracted IP is : %s",comm_args[1]);
        //fflush(stdout);

        if(client_block(comm_args[1])==TRUE)
        {
            success(comm_args[0]);
        }else
        {
            error_(comm_args[0]);
        }
        end_(comm_args[0]);
    }
    else if( strncmp("LOGIN",comm_args[0],sizeof("LOGIN")-1)==0 && arguments==2 && mode==CLIENT &&isLoggedIn==FALSE )
    {
        if(client_login(comm_args[1],atoi(comm_args[2]))==TRUE)
        {
            success(comm_args[0]);
            isLoggedIn=TRUE;
            isLoggedOut=FALSE;
            FD_SET(client_sock, &master);                   //Add client_socket to client's master FD list
            //FD_SET(client_sock, &read_fds);
            if(client_sock>fdmax)
                fdmax=client_sock;
            //printf("\nDebug: Client_socket successfully added to list!");
            //fflush(stdout);
        }else
        {
            error_(comm_args[0]);
        }

        end_(comm_args[0]);

    }
    else if( strncmp("REFRESH",comm_args[0],sizeof("REFRESH")-1)==0 && arguments==0 && mode==CLIENT && isLoggedIn==TRUE)
    {
        int i,list_id;
        char *ip_addr=(char *)calloc(INET_ADDRSTRLEN+1,sizeof(char));
        char *host_name=(char *)calloc(50,sizeof(char));

        if(client_refresh()==TRUE)
        {
            success(comm_args[0]);
            for(i=0,list_id=1;i<=3;i++)
            {
                if(clients[i].port!=NOT_CONNECTED)
                {
                    //printf("\nDEBUG: 1");
                    memset(host_name,'\0',50);
                    memset(ip_addr,'\0',INET_ADDRSTRLEN+1);
                    //printf("\nDEBUG: 2");
                    //printf("\nIP before ntop is: %d",clients[i].ip);
                    inet_ntop(AF_INET,&(clients[i].ip),ip_addr,INET_ADDRSTRLEN);
                    //printf("\nDEBUG: 3");
                    get_name(host_name,clients[i].ip);
                    //printf("\nDEBUG: 4");
                    cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", list_id++, host_name, ip_addr, clients[i].port);
                    //printf("\nDEBUG: 5");
                }
            }

        }
        else{
            error_(comm_args[0]);
        }

        free(ip_addr);
        free(host_name);

        end_(comm_args[0]);

    }
    else if( strncmp("SEND",comm_args[0],sizeof("SEND")-1)==0 && isLoggedIn==TRUE&& mode==CLIENT && arguments>=2)
    {
        char *ip,*msg;//comm_args[1];
        strtok(comm_line," ");
        ip=strtok(NULL," ");
        msg=strtok(NULL,"\n");

        if(send_message_client(ip,msg)==TRUE)
        {
            success(comm_args[0]);
        }else
        {
            error_(comm_args[0]);
        }
        end_(comm_args[0]);
    }
    else if( strncmp("BROADCAST",comm_args[0],sizeof("BROADCAST")-1)==0  && isLoggedIn==TRUE && mode==CLIENT &&arguments>=1)
    {
        char *msg;

        /* Extract message from the entire command */
        strtok(comm_line," ");                          //Separate command string
        msg=strtok(NULL,"\n");

        if(send_broadcast(msg)==TRUE)
            success(comm_args[0]);
        else
            error_(comm_args[0]);

        end_(comm_args[0]);

    }
    else if( strncmp("UNBLOCK",comm_args[0],sizeof("UNBLOCK")-1)==0 && arguments==1 && mode==CLIENT && isLoggedIn==TRUE)
    {
        if(client_unblock(comm_args[1])==TRUE)
        {
            success(comm_args[0]);
        }else{
            error_(comm_args[0]);
        }

        end_(comm_args[0]);
    }
    else if( strncmp("LOGOUT",comm_args[0],sizeof("LOGOUT")-1)==0 && arguments==0 && mode==CLIENT && isLoggedIn==TRUE)
    {
        if(client_logout()==TRUE)
        {
            success(comm_args[0]);
            isLoggedIn=FALSE;
            isLoggedOut=TRUE;
            //Stop listening on client_sock
            FD_CLR(client_sock, &master);

        }else
        {
            error_(comm_args[0]);
        }

        end_(comm_args[0]);
    }
    else if( strncmp("SENDFILE",comm_args[0],sizeof("SENDFILE")-1)==0 && arguments==2 && mode==CLIENT && isLoggedIn==TRUE){
    }else{
    }
}
Exemplo n.º 5
0
/**
 * Prints and logs the successful execution of a command
 * @param Command name
 */
void success(char* command){

    cse4589_print_and_log("[%s:SUCCESS]\n",command);

}
Exemplo n.º 6
0
/**
 * Prints and logs the end of execution of a command
 * @param Command name
 */
void end_(char* command){

    cse4589_print_and_log("[%s:END]\n",command);

}
Exemplo n.º 7
0
/**
 * Prints and logs an error message in execution of a command
 * @param Command name
 */
void error_(char* command){

    cse4589_print_and_log("[%s:ERROR]\n",command);

}
int fnAcademicIntegrity() {
	cse4589_print_and_log(
			"I have read and understood the course academic integrity policy located at http://www.cse.buffalo.edu/faculty/dimitrio/courses/cse4589_f14/index.html#integrity");
	return 1;
}
/**
 * fnUDPListen function
 *
 * @param  inputCmd Command line input
 * @param  portNumber Process port number
 * @return char* Input Typed by User
 *
 * Start Listening Port and receive input commands and other packets
 */
void fnUDPListen() {
	/*
	 * Declaration of variables Start
	 */
	int fdMax, sockIndex;
	struct addrinfo server_addr, *servinfo, *p;
	int getaddrinfoResult, selectResult;
	fd_set read_fds;
	FD_ZERO(&master);               // clear the master and temp sets
	FD_ZERO(&read_fds);
	int receivedPacketSize, numberOfReceivedBytes, numberOfBytesToBeSent;
	socklen_t theirAddrLength;
	char bufferForReceivedPacket[1024];
	char * bufferForPacketToBeSent;
	struct sockaddr_storage theirAddr;
	struct in_addr addr;
	struct timeval tv;
	int i;
	int packetReceived;
	/*
	 * Declaration of variables End
	 */

	servinfo = malloc(100);
	p = malloc(100);

	size_t sizeOfPacket = sizeof(uint32_t) * 2
			+ (3 * sizeof(uint32_t) * numberOfServers);

//	bufferForReceivedPacket = (char *) malloc(sizeOfPacket);
	bufferForPacketToBeSent = (char *) malloc(sizeOfPacket);

//Ref: Beej Guide
	memset(&server_addr, 0, sizeof server_addr);
	server_addr.ai_family = AF_INET;
	server_addr.ai_socktype = SOCK_DGRAM;
	server_addr.ai_flags = AI_PASSIVE;

//	strcpy(myPort,"4567");

	if ((getaddrinfoResult = getaddrinfo(NULL, myPort, &server_addr, &servinfo))
			!= 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(getaddrinfoResult));
//		return 1;
	}

	for (p = servinfo; p != NULL; p = p->ai_next) {
		if ((udpSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol))
				== -1) {
			perror("listener:socket\n");
			continue;
		}

		if (bind(udpSocket, p->ai_addr, p->ai_addrlen) == -1) {
			close(udpSocket);
			perror("listener:bind");
			continue;
		}

		break;
	}

	if (p == NULL) {
		fprintf(stderr, "listener: failed to bind socket\n");
//		return 2;
	}

	freeaddrinfo(servinfo);

	printf("listener waiting to recvfrom\n");

	FD_SET(udpSocket, &master);
	FD_SET(0, &master); //Add STDIN
	fdMax = udpSocket;

	tv.tv_sec = timeOut;
	tv.tv_usec = 0;
//	optval = 1;
//	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,(const void *)&optval , sizeof(int));

	while (1) {
		read_fds = master; // create a copy
		selectResult = select(fdMax + 1, &read_fds, NULL, NULL, &tv);
		if (selectResult == -1) {
//			perror("Select error");
			exit(4);
		} else if (selectResult > 0) {
			for (sockIndex = 0; sockIndex <= fdMax; sockIndex++) {
//				printf("Socket received\n");
				if (FD_ISSET(sockIndex, &read_fds)) {
//					printf("FD set\n");
					if (sockIndex == 0) {
						char * userSelection = fnReadUserInput(); //read_line(fgets(GetUserInput,sizeof(GetUserInput),stdin));
//						userSelection =
						fnSelectCommandAction(userSelection);
					}
					if (sockIndex == udpSocket) {
//						printf("Socket received from node in the network\n");
//						receivedPacketSize = sizeof(uint32_t) * 2
//								+ (3 * sizeof(uint32_t) * numberOfServers);

//						bufferForReceivedPacket = (char *) malloc(
//								sizeOfPacket);

						printf("sizeOfPacket = %d\n", sizeOfPacket);

						printf("Original size of buffer = %d\n",
								sizeof(bufferForReceivedPacket));

//						printf(
//								"All set to receive incoming packet in buffer\n");

//						numberOfReceivedBytes = recvfrom(sockIndex,
//														bufferForReceivedPacket, receivedPacketSize, 0,
//														(struct sockaddr *) &theirAddr,
//														&theirAddrLength);

						numberOfReceivedBytes = recvfrom(sockIndex,
								bufferForReceivedPacket,
								sizeof(bufferForReceivedPacket), 0,
								NULL,
								NULL);

						if (numberOfReceivedBytes == -1) {
							perror("Error on recvfrom");
							exit(1);
						}

						printf("Number of bytes received = %d\n",
								numberOfReceivedBytes);

						printf(
								"Packet received from network\nNow verifying if received from neighbor\n");

						printf("Packet size being sent = %d\n",
								sizeof(bufferForReceivedPacket));
//						if (fnVerifyIfNeighbor(bufferForReceivedPacket)) {
//						cse4589_dump_packet(bufferForReceivedPacket,
//									sizeOfPacket);
						packetReceived = fnStoreReceivedPacket(
								bufferForReceivedPacket);
						//add logic to update routingPacketToBeSentToNeighbors struct and routingDetails struct
						if (packetReceived > 0) {
							cse4589_print_and_log(
									"RECEIVED A MESSAGE FROM SERVER %d\n",
									packetReceived);
							fnFillPacketStruct();
							packetCounter++;
						}
//						}
					}
				}
			}
		} else if (selectResult == 0) {
			//when timeout occurs
			for (i = 0; i < numberOfNeighbors; i++) {
				printf("Check to send updates to all neighbors\n");
				if (routingDetails[neighbor[i]].counter != 127) {
					routingDetails[neighbor[i]].counter++;
				}

				if (routingDetails[neighbor[i]].counter == 3) {
					routingDetails[neighbor[i]].counter = 127;
					//						routingDetails[neighbor[i]].totalCost = INF;
					costMatrix[myServerID][i] = INF;
				}

				fnBellmanFordAlgorithm();
				fnFillPacketStruct();
				bufferForPacketToBeSent = fnCreatePacket();

//					if ((numberOfBytesToBeSent = sendto(udpSocket,
//							bufferForPacketToBeSent,
//							sizeof(bufferForPacketToBeSent), 0,
//							(struct sockaddr *) &server_addr,
//							sizeof server_addr)) == -1) {
//						perror("Packet sending error");
//						return 0;
//					}
//					return 1;
				fnSendPacket(neighbor[i]);
			}

			tv.tv_sec = timeOut;
			tv.tv_usec = 0;
		}
	}
//	return 0;
}
/**
 * fnSelectCommandAction function
 *
 * @param  inputCmd Command line input
 * @param  portNumber Process port number
 * @return char* Input Typed by User
 *
 *
 */
void fnSelectCommandAction(char* inputCmd) {
	int count = 0;
	int i;
	int flag = 1;
	int functionExecuteSuccess = 0;
	char *token;
	char outputCmd1[50];
	char outputCmd2[50];
	char outputCmd3[50];
	char outputCmd4[50];
	char errorMessage[1024];

	token = (char *) malloc(50);
//	strcpy(errorMessage, (char *) malloc(255);

	token = strtok(inputCmd, " \n");

	while (token != NULL) {

		if (count < 4) {

			if (count == 0) {
				strcpy(outputCmd1, token);
			}

			else if (count == 1) {
				strcpy(outputCmd2, token);

			}

			else if (count == 2) {
				strcpy(outputCmd3, token);

			}

			else if (count == 3) {
				strcpy(outputCmd4, token);

			}
		} else
			strcpy(errorMessage, "Invalid command or invalid number of arguments");
		cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);

		token = strtok(NULL, " \n");

		count += 1;
	}

	if (count == 1) {
		if (strcasecmp(outputCmd1, "STEP") == 0) {
			functionExecuteSuccess = fnStep();
			if (functionExecuteSuccess == 1) {
				cse4589_print_and_log("%s:SUCCESS\n", outputCmd1);
			} else {
				strcpy(errorMessage, "Command execution failed to send routing updates to some or all neighbors");
				cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
			}
		} else if (strcasecmp(outputCmd1, "PACKETS") == 0) {
			cse4589_print_and_log("%s:SUCCESS\n", outputCmd1);

			fnPackets();

		} else if (strcasecmp(outputCmd1, "DISPLAY") == 0) {
			cse4589_print_and_log("%s:SUCCESS\n", outputCmd1);

			fnDisplay();

		} else if (strcasecmp(outputCmd1, "CRASH") == 0) {
			fnCrash();
		} else if (strcasecmp(outputCmd1, "DUMP") == 0) {
			functionExecuteSuccess = fnDump();
			if (functionExecuteSuccess == 1) {
				cse4589_print_and_log("%s:SUCCESS\n", outputCmd1);
			} else {
				strcpy(errorMessage, "Command execution failed to dump the packet");
				cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
			}
		} else if (strcasecmp(outputCmd1, "ACADEMIC_INTEGRITY") == 0) {
			cse4589_print_and_log("%s:SUCCESS\n", outputCmd1);

			fnAcademicIntegrity();
		} else {
			fnErrorMsg();
			strcpy(errorMessage, "Command execution failed because it is not a valid command");
			cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
		}
	}

	else if (count == 2) {
		if (strcasecmp(outputCmd1, "DISABLE") == 0) {
			for (i = 0; i < strlen(outputCmd2); i++) {
				if (isdigit(outputCmd2[i])) {
				}

				else {
					flag = 0;
					break;
				}
			}
			if (flag == 1) {

				functionExecuteSuccess = fnDisable(atoi(outputCmd2));
				if (functionExecuteSuccess == 1) {
					cse4589_print_and_log("%s:SUCCESS\n", outputCmd1);
				} else {
					strcpy(errorMessage, "Command execution failed to disable the link in the network");
					cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
				}
			} else {
				strcpy(errorMessage, "Command execution failed Input command 2 can only be numeric");
				cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
			}
		} else {
			fnErrorMsg();
			strcpy(errorMessage, "Command execution failed because it is not a valid command");
			cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
		}
	}

	else if (count == 4) {
		if (strcasecmp(outputCmd1, "UPDATE") == 0) {
			for (i = 0; i < strlen(outputCmd2); i++) {
				if (isdigit(outputCmd2[i])) {
				}

				else {
					flag = 0;
					break;
				}
			}
			if (flag == 1) {
				for (i = 0; i < strlen(outputCmd3); i++) {
					if (isdigit(outputCmd3[i])) {
					}

					else {
						flag = 0;
						break;
					}
				}

				if (flag == 1) {
					for (i = 0; i < strlen(outputCmd4); i++) {
						if (isdigit(outputCmd4[i])) {
						}

						else {
							flag = 0;
							break;
						}
					}
					if (flag == 1) {
						functionExecuteSuccess = fnUpdate(atoi(outputCmd2),
								atoi(outputCmd3), atoi(outputCmd4));
						if (functionExecuteSuccess == 1) {
							cse4589_print_and_log("%s:SUCCESS\n", outputCmd1);
						} else if (functionExecuteSuccess == 2) {
							strcpy(errorMessage, "Command execution failed ServerID2 is not a neighbor for the current node");
							cse4589_print_and_log("%s:%s\n", outputCmd1,
									errorMessage);
						} else if (functionExecuteSuccess == 3) {
							strcpy(errorMessage, "Command execution failed ServerID1 is not a neighbor for the current node");
							cse4589_print_and_log("%s:%s\n", outputCmd1,
									errorMessage);
						} else {
							strcpy(errorMessage, "Command execution failed to update the link cost");
							cse4589_print_and_log("%s:%s\n", outputCmd1,
									errorMessage);
						}
					} else {
						strcpy(errorMessage, "Command execution failed Input command 4 can only be numeric");
						cse4589_print_and_log("%s:%s\n", outputCmd1,
								errorMessage);
					}
				} else {
					strcpy(errorMessage, "Command execution failed Input command 3 can only be numeric");
					cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
				}
			} else {
				strcpy(errorMessage, "Command execution failed Input command 2 can only be numeric");
				cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
			}
		} else {
			fnErrorMsg();
			strcpy(errorMessage, "Command execution failed because it is not a valid command");
			cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
		}
	} else {
		fnErrorMsg();
		strcpy(errorMessage, "Command execution failed because it is not a valid command");
		cse4589_print_and_log("%s:%s\n", outputCmd1, errorMessage);
	}

	fflush(stdout);
}
Exemplo n.º 11
0
int process_cmd(char *cmd,int sockfd){

 char * msg;
 commands cmd_pkt;
 char *cmds;
 cmds =strdup(cmd);
 cmd_pkt = getdlmtdcmdstr(cmd);

 if(cmd_pkt.no_of_args == -1)
  return 0;
 //printf("%s:%s\n",cmd_pkt.cmnd,"");
 /*call method to validate the nodes*/
 if(strcasecmp(cmd_pkt.arglist[0],"UPDATE")==0){
    msg ="Invalid Arguments.Usage - update <server-ID1> <server-ID2> <Link Cost>";
   if(cmd_pkt.no_of_args != 4 ){
     //printf("%s:%s\n",cmd_pkt.cmnd,msg);
     cse4589_print_and_log("%s:%s\n",cmds,msg);
     //printf("\n[PA3]$:Invalid Arguments.Usage - update <server-ID1> <server-ID2> <Link Cost>");
     return -1;
   }
   
   if(validate_nodes(cmd_pkt.arglist,0) == -1){
     msg ="Invalid Arguments.server-ID1 - Node Id server-ID2:Must be neighbor node Id";
     cse4589_print_and_log("%s:%s\n",cmds,msg);
   
   return -1;
   }
   else{
    update_rout_info(cmd_pkt.arglist); 
    compute_bellman_ford();
  }
   //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
   cse4589_print_and_log("%s:SUCCESS\n",cmds);
   
 }/*call method to send the update packet */ 
   else if(strcasecmp(cmd_pkt.arglist[0],"STEP")==0){
    if(cmd_pkt.no_of_args > 1 ){
     msg ="Invalid Arguments.Usage - step"; 
     cse4589_print_and_log("%s:%s\n",cmds,msg);
     //printf("\n[PA3]$:Invalid Arguments.Usage - step");
     return -1;
   }
   send_update_packet_nnodes(sockfd);
   //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
   cse4589_print_and_log("%s:SUCCESS\n",cmds); 

 }
 else if(strcasecmp(cmd_pkt.arglist[0],"PACKETS")==0){
  if(cmd_pkt.no_of_args > 1 ){
   msg ="Invalid Arguments.Usage - packets";
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   //printf("\n[PA3]$:Invalid Arguments.Usage - packets");
   return -1;
 }
  //display the packets received
  //printf("\n The number of packets sent : %d",packet_count);
  cse4589_print_and_log("%s:SUCCESS\n",cmds);
  cse4589_print_and_log("%d\n",packet_count);
  //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
  packet_count = 0 ;

}else if(strcasecmp(cmd_pkt.arglist[0],"DISPLAY")==0){
  if(cmd_pkt.no_of_args > 1 ){
    
   msg ="Invalid Arguments.Usage - display";
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   //printf("\n[PA3]$:Invalid Arguments.Usage - display");
   return -1;
 }
  cse4589_print_and_log("%s:SUCCESS\n",cmds);
  dsplay_rt_table();
}else if(strcasecmp(cmd_pkt.arglist[0],"DISABLE")==0){
  if(cmd_pkt.no_of_args != 2  ){
   msg ="Invalid Arguments.Usage - disable <server-id>"; 
   //printf("\n[PA3]$:Invalid Arguments.Usage - disable <server-ID>");
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   return -1;
 } 
  int iret = 0;
  iret = disable_link(cmd_pkt.arglist); 
    //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
  msg ="Invalid Arguments.Usage - Server Id has to be a neighbor node id"; 
  if(iret == 1)
  cse4589_print_and_log("%s:SUCCESS\n",cmds);
  else
  cse4589_print_and_log("%s:%s\n",cmds,msg);

}else if(strcasecmp(cmd_pkt.arglist[0],"CRASH")==0){
 if(cmd_pkt.no_of_args > 1 ){
   msg ="Invalid Arguments.Usage - crash";
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   //printf("\n[PA3]$:Invalid Arguments.Usage - crash");
   return -1;
 }
  gotosleep();
 
}else if(strcasecmp(cmd_pkt.arglist[0],"DUMP")==0){
  if(cmd_pkt.no_of_args > 1 ){
   msg ="Invalid Arguments.Usage - dump"; 
   //printf("\n[PA3]$:Invalid Arguments.Usage - dump");
   cse4589_print_and_log("%s:%s\n",cmds,msg);
   return -1;
 }
 build_rtpkt_dump(1);
 //printf("%s:SUCCESS\n",cmd_pkt.cmnd);
 cse4589_print_and_log("%s:SUCCESS\n",cmds);
}else if(strcasecmp(cmd_pkt.arglist[0],"academic_integrity")== 0)
{
  //printf("\n I have read and understood the course academic integrity policy located at http://www.cse.buffalo.edu/faculty/dimitrio/courses/cse4589_f14/index.html#integrity");
  cse4589_print_and_log("I have read and understood the course academic integrity policy located at http://www.cse.buffalo.edu/faculty/dimitrio/courses/cse4589_f14/index.html#integrity");
}else{
 printf("\n[PA3]$:Command not found");
 fflush(stdout);//cse4589_print_and_log("Command not found\n");
 return -1;
}
free(cmds);
return 1;
}