aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/PortAllocBridge.java
blob: fdb585209c8d99d2e69cc36952a3b97b16929545 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.model;

import java.util.ArrayList;
import java.util.List;

/**
 * API for allocating network ports
 * This class acts as a bridge between NetworkPortRequestor and HostPorts
 * for a single call to allocatePorts(), gathering the resulting port
 * allocations in a list of integers.
 *
 * @author arnej
 */
public class PortAllocBridge {

    private final HostPorts host;
    private final NetworkPortRequestor service;
    private final List<Integer> ports = new ArrayList<>();

    public PortAllocBridge(HostPorts host, NetworkPortRequestor service) {
        this.host = host;
        this.service = service;
    }

    public int requirePort(int port, String suffix) {
        int got = host.requireNetworkPort(port, service, suffix);
        ports.add(got);
        return got;
    }

    public int wantPort(int port, String suffix) {
        int got = host.wantNetworkPort(port, service, suffix);
        ports.add(got);
        return got;
    }

    public int allocatePort(String suffix) {
        int got = host.allocateNetworkPort(service, suffix);
        ports.add(got);
        return got;
    }

    public List<Integer> result() {
        return ports;
    }

}