reduce noise in examples

This commit is contained in:
Alexey Rybalchenko
2021-05-13 22:22:49 +02:00
committed by Dennis Klein
parent a7dbeadd1c
commit aaf74ad93f
119 changed files with 1766 additions and 3143 deletions

View File

@@ -6,21 +6,12 @@
# copied verbatim in the file "LICENSE" #
################################################################################
add_library(ExampleReqRepLib STATIC
"Client.cxx"
"Client.h"
"Server.cxx"
"Server.h"
)
target_link_libraries(ExampleReqRepLib PUBLIC FairMQ)
add_executable(fairmq-ex-req-rep-client runClient.cxx)
target_link_libraries(fairmq-ex-req-rep-client PRIVATE ExampleReqRepLib)
add_executable(fairmq-ex-req-rep-client client.cxx)
target_link_libraries(fairmq-ex-req-rep-client PRIVATE FairMQ)
add_executable(fairmq-ex-req-rep-server runServer.cxx)
target_link_libraries(fairmq-ex-req-rep-server PRIVATE ExampleReqRepLib)
add_executable(fairmq-ex-req-rep-server server.cxx)
target_link_libraries(fairmq-ex-req-rep-server PRIVATE FairMQ)
add_custom_target(ExampleReqRep DEPENDS fairmq-ex-req-rep-client fairmq-ex-req-rep-server)

View File

@@ -1,80 +0,0 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* Client.cpp
*
* @since 2014-10-10
* @author A. Rybalchenko
*/
#include <thread> // this_thread::sleep_for
#include <chrono>
#include "Client.h"
using namespace std;
namespace example_req_rep
{
Client::Client()
: fText()
, fMaxIterations(0)
, fNumIterations(0)
{
}
void Client::InitTask()
{
fText = fConfig->GetProperty<string>("text");
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
}
bool Client::ConditionalRun()
{
string* text = new string(fText);
// create message object with a pointer to the data buffer,
// its size,
// custom deletion function (called when transfer is done),
// and pointer to the object managing the data buffer
FairMQMessagePtr req(NewMessage(const_cast<char*>(text->c_str()), // data
text->length(), // size
[](void* /*data*/, void* object) { delete static_cast<string*>(object); }, // deletion callback
text)); // object that manages the data
FairMQMessagePtr rep(NewMessage());
LOG(info) << "Sending \"" << fText << "\" to server.";
if (Send(req, "data") > 0)
{
if (Receive(rep, "data") >= 0)
{
LOG(info) << "Received reply from server: \"" << string(static_cast<char*>(rep->GetData()), rep->GetSize()) << "\"";
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations)
{
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
}
this_thread::sleep_for(chrono::seconds(1));
return true;
}
}
return false;
}
Client::~Client()
{
}
} // namespace example_req_rep

View File

@@ -1,42 +0,0 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* Client.h
*
* @since 2014-10-10
* @author A. Rybalchenko
*/
#ifndef FAIRMQEXAMPLEREQREPCLIENT_H
#define FAIRMQEXAMPLEREQREPCLIENT_H
#include <string>
#include "FairMQDevice.h"
namespace example_req_rep
{
class Client : public FairMQDevice
{
public:
Client();
virtual ~Client();
protected:
std::string fText;
uint64_t fMaxIterations;
uint64_t fNumIterations;
virtual bool ConditionalRun();
virtual void InitTask();
};
} // namespace example_req_rep
#endif /* FAIRMQEXAMPLEREQREPCLIENT_H */

View File

@@ -1,66 +0,0 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* Server.cxx
*
* @since 2014-10-10
* @author A. Rybalchenko
*/
#include "Server.h"
using namespace std;
namespace example_req_rep
{
Server::Server()
: fMaxIterations(0)
, fNumIterations(0)
{
OnData("data", &Server::HandleData);
}
void Server::InitTask()
{
// Get the fMaxIterations value from the command line options (via fConfig)
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
}
bool Server::HandleData(FairMQMessagePtr& req, int /*index*/)
{
LOG(info) << "Received request from client: \"" << string(static_cast<char*>(req->GetData()), req->GetSize()) << "\"";
string* text = new string("Thank you for the \"" + string(static_cast<char*>(req->GetData()), req->GetSize()) + "\"!");
LOG(info) << "Sending reply to client.";
FairMQMessagePtr rep(NewMessage(const_cast<char*>(text->c_str()), // data
text->length(), // size
[](void* /*data*/, void* object) { delete static_cast<string*>(object); }, // deletion callback
text)); // object that manages the data
if (Send(rep, "data") > 0)
{
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations)
{
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
}
return true;
}
return false;
}
Server::~Server()
{
}
} // namespace example_req_rep

View File

@@ -1,40 +0,0 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
/**
* Server.h
*
* @since 2014-10-10
* @author A. Rybalchenko
*/
#ifndef FAIRMQEXAMPLEREQREPSERVER_H
#define FAIRMQEXAMPLEREQREPSERVER_H
#include "FairMQDevice.h"
namespace example_req_rep
{
class Server : public FairMQDevice
{
public:
Server();
virtual ~Server();
protected:
virtual void InitTask();
bool HandleData(FairMQMessagePtr&, int);
private:
uint64_t fMaxIterations;
uint64_t fNumIterations;
};
} // namespace example_req_rep
#endif /* FAIRMQEXAMPLEREQREPSERVER_H */

View File

@@ -0,0 +1,83 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <fairmq/Device.h>
#include <fairmq/runDevice.h>
#include <chrono>
#include <string>
#include <thread> // this_thread::sleep_for
namespace bpo = boost::program_options;
class Client : public FairMQDevice
{
public:
Client()
: fMaxIterations(0)
, fNumIterations(0)
{}
void InitTask() override
{
fText = fConfig->GetProperty<std::string>("text");
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
}
bool ConditionalRun() override
{
std::string* text = new std::string(fText);
// create message object with a pointer to the data buffer,
// its size,
// custom deletion function (called when transfer is done),
// and pointer to the object managing the data buffer
FairMQMessagePtr req(NewMessage(const_cast<char*>(text->c_str()), // data
text->length(), // size
[](void* /*data*/, void* object) { delete static_cast<std::string*>(object); }, // deletion callback
text)); // object that manages the data
FairMQMessagePtr rep(NewMessage());
LOG(info) << "Sending \"" << fText << "\" to server.";
if (Send(req, "data") > 0) {
if (Receive(rep, "data") >= 0) {
LOG(info) << "Received reply from server: \"" << std::string(static_cast<char*>(rep->GetData()), rep->GetSize()) << "\"";
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) {
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
return true;
}
}
return false;
}
private:
std::string fText;
uint64_t fMaxIterations;
uint64_t fNumIterations;
};
void addCustomOptions(bpo::options_description& options)
{
options.add_options()
("text", bpo::value<std::string>()->default_value("Hello"), "Text to send out")
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
std::unique_ptr<fair::mq::Device> getDevice(fair::mq::ProgOptions& /*config*/)
{
return std::make_unique<Client>();
}

View File

@@ -1,24 +0,0 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include "runFairMQDevice.h"
#include "Client.h"
namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& options)
{
options.add_options()
("text", bpo::value<std::string>()->default_value("Hello"), "Text to send out")
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/)
{
return new example_req_rep::Client();
}

View File

@@ -1,23 +0,0 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include "runFairMQDevice.h"
#include "Server.h"
namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& options)
{
options.add_options()
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/)
{
return new example_req_rep::Server();
}

View File

@@ -0,0 +1,72 @@
/********************************************************************************
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <fairmq/Device.h>
#include <fairmq/runDevice.h>
#include <string>
namespace bpo = boost::program_options;
class Server : public FairMQDevice
{
public:
Server()
: fMaxIterations(0)
, fNumIterations(0)
{
OnData("data", &Server::HandleData);
}
protected:
void InitTask() override
{
// Get the fMaxIterations value from the command line options (via fConfig)
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
}
bool HandleData(FairMQMessagePtr& req, int)
{
LOG(info) << "Received request from client: \"" << std::string(static_cast<char*>(req->GetData()), req->GetSize()) << "\"";
std::string* text = new std::string("Thank you for the \"" + std::string(static_cast<char*>(req->GetData()), req->GetSize()) + "\"!");
LOG(info) << "Sending reply to client.";
FairMQMessagePtr rep(NewMessage(const_cast<char*>(text->c_str()), // data
text->length(), // size
[](void* /*data*/, void* object) { delete static_cast<std::string*>(object); }, // deletion callback
text)); // object that manages the data
if (Send(rep, "data") > 0) {
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) {
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
}
return true;
}
return false;
}
private:
uint64_t fMaxIterations;
uint64_t fNumIterations;
};
void addCustomOptions(bpo::options_description& options)
{
options.add_options()
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
std::unique_ptr<fair::mq::Device> getDevice(fair::mq::ProgOptions& /*config*/)
{
return std::make_unique<Server>();
}