Belle II Software  release-05-01-25
ZMQAddressUtils.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <framework/pcore/zmq/utils/ZMQAddressUtils.h>
11 #include <TRandom.h>
12 #include <algorithm>
13 #include <fstream>
14 
15 using namespace Belle2;
16 
17 namespace {
18  std::string randomName()
19  {
20  auto randchar = []() -> char {
21  const char charset[] = "abcdefghijklmnopqrstuvwxyz";
22  const size_t max_index = (sizeof(charset) - 1);
23  return charset[gRandom->Integer(max_index)];
24  };
25 
26  const unsigned int length = 4;
27  std::string str(length, 0);
28  std::generate_n(str.begin(), length, randchar);
29  return str;
30  }
31 
32  std::string randomPort()
33  {
34  // 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.
35  const unsigned int port = 5000 + gRandom->Integer(1000);
36  return std::to_string(port);
37  }
38 }
39 
40 std::string ZMQAddressUtils::randomSocketName(const std::string& hostname)
41 {
42  std::string socket_name = "tcp://" + hostname + ":" + randomPort();
43  return socket_name;
44 }
45 
47 {
48  std::string socket_name = "ipc://basf2_socket_" + randomName() + ".socket";
49  while (std::ifstream(socket_name)) {
50  socket_name = "ipc://basf2_socket_" + randomName() + ".socket";
51  }
52 
53  return socket_name;
54 }
55 
56 std::string ZMQAddressUtils::getSocketAddress(const std::string& socketAddress, ZMQAddressType socketPart)
57 {
58  const std::string& prefix = "tcp://";
59  if (socketAddress.compare(0, prefix.size(), prefix) == 0) {
60  if (socketPart == ZMQAddressType::c_input) {
61  return socketAddress + "0";
62  } else if (socketPart == ZMQAddressType::c_output) {
63  return socketAddress + "1";
64  } else if (socketPart == ZMQAddressType::c_pub) {
65  return socketAddress + "2";
66  } else if (socketPart == ZMQAddressType::c_sub) {
67  return socketAddress + "3";
68  } else if (socketPart == ZMQAddressType::c_control) {
69  return socketAddress + "4";
70  }
71  } else {
72  if (socketPart == ZMQAddressType::c_input) {
73  return socketAddress + "_input";
74  } else if (socketPart == ZMQAddressType::c_output) {
75  return socketAddress + "_output";
76  } else if (socketPart == ZMQAddressType::c_pub) {
77  return socketAddress + "_pub";
78  } else if (socketPart == ZMQAddressType::c_sub) {
79  return socketAddress + "_sub";
80  } else if (socketPart == ZMQAddressType::c_control) {
81  return socketAddress + "_control";
82  }
83  }
84  return socketAddress;
85 }
Belle2::c_output
@ c_output
Input socket.
Definition: ZMQAddressUtils.h:30
Belle2::c_sub
@ c_sub
Multicast publish socket.
Definition: ZMQAddressUtils.h:32
Belle2::ZMQAddressUtils::getSocketAddress
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.
Definition: ZMQAddressUtils.cc:56
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ZMQAddressType
ZMQAddressType
The type of a ZMQ socket address (which socket to use)
Definition: ZMQAddressUtils.h:28
Belle2::ZMQAddressUtils::randomSocketName
static std::string randomSocketName()
Generate a random socket name in the form ipc:///socketname.
Definition: ZMQAddressUtils.cc:46
Belle2::c_pub
@ c_pub
Output socket.
Definition: ZMQAddressUtils.h:31
Belle2::c_control
@ c_control
Multicast subscribe socket.
Definition: ZMQAddressUtils.h:33