/********************************************************************************
 * Function Name : processEvents
 * Description : Inherited method from ISocketListener which process the received
				 Command from client and replies.
 * Param : fd      [in] - file Descriptor on which the data is recieved
		   revents [in] - Type of Events (POLLIN or POLLOUT)
 * Return : Returns status of connection with Client.
 ********************************************************************************/
int ServerHandler::processEvents( int fd, short revents ) {
	int result = 0;
	char buffer[80];
	char reply[80] = "Invalid command";
	
	DEBUG("");

	do {
		//Receive data on the existing connection still recv fails.
		result = recv(fd, buffer, sizeof(buffer), 0);
		if (result < 0) {
			DEBUG_ERROR("Receive Data Failed");
			break;
		}
		if (result == 0) {
			DEBUG_ERROR(" Connection closed");
			break;
		}
		processDataReceived(buffer, reply);
		//Send Reply to Client Socket
		result = send(fd, reply, sizeof(reply), 0);
		if (result < 0) {
			DEBUG_ERROR(Send Data Failed);
			break;
		}
	} while(1);
	return TRUE;
}
Beispiel #2
0
void CTeleInfo::doWork(boost::shared_ptr<yApi::IYPluginApi> api)
{
   std::cout << "Teleinfo is starting..." << std::endl;

   m_isDeveloperMode = api->isDeveloperMode();

   // Load configuration values (provided by database)
   m_configuration.initializeWith(api->getConfiguration());

   // Create the transceiver
   m_transceiver = CTeleInfoFactory::constructTransceiver(api);

   // Create the buffer handler
   m_receiveBufferHandler = CTeleInfoFactory::GetBufferHandler(api->getEventHandler(),
                                                               kEvtPortDataReceived,
                                                               512);

   m_waitForAnswerTimer = api->getEventHandler().createTimer(kAnswerTimeout,
                                                             shared::event::CEventTimer::kOneShot,
                                                             boost::posix_time::seconds(15));
   m_waitForAnswerTimer->stop();

   // Create the connection
   createConnection(api);

   // the main loop
   std::cout << "Teleinfo plugin is running..." << std::endl;

   // Timer used to read periodically information
   api->getEventHandler().createTimer(kEvtTimerRefreshTeleInfoData,
                                      shared::event::CEventTimer::kPeriodic,
                                      boost::posix_time::seconds(30));

   while (1)
   {
      // Wait for an event
      switch (api->getEventHandler().waitForEvents())
      {
      case yApi::IYPluginApi::kEventStopRequested:
      {
         std::cout << "Stop requested" << std::endl;
         api->setPluginState(yApi::historization::EPluginState::kStopped);
         return;
      }
      case kEvtPortConnection:
      {
         std::cout << "Teleinfo plugin :  Port Connection" << std::endl;
         api->setPluginState(yApi::historization::EPluginState::kCustom, "connecting");

         if (api->getEventHandler().getEventData<bool>())
            processTeleInfoConnectionEvent(api);
         else
            processTeleInfoUnConnectionEvent(api);

         break;
      }
      case kEvtPortDataReceived:
      {
         std::cout << "TeleInfo plugin :  DataReceived" << std::endl;

         processDataReceived(api,
                             api->getEventHandler().getEventData<const shared::communication::CByteBuffer>());

         break;
      }
      case kEvtTimerRefreshTeleInfoData:
      {
         // When received this timer, we restart the reception through the serial port
         std::cout << "Teleinfo plugin :  Resume COM" << std::endl;
         m_transceiver->ResetRefreshTags();
         m_receiveBufferHandler->resume();

         //Lauch a new time the time out to detect connexion failure
         m_waitForAnswerTimer->start();

         break;
      }
      case yApi::IYPluginApi::kEventUpdateConfiguration:
      {
         api->setPluginState(yApi::historization::EPluginState::kCustom, "updateConfiguration");
         onUpdateConfiguration(api, api->getEventHandler().getEventData<shared::CDataContainer>());
         api->setPluginState(yApi::historization::EPluginState::kRunning);

         break;
      }
      case kErrorRetryTimer:
      {
         createConnection(api);
         break;
      }
      case kAnswerTimeout:
      {
         std::cerr << "No answer received, try to reconnect in a while..." << std::endl;
         errorProcess(api);
         break;
      }
      default:
      {
         std::cerr << "Unknown message id" << std::endl;
         break;
      }
      }
   }
}