Esempio n. 1
0
//------------------------------------------------------------------------------
// Read a record
//------------------------------------------------------------------------------
const DataRecordHandle* NetInput::readRecordImp()
{
   // First pass?  Does the file need to be opened?
   if (firstPassFlg) {
      if ( !networkInitialized ) {
         initNetworks();
      }
      firstPassFlg = false;
   }


   DataRecordHandle* handle = 0;

   // When the file is open and ready ...
   if ( networkInitialized && netHandler->isConnected() ) {

      // ---
      // Try to read a message into 'ibuf'
      // ---
      unsigned int n = 0;
      n = netHandler->recvData( ibuf, MAX_INPUT_BUFFER_SIZE );

      // ---
      // If we've successfully read a message from the network
      // then parse it as a DataRecord and put it into a Handle.
      // ---
      if (n > 0) {
         // Parse the data record
         std::string wireFormat(ibuf, n);
         Pb::DataRecord* dataRecord = new Pb::DataRecord();
         bool ok = dataRecord->ParseFromString(wireFormat);

         if (ok) {
            // Create a handle for the data record (it now has ownership)
            handle = new DataRecordHandle(dataRecord);
         }

         else if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) {
            std::cerr << "NetInput::readRecord() -- ParseFromString() error" << std::endl;
            delete dataRecord;
            dataRecord = 0;
         }

      }
   }  
   return handle;
}
Esempio n. 2
0
//------------------------------------------------------------------------------
// Serialize and write a DataRecord to a network
//------------------------------------------------------------------------------
void NetOutput::processRecordImp(const DataRecordHandle* const handle)
{
   bool thisIsEodMsg = false;

   // ---
   // Open the file, if it hasn't been already ...
   // ---
   if ( !networkInitialized && !networkInitFailed ) initNetworks();

   if (handle != 0 && networkInitialized && netHandler->isConnected()) {

      // The DataRecord to be sent
      const Pb::DataRecord* dataRecord = handle->getRecord();

      // Serialize the DataRecord
      std::string wireFormat;
      bool ok = dataRecord->SerializeToString(&wireFormat);

      // Write the serialized message to the network
      if (ok) {
         netHandler->sendData( wireFormat.c_str(), wireFormat.length() );
      }

      else if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) {
         // If we had an error serializing the DataRecord
         std::cerr << "NetOutput::processRecordImp() -- SerializeToString() error" << std::endl;
      }

      // Check for END_OF_DATA message
      thisIsEodMsg = (dataRecord->id() == REID_END_OF_DATA);

   }

   // ---
   // Close the file at END_OF_DATA message
   // ---
   if (thisIsEodMsg) {
      closeConnections();
   }
}
Esempio n. 3
0
//------------------------------------------------------------------------------
// reset() -- Reset the I/O handler
//------------------------------------------------------------------------------
void IoHandler::reset()
{
   BaseClass::reset();

   // Reset our I/O devices
   if (devices != nullptr) {
      List::Item* item = devices->getFirstItem();
      while (item != nullptr) {
         Pair* const pair = static_cast<Pair* const>(item->getValue());
         IoDevice* const p = static_cast<IoDevice* const>(pair->object());
         p->reset();
         item = item->getNext();
      }
   }

   // Initialize the networks
   if (!netInitialized && !netInitFailed) {
      netInitialized = initNetworks();
      netInitFailed = !netInitialized;
   }

   clear();
}
int sageDisplayManager::init(char *data)
{
   char token[TOKEN_LEN];
   char masterIp[SAGE_IP_LEN];
   int screenWidth, screenHeight;
   int dimX, dimY;
   int revY[MAX_TILES_PER_NODE];
   sageDisplayConfig dispCfg;
/*
   getToken(data, token);
   nwCfg.rcvBufSize = atoi(token);

   getToken(data, token);
   nwCfg.sendBufSize = atoi(token);

   getToken(data, token);
   nwCfg.mtuSize = atoi(token);

   getToken(data, token);
   streamPort = atoi(token);

   getToken(data, token);
   shared->bufSize = atoi(token)*1048576; // receiverBufSize to MB

   getToken(data, token);
   int fullScreen = atoi(token);

   int tokenNum = getToken(data, token);
   totalRcvNum = atoi(token);

   if (tokenNum < 1) {
      sage::printLog("sageDisplayManager::init() : insufficient parameters in RCV_INIT message");
      return -1;
   }

   getToken(data, masterIp);

   getToken(data, token);
   screenWidth = atoi(token);
   getToken(data, token);
   screenHeight = atoi(token);
   getToken(data, token);
   dimX = atoi(token);
   getToken(data, token);
   dimY = atoi(token);
   getToken(data, token);
   dispCfg.winX = atoi(token);
   tokenNum = getToken(data, token);
   dispCfg.winY = atoi(token);

   if (tokenNum < 1) {
      sage::printLog("[%d] SDM::init() : insufficient parameters in RCV_INIT message !", shared->nodeID);
      return -1;
   }

   for (int i=0; i<dimY; i++) {
      for (int j=0; j<dimX; j++) {
         getToken(data, token);  // will remove this line (tile id read) later.....
         //shared->tileTable.addEntry(atoi(token));
         getToken(data, token);
         dispCfg.tileRect[i*dimX + j].x = atoi(token);
         getToken(data, token);
         dispCfg.tileRect[i*dimX + j].y = atoi(token);
         dispCfg.tileRect[i*dimX + j].width = screenWidth;
         dispCfg.tileRect[i*dimX + j].height = screenHeight;
         getToken(data, token);
         revY[i*dimX + j] = atoi(token);
      }
   }
   */
   char *tokenbuf;

   getToken(data, token, &tokenbuf);
      nwCfg.rcvBufSize = atoi(token);

      getToken(NULL, token, &tokenbuf);
      nwCfg.sendBufSize = atoi(token);

      getToken(NULL, token, &tokenbuf);
      nwCfg.mtuSize = atoi(token);

      getToken(NULL, token, &tokenbuf);
      streamPort = atoi(token);

      getToken(NULL, token, &tokenbuf);
      shared->bufSize = atoi(token)*1048576; // receiverBufSize to MB

      getToken(NULL, token, &tokenbuf);
      int fullScreen = atoi(token);

      getToken(NULL, token, &tokenbuf);
      totalRcvNum = atoi(token);


      getToken(NULL, token, &tokenbuf);
      strcpy(masterIp, token);

      getToken(NULL, token, &tokenbuf);
      screenWidth = atoi(token);
      getToken(NULL, token, &tokenbuf);
      screenHeight = atoi(token);
      getToken(NULL, token, &tokenbuf);
      dimX = atoi(token);
      getToken(NULL, token, &tokenbuf);
      dimY = atoi(token);
      getToken(NULL, token, &tokenbuf);
      dispCfg.winX = atoi(token);
      getToken(NULL, token, &tokenbuf);
      dispCfg.winY = atoi(token);

      for (int i=0; i<dimY; i++) {
         for (int j=0; j<dimX; j++) {
            getToken(NULL, token, &tokenbuf); // will remove this line (tile id read) later.....
            //shared->tileTable.addEntry(atoi(token));
            getToken(NULL, token, &tokenbuf);
            dispCfg.tileRect[i*dimX + j].x = atoi(token);
            getToken(NULL, token, &tokenbuf);
            dispCfg.tileRect[i*dimX + j].y = atoi(token);
            dispCfg.tileRect[i*dimX + j].width = screenWidth;
            dispCfg.tileRect[i*dimX + j].height = screenHeight;
            getToken(NULL, token, &tokenbuf);
            revY[i*dimX + j] = atoi(token);
         }
      }

   //shared->tileTable.generateTable();

   //sage::printLog("SDM::init() : SDM %d init message has successfully parsed",shared->nodeID);

   dispCfg.width = screenWidth;
   dispCfg.height = screenHeight;
   dispCfg.dimX = dimX;
   dispCfg.dimY = dimY;

   if (dimX > 1)
      dispCfg.mullionX = (int)floor((dispCfg.tileRect[1].x - dispCfg.tileRect[0].x - screenWidth)/2.0 + 0.5);
   else
      dispCfg.mullionX = 0;

   if (dimY > 1)
      dispCfg.mullionY = (int)floor((revY[0] - revY[dimX*(dimY-1)] - screenHeight)/2.0 + 0.5);
   else
      dispCfg.mullionY = 0;

   dispCfg.xpos = dispCfg.tileRect[0].x;
   dispCfg.ypos = revY[dimX*(dimY-1)];
   dispCfg.fullScreenFlag = fullScreen;
   dispCfg.red = 0;
   dispCfg.green = 0;
   dispCfg.blue = 0;
   dispCfg.displayID = displayID;

#if defined(__APPLE__)
   shared->context = (displayContext *) new appleMultiContext;
#else
   shared->context = (displayContext *) new sdlSingleContext;
#endif
   if (shared->context->init(dispCfg) < 0) {
      sage::printLog("[%d] SDM::init() : Error creating display object ", shared->nodeID);
      return -1;
   }

   // clear the screen after startup
   shared->context->clearScreen();
   shared->context->refreshScreen();

   //sage::printLog("sageDisplayManager::init() : SDM %d is creating sageDisplay object", shared->nodeID);
   shared->displayObj = new sageDisplay(shared->context, dispCfg);

   if (initNetworks() < 0)
      return -1;

   // sageSyncClient
	pthread_t thId;
   if ( syncLevel > 0 ) {
		shared->syncClientObj = new sageSyncClient(syncLevel);

   // connect to syncMaster
	   // The parameter shared->nodeID will trigger send() which is for new sync
	   if (shared->syncClientObj->connectToServer(masterIp, syncPort, shared->nodeID) < 0) {
		   sage::printLog("SDM::init() : SDM %d, Fail to connect to syncServer !", shared->nodeID);
		   return -1;
	   }
	   else {
		   //sage::printLog("SDM::init() : SDM %d, Connected to sync master %s:%d", shared->nodeID, masterIp, syncPort);
	   }

	   sage::sleep(1);
	   if ( syncLevel == 2 ) {
		   if (shared->syncClientObj->connectToBarrierServer(masterIp, syncBarrierPort, shared->nodeID) < 0) {
			   sage::printLog("[%d] SDM::init() : Failed to connect to syncBarrierServer !", shared->nodeID);
			   return -1;
		   }
		   else {
			   //sage::printLog("SDM::init() : SDM %d, Connected to sync master barrier %s:%d", shared->nodeID, masterIp, syncBarrierPort);
		   }
	   }

	   // start syncCheckThread
	   // this thread will continuously call shared->syncClinetObj->waitForSync()
	   if (pthread_create(&thId, 0, syncCheckThread, (void*)this) != 0) {
		   sage::printLog("[%d] SDM::init() : Failed to create syncCheckThread !", shared->nodeID);
		   return -1;
	   }

	   // starting sync mainLoop
	   if (syncMaster) {
		   syncBBServerObj->startManagerThread(totalRcvNum, syncRefreshRate, syncMasterPollingInterval);
	   }
   }
   else if ( syncLevel == -1 ) {
	   shared->syncClientObj = new sageSyncClient(syncLevel);
		// connect to syncMaster
		if (shared->syncClientObj->connectToServer(masterIp, syncPort) < 0) {
			sage::printLog("SDM::init() : SDM %d, Fail to connect to syncServer !", shared->nodeID);
			return -1;
		}
		//sage::printLog("Connected to sync master %s:%d", masterIp, syncPort);

		pthread_t thId;

		// start syncCheckThread
		// this thread will continuously call shared->syncClinetObj->waitForSync()
		if (pthread_create(&thId, 0, syncCheckThread, (void*)this) != 0) {
			sage::printLog("[%d] SDM::init() : Failed to create syncCheckThread !", shared->nodeID);
			return -1;
		}
	   //sage::printLog("SDM::init() : old synch client is created. ");

   }

   if ( syncLevel == 0 || syncLevel == -1 ) {
		if (pthread_create(&thId, 0, refreshThread, (void*)this) != 0) {
		   sage::printLog("[%d] SDM::init() : Failed to create refreshThread !", shared->nodeID);
		   return -1;
		}
   }

   return 0;
}