mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
Rename /example to /examples and move MQ examples in it
This commit is contained in:
85
examples/MQ/5-req-rep/CMakeLists.txt
Normal file
85
examples/MQ/5-req-rep/CMakeLists.txt
Normal file
@@ -0,0 +1,85 @@
|
||||
################################################################################
|
||||
# Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
|
||||
# #
|
||||
# This software is distributed under the terms of the #
|
||||
# GNU Lesser General Public Licence version 3 (LGPL) version 3, #
|
||||
# copied verbatim in the file "LICENSE" #
|
||||
################################################################################
|
||||
|
||||
Set(INCLUDE_DIRECTORIES
|
||||
${CMAKE_SOURCE_DIR}/fairmq
|
||||
${CMAKE_SOURCE_DIR}/fairmq/devices
|
||||
${CMAKE_SOURCE_DIR}/fairmq/tools
|
||||
${CMAKE_SOURCE_DIR}/fairmq/options
|
||||
${CMAKE_SOURCE_DIR}/examples/MQ/5-req-rep
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
Set(SYSTEM_INCLUDE_DIRECTORIES
|
||||
${Boost_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
If(NANOMSG_FOUND)
|
||||
Set(INCLUDE_DIRECTORIES
|
||||
${INCLUDE_DIRECTORIES}
|
||||
${CMAKE_SOURCE_DIR}/fairmq/nanomsg
|
||||
)
|
||||
Set(SYSTEM_INCLUDE_DIRECTORIES
|
||||
${SYSTEM_INCLUDE_DIRECTORIES}
|
||||
${ZMQ_INCLUDE_DIR}
|
||||
)
|
||||
Else(NANOMSG_FOUND)
|
||||
Set(INCLUDE_DIRECTORIES
|
||||
${INCLUDE_DIRECTORIES}
|
||||
${CMAKE_SOURCE_DIR}/fairmq/zeromq
|
||||
)
|
||||
Set(SYSTEM_INCLUDE_DIRECTORIES
|
||||
${SYSTEM_INCLUDE_DIRECTORIES}
|
||||
${ZMQ_INCLUDE_DIR}
|
||||
)
|
||||
EndIf(NANOMSG_FOUND)
|
||||
|
||||
Include_Directories(${INCLUDE_DIRECTORIES})
|
||||
Include_Directories(SYSTEM ${SYSTEM_INCLUDE_DIRECTORIES})
|
||||
|
||||
Set(LINK_DIRECTORIES
|
||||
${Boost_LIBRARY_DIRS}
|
||||
)
|
||||
|
||||
Link_Directories(${LINK_DIRECTORIES})
|
||||
|
||||
set(SRCS
|
||||
"FairMQExample5Client.cxx"
|
||||
"FairMQExample5Server.cxx"
|
||||
)
|
||||
|
||||
set(DEPENDENCIES
|
||||
${DEPENDENCIES}
|
||||
FairMQ
|
||||
)
|
||||
|
||||
set(LIBRARY_NAME FairMQExample5)
|
||||
|
||||
GENERATE_LIBRARY()
|
||||
|
||||
set(Exe_Names
|
||||
ex5-client
|
||||
ex5-server
|
||||
)
|
||||
|
||||
set(Exe_Source
|
||||
runExample5Client.cxx
|
||||
runExample5Server.cxx
|
||||
)
|
||||
|
||||
list(LENGTH Exe_Names _length)
|
||||
math(EXPR _length ${_length}-1)
|
||||
|
||||
ForEach(_file RANGE 0 ${_length})
|
||||
list(GET Exe_Names ${_file} _name)
|
||||
list(GET Exe_Source ${_file} _src)
|
||||
set(EXE_NAME ${_name})
|
||||
set(SRCS ${_src})
|
||||
set(DEPENDENCIES FairMQExample5)
|
||||
GENERATE_EXECUTABLE()
|
||||
EndForEach(_file RANGE 0 ${_length})
|
103
examples/MQ/5-req-rep/FairMQExample5Client.cxx
Normal file
103
examples/MQ/5-req-rep/FairMQExample5Client.cxx
Normal file
@@ -0,0 +1,103 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* FairMQExample5Client.cpp
|
||||
*
|
||||
* @since 2014-10-10
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
#include "FairMQExample5Client.h"
|
||||
#include "FairMQLogger.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
FairMQExample5Client::FairMQExample5Client()
|
||||
: fText()
|
||||
{
|
||||
}
|
||||
|
||||
FairMQExample5Client::~FairMQExample5Client()
|
||||
{
|
||||
}
|
||||
|
||||
void FairMQExample5Client::CustomCleanup(void *data, void *hint)
|
||||
{
|
||||
delete (string*)hint;
|
||||
}
|
||||
|
||||
void FairMQExample5Client::Run()
|
||||
{
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
|
||||
|
||||
string* text = new string(fText);
|
||||
|
||||
unique_ptr<FairMQMessage> request(fTransportFactory->CreateMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text));
|
||||
unique_ptr<FairMQMessage> reply(fTransportFactory->CreateMessage());
|
||||
|
||||
LOG(INFO) << "Sending \"" << fText << "\" to server.";
|
||||
|
||||
if (fChannels.at("data").at(0).Send(request) > 0)
|
||||
{
|
||||
if (fChannels.at("data").at(0).Receive(reply) >= 0)
|
||||
{
|
||||
LOG(INFO) << "Received reply from server: \"" << string(static_cast<char*>(reply->GetData()), reply->GetSize()) << "\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FairMQExample5Client::SetProperty(const int key, const string& value)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case Text:
|
||||
fText = value;
|
||||
break;
|
||||
default:
|
||||
FairMQDevice::SetProperty(key, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
string FairMQExample5Client::GetProperty(const int key, const string& default_ /*= ""*/)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case Text:
|
||||
return fText;
|
||||
break;
|
||||
default:
|
||||
return FairMQDevice::GetProperty(key, default_);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQExample5Client::SetProperty(const int key, const int value)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
default:
|
||||
FairMQDevice::SetProperty(key, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int FairMQExample5Client::GetProperty(const int key, const int default_ /*= 0*/)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
default:
|
||||
return FairMQDevice::GetProperty(key, default_);
|
||||
}
|
||||
}
|
46
examples/MQ/5-req-rep/FairMQExample5Client.h
Normal file
46
examples/MQ/5-req-rep/FairMQExample5Client.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* FairMQExample5Client.h
|
||||
*
|
||||
* @since 2014-10-10
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#ifndef FAIRMQEXAMPLE5CLIENT_H_
|
||||
#define FAIRMQEXAMPLE5CLIENT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "FairMQDevice.h"
|
||||
|
||||
class FairMQExample5Client : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
Text = FairMQDevice::Last,
|
||||
Last
|
||||
};
|
||||
FairMQExample5Client();
|
||||
virtual ~FairMQExample5Client();
|
||||
|
||||
static void CustomCleanup(void* data, void* hint);
|
||||
|
||||
virtual void SetProperty(const int key, const std::string& value);
|
||||
virtual std::string GetProperty(const int key, const std::string& default_ = "");
|
||||
virtual void SetProperty(const int key, const int value);
|
||||
virtual int GetProperty(const int key, const int default_ = 0);
|
||||
|
||||
protected:
|
||||
std::string fText;
|
||||
|
||||
virtual void Run();
|
||||
};
|
||||
|
||||
#endif /* FAIRMQEXAMPLECLIENT_H_ */
|
55
examples/MQ/5-req-rep/FairMQExample5Server.cxx
Normal file
55
examples/MQ/5-req-rep/FairMQExample5Server.cxx
Normal file
@@ -0,0 +1,55 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* FairMQExample5Server.cxx
|
||||
*
|
||||
* @since 2014-10-10
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
#include "FairMQExample5Server.h"
|
||||
#include "FairMQLogger.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
FairMQExample5Server::FairMQExample5Server()
|
||||
{
|
||||
}
|
||||
|
||||
void FairMQExample5Server::CustomCleanup(void *data, void *hint)
|
||||
{
|
||||
delete (string*)hint;
|
||||
}
|
||||
|
||||
void FairMQExample5Server::Run()
|
||||
{
|
||||
while (CheckCurrentState(RUNNING))
|
||||
{
|
||||
unique_ptr<FairMQMessage> request(fTransportFactory->CreateMessage());
|
||||
|
||||
if (fChannels.at("data").at(0).Receive(request) >= 0)
|
||||
{
|
||||
LOG(INFO) << "Received request from client: \"" << string(static_cast<char*>(request->GetData()), request->GetSize()) << "\"";
|
||||
|
||||
string* text = new string("Thank you for the \"" + string(static_cast<char*>(request->GetData()), request->GetSize()) + "\"!");
|
||||
|
||||
LOG(INFO) << "Sending reply to client.";
|
||||
|
||||
unique_ptr<FairMQMessage> reply(fTransportFactory->CreateMessage(const_cast<char*>(text->c_str()), text->length(), CustomCleanup, text));
|
||||
|
||||
fChannels.at("data").at(0).Send(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FairMQExample5Server::~FairMQExample5Server()
|
||||
{
|
||||
}
|
32
examples/MQ/5-req-rep/FairMQExample5Server.h
Normal file
32
examples/MQ/5-req-rep/FairMQExample5Server.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* FairMQExample5Server.h
|
||||
*
|
||||
* @since 2014-10-10
|
||||
* @author A. Rybalchenko
|
||||
*/
|
||||
|
||||
#ifndef FAIRMQEXAMPLE5SERVER_H_
|
||||
#define FAIRMQEXAMPLE5SERVER_H_
|
||||
|
||||
#include "FairMQDevice.h"
|
||||
|
||||
class FairMQExample5Server : public FairMQDevice
|
||||
{
|
||||
public:
|
||||
FairMQExample5Server();
|
||||
virtual ~FairMQExample5Server();
|
||||
|
||||
static void CustomCleanup(void *data, void* hint);
|
||||
|
||||
protected:
|
||||
virtual void Run();
|
||||
};
|
||||
|
||||
#endif /* FAIRMQEXAMPLE5SERVER_H_ */
|
4
examples/MQ/5-req-rep/README.md
Normal file
4
examples/MQ/5-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.
|
114
examples/MQ/5-req-rep/runExample5Client.cxx
Normal file
114
examples/MQ/5-req-rep/runExample5Client.cxx
Normal file
@@ -0,0 +1,114 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* runExampleClient.cxx
|
||||
*
|
||||
* @since 2013-04-23
|
||||
* @author D. Klein, A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/program_options.hpp"
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQExample5Client.h"
|
||||
|
||||
#ifdef NANOMSG
|
||||
#include "FairMQTransportFactoryNN.h"
|
||||
#else
|
||||
#include "FairMQTransportFactoryZMQ.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct DeviceOptions
|
||||
{
|
||||
DeviceOptions() :
|
||||
text() {}
|
||||
|
||||
string text;
|
||||
} DeviceOptions_t;
|
||||
|
||||
inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
|
||||
{
|
||||
if (_options == NULL)
|
||||
throw runtime_error("Internal error: options' container is empty.");
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
bpo::options_description desc("Options");
|
||||
desc.add_options()
|
||||
("text,t", bpo::value<string>()->default_value("something"), "Text to send to server")
|
||||
("help", "Print help messages");
|
||||
|
||||
bpo::variables_map vm;
|
||||
bpo::store(bpo::parse_command_line(_argc, _argv, desc), vm);
|
||||
|
||||
if (vm.count("help"))
|
||||
{
|
||||
LOG(INFO) << "EPN" << endl << desc;
|
||||
return false;
|
||||
}
|
||||
|
||||
bpo::notify(vm);
|
||||
|
||||
if ( vm.count("text") )
|
||||
_options->text = vm["text"].as<string>();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
FairMQExample5Client client;
|
||||
client.CatchSignals();
|
||||
|
||||
DeviceOptions_t options;
|
||||
try
|
||||
{
|
||||
if (!parse_cmd_line(argc, argv, &options))
|
||||
return 0;
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
LOG(ERROR) << e.what();
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG(INFO) << "PID: " << getpid();
|
||||
|
||||
#ifdef NANOMSG
|
||||
FairMQTransportFactory* transportFactory = new FairMQTransportFactoryNN();
|
||||
#else
|
||||
FairMQTransportFactory* transportFactory = new FairMQTransportFactoryZMQ();
|
||||
#endif
|
||||
|
||||
client.SetTransport(transportFactory);
|
||||
|
||||
client.SetProperty(FairMQExample5Client::Id, "client");
|
||||
client.SetProperty(FairMQExample5Client::Text, options.text);
|
||||
client.SetProperty(FairMQExample5Client::NumIoThreads, 1);
|
||||
|
||||
FairMQChannel requestChannel("req", "connect", "tcp://localhost:5005");
|
||||
requestChannel.UpdateSndBufSize(10000);
|
||||
requestChannel.UpdateRcvBufSize(10000);
|
||||
requestChannel.UpdateRateLogging(1);
|
||||
|
||||
client.fChannels["data"].push_back(requestChannel);
|
||||
|
||||
client.ChangeState("INIT_DEVICE");
|
||||
client.WaitForEndOfState("INIT_DEVICE");
|
||||
|
||||
client.ChangeState("INIT_TASK");
|
||||
client.WaitForEndOfState("INIT_TASK");
|
||||
|
||||
client.ChangeState("RUN");
|
||||
client.InteractiveStateLoop();
|
||||
|
||||
return 0;
|
||||
}
|
63
examples/MQ/5-req-rep/runExample5Server.cxx
Normal file
63
examples/MQ/5-req-rep/runExample5Server.cxx
Normal file
@@ -0,0 +1,63 @@
|
||||
/********************************************************************************
|
||||
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||
* *
|
||||
* This software is distributed under the terms of the *
|
||||
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
/**
|
||||
* runExampleServer.cxx
|
||||
*
|
||||
* @since 2013-04-23
|
||||
* @author D. Klein, A. Rybalchenko
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQExample5Server.h"
|
||||
|
||||
#ifdef NANOMSG
|
||||
#include "FairMQTransportFactoryNN.h"
|
||||
#else
|
||||
#include "FairMQTransportFactoryZMQ.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
FairMQExample5Server server;
|
||||
server.CatchSignals();
|
||||
|
||||
LOG(INFO) << "PID: " << getpid();
|
||||
|
||||
#ifdef NANOMSG
|
||||
FairMQTransportFactory* transportFactory = new FairMQTransportFactoryNN();
|
||||
#else
|
||||
FairMQTransportFactory* transportFactory = new FairMQTransportFactoryZMQ();
|
||||
#endif
|
||||
|
||||
server.SetTransport(transportFactory);
|
||||
|
||||
server.SetProperty(FairMQExample5Server::Id, "server");
|
||||
server.SetProperty(FairMQExample5Server::NumIoThreads, 1);
|
||||
|
||||
FairMQChannel replyChannel("rep", "bind", "tcp://*:5005");
|
||||
replyChannel.UpdateSndBufSize(10000);
|
||||
replyChannel.UpdateRcvBufSize(10000);
|
||||
replyChannel.UpdateRateLogging(1);
|
||||
|
||||
server.fChannels["data"].push_back(replyChannel);
|
||||
|
||||
server.ChangeState("INIT_DEVICE");
|
||||
server.WaitForEndOfState("INIT_DEVICE");
|
||||
|
||||
server.ChangeState("INIT_TASK");
|
||||
server.WaitForEndOfState("INIT_TASK");
|
||||
|
||||
server.ChangeState("RUN");
|
||||
server.InteractiveStateLoop();
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user