Tunnel session rework

This commit is contained in:
crschnick
2025-05-30 16:42:46 +00:00
parent 914fd6be37
commit ccc7188e9b
20 changed files with 107 additions and 68 deletions
@@ -1,16 +0,0 @@
package io.xpipe.core.store;
import io.xpipe.core.process.ShellControl;
public abstract class NetworkTunnelSession extends Session {
protected NetworkTunnelSession(SessionListener listener) {
super(listener);
}
public abstract int getLocalPort();
public abstract int getRemotePort();
public abstract ShellControl getShellControl();
}
@@ -1,28 +0,0 @@
package io.xpipe.core.store;
public interface NetworkTunnelStore extends DataStore {
DataStore getNetworkParent();
default boolean requiresTunnel() {
return getNetworkParent() != null;
}
default boolean isLocallyTunnelable() {
NetworkTunnelStore current = this;
while (true) {
var p = current.getNetworkParent();
if (p == null) {
return true;
}
if (p instanceof NetworkTunnelStore t) {
current = t;
} else {
return false;
}
}
}
NetworkTunnelSession createTunnelSession(int localPort, int remotePort, String address) throws Exception;
}
@@ -1,29 +0,0 @@
package io.xpipe.core.store;
public abstract class Session implements AutoCloseable {
protected SessionListener listener;
protected Session(SessionListener listener) {
this.listener = listener;
}
public void addListener(SessionListener n) {
var current = this.listener;
this.listener = running -> {
current.onStateChange(running);
n.onStateChange(running);
};
}
public abstract boolean isRunning() throws Exception;
public abstract void start() throws Exception;
public abstract void stop() throws Exception;
@Override
public void close() throws Exception {
stop();
}
}
@@ -1,6 +0,0 @@
package io.xpipe.core.store;
public interface SessionListener {
void onStateChange(boolean running);
}
@@ -1,82 +0,0 @@
package io.xpipe.core.store;
public interface SingletonSessionStore<T extends Session>
extends ExpandedLifecycleStore, InternalCacheDataStore, SessionListener {
@Override
default void finalizeStore() throws Exception {
stopSessionIfNeeded();
}
default void setSessionEnabled(boolean value) {
setCache("sessionEnabled", value);
}
default boolean isSessionRunning() {
return getCache("sessionRunning", Boolean.class, false);
}
default boolean isSessionEnabled() {
return getCache("sessionEnabled", Boolean.class, false);
}
@Override
default void onStateChange(boolean running) {
setSessionEnabled(running);
setCache("sessionRunning", running);
}
T newSession() throws Exception;
Class<?> getSessionClass();
@SuppressWarnings("unchecked")
default T getSession() {
return (T) getCache("session", getSessionClass(), null);
}
default void startSessionIfNeeded() throws Exception {
synchronized (this) {
var s = getSession();
if (s != null) {
if (s.isRunning()) {
return;
}
s.start();
return;
}
try {
setSessionEnabled(true);
s = newSession();
if (s != null) {
s.start();
setCache("session", s);
onStateChange(true);
} else {
setSessionEnabled(false);
}
} catch (Exception ex) {
setSessionEnabled(false);
onStateChange(false);
throw ex;
}
}
}
default void stopSessionIfNeeded() throws Exception {
synchronized (this) {
var ex = getSession();
setSessionEnabled(false);
if (ex != null) {
try {
ex.stop();
} finally {
setCache("session", null);
onStateChange(false);
}
}
}
}
}