-
Notifications
You must be signed in to change notification settings - Fork 887
feat: spawn stream support for Executor #1972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RohitKushvaha01
wants to merge
15
commits into
Acode-Foundation:main
Choose a base branch
from
RohitKushvaha01:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ebcdce5
feat: spawn stream support for Executor
RohitKushvaha01 35cda61
fix issues
RohitKushvaha01 eee71e8
feat: improvements
RohitKushvaha01 bbed883
feat: improvements
RohitKushvaha01 cad63f8
Update src/plugins/terminal/www/Executor.js
RohitKushvaha01 2f1d575
Update src/plugins/terminal/src/android/Executor.java
RohitKushvaha01 e145f47
Update src/plugins/terminal/src/android/ProcessServer.java
RohitKushvaha01 6d27ded
Update src/plugins/terminal/src/android/ProcessServer.java
RohitKushvaha01 44f291a
fix: issues
RohitKushvaha01 2b7ac85
Update src/plugins/terminal/src/android/Executor.java
RohitKushvaha01 abb7768
Update src/plugins/terminal/src/android/ProcessServer.java
RohitKushvaha01 4b204a8
fix: issues
RohitKushvaha01 b9c8a5f
fix: issues
RohitKushvaha01 86827e8
Update src/plugins/terminal/src/android/Executor.java
RohitKushvaha01 a6d835a
fix: remove duplicate import
RohitKushvaha01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package com.foxdebug.acode.rk.exec.terminal; | ||
|
|
||
| import org.java_websocket.WebSocket; | ||
| import org.java_websocket.handshake.ClientHandshake; | ||
| import org.java_websocket.server.WebSocketServer; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.InetSocketAddress; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| class ProcessServer extends WebSocketServer { | ||
|
|
||
| private final String[] cmd; | ||
| private final CountDownLatch readyLatch = new CountDownLatch(1); | ||
| private final AtomicReference<Exception> startError = new AtomicReference<>(); | ||
|
|
||
| private static final class ConnState { | ||
| final Process process; | ||
| final OutputStream stdin; | ||
|
|
||
| ConnState(Process process, OutputStream stdin) { | ||
| this.process = process; | ||
| this.stdin = stdin; | ||
| } | ||
| } | ||
|
|
||
| ProcessServer(int port, String[] cmd) { | ||
| super(new InetSocketAddress("127.0.0.1", port)); | ||
| this.cmd = cmd; | ||
| } | ||
|
|
||
| void startAndAwait() throws Exception { | ||
| start(); | ||
| readyLatch.await(); | ||
| Exception err = startError.get(); | ||
| if (err != null) throw err; | ||
| } | ||
|
|
||
| @Override | ||
| public void onStart() { | ||
| readyLatch.countDown(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(WebSocket conn, Exception ex) { | ||
| if (conn == null) { | ||
| // Bind/startup failure — unblock startAndAwait() so it can throw. | ||
| startError.set(ex); | ||
| readyLatch.countDown(); | ||
| } | ||
| // Per-connection errors: do nothing. onClose fires immediately after | ||
| // for the same connection, which is the single place cleanup happens. | ||
| } | ||
|
|
||
| @Override | ||
| public void onOpen(WebSocket conn, ClientHandshake handshake) { | ||
| try { | ||
| Process process = new ProcessBuilder(cmd).redirectErrorStream(true).start(); | ||
| InputStream stdout = process.getInputStream(); | ||
| OutputStream stdin = process.getOutputStream(); | ||
|
|
||
| conn.setAttachment(new ConnState(process, stdin)); | ||
|
|
||
| new Thread(() -> { | ||
| try { | ||
| byte[] buf = new byte[8192]; | ||
| int len; | ||
| while ((len = stdout.read(buf)) != -1) { | ||
| conn.send(ByteBuffer.wrap(buf, 0, len)); | ||
| } | ||
| } catch (Exception ignored) {} | ||
| conn.close(1000, "process exited"); | ||
| }).start(); | ||
|
|
||
| } catch (Exception e) { | ||
| conn.close(1011, "Failed to start process: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(WebSocket conn, ByteBuffer msg) { | ||
| try { | ||
| ConnState state = conn.getAttachment(); | ||
| state.stdin.write(msg.array(), msg.position(), msg.remaining()); | ||
| state.stdin.flush(); | ||
| } catch (Exception ignored) {} | ||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public void onMessage(WebSocket conn, String message) { | ||
| try { | ||
| ConnState state = conn.getAttachment(); | ||
| state.stdin.write(message.getBytes(StandardCharsets.UTF_8)); | ||
| state.stdin.flush(); | ||
| } catch (Exception ignored) {} | ||
| } | ||
|
|
||
| @Override | ||
| public void onClose(WebSocket conn, int code, String reason, boolean remote) { | ||
| try { | ||
| ConnState state = conn.getAttachment(); | ||
| if (state != null) state.process.destroy(); | ||
| } catch (Exception ignored) {} | ||
RohitKushvaha01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // stop() calls w.join() on every worker thread. If called directly from | ||
| // onClose (which runs on a WebSocketWorker thread), it deadlocks waiting | ||
| // for itself to finish. A separate thread sidesteps that entirely. | ||
| new Thread(() -> { | ||
| try { | ||
| stop(); | ||
| } catch (Exception ignored) {} | ||
| }).start(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.