package com.atlassian.bitbucket.mesh.network;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.stub.AbstractStub;

import javax.annotation.Nonnull;
import java.util.function.Function;

public interface MeshChannel {

    /**
     * The key used to register the target {@link MeshNode} in the {@link CallOptions call options}
     */
    CallOptions.Key<MeshNode> OPT_TARGET_NODE = CallOptions.Key.create("mesh-target-node");

    /**
     * The key used to registered the {@link MeshChannel} in the {@link CallOptions call options}
     * @since 1.2
     */
    CallOptions.Key<MeshChannel> OPT_CHANNEL = CallOptions.Key.create("mesh-channel");

    /**
     * @return {@code true} if the channel was closed; {@code false} if the channel had already been closed
     * @since 1.2
     */
    boolean close();

    /**
     * Creates a new stub which will connect over this channel.
     *
     * @param stubFactory the factory to create the new stub
     * @param <S> the type of stub to be returned
     * @return the created stub
     */
    <S extends AbstractStub<S>> S newStub(@Nonnull Function<Channel, S> stubFactory);
}
