1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+
20+ package org .apache .plc4x .java .spi .transports .api ;
21+
22+ import org .apache .plc4x .java .spi .transports .api .config .TransportConfiguration ;
23+
24+ import java .util .function .Consumer ;
25+
26+ /**
27+ * Extension of TransportInstance that supports asynchronous, event-driven I/O.
28+ * This eliminates the need for polling loops.
29+ * <p>
30+ * Implementations register with the underlying I/O mechanism (e.g., NIO Selector)
31+ * and notify registered listeners when data becomes available, enabling zero-polling,
32+ * event-driven architectures.
33+ *
34+ * @param <T> the configuration type
35+ */
36+ public interface AsyncTransportInstance <T extends TransportConfiguration > extends TransportInstance <T > {
37+
38+ /**
39+ * Registers a listener that will be called when data becomes available.
40+ * This allows for event-driven architectures without polling.
41+ * <p>
42+ * The transport implementation is responsible for monitoring the underlying
43+ * I/O channel and invoking this listener when data arrives.
44+ *
45+ * @param listener callback invoked when data is available
46+ */
47+ void registerDataListener (Runnable listener );
48+
49+ /**
50+ * Removes the data available listener, stopping event notifications.
51+ */
52+ void removeDataListener ();
53+
54+ /**
55+ * Registers a listener that will be called when the transport is disconnected.
56+ * <p>
57+ * This is critical for properly handling connection failures - any pending
58+ * operations waiting for responses should be completed exceptionally when
59+ * the transport dies, rather than waiting for a timeout.
60+ * <p>
61+ * The listener receives the exception that caused the disconnect, or null
62+ * if the disconnect was graceful (e.g., the remote side closed normally).
63+ *
64+ * @param listener callback invoked when the transport disconnects
65+ */
66+ default void registerDisconnectListener (Consumer <Throwable > listener ) {
67+ // Default no-op for backward compatibility
68+ }
69+
70+ /**
71+ * Removes the disconnect listener.
72+ */
73+ default void removeDisconnectListener () {
74+ // Default no-op for backward compatibility
75+ }
76+
77+ }
0 commit comments