int main(int argc, char *argv[])
{
    int src_GUID = -1, dst_GUID = -1;

    /* parameter parsing */
    while(1) {
        int option_index = 0, c = 0;
        static struct option long_options[] =
        {
            {"h", no_argument, 0, 0},
            {"help", no_argument, 0, 0},
            {"v", no_argument, 0, 0},
            {"version", no_argument, 0, 0},
            {"orbit", no_argument, 0, 0},
            {"d", no_argument, 0, 0},
            {"m", required_argument, 0, 0},
            {"o", required_argument, 0, 0},
            {0, 0, 0, 0}
        };

        c = getopt_long_only(argc, argv, "", long_options, &option_index);

        /* no more options to parse */
        if(c == -1) break;

        /* unrecognized option */
        if(c == '?')
        {
            help();
            return 0;
        }

        switch(option_index)
        {
            /* h, help */
        case 0:
        case 1:
            help();
            return 0;
            break;

            /* v, version */
        case 2:
        case 3:
            printf("Real-Time CPS Server Version: 0.1\n" \
            "Compilation Date.....: unknown\n" \
            "Compilation Time.....: unknown\n");
            return 0;
            break;

            /* orbit, run in orbit mode */
        case 4:
            orbit = 1;
            break;

            /* debug mode */
        case 5:
            debug = 1;
            break;

            /* mine GUID */
        case 6:
            src_GUID = strtol(optarg, NULL, 10);
            break;

            /* other's GUID */
        case 7:
            dst_GUID = strtol(optarg, NULL, 10);
            break;

        default:
            help();
            return 0;
        }
    }

    /* register signal handler for <CTRL>+C in order to clean up */
    // if(signal(SIGINT, signal_handler) == SIG_ERR)
    // {
    //     printf("could not register signal handler\n");
    //     exit(EXIT_FAILURE);
    // }

    // init the mutex lock
    if (pthread_mutex_init(&user_map_lock, NULL) != 0
        || pthread_mutex_init(&queue_map_lock, NULL) != 0
        || pthread_mutex_init(&sem_map_lock, NULL) != 0)
    {
        printf("\n mutex init failed\n");
        return 1;
    }

    if (orbit)
    {
        if (src_GUID != -1 && dst_GUID != -1)
        {
            if (debug) printf("src_GUID: %d, dst_GUID: %d\n", src_GUID, dst_GUID);
            /* init new Message Distributor */
            MsgD.init(src_GUID, dst_GUID, debug);
        }
        else
        {
            printf("ERROR: please enter src_GUID and dst_GUID with flags -m & -o\n");
            exit(1);
        }

    }

    // imgM.init_DB(100,"./imgDB/","./indexImgTable","ImgIndex.yml");
    imgM.init_matchImg("./indexImgTable", "ImgIndex.yml", "./infoDB/");

    server_run();

    return 0;
}
Exemple #2
0
/******************************************************************************
Description.: this is the transmit child thread
              it is responsible to send out one frame
Input Value.:
Return Value:
******************************************************************************/
void *result_child(void *arg)
{
    if (debug) printf("result child thread\n");

    struct arg_result *args = (struct arg_result *)arg;
    int sock = args->sock;
    char *file_name = args->file_name;
    int matchedIndex;
    char defMsg[] = "none";
    char sendInfo[200];
    vector<float> coord;
    ImgMatch imgM;

    // start matching the image
    imgM.matchImg(file_name);
    matchedIndex = imgM.getMatchedImgIndex();
    if (matchedIndex == 0) 
    {   
        // write none to client
        if (!orbit) {
            if (write(sock, defMsg, sizeof(defMsg)) < 0)
            {
               errorSocket("ERROR writting to socket", sock);
            }
            printf("Not match.\n\n");

        }
        else
        {
            MsgD.send(sock, defMsg, sizeof(defMsg));
            printf("Not match.\n\n");
        }

        // if (debug) printf("not match\n");            
    }
    else
    {
        // send result to client
        coord = imgM.calLocation();
        string info = imgM.getInfo();
        sprintf(sendInfo, "%s,%f,%f,%f,%f,%f,%f,%f,%f", info.c_str(), coord.at(0), coord.at(1), coord.at(2), coord.at(3), coord.at(4), coord.at(5), coord.at(6), coord.at(7));
        printf("Matched Index: %d\n\n", matchedIndex);

        if (debug) printf("sendInfo: %s\n", sendInfo);
        if (!orbit)
        {
            if (write(sock, sendInfo, sizeof(sendInfo)) < 0)
            {
                errorSocket("ERROR writting to socket", sock);
            }
        }
        else
        {
            MsgD.send(sock, sendInfo, sizeof(sendInfo));
        }
        // if (debug) printf("matched image index: %d\n", matchedIndex);

    }

    if (debug) printf("------------- end matching -------------\n");

    return NULL;

}
/******************************************************************************
Description: function for sending back the result
Input Value.:
Return Value:
******************************************************************************/
void server_result (int sock, string userID)
{
    // printf("result thread\n\n");

    int n;
    char response[BUFFER_SIZE] = "ok";
    char defMsg[BUFFER_SIZE] = "none";
    int matchedIndex;
    char sendInfo[BUFFER_SIZE];
    vector<float> coord;
    sem_t *sem_match = new sem_t(); // create a new semaphore in heap
    queue<string> *imgQueue = 0;    // queue storing the file names 

    //  Init semaphore and put the address of semaphore into map
    if (sem_init(sem_match, 0, 0) != 0)
    {
        errorSocket("ERROR semaphore init failed", sock);
    }
    // grap the lock
    pthread_mutex_lock(&sem_map_lock);
    sem_map[userID] = sem_match;
    pthread_mutex_unlock(&sem_map_lock);

    // reponse to the client
    if (!orbit)
    {
        n = write(sock, response, sizeof(response));
        if (n < 0)
        {
            error("ERROR writting to socket");
        }
    }
    else
    {
        MsgD.send(sock, response, BUFFER_SIZE);
    }

    while(!global_stop) 
    {
        sem_wait(sem_match);
        // get the address of image queue
        if (imgQueue == 0)
        {
            imgQueue = queue_map[userID];
        }

        // check if the queue is empty
        if (imgQueue->empty())
        {
            sem_map.erase(userID);
            queue_map.erase(userID);
            user_map.erase(userID);
            delete(sem_match);
            delete(imgQueue);
            sem_destroy(sem_match);
            MsgD.close(sock, 0);
            printf("[server] client disconnectted\n");
            pthread_exit(NULL); //terminate calling thread!
        }

        if (debug) printf("\n----------- start matching -------------\n");
        string file_name = imgQueue->front(); 
        if (debug) printf("file name: %s\n", file_name.c_str());
        imgQueue->pop();

        // start matching the image
        imgM.matchImg(file_name);
        matchedIndex = imgM.getMatchedImgIndex();
        if (matchedIndex == 0) 
        {   
            // write none to client
            if (!orbit) {
                if (write(sock, defMsg, sizeof(defMsg)) < 0)
                {
                   errorSocket("ERROR writting to socket", sock);
                }

            }
            else
            {
                MsgD.send(sock, defMsg, BUFFER_SIZE);
            }

            if (debug) printf("not match\n");            
        }
        else
        {
            // write index to client
            coord = imgM.calLocation();
            sprintf(sendInfo, "%d,%f,%f,%f,%f,%f,%f,%f,%f", matchedIndex, coord.at(0), coord.at(1), coord.at(2), coord.at(3), coord.at(4), coord.at(5), coord.at(6), coord.at(7));
            if (debug) printf("sendInfo: %s\n", sendInfo);
            if (!orbit)
            {
                if (write(sock, sendInfo, sizeof(sendInfo)) < 0)
                {
                    errorSocket("ERROR writting to socket", sock);
                }
            }
            else
            {
                MsgD.send(sock, sendInfo, BUFFER_SIZE);
            }
            if (debug) printf("matched image index: %d\n", matchedIndex);

        }

        if (debug) printf("------------- end matching -------------\n");
    }

    if (!orbit)
    {
        close(sock);    
    }
    printf("[server] Connection closed. --- result\n\n");
    delete(sem_match);
    pthread_exit(NULL); //terminate calling thread!

}