示例#1
0
/**
  * Function called when data is recevied by the socket : The corresponding proto is created and the coresponding event is rised.
  */
void UDPListener::processPendingUnicastDatagrams()
{
   while (this->unicastSocket.hasPendingDatagrams())
   {
      QHostAddress peerAddress;
      const Common::MessageHeader& header =  UDPListener::readDatagramToBuffer(this->unicastSocket, peerAddress);
      if (header.isNull())
         continue;

      switch (header.getType())
      {
      case Common::MessageHeader::CORE_CHUNKS_OWNED:
         {
            Protos::Core::ChunksOwned chunksOwnedMessage;
            chunksOwnedMessage.ParseFromArray(this->bodyBuffer, header.getSize());

            if (chunksOwnedMessage.tag() != this->currentIMAliveTag)
            {
               L_WARN(QString("ChunksOwned : tag (%1) doesn't match current tag (%2)").arg(chunksOwnedMessage.tag()).arg(currentIMAliveTag));
               continue;
            }

            if (chunksOwnedMessage.chunk_state_size() != this->currentChunkDownloads.size())
            {
               L_WARN(QString("ChunksOwned : The size (%1) doesn't match the expected one (%2)").arg(chunksOwnedMessage.chunk_state_size()).arg(this->currentChunkDownloads.size()));
               continue;
            }

            for (int i = 0; i < chunksOwnedMessage.chunk_state_size(); i++)
            {
               if (chunksOwnedMessage.chunk_state(i))
                  this->currentChunkDownloads[i]->addPeerID(header.getSenderID());
               else
                  this->currentChunkDownloads[i]->rmPeerID(header.getSenderID());
            }
         }
         break;

      case Common::MessageHeader::CORE_FIND_RESULT:
         {
            Protos::Common::FindResult findResultMessage;
            findResultMessage.ParseFromArray(this->bodyBuffer, header.getSize());
            findResultMessage.mutable_peer_id()->set_hash(header.getSenderID().getData(), Common::Hash::HASH_SIZE);
            emit newFindResultMessage(findResultMessage);
         }
         break;

      default:
         L_WARN(QString("Unkown header type from unicast socket : %1").arg(header.getType(), 0, 16));
      }
   }
}
示例#2
0
/**
  * Function called when data is recevied by the socket : The corresponding proto is created and the coresponding event is rised.
  */
void UDPListener::processPendingUnicastDatagrams()
{
   while (this->unicastSocket.hasPendingDatagrams())
   {
      QHostAddress peerAddress;
      const Common::MessageHeader& header =  UDPListener::readDatagramToBuffer(this->unicastSocket, peerAddress);
      if (header.isNull())
         continue;

      try
      {
         const Common::Message& message = Common::Message::readMessageBody(header, this->bodyBuffer);

         switch (header.getType())
         {
         case Common::MessageHeader::CORE_CHUNKS_OWNED:
            {
               const Protos::Core::ChunksOwned& chunksOwnedMessage = message.getMessage<Protos::Core::ChunksOwned>();

               if (chunksOwnedMessage.tag() != this->currentIMAliveTag)
               {
                  L_WARN(QString("ChunksOwned : tag (%1) doesn't match current tag (%2)").arg(chunksOwnedMessage.tag()).arg(currentIMAliveTag));
                  continue;
               }

               if (chunksOwnedMessage.chunk_state_size() != this->currentChunkDownloaders.size())
               {
                  L_WARN(QString("ChunksOwned : The size (%1) doesn't match the expected one (%2)").arg(chunksOwnedMessage.chunk_state_size()).arg(this->currentChunkDownloaders.size()));
                  continue;
               }

               for (int i = 0; i < chunksOwnedMessage.chunk_state_size(); i++)
                  if (PM::IPeer* peer = this->peerManager->getPeer(header.getSenderID()))
                  {
                     if (chunksOwnedMessage.chunk_state(i))
                        this->currentChunkDownloaders[i]->addPeer(peer);
                     else
                        this->currentChunkDownloaders[i]->rmPeer(peer);
                  }
            }
            break;

         case Common::MessageHeader::CORE_FIND_RESULT:
            {
               Protos::Common::FindResult findResultMessage = message.getMessage<Protos::Common::FindResult>();
               findResultMessage.mutable_peer_id()->set_hash(header.getSenderID().getData(), Common::Hash::HASH_SIZE);
               emit newFindResultMessage(findResultMessage);
            }
            break;

         default:; // Ignore other messages.
         }

         emit received(message);
      }
      catch(Common::ReadErrorException&)
      {
         L_WARN(QString("Unable to read an unicast message from peer %1 %2").arg(header.getSenderID().toStr()).arg(peerAddress.toString()));
      }
   }
}