001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.client.mcp.v20260728;
018
019import java.io.*;
020
021/**
022 * Closeable handle for a live {@link McpClient#listen(org.apache.juneau.bean.mcp.v20260728.SubscriptionFilter, McpSubscriptionListener)}
023 * subscription.
024 *
025 * @since 10.0.0
026 */
027public interface McpSubscriptionHandle extends Closeable {
028
029   /**
030    * The client-generated JSON-RPC request id this subscription's opening
031    * {@link McpClient#listen(org.apache.juneau.bean.mcp.v20260728.SubscriptionFilter, McpSubscriptionListener)}
032    * call sent, and which the server echoes back as the {@code subscriptionId} on every frame of this stream
033    * (the acknowledged frame, each change notification, and the terminal frame). Callers can use this to
034    * correlate this handle against server-side logs/traces for the same subscription.
035    *
036    * @return The request id. Never <jk>null</jk> or empty.
037    */
038   String id();
039
040   /**
041    * Cancels the subscription: signals the background pump to stop and closes the underlying stream. Idempotent.
042    *
043    * <p>
044    * Asynchronous: this method returns as soon as the stop signal is issued and the stream close is requested,
045    * <i>without</i> waiting for the background pump thread to actually finish running. A caller that needs to
046    * know the pump has fully stopped must poll {@link #isOpen()} or otherwise synchronize with it separately —
047    * this method does not join it.
048    */
049   void cancel();
050
051   /**
052    * Alias for {@link #cancel()}, satisfying {@link Closeable} for try-with-resources use.
053    *
054    * <p>
055    * Deliberately declares no checked {@link IOException} (narrows {@link Closeable#close()}'s throws clause) —
056    * the background pump owns the stream's lifecycle and closing it is always a local, non-throwing operation.
057    *
058    * <p>
059    * Like {@link #cancel()}, this is asynchronous: it signals the pump to stop but does not wait for it to exit.
060    */
061   @Override
062   void close();
063
064   /**
065    * @return <jk>true</jk> if the subscription is still open (not cancelled, errored, or gracefully completed).
066    */
067   boolean isOpen();
068}