mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
Refactor the examples after move from FairRoot
This commit is contained in:
committed by
Mohammad Al-Turany
parent
31cba0515e
commit
bab7e13737
51
examples/req-rep/CMakeLists.txt
Normal file
51
examples/req-rep/CMakeLists.txt
Normal file
@@ -0,0 +1,51 @@
|
||||
################################################################################
|
||||
# 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" #
|
||||
################################################################################
|
||||
|
||||
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-server runServer.cxx)
|
||||
target_link_libraries(fairmq-ex-req-rep-server PRIVATE ExampleReqRepLib)
|
||||
|
||||
add_custom_target(ExampleReqRep DEPENDS fairmq-ex-req-rep-client fairmq-ex-req-rep-server)
|
||||
|
||||
install(
|
||||
TARGETS
|
||||
fairmq-ex-req-rep-client
|
||||
fairmq-ex-req-rep-server
|
||||
|
||||
LIBRARY DESTINATION ${PROJECT_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${PROJECT_INSTALL_BINDIR}
|
||||
)
|
||||
|
||||
# configure run script with different executable paths for build and for install directories
|
||||
set(EX_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/fairmq-start-ex-req-rep.sh.in ${CMAKE_CURRENT_BINARY_DIR}/fairmq-start-ex-req-rep.sh)
|
||||
set(EX_BIN_DIR ${CMAKE_INSTALL_PREFIX}/${PROJECT_INSTALL_BINDIR})
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/fairmq-start-ex-req-rep.sh.in ${CMAKE_CURRENT_BINARY_DIR}/fairmq-start-ex-req-rep.sh_install)
|
||||
|
||||
install(
|
||||
PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/fairmq-start-ex-req-rep.sh_install
|
||||
DESTINATION ${PROJECT_INSTALL_BINDIR}
|
||||
RENAME fairmq-start-ex-req-rep.sh
|
||||
)
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test-ex-req-rep.sh.in ${CMAKE_CURRENT_BINARY_DIR}/test-ex-req-rep.sh)
|
||||
|
||||
add_test(NAME Example-ReqRep COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test-ex-req-rep.sh)
|
||||
set_tests_properties(Example-ReqRep PROPERTIES TIMEOUT "30" RUN_SERIAL true PASS_REGULAR_EXPRESSION "Received reply from server: ")
|
80
examples/req-rep/Client.cxx
Normal file
80
examples/req-rep/Client.cxx
Normal file
@@ -0,0 +1,80 @@
|
||||
/********************************************************************************
|
||||
* 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->GetValue<string>("text");
|
||||
fMaxIterations = fConfig->GetValue<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
|
42
examples/req-rep/Client.h
Normal file
42
examples/req-rep/Client.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/********************************************************************************
|
||||
* 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 */
|
4
examples/req-rep/README.md
Normal file
4
examples/req-rep/README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
Example 5: Request & Reply
|
||||
===============
|
||||
|
||||
This topology contains two devices that communicate with each other via the **REQUEST-REPLY** pettern. Bidirectional communication via a single socket.
|
66
examples/req-rep/Server.cxx
Normal file
66
examples/req-rep/Server.cxx
Normal file
@@ -0,0 +1,66 @@
|
||||
/********************************************************************************
|
||||
* 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->GetValue<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
|
40
examples/req-rep/Server.h
Normal file
40
examples/req-rep/Server.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/********************************************************************************
|
||||
* 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 */
|
11
examples/req-rep/fairmq-start-ex-req-rep.sh.in
Executable file
11
examples/req-rep/fairmq-start-ex-req-rep.sh.in
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
CLIENT="fairmq-ex-req-rep-client"
|
||||
CLIENT+=" --id client"
|
||||
CLIENT+=" --channel-config name=data,type=req,method=connect,rateLogging=0,address=tcp://127.0.0.1:5005"
|
||||
xterm -geometry 80x23+0+0 -hold -e @EX_BIN_DIR@/$CLIENT &
|
||||
|
||||
SERVER="fairmq-ex-req-rep-server"
|
||||
SERVER+=" --id server"
|
||||
SERVER+=" --channel-config name=data,type=rep,method=bind,rateLogging=0,address=tcp://127.0.0.1:5005"
|
||||
xterm -geometry 80x23+500+0 -hold -e @EX_BIN_DIR@/$SERVER &
|
24
examples/req-rep/runClient.cxx
Normal file
24
examples/req-rep/runClient.cxx
Normal file
@@ -0,0 +1,24 @@
|
||||
/********************************************************************************
|
||||
* 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 FairMQProgOptions& /*config*/)
|
||||
{
|
||||
return new example_req_rep::Client();
|
||||
}
|
23
examples/req-rep/runServer.cxx
Normal file
23
examples/req-rep/runServer.cxx
Normal file
@@ -0,0 +1,23 @@
|
||||
/********************************************************************************
|
||||
* 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 FairMQProgOptions& /*config*/)
|
||||
{
|
||||
return new example_req_rep::Server();
|
||||
}
|
26
examples/req-rep/test-ex-req-rep.sh.in
Executable file
26
examples/req-rep/test-ex-req-rep.sh.in
Executable file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
# setup a trap to kill everything if the test fails/timeouts
|
||||
trap 'kill -TERM $CLIENT_PID; kill -TERM $SERVER_PID; wait $CLIENT_PID; wait $SERVER_PID;' TERM
|
||||
|
||||
CLIENT="fairmq-ex-req-rep-client"
|
||||
CLIENT+=" --id client"
|
||||
CLIENT+=" --verbosity veryhigh"
|
||||
CLIENT+=" --control static --color false"
|
||||
CLIENT+=" --max-iterations 1"
|
||||
CLIENT+=" --channel-config name=data,type=req,method=connect,rateLogging=0,address=tcp://127.0.0.1:5005"
|
||||
@CMAKE_CURRENT_BINARY_DIR@/$CLIENT &
|
||||
CLIENT_PID=$!
|
||||
|
||||
SERVER="fairmq-ex-req-rep-server"
|
||||
SERVER+=" --id server"
|
||||
SERVER+=" --verbosity veryhigh"
|
||||
SERVER+=" --control static --color false"
|
||||
SERVER+=" --max-iterations 1"
|
||||
SERVER+=" --channel-config name=data,type=rep,method=bind,rateLogging=0,address=tcp://127.0.0.1:5005"
|
||||
@CMAKE_CURRENT_BINARY_DIR@/$SERVER &
|
||||
SERVER_PID=$!
|
||||
|
||||
# wait for everything to finish
|
||||
wait $CLIENT_PID
|
||||
wait $SERVER_PID
|
Reference in New Issue
Block a user