示例#1
0
void mmEM::initialization(param myParam, vector_2str stringX, vector_2str stringY)
{

	if (myParam.limitPair)
	{
		readInitFile(myParam);
	}

	if (stringX.size() != stringY.size())
	{
		cerr << "error: data are not in pairs of x and y " << endl;
		cerr << "# of x instances : " << stringX.size() << endl;
		cerr << "# of y instances : " << stringY.size() << endl;
		exit(-1);
	}

	// initialization with uniform distribution all possible alignments //
	
	long double totalCount = 0; // keep track how many observations

	// for each x and y pair //
	for (int i=0; i < stringX.size(); i++)
	{
		// over lengths of x and y
		for (int xl = 0; xl < stringX[i].size(); xl++)
		{
			for (int yl = 0; yl < stringY[i].size(); yl++)
			{
				if (myParam.delX)
				{
					for (int j=0; (j < myParam.maxX) && (xl-j >= 0); j++)
					{
						//string ssX = stringX[i].substr(xl-j,j+1);
						string ssX = join(stringX[i], xl-j , j+1);
						counts[ssX][myParam.nullChar] = 1;
					}
				}

				if (myParam.delY)
				{
					for (int k=0; (k < myParam.maxY) && (yl-k >=0); k++)
					{
						// string ssY = stringY[i].substr(yl-k,k+1);
						string ssY = join(stringY[i], yl-k, k+1);
						counts[myParam.nullChar][ssY] = 1;
					}
				}

				for (int j = 0; (j < myParam.maxX) && (xl-j >= 0); j++)
				{
					for (int k=0; (k < myParam.maxY) && (yl-k >=0); k++)
					{
						//string ssX = stringX[i].substr(xl-j,j+1);
						//string ssY = stringY[i].substr(yl-k,k+1);
						string ssX = join(stringX[i], xl-j, j+1);
						string ssY = join(stringY[i], yl-k, k+1);
						
						// if it defines a limit set //
						if (myParam.limitPair)
						{
							if (limitSet.find(ssX + myParam.sepChar + ssY) == limitSet.end())
							{
								continue;
							}
						}
						counts[ssX][ssY] = 1;
					}
				}
			}
		}
	}

	// if there is an initial mapping file //
	// while reading the initFile, fill up the limit set //
	if (myParam.initFile != "")
	{
		cout << "Reading the initial file: " << myParam.initFile << endl;
		ifstream INITFILE;
		INITFILE.open(myParam.initFile.c_str());
		if (! INITFILE)
		{
			cerr << "error: unable to open file " << myParam.initFile << endl;
			exit(-1);
		}

		vector_initTable initCount;
		while (! INITFILE.eof() )
		{
			string line;
			vector<string> lineList;

			getline(INITFILE, line);

			initTable initTmp;

			if (line == "")
			{
				continue;
			}
			//string t0,t1,t3;
			//if (myParam.inFormat == "l2p")
			//{
			// should read it as the model format //

			lineList = splitBySpace(line);
			
			initTmp.xstring = lineList[0];
			initTmp.ystring = lineList[1];
			
			if (lineList.size() > 2)
			{
				initTmp.prob = (long double)atof(lineList[2].c_str());
			}
			else
			{
				initTmp.prob = 1;
			}
			initCount.push_back(initTmp);
		}
		INITFILE.close();

		// sort initCount //
		cout << "Sorting the initial count table" << endl;
		sort(initCount.begin(), initCount.end(), initTableSortedFn);
		
		long double total_add_prob = 0;
		for (vector_initTable::iterator pos = initCount.begin(); pos != initCount.end(); pos++)
		{
			if ((total_add_prob < myParam.initProbCut) || (pos->prob ==	1))
			{
				counts[pos->xstring][pos->ystring] += ((counts[pos->xstring].size() * 10) + stringX.size()) * pos->prob ;
			}
			else
			{
				break;
			}

			if (pos->prob != 1)
			{
				total_add_prob += pos->prob;
			}
		}
	}

}
void nonBeaconConnector(int *param)
{
	int i=0,status = 0;
	pthread_t myChildThreads_nonbeacon[1000];
	int myChildThreadCtr = 0;
	struct hostent *beacon;
	time_t mysystime;
	no_of_connection = 0;

	readInitFile();
	for(i = 0; i < nonBeaconNodeInfo->initNeighbors; i++)
	{
		beacon = NULL;
		int neighborbeaconSock=0;
		if((neighborbeaconSock = socket(AF_INET,SOCK_STREAM,0)) == -1)
		{
			writeErrorToLogFile((char *)"\nError creating socket\n");
			i--;
			continue;
		}
		struct sockaddr_in serv_addr;
		char beaconip[16];
		serv_addr.sin_family = AF_INET;
		char char_key[100] = "\0";
		sprintf(char_key,"%s%d",initNeighborsHostnameList[i],initNeighborsPortList[i]);
		string key = string(char_key);

		//Get the Peer information
		beacon = gethostbyname(initNeighborsHostnameList[i]);
		memset(beaconip,0,16);
		strcpy(beaconip,inet_ntoa(*((struct in_addr *)beacon->h_addr_list[0])));
		free(beacon);

		serv_addr.sin_addr.s_addr = inet_addr(beaconip);
		serv_addr.sin_port = htons(initNeighborsPortList[i]);

		//Send connection request
		status = connect(neighborbeaconSock,(struct sockaddr *)&serv_addr,sizeof(serv_addr));			// connect to the host and port specified in
		if (status == 0)
		{

			connectionDetails *cd = new connectionDetails;
			no_of_connection++;
			//Get Peer information
			socklen_t len;
			struct sockaddr_storage addr;
			char ipstr[16];
			int port;
			len = sizeof(addr);
			getpeername(neighborbeaconSock, (struct sockaddr*)&addr, &len);
			struct sockaddr_in *s = (struct sockaddr_in *)&addr;
			port = ntohs(s->sin_port);
			inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);

			//Prepare the connection details
			strcpy(cd->hostname,initNeighborsHostnameList[i]);
			cd->port = initNeighborsPortList[i];
			cd->socketDesc = neighborbeaconSock;
			cd->wellKnownPort = initNeighborsPortList[i];
			cd->notOperational=0;
			mysystime = time(NULL);
			cd->lastReadActivity = mysystime;
			cd->lastWriteActivity = mysystime;
			cd->isJoinConnection = 0;
			cd->threadsExitedCount = 0;
			cd->helloStatus=0;
			cd->tiebreak = 0;
			pthread_mutex_init(&cd->connectionLock,NULL);
			pthread_cond_init(&cd->messagingQueueCV, NULL);
			pthread_mutex_init(&cd->messagingQueueLock,NULL);



			char *mapkey = (char *)malloc(100);
			memset(mapkey,0,100);
			strcpy(mapkey,char_key);

			//Create a hello message to be sent to all the beacons
			struct message *mymessage;
			struct helloMessage *myhelloMsg;

			myhelloMsg = createHello(nonBeaconNodeInfo->hostname,nonBeaconNodeInfo->port);

			mymessage = CreateMessageHeader(HELLO, /*Message Type */
					1, /* TTL */
					myhelloMsg -> dataLength, /*data body length*/
					NULL, /*char *messageHeader*/
					NULL, /*char *message*/
					(void*)myhelloMsg, /*void *messageStruct*/
					mapkey,/*char* temp_connectedNeighborsMapKey*/
					MYMESSAGE, /*My Message or notmymessage*/
					nonBeaconNodeInfo /*Node Info Pointer*/);

			//Create messsage header and add msg body created above
			pthread_mutex_lock(&helloMsgMapLock);
			helloMsgMap.insert(HELLOMSGMAPTYPE::value_type(mapkey,1));
			pthread_mutex_unlock(&helloMsgMapLock);

			char keytype[256] = "\0";
			int j = 0;
			while(j<20)
			{
				keytype[j] = *(mymessage->uoid + j);
				*(cd->hellomsguoid + j) = *(mymessage->uoid + j);
				j++;
			}
			string uoidKey = string(keytype);
			cd->hellomsguoid[j] = '\0';

			pthread_mutex_lock(&connectedNeighborsMapLock);
			connectedNeighborsMap.insert(CONNMAPTYPE::value_type(key,cd));
			pthread_mutex_unlock(&connectedNeighborsMapLock);

			pthread_mutex_lock(&messageCacheMapLock);
			messageCacheMap.insert(MESSCACHEMAPTYPE::value_type(uoidKey,mymessage));
			pthread_mutex_unlock(&messageCacheMapLock);

			//Create reader and writer threads and give them the connection key
			pthread_t childThread1;
			pthread_create(&childThread1,NULL,(void* (*)(void*))reader,mapkey);
			myChildThreads_nonbeacon[myChildThreadCtr] = childThread1;
			myChildThreadCtr++;
			pthread_t childThread2;
			pthread_create(&childThread2,NULL,(void* (*)(void*))writer,mapkey);
			myChildThreads_nonbeacon[myChildThreadCtr] = childThread2;
			myChildThreadCtr++;

			//Call the function that sends the message to all the connections
			sendMessageToNode(mymessage);
		}
	}

	if(no_of_connection < nonBeaconNodeInfo->minNeighbors)
	{
		/*
		 * Could not connect to MinNeighbors
		 * Delete init_neighbor_list file and rejoin the network
		 */

		/*
		 * Ask all the threads created till now to kill themselves and exit
		 */
		pthread_mutex_lock(&systemWideLock);
		autoshutdownFlag = 1;
		keyboardShutdown = 1;
		pthread_mutex_unlock(&systemWideLock);

		pthread_kill(keyboardThreadNonbeacon,SIGUSR1);

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

		pthread_kill(nonBeaconListenerThread,SIGUSR1);

		pthread_mutex_lock(&checkThreadLock);
		pthread_cond_signal(&checkThreadCV);
		pthread_mutex_unlock(&checkThreadLock);
		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);
			close(cd->socketDesc);
			pthread_mutex_unlock(&cd->connectionLock);
		}
		pthread_mutex_unlock(&connectedNeighborsMapLock);

		pthread_mutex_lock(&helloMsgMapLock);
		for(helloMsgMapIter = helloMsgMap.begin();helloMsgMapIter != helloMsgMap.end();helloMsgMapIter++)
		{
			helloMsgMap.erase(helloMsgMapIter->first);
		}
		pthread_mutex_unlock(&helloMsgMapLock);

		pthread_mutex_lock(&checkMsgMapLock);
		for(checkMsgMapIter = checkMsgMap.begin();checkMsgMapIter != checkMsgMap.end();checkMsgMapIter++)
		{
			checkMsgMap.erase(checkMsgMapIter->first);
		}
		pthread_mutex_unlock(&checkMsgMapLock);

		pthread_mutex_lock(&systemWideLock);
		//restartState = 1;

		/*
		 * If node is not able to connect to enough neighors then it needs to Quit
		 */

		restartState = 0;
		checkStatus = 0;
		pthread_mutex_unlock(&systemWideLock);

		pthread_exit(NULL);
	}

	pthread_exit(NULL);
}