Exemplo n.º 1
0
bool MessageRouter::registerHandler(
   MessageHandler& handler,
   const bool& isDefaultHandler)
{
   bool isRegistered = false;

   // Find an empty slot and store the handler.
   for (int i = 0; i < MAX_NUM_HANDLERS; i++)
   {
      if (handlers[i] == 0)
      {
         handlers[i] = &handler;
         isRegistered = true;
         break;
      }
   }

   // Log a failure to register.
   if (isRegistered == false)
   {
      Logger::logDebug("Failed to register handler " + handler.getId() + ".\n");
   }
   else
   {
      Logger::logDebug("Registered message handler " + handler.getId() + ".\n");

      if (isDefaultHandler)
      {
         defaultHandler = &handler;
         Logger::logDebug("Handler " + handler.getId() + " set as default handler.\n");
      }
   }

   return (isRegistered);
}
Exemplo n.º 2
0
void MessageRouter::handleMessages(
   Adapter& adapter)
{
   bool handledMessage = false;

   // Get the first available message.
   const Message* message = adapter.getMessage();

   if (message)
   {
      Logger::logDebug("MessageRouter::handleMessages: Got message " + toString(message->getMessageId()) + ".\n");
   }

   while (message)
   {
      MessageHandler* handler = getHandler(message->getDestination());

      if (!handler)
      {
         Logger::logDebug("Failed to handle message " + toString(message->getMessageId()) +
                          ". No handler specified for destination \"" + message->getDestination() + "\"\n");
      }
      else
      {
         // Allow the handler to handle the message.
         handledMessage = handler->handleMessage(*message);

         if (!handledMessage)
         {
            Logger::logDebug("Component \"" + handler->getId() + "\" failed to handle message " +
                             toString(message->getMessageId()) + ". Unsupported message.\n");
         }
      }

      // Free up the memory from the handled message.
      delete message;

      // Get the next available message.
      message = adapter.getMessage();

      if (message)
      {
         Logger::logDebug("MessageRouter::handleMessages: Got message " + toString(message->getMessageId()) + ".\n");
      }
   }
}