示例#1
0
文件: provider.c 项目: usbuild/RTSS
int add_station(char *name) {
    if(strlen(name) == 0) return 1;
    return insert_station(name);
}
示例#2
0
train_processing(char * trainMessage)
{
    char string[]="dest:doomtrain:stopstation:add(1111@1hostname,2222@2hostname):res1+1,res2+1,res3-1";
// char string[]="dest";
//    char *inputString = string;
    char *inputString = trainMessage;
    /* Delimeter set for State machine */
    char *delimeterColon = ":";
    char *delimeterAt = "@";
    char *delimeterAdd = "(";
    char *delimeterParenthesisClose = ")";
    char *delimeterPlus = "+";
    char *delimeterMinus = "-";
    char *delimeterComa = ",";
    char *delimeterStationNameColon = ":";
    char * token = NULL;
    char * tokenAllResourcePair = NULL;
    char * resourceName = NULL;
    char * resourceQuantity = NULL;
    char * tokenResourcePair = NULL;
    char * tokenAllConnectingStations = NULL;
    char * tokenConnectingStation = NULL;
    char * portNumber = NULL;
    char * intPortNumber;
    char * delim = NULL;
    int nextStationPortNum=0;
    int len=0;

    delim = delimeterColon;
    int state = -1;

    /* For sending messages to other station*/
    int sock_send, n_send;
    unsigned int length_send;
    struct sockaddr_in server_send, from_send;
    struct hostent *hp_send;
    char buffer_send[256];
    sock_send= socket(AF_INET, SOCK_DGRAM, 0);
    if (sock_send < 0) error("socket");
    server_send.sin_family = AF_INET;
    /*-------------------------------------------*/

    fprintf(stderr, "\n Input String = ---%s---END\n", inputString);

    while ( token = strsep(&inputString, delimeterColon)  )
    {
//      fprintf(stderr, "\ntoken---%s---Remaining stringis:---%s----", token, inputString);
        if ( -1 == state)
        {
            state++;
            if (strcmp("dest", token) ==0)
            {
                fprintf(stderr, "\n Station Name matches %s Remaining string  is : %s END\n", token, inputString);
                continue;
            }
            else
            {
                discardedTrainCountWrongStation++;
                break;
            }
        }

        if ( '\0' == token)
        {
            fprintf(stderr, "\nNothing left to END\n");
            break;
        }
        if (NULL ==inputString )
            token[strlen(token) -1] = '\0';
        fprintf(stderr, "\nTOKEN ::::;%s;:::::END\n", token);

        /* case : doomtrainstop and stop train */
        if (strcmp("doomtrain",token) ==0)
        {
            printf("\n Doomtrain encountered: Send doomtrain message to all connected stationEND\n");
            /*TODO : Doomtrain encountered: Send doomtrain message to all connected station*/
            display_all_stations(stationList);
            send_doomtrain_to_all_connected_stations(stationList);
            shut_down(0);
        }
        else if (strcmp("stopstation",token) ==0)
        {
            fprintf(stderr, "\n stoptrain encounteredEND\n");
            /* This is tricky: will do this in last, lets make everything else before this*/
            /*TODO : 1. Process this train
                     2. Send remaining message to next station
                     3. Shut down -- Done*/
            shut_down(0);
        }
        else if (strncmp("add(",token,4) ==0)
        {
//       fprintf(stderr, "\n add( encountered");    /* All Connecting Station are here*/
            tokenAllConnectingStations = strsep(&token, delimeterAdd); // must return NULL
            tokenAllConnectingStations = strsep(&token, delimeterParenthesisClose);
//        fprintf(stderr, "\n tokenAllConnectingStations %s  token %s",tokenAllConnectingStations, token);

            for (; tokenConnectingStation = strsep(&tokenAllConnectingStations, delimeterComa);)
            {
                if( NULL != strrchr(tokenConnectingStation,'@'))
                {
                    portNumber = strsep(&tokenConnectingStation, delimeterAt);

                    if( atoi(portNumber) < 0 | atoi(portNumber) > 65535)
                    {
                        discardedTrainCountFormatErrors++;
                        return;
                    }
                    stationList = insert_station(stationList, atoi(portNumber), tokenConnectingStation);
                    //      fprintf(stderr, "\n insert_station portNumber: %s hostname : %s",portNumber, tokenConnectingStation);
                }
                else
                {
                    /* Bad Port Hostname pair*/
                    //  fprintf(stderr, "In Port Hostname pair @ not foundEND\n");
                    //  exit;
                    discardedTrainCountFormatErrors++;
                    return;
                }
            }         /* End : Connecting Station are here*/
        }
        else if ( ( NULL != strrchr(token,'+')) && ( NULL != strrchr(token,'-')))
        {
            //       fprintf(stderr, "\n In Resource List %s %s",token , inputString);
            tokenAllResourcePair=token;
            /* Resources are here*/
            for (; tokenResourcePair = strsep(&tokenAllResourcePair, delimeterComa );)
            {
                //fprintf(stderr, "\n In Resource tokenAllResourcePair  %s  tokenResourcePair %s",tokenAllResourcePair , tokenResourcePair);
                if ( NULL ==  tokenResourcePair)
                {
                    fprintf(stderr, "\n No more tokenResourcePairEND\n");
                    break;
                }
                else
                    if( NULL != strrchr(tokenResourcePair,'+'))
                    {
                        resourceName = strsep(&tokenResourcePair, delimeterPlus);
                        resourceList = insert_resources(resourceList, atoi(tokenResourcePair), resourceName);
//                        fprintf(stderr, "\n                insert_resources  resourceName : %s resourceQuantity %s",resourceName, tokenResourcePair);
                    }
                    else if( NULL != strrchr(tokenResourcePair,'-'))
                    {
                        resourceName = strsep(&tokenResourcePair, delimeterMinus);
                        resourceList = insert_resources(resourceList, atoi(tokenResourcePair), resourceName);
                        //                      fprintf(stderr, "\n                insert_resources  resourceName : %s resourceQuantity %s",resourceName, tokenResourcePair);
                    }
                    else
                    {
                        /* Bad resource & quantity*/
                        discardedTrainCountFormatErrors++;
                        return;
                        fprintf(stderr, "\nIn resource pair, + or - not found= ---%s---END\n",tokenAllResourcePair);
                    }
            }         /* End : Resources are here*/
        }
        else
        {
            /* here token is next station , search this station and its port*/
            /* token + input string */
            //      fprintf(stderr, "\n $$$New Station is Token --with data in inputString$$$");
            //     fprintf(stderr, "\n Please send me to next station: token  = ---%s---",token );
            /*
                        len = strlen(token);
                        while ( len > 0 && token[len - 1] == '\n' )
                        {
                            token[len - 1] = '\0';
                            --len;
                        }
            */
            //     fprintf(stderr, "\n Pls send me2 : inputString  = ---%s---",inputString );
            //          display_all_stations(stationList);

            nextStationPortNum = search_station(stationList,token);
            //         fprintf(stderr, "\n For station name = ---%s--- , portNum = ---%d---",token, nextStationPortNum );
            if ( 0 == nextStationPortNum )
            {
                fprintf(stderr, "\n ERROR : For station name = ---%s--- , portNum = ---%d---END\n",token, nextStationPortNum );
            }
            else
            {
                /* Send message code*/
                /*TODO :  Authentication to be handled i.e. Check page 4*/
                //      fprintf(stderr, "\n\nstart of Send message code\n\n");
                hp_send = gethostbyname("localhost");
                if (0 ==hp_send) error("Unknown host");

                bcopy((char *)hp_send->h_addr,  (char *)&server_send.sin_addr, hp_send->h_length);
                server_send.sin_port = htons(nextStationPortNum);
                length_send=sizeof(struct sockaddr_in);
                bzero(buffer_send,256);
                //         char *strcat(char *dest, const char *src);
                strcat (buffer_send,token);
                if ( 0< strlen(inputString))
                    strcat (buffer_send,inputString);
                n_send=sendto(sock_send,buffer_send,
                strlen(buffer_send),0,(const struct sockaddr *)&server_send,length_send);
                if (n_send < 0)
                {
                    processedTrainCountInvalidNextStation++;
                    error("Sendto");
                }
                close(sock_send);
                //        fprintf(stderr, "\n\nEnd of Send message code\n\n");
            }

        }
    } /* first for loop ends here */
    processedTrainCountWithoutErrors++;
    state = -1;
}