NRP Core  1.4.1
utils.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 // This file contains helper functions that are not classified in any of the more specific util files
23 
24 #ifndef UTILS_H
25 #define UTILS_H
26 
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/prctl.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <string.h>
34 #include <string>
35 #include <stdexcept>
36 #include <unistd.h>
37 #include <boost/python.hpp>
38 #include <boost/asio.hpp>
39 
49 inline int bindOrFindFreePort(std::string hostIpv4, int port = 0)
50 {
51  using namespace boost::asio;
52 
53  ip::address addressStruct;
54 
55  if(hostIpv4 == "localhost")
56  {
57  addressStruct = ip::address_v4::loopback();
58  }
59  else
60  {
61  addressStruct = ip::address::from_string(hostIpv4);
62  }
63 
64  io_service service;
65  ip::tcp::socket socket(service);
66  socket.open(ip::tcp::v4());
67  socket.bind(ip::tcp::endpoint(addressStruct, port));
68 
69  int newPort = socket.local_endpoint().port();
70 
71  // Close the socket, so that the port can be used by the caller
72  socket.close();
73 
74  return newPort;
75 }
76 
84 inline void bindToAddress(std::string hostIpv4, int port)
85 { bindOrFindFreePort(hostIpv4, port); }
86 
94 inline int getFreePort(std::string hostIpv4)
95 { return bindOrFindFreePort(hostIpv4); }
96 
100 inline void appendPythonPath(const std::string &path)
101 {
102  boost::python::handle pathH(boost::python::borrowed(PySys_GetObject("path")));
103  boost::python::list paths(pathH);
104  paths.append(path);
105 
106  PySys_SetObject("path", paths.ptr());
107 }
108 
109 #endif // UTILS_H
getFreePort
int getFreePort(std::string hostIpv4)
Returns a free port number.
Definition: utils.h:94
bindOrFindFreePort
int bindOrFindFreePort(std::string hostIpv4, int port=0)
Attempts to bind to a given address.
Definition: utils.h:49
bindToAddress
void bindToAddress(std::string hostIpv4, int port)
Attempts to bind to a given address.
Definition: utils.h:84
python_json_engine.port
port
Definition: python_json_engine.py:197
appendPythonPath
void appendPythonPath(const std::string &path)
Appends 'path' to PYTHON_PATH env variable.
Definition: utils.h:100