FairMQ: Extend Multipart and messaging API

- Extend the multipart API to allow sending vectors of messages or helper
   thin wrapper FairMQParts. See example in examples/MQ/8-multipart.
 - NewMessage() can be used in devices instead of
   fTransportFactory->CreateMessage().
   Possible arguments remain unchanged (no args, size or data+size).
 - Send()/Receive() methods can be used in devices instead of
   fChannels.at("chan").at(i).Send()/Receive():
   Send(msg, "chan", i = 0), Receive(msg, "chan", i = 0).
 - Use the new methods in MQ examples and tests.
 - No breaking changes, but FAIRMQ_INTERFACE_VERSION is incremented to 3
   to allow to check for new methods.
This commit is contained in:
Alexey Rybalchenko
2016-02-23 18:00:35 +01:00
parent 82ab7670a9
commit e1fef82657
33 changed files with 393 additions and 246 deletions

View File

@@ -451,55 +451,79 @@ int FairMQChannel::Send(const unique_ptr<FairMQMessage>& msg) const
return -2;
}
int FairMQChannel::SendAsync(const unique_ptr<FairMQMessage>& msg) const
uint64_t FairMQChannel::Send(const std::vector<std::unique_ptr<FairMQMessage>>& msgVec) const
{
return fSocket->Send(msg.get(), fNoBlockFlag);
// Sending vector typicaly handles more then one part
if (msgVec.size() > 1)
{
uint64_t totalSize = 0;
for (unsigned int i = 0; i < msgVec.size() - 1; ++i)
{
int nbytes = SendPart(msgVec[i]);
if (nbytes >= 0)
{
totalSize += nbytes;
}
else
{
return nbytes;
}
}
int n = Send(msgVec.back());
if (n >= 0)
{
totalSize += n;
}
else
{
return n;
}
return totalSize;
} // If there's only one part, send it as a regular message
else if (msgVec.size() == 1)
{
return Send(msgVec.back());
}
else // if the vector is empty, something might be wrong
{
LOG(WARN) << "Will not send empty vector";
return -1;
}
}
int FairMQChannel::SendPart(const unique_ptr<FairMQMessage>& msg) const
uint64_t FairMQChannel::Receive(std::vector<std::unique_ptr<FairMQMessage>>& msgVec) const
{
return fSocket->Send(msg.get(), fSndMoreFlag);
}
// Warn if the vector is filled before Receive() and empty it.
if (msgVec.size() > 0)
{
LOG(WARN) << "Message vector contains elements before Receive(), they will be deleted!";
msgVec.clear();
}
int FairMQChannel::SendPartAsync(const unique_ptr<FairMQMessage>& msg) const
{
return fSocket->Send(msg.get(), fSndMoreFlag|fNoBlockFlag);
}
uint64_t totalSize = 0;
// int FairMQChannel::SendParts(initializer_list<unique_ptr<FairMQMessage>> partsList) const
// {
// int totalSize = 0;
// initializer_list<unique_ptr<FairMQMessage>>::iterator it = partsList.end();
// auto &last = --it;
// for (auto &p : partsList)
// {
// if (&p != last)
// {
// int nbytes = SendPart(p);
// if (nbytes >= 0)
// {
// totalSize += nbytes;
// }
// else
// {
// return nbytes;
// }
// }
// else
// {
// int nbytes = Send(p);
// if (nbytes >= 0)
// {
// totalSize += nbytes;
// }
// else
// {
// return nbytes;
// }
// }
// }
// return totalSize;
// }
do
{
std::unique_ptr<FairMQMessage> part(fTransportFactory->CreateMessage());
int nbytes = Receive(part);
if (nbytes >= 0)
{
msgVec.push_back(std::move(part));
totalSize += nbytes;
}
else
{
return nbytes;
}
}
while (ExpectsAnotherPart());
return totalSize;
}
int FairMQChannel::Receive(const unique_ptr<FairMQMessage>& msg) const
{
@@ -519,11 +543,6 @@ int FairMQChannel::Receive(const unique_ptr<FairMQMessage>& msg) const
return -2;
}
int FairMQChannel::ReceiveAsync(const unique_ptr<FairMQMessage>& msg) const
{
return fSocket->Receive(msg.get(), fNoBlockFlag);
}
int FairMQChannel::Send(FairMQMessage* msg, const string& flag) const
{
if (flag == "")
@@ -624,64 +643,6 @@ int FairMQChannel::Receive(FairMQMessage* msg, const int flags) const
}
}
void FairMQChannel::SetSendTimeout(const int timeout)
{
// if (fSocket)
// {
// if (fSocket->SetSendTimeout(timeout, fAddress, fMethod))
// {
fSndTimeoutInMs = timeout;
// return true;
// }
// }
// LOG(ERROR) << "SetSendTimeout() failed - socket is not initialized!";
// return false;
}
int FairMQChannel::GetSendTimeout() const
{
return fSndTimeoutInMs;
// if (fSocket)
// {
// return fSocket->GetSendTimeout();
// }
// else
// {
// LOG(ERROR) << "GetSendTimeout() failed - socket is not initialized!";
// return -1;
// }
}
void FairMQChannel::SetReceiveTimeout(const int timeout)
{
// if (fSocket)
// {
// if (fSocket->SetReceiveTimeout(timeout, fAddress, fMethod))
// {
fRcvTimeoutInMs = timeout;
// return true;
// }
// }
// LOG(ERROR) << "SetReceiveTimeout() failed - socket is not initialized!";
// return false;
}
int FairMQChannel::GetReceiveTimeout() const
{
return fRcvTimeoutInMs;
// if (fSocket)
// {
// return fSocket->GetReceiveTimeout();
// }
// else
// {
// LOG(ERROR) << "GetReceiveTimeout() failed - socket is not initialized!";
// return -1;
// }
}
bool FairMQChannel::ExpectsAnotherPart() const
{
int64_t more = 0;