Belle II Software light-2406-ragdoll
ZMQAddressUtils.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/pcore/zmq/utils/ZMQAddressUtils.h>
9#include <TRandom.h>
10#include <algorithm>
11#include <fstream>
12
13using namespace Belle2;
14
15namespace {
16 std::string randomName()
17 {
18 auto randchar = []() -> char {
19 const char charset[] = "abcdefghijklmnopqrstuvwxyz";
20 const size_t max_index = (sizeof(charset) - 1);
21 return charset[gRandom->Integer(max_index)];
22 };
23
24 const unsigned int length = 4;
25 std::string str(length, 0);
26 std::generate_n(str.begin(), length, randchar);
27 return str;
28 }
29
30 std::string randomPort()
31 {
32 // Port should be in the range of 50000 to 60000. As the last digit will be added by the getSocketAddress, we will not do it here.
33 const unsigned int port = 5000 + gRandom->Integer(1000);
34 return std::to_string(port);
35 }
36}
37
38std::string ZMQAddressUtils::randomSocketName(const std::string& hostname)
39{
40 std::string socket_name = "tcp://" + hostname + ":" + randomPort();
41 return socket_name;
42}
43
45{
46 // std::string socket_name = "ipc://basf2_socket_" + randomName() + ".socket";
47 std::string socket_name = "ipc:///tmp/basf2_socket_" + randomName() + ".socket";
48 while (std::ifstream(socket_name)) {
49 socket_name = "ipc://basf2_socket_" + randomName() + ".socket";
50 }
51
52 return socket_name;
53}
54
55std::string ZMQAddressUtils::getSocketAddress(const std::string& socketAddress, ZMQAddressType socketPart)
56{
57 const std::string& prefix = "tcp://";
58 if (socketAddress.compare(0, prefix.size(), prefix) == 0) {
59 if (socketPart == ZMQAddressType::c_input) {
60 return socketAddress + "0";
61 } else if (socketPart == ZMQAddressType::c_output) {
62 return socketAddress + "1";
63 } else if (socketPart == ZMQAddressType::c_pub) {
64 return socketAddress + "2";
65 } else if (socketPart == ZMQAddressType::c_sub) {
66 return socketAddress + "3";
67 } else if (socketPart == ZMQAddressType::c_control) {
68 return socketAddress + "4";
69 }
70 } else {
71 if (socketPart == ZMQAddressType::c_input) {
72 return socketAddress + "_input";
73 } else if (socketPart == ZMQAddressType::c_output) {
74 return socketAddress + "_output";
75 } else if (socketPart == ZMQAddressType::c_pub) {
76 return socketAddress + "_pub";
77 } else if (socketPart == ZMQAddressType::c_sub) {
78 return socketAddress + "_sub";
79 } else if (socketPart == ZMQAddressType::c_control) {
80 return socketAddress + "_control";
81 }
82 }
83 return socketAddress;
84}
static std::string randomSocketName()
Generate a random socket name in the form ipc:///socketname.
static std::string getSocketAddress(const std::string &socketAddress, ZMQAddressType socketPart)
Create a full socket address for the given type from a random socket address, ba adding a suffix.
ZMQAddressType
The type of a ZMQ socket address (which socket to use)
@ c_sub
Multicast publish socket.
@ c_control
Multicast subscribe socket.
@ c_pub
Output socket.
@ c_output
Input socket.
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24