void nonBeaconTimer(void *param)
{
	ndInfoTimer = (struct NodeInfo *)param;

	/*
	 * Check if the JOIN msg has timed out
	 */

	struct message *mapmessage;
	mysystime = 0;
	struct timeval tvTimer;
	bool msgstatus = true;
	no_of_hello_expired = 0;

	/*
	 * From this point onwards the timer has taken the decision that the node will participate in
	 * joining the SERVANT network
	 */

	tvTimer.tv_sec = 0;
	tvTimer.tv_usec = 100000;

	while(1)
	{
		/*
		 * Check the
		 */
		select(NULL,NULL,NULL,NULL,&tvTimer);


		pthread_mutex_lock(&systemWideLock);
		if(autoshutdownFlag == 1)
		{
			/*
			 * send signal to all the threads that are waiting:
			 * 1. Listener thread
			 * 2. dispathcher thread
			 * 3. Keyboard Thread
			 * 4. all writer threads from the connections establisthed so far
			 */

			if(keyboardShutdown == 1)
			{
				pthread_mutex_unlock(&systemWideLock);
				pthread_exit(NULL);
			}
			pthread_mutex_unlock(&systemWideLock);


			pthread_kill(nonBeaconListenerThread,SIGUSR1);


			pthread_mutex_lock(&eventDispatcherQueueLock);
			pthread_cond_signal(&eventDispatcherQueueCV);
			pthread_mutex_unlock(&eventDispatcherQueueLock);


			pthread_mutex_lock(&checkThreadLock);
			pthread_cond_signal(&checkThreadCV);
			pthread_mutex_unlock(&checkThreadLock);

			pthread_kill(keyboardThreadNonbeacon,SIGUSR1);


			connectionDetails *cd;
			pthread_mutex_lock(&connectedNeighborsMapLock);
			for(connectedNeighborsMapIter = connectedNeighborsMap.begin(); connectedNeighborsMapIter != connectedNeighborsMap.end();connectedNeighborsMapIter++)
			{
				cd = connectedNeighborsMapIter->second;
				pthread_mutex_lock(&cd->connectionLock);
				cd->notOperational = 1;
				pthread_mutex_lock(&cd->messagingQueueLock);
				pthread_cond_signal(&cd->messagingQueueCV);
				pthread_mutex_unlock(&cd->messagingQueueLock);
				pthread_mutex_unlock(&cd->connectionLock);
			}
			pthread_mutex_unlock(&connectedNeighborsMapLock);

			/*Write the indexes and File lists to Minifile system*/
			writeIndexToFile(KeywordIndexMap,kwrdIndexFileName);

			writeIndexToFile(SHA1IndexMap,sha1IndexFileName);

			writeIndexToFile(fileNameIndexMap,fileNameIndexFileName);

			writeListToFile(LRUList,lruListFileName);

			writeListToFile(PermFileList,permFileListFileName);

			pthread_exit(NULL);
		}
		pthread_mutex_unlock(&systemWideLock);

		pthread_mutex_lock(&messageCacheMapLock);
		no_of_hello_expired = 0;
		for(messageCacheMapIter = messageCacheMap.begin(); messageCacheMapIter != messageCacheMap.end(); messageCacheMapIter++)
		{
			mapmessage = messageCacheMapIter->second;
			mysystime = time(NULL);
			if(((((mysystime - mapmessage->timestamp) >= ndInfoTimer->msgLifetime && mapmessage->messType != HELLO)
				|| ((mysystime - mapmessage->timestamp) >= ndInfoTimer->keepAliveTimeout && mapmessage->messType == HELLO)) && mapmessage->messType != GET)
				|| ((mysystime - mapmessage->timestamp) >= ndInfoTimer->getMsgLifeTime && mapmessage->messType == GET))
			{
				//messageCacheMap.erase(messageCacheMapIter->first);
				if(mapmessage->myMessage == MYMESSAGE){
					if(mapmessage->messType == HELLO || mapmessage->messType == CHECK || mapmessage->messType == SEARCH)
					{
						messageCacheMap.erase(messageCacheMapIter->first);
						msgstatus = handleMessageAtTimer(mapmessage);
						if(msgstatus == false)
						{

							messageCacheMap.erase(messageCacheMapIter->first);
							pthread_mutex_unlock(&messageCacheMapLock);
							pthread_exit(NULL);
						}
					}
					else
					{
						messageCacheMap.erase(messageCacheMapIter->first);
					}
				}
				else{
					// If not mymessage and life time is expired I need to erase it from Message Cache,
					// irrespective of its type
					messageCacheMap.erase(messageCacheMapIter->first);
				}

			}
		}
		pthread_mutex_unlock(&messageCacheMapLock);


		tvTimer.tv_sec = 0;
		tvTimer.tv_usec = 50000;
		select(NULL,NULL,NULL,NULL,&tvTimer);
		scanAndEraseConnectedNeighborsMap(0);

		tvTimer.tv_sec = 0;
		tvTimer.tv_usec = 25000;
		select(NULL,NULL,NULL,NULL,&tvTimer);
		checkLastWriteActivityOfConnection();

		tvTimer.tv_sec = 0;
		tvTimer.tv_usec = 100000;
	}
	pthread_exit(NULL);
}
Exemplo n.º 2
0
int main()
{
    node *head=NULL;
    int index;
    Route r;
    head=loadRoutesFromFile();

    /* infinite loop continues until the break statement encounter*/
    while(1)
    {
        printMenu();
        fflush(stdin); // flush the input buffer
        int choice  = getche(); // get the input from keyboard
        switch(choice)
        {
        case '1':  // if user pressed 1 and wants to add route
            system("cls");

            Route newRoute = createNewRoute(head);
            printf("\nNew route added successfully! ");
            head=push(head,newRoute);
            break;


        case '2': // list all routes in given season
            system("cls");

            printLinkedList(head);

            getche();

            break;
        case '3': // write list to file
            system("cls");

            writeListToFile(head);
            printf("\nSaved successfully! ");
            getche();

            break;

        case '4': // show routes by country
            system("cls");

            char country[50+1];
            printf("\nEnter the country to filter by (case insensitive): ");
            while(1)
            {
                scanf("%s", &country);
                if(strlen(&country) >0 && strlen(&country)<50 )
                {
                    break;
                }
                else
                {
                    printf("\nInvalid string length!");
                    fflush(stdin);
                    printf("\nEnter new country: ");
                }
            }
            strupr(country);// make it uppercase

            PrintRoutesFromGivenCountry(head, country);

            getche();
            break;
        case '5': // show routes by season
            system("cls");

            int season;
            printf("\nEnter season as integer (Spring=1, Summer=2, Autumn=3, Winter=4): ");

            while(1)
            {
                if(scanf("%i", &season)==1 && season > 0 && season < 5)
                {
                    break;
                }
                else
                {
                    printf("\nInvalid season integer!");
                    fflush(stdin);
                    printf("\nEnter new season as an integer (Spring=1, Summer=2, Autumn=3, Winter=4): ");
                }
            }

            printRoutesBySeason(head,season);
            getche();
            break;

        case '6': //  delete by code
            system("cls");

            printf("\n Give the code of the route to delete: ");
            scanf("%i",&index);
            head = deleteRouteByCode(head,index,&r);

            printf("\n Successfully deleted this route:");
            printf("\n Code: %i, To:%s, during %s for %i days. Cost: $%.2f", r.code, r.country, seasonStrings[r.season-1], r.durationDays, r.price); // print the route details

            getche();
            break;

        case '7': //  modify by code
            system("cls");

            printf("\n Give the code of the route to modify: ");
            scanf("%i",&index);
            head = modifyRouteByCode(head,index,&r);

            printf("\n Successfully modified this route:");
            printf("\n Code: %i, To:%s, during %s for %i days. Cost: $%.2f", r.code, r.country, seasonStrings[r.season-1], r.durationDays, r.price); // print the route details

            getche();
            break;

        case '8':
            system("cls");

            writeListToFile(head);
            printf("\nGood bye! ");
            getche();


            exit(0);
            break; // exit from the program
        }
    }

    return 0;
}