unsigned int
MsgAggregatingCommunicationManager::checkPhysicalLayerForMessages(int maxNum) {
    int numberOfMessagesReceived = 0;
    bool noMessagesLeft = false;
    SerializedInstance* msg = NULL;

    while (numberOfMessagesReceived != maxNum && noMessagesLeft == false) {
        msg = retrieveMessageFromPhysicalLayer();
        if (msg == NULL) {
            // there are no messages at this time
            noMessagesLeft = true;
            waitedWithNoInputMessages++;
        } else {
            // there are messages to pick up
            numberOfMessagesReceived++;
            waitedWithNoInputMessages = 0;

            // deserialize the message and route it to the appropriate
            // receiver
            routeMessage(dynamic_cast<KernelMessage*>(msg->deserialize()));
            // since we allocated it, we delete it
            delete msg;
        }
    }// end while loop

    return numberOfMessagesReceived;
}
Example #2
0
void 
TerminateTokenTest::testSerialization(){
  KernelMessageTest::testSerialization( testToken, TerminateToken::getTerminateTokenType() );

  SerializedInstance *serialized = static_cast<Serializable *>(testToken)->serialize();
  TerminateToken *deserialized = dynamic_cast<TerminateToken *>(serialized->deserialize());
  checkDefaults( deserialized );
  delete deserialized;
}
void
InitializationMessageTest::testSerialization() {
    KernelMessageTest::testSerialization( testMessage,
                                          InitializationMessage::getInitializationMessageType() );

    SerializedInstance *serialized = static_cast<Serializable *>(testMessage)->serialize();
    InitializationMessage *deserialized =
        dynamic_cast<InitializationMessage *>( serialized->deserialize() );

    CPPUNIT_ASSERT( deserialized->getDataType() == InitializationMessage::getInitializationMessageType() );

    CPPUNIT_ASSERT( deserialized->getObjectNames() == getDefaultObjectNames() );
    CPPUNIT_ASSERT( deserialized->getNumSimulationManagers() == getDefaultNumSimulationManagers() );
    delete deserialized;
}
Example #4
0
void
EventTest::testSerialization(const Event* toTest, const string& dataType) {
    SerializableTest::testSerialization(toTest, dataType);

    const VTime& sendTime = toTest->getSendTime();
    const VTime& recvTime = toTest->getReceiveTime();
    const ObjectID& receiver = toTest->getReceiver();
    const ObjectID& sender = toTest->getSender();

    SerializedInstance* serialized = static_cast<const Serializable*>(toTest)->serialize();
    Event* deserialized = dynamic_cast<Event*>(serialized->deserialize());
    CPPUNIT_ASSERT(sendTime == deserialized->getSendTime());
    CPPUNIT_ASSERT(recvTime == deserialized->getReceiveTime());
    CPPUNIT_ASSERT(sender == deserialized->getSender());
    CPPUNIT_ASSERT(receiver == deserialized->getReceiver());
}
void
CommunicationManagerImplementationBase::waitForStart(){
  bool finished = false;
  KernelMessage *msg = NULL;
  while( !finished ){
    SerializedInstance *networkMessage = retrieveMessageFromPhysicalLayer();
    if( networkMessage != 0 ){
      msg = dynamic_cast<KernelMessage *>(networkMessage->deserialize());
      
      delete networkMessage;
      
      if( msg->getDataType() == "StartMessage" ){
	finished = true;
      }
      routeMessage(msg);
    }
  }
}
Example #6
0
void
MessageManager::sendMessage(KernelMessage* msg, int dest) {

    AggregationSendCriteria whatToDo;

    SerializedInstance* toSend = msg->serialize();

    whatToDo = getSendCriteriaInFixedPessimism(msg, toSend->getSize());

    switch (whatToDo) {
    case SEND_AND_WRITE:
        sendMessage();
        writeMessage(toSend, dest);
        break;
    case WRITE_AND_SEND:
    case SEND:
        writeMessage(toSend, dest);
        sendMessage();
        break;
    case WRITE:
        writeMessage(toSend, dest);
        break;
    case SEND_WRITE_SEND:
        sendMessage();
        writeMessage(toSend, dest);
        sendMessage();
        break;
    case DO_NOT_SEND:
        writeMessage(toSend, dest);
        break;
    default:
        cerr << "Invalid aggregation criterion received, writing the message anyway";
        cerr << " " << whatToDo << endl;
        writeMessage(toSend, dest);
        break;
    }

    incrementReceiveCriterion();
    checkToReceive();
    delete toSend;
}
unsigned int
CommunicationManagerImplementationBase::checkPhysicalLayerForMessages(int maxNum) {
    int numberOfMessagesReceived = 0;
    bool noMessagesLeft = false;

    while (numberOfMessagesReceived != maxNum && noMessagesLeft == false) {
        SerializedInstance* msg = retrieveMessageFromPhysicalLayer();
        if (msg == NULL) {
            // there are no messages at this time
            noMessagesLeft = true;
        } else {
            // there are messages to pick up
            numberOfMessagesReceived++;
            // deserialize the message and route it to the appropriate
            // receiver
            KernelMessage* kMsg = dynamic_cast<KernelMessage*>(msg->deserialize());
            delete msg;
            routeMessage(kMsg);
        }
    }// end while loop

    return numberOfMessagesReceived;
}
void
CommunicationManagerImplementationBase::waitForInitialization(unsigned int numExpected){
  unsigned int numReceived = 0;

  vector<string> *objectNames =
    mySimulationManager->getSimulationObjectNames();

  unsigned int numberOfSimulationManagers =
    mySimulationManager->getNumberOfSimulationManagers();

  unsigned int mySimulationManagerID =
    mySimulationManager->getSimulationManagerID();


  // send everybody execept ourselves the init message
  for(unsigned int count = 0; count < numberOfSimulationManagers; count++){
    if(mySimulationManagerID != count){
      KernelMessage *messageToSend =
	new InitializationMessage( mySimulationManagerID, 
				   count,
				   *objectNames,
				   numberOfSimulationManagers);
      sendMessage(messageToSend, count);
    }
  }
  delete objectNames;

  bool circulate = false;  
  if (numExpected==0)
  {
	  circulate=true;
  }
  // now wait to hear from others
  while(numReceived != numExpected ||
	(mySimulationManagerID != 0 && circulate == false)){
    SerializedInstance *networkMessage = retrieveMessageFromPhysicalLayer();
    if(networkMessage != NULL){
      KernelMessage *msg = dynamic_cast<KernelMessage *>(networkMessage->deserialize());
      if( msg != NULL ){
	if( msg->getDataType() == getInitializationMessageType() ){
	  numReceived++;
	  routeMessage(msg);
	}
	else if( msg->getDataType() == getCirculateInitializationMessageType() ){
	  circulate = true;
	}
	else{
	  // we didn't find an initialization message, but we need to
	  // check for other types of messages
	  routeMessage(msg);
	}
      }
      delete networkMessage;
    }
  }// end of while loop

  // Now we need to circulate a message to synchronize all communication managers and
  // make sure they are in the same "phase". When comm mgr 0 sends this, all other
  // comm mgrs are still stuck in the while loop above.

  KernelMessage *messageToSend = new CirculateInitializationMessage( mySimulationManagerID,
        (mySimulationManagerID+1) % numberOfSimulationManagers );
  sendMessage(messageToSend, (mySimulationManagerID+1) % numberOfSimulationManagers);

  
  // If we are simulation manager 0 we have to catch the CirculateInitializationMessage.
  while (circulate == false) {
    SerializedInstance *networkMessage = retrieveMessageFromPhysicalLayer();
    if(networkMessage != NULL){
      KernelMessage *msg = dynamic_cast<KernelMessage *>(networkMessage->deserialize());
      
      if (msg != NULL && msg->getDataType() == getCirculateInitializationMessageType() ){
	circulate = true;
      }
      else if (msg != NULL){
	// we didn't find an initialization message, but we need to
	// check for other types of messages
	routeMessage(msg);
      }
      delete networkMessage;
    }
  }// end of while loop
}
/// Used in optimistic fossil collection to restore the state of the stream.
void
TimeWarpSimulationStream::restoreCheckpoint(std::ifstream* inFile, unsigned int restoreTime) {
    char del = '_';
    char delIn;

    // First, empty out what is already in the queues.
    if (inFileQueue != NULL) {
        inFileQueue->fossilCollect(mySimulationManager->getPositiveInfinity());
    }

    if (outFileQueue != NULL) {
        outFileQueue->fossilCollect(mySimulationManager->getPositiveInfinity());
    }

    // If there is an input file, read the file position, and set the file position.
    if (inFileQueue != NULL) {
        if (inFile->peek() == del) {
            inFile->read(&delIn, sizeof(delIn));
        }

        streampos curPos;
        inFile->read((char*)(&curPos), sizeof(curPos));
        inFileQueue->restoreFilePosition(curPos);
    }

    // If there is an output file, restore the data set.
    if (outFileQueue != NULL) {
        string* line;
        unsigned int lineSize;
        unsigned int timeSize;

        while (inFile->peek() != del) {
            inFile->read((char*)(&timeSize), sizeof(timeSize));
            inFile->read(&delIn, sizeof(delIn));

            if (delIn != del) {
                cerr << mySimulationManager->getSimulationManagerID() <<  " - ALIGNMENT ERROR in "
                     << "OutFileQueue restoreCheckpoint. Got: " << delIn << endl;
                abort();
            }

            // Read the time in.
            char* timeBuf = new char[timeSize];
            inFile->read(timeBuf, timeSize);
            SerializedInstance* serVTime = new SerializedInstance(timeBuf, timeSize);
            VTime* restoredTime = dynamic_cast<VTime*>(serVTime->deserialize());

            inFile->read(&delIn, sizeof(delIn));

            if (delIn != del) {
                cerr << mySimulationManager->getSimulationManagerID() <<  " - ALIGNMENT ERROR in "
                     << "outFileQueue restoreCheckpoint. Got: " << delIn << endl;
                abort();
            }

            // Read the file line data.
            inFile->read((char*)(&lineSize), sizeof(lineSize));
            char* lineBuf = new char[lineSize];
            inFile->read(lineBuf, lineSize);

            line = new string(lineBuf, lineSize);

            // Restore the file queue.
            outFileQueue->storeLine(*restoredTime, line);
        }

        if (inFile->peek() == del) {
            inFile->read(&delIn, sizeof(delIn));
        }

        // Set the file to the position of the last rollback and delete any file
        // commits that occurred after the rollback.
        streampos end;
        inFile->read((char*)(&end), sizeof(end));
        outFileQueue->setPosAndClear(end);
    }
}
/// Used in optimistic fossil collection to save the state of the stream.
void
TimeWarpSimulationStream::saveCheckpoint(std::ofstream* outFile, unsigned int saveTime) {
    char del = '_';

    // If there is an input file, save the position of the file at the checkpoint time.
    if (inFileQueue != NULL) {
        outFile->write(&del, sizeof(del));

        std::multiset< InFileData >::iterator it = inFileQueue->begin();
        while (it != inFileQueue->end() && it->getTime().getApproximateIntTime() < saveTime) {
            it++;
        }

        // If there are no position saves after the checkpoint time, use the current
        // position in the file.
        streampos curPos;
        if (it != inFileQueue->end()) {
            curPos = it->getPosition();
        } else {
            curPos = inFileQueue->access()->tellg();
        }

        outFile->write((char*)(&curPos), sizeof(curPos));
    }

    // If there is an output file, save all data that is less than the checkpoint time
    // and save the current end of file position.
    if (outFileQueue != NULL) {
        const VTime* time;
        unsigned int timeSize;
        const char* timePtr;
        SerializedInstance* toWrite;
        const string* line;
        unsigned int lineSize;

        std::multiset< FileData >::iterator it = outFileQueue->begin();
        while (it != outFileQueue->end() && it->getTime().getApproximateIntTime() < saveTime) {
            time = &it->getTime();
            toWrite = new SerializedInstance(time->getDataType());
            time->serialize(toWrite);

            timePtr = &toWrite->getData()[0];
            timeSize = toWrite->getSize();

            line = it->getLine();
            lineSize = line->size();

            outFile->write((char*)(&timeSize), sizeof(timeSize));
            outFile->write(&del, sizeof(del));
            outFile->write(timePtr, timeSize);
            outFile->write(&del, sizeof(del));
            outFile->write((char*)(&lineSize), sizeof(lineSize));
            outFile->write(line->c_str(), lineSize);

            it++;
        }
        outFile->write(&del, sizeof(del));

        streampos end = outFileQueue->getEOFPosition();
        outFile->write((char*)(&end), sizeof(end));
    }
}
void ThreadedOptFossilCollManager::restoreCheckpoint(unsigned int restoredTime) {
	for (int i = 0; i < nextCheckpointTime.size(); i++) {
		lastCheckpointTime[i] = restoredTime;
		nextCheckpointTime[i] = restoredTime + checkpointPeriod;
	}
	utils::debug << mySimManager->getSimulationManagerID()
			<< " - Restoring to checkpoint: " << restoredTime << endl;

	// Restore the states to the objects. The actual state queue will be filled
	// after all of the events have been transmitted
	unsigned int size;
	char delIn;
	SimulationObject *object;
	unsigned int simMgrID = mySimManager->getSimulationManagerID();

	vector<State*> *states = checkpointedStates.find(restoredTime)->second;
	vector<Event*> restoredEvents;
	for (int i = 0; i < mySimManager->getNumberOfSimulationObjects(); i++) {
		object = mySimManager->getObjectHandle(ObjectID(i, simMgrID));
		object->setSimulationTime(mySimManager->getZero());
		object->getState()->copyState((*states)[i]);

		stringstream filename;
		filename << ckptFilePath << "LP"
				<< mySimManager->getSimulationManagerID() << "."
				<< restoredTime << "." << i;
		ifstream ckFile;
		ckFile.open(filename.str().c_str());
		if (!ckFile.is_open()) {
			cerr << mySimManager->getSimulationManagerID()
					<< " - Could not open file: " << filename.str()
					<< ", aborting simulation." << endl;
			abort();
		}

		// Read in the events.
		while (ckFile.peek() != EOF) {
			ckFile.read((char*) (&size), sizeof(size));
			ckFile.read(&delIn, sizeof(delIn));

			if (delIn != delimiter) {
				cerr << mySimManager->getSimulationManagerID()
						<< "ALIGNMENT ERROR in Event"
						<< "restoreCheckpoint. Got: " << delIn << endl;
				abort();
			}

			char *buf = new char[size];
			ckFile.read(buf, size);
			SerializedInstance *serEvent = new SerializedInstance(buf, size);
			Event *restoredEvent =
					dynamic_cast<Event*> (serEvent->deserialize());
			delete[] buf;
			delete serEvent;

			restoredEvents.push_back(restoredEvent);
			utils::debug << "restoring to objID "
					<< restoredEvent->getSender().getSimulationObjectID()
					<< endl;
		}

		ckFile.close();
	}

	std::sort(restoredEvents.begin(), restoredEvents.end(),
			receiveTimeLessThanEventIdLessThan());

	// Restore the output queue and transmit the events to restore the event set.
	for (int i = 0; i < restoredEvents.size(); i++) {
		mySimManager->handleEvent(restoredEvents[i]);
	}

	lastRestoreTime = restoredTime;

	// Set recovering to false
	mySimManager->setRecoveringFromCheckpoint(false);
	//myCommManager->setRecoveringFromCheckpoint(false);

	utils::debug << mySimManager->getSimulationManagerID()
			<< " - Done with restore process, " << restoredTime << endl;
}