Project
Loading...
Searching...
No Matches
FreePortFinder.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
12#include "Framework/Logger.h"
13
14#include <sys/socket.h>
15#include <netinet/in.h>
16#include <unistd.h>
17#include <cstring>
18
19namespace o2
20{
21namespace framework
22{
23
24FreePortFinder::FreePortFinder(unsigned short initialPort, unsigned short finalPort, unsigned short step)
25 : mInitialPort{initialPort},
26 mFinalPort{finalPort},
27 mStep{step},
28 mSocket{socket(AF_INET, SOCK_STREAM, 0)}
29{
30}
31
32bool FreePortFinder::testPort(int port) const
33{
34 struct sockaddr_in addr;
35 memset(&addr, 0, sizeof(addr));
36 addr.sin_family = AF_INET;
37 addr.sin_addr.s_addr = INADDR_ANY;
38 addr.sin_port = htons(port);
39 return bind(mSocket, (struct sockaddr*)&addr, sizeof(addr)) == 0;
40}
41
43{
44 for (mPort = mInitialPort; mPort < mFinalPort; mPort += mStep) {
45 if (!testPort(mPort)) {
46 if (mVerbose) {
47 LOG(warn) << "Port range [" << mPort << ", " << mPort + mStep
48 << "] already taken. Skipping";
49 }
50 continue;
51 }
52 if (mVerbose) {
53 LOG(info) << "Using port range [" << mPort << ", " << mPort + mStep << "]";
54 }
55 break;
56 }
57}
58
60{
61 mVerbose = v;
62}
63
65{
66 close(mSocket);
67}
68
69unsigned short FreePortFinder::port() const { return mPort + 1; }
70unsigned short FreePortFinder::range() const { return mStep; }
71
72} // namespace framework
73} // namespace o2
bool testPort(int port) const
Test one specific port.
unsigned short range() const
Get the range size.
unsigned short port() const
Get the first port in the selected range.
FreePortFinder(unsigned short initialPort, unsigned short finalPort, unsigned short step)
const GLdouble * v
Definition glcorearb.h:832
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"