mirror of
https://github.com/xpipe-io/xpipe.git
synced 2026-08-10 19:40:57 +00:00
Tunnel session rework
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user