summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/dispatch/CloseableChannel.java
blob: 643b8f81318d76893d789f195030fd6f62b43c60 (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
50
51
52
53
54
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch;

import com.yahoo.fs4.BasicPacket;
import com.yahoo.fs4.ChannelTimeoutException;
import com.yahoo.fs4.mplex.Backend;
import com.yahoo.fs4.mplex.FS4Channel;
import com.yahoo.fs4.mplex.InvalidChannelException;
import com.yahoo.search.Query;

import java.io.Closeable;
import java.io.IOException;
import java.util.Optional;

/**
 * @author ollivir
 */
public class CloseableChannel implements Closeable {
    private FS4Channel channel;
    private final Optional<Integer> distributionKey;

    public CloseableChannel(Backend backend) {
        this(backend, Optional.empty());
    }

    public CloseableChannel(Backend backend, Optional<Integer> distributionKey) {
        this.channel = backend.openChannel();
        this.distributionKey = distributionKey;
    }

    public void setQuery(Query query) {
        channel.setQuery(query);
    }

    public boolean sendPacket(BasicPacket packet) throws InvalidChannelException, IOException {
        return channel.sendPacket(packet);
    }

    public BasicPacket[] receivePackets(long timeout, int packetCount) throws InvalidChannelException, ChannelTimeoutException {
        return channel.receivePackets(timeout, packetCount);
    }

    public Optional<Integer> distributionKey() {
        return distributionKey;
    }

    @Override
    public void close() {
        if (channel != null) {
            channel.close();
            channel = null;
        }
    }
}