[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/geiser-kawa 02cd922 046/119: Add workaround to free port w
From: |
Philip Kaludercic |
Subject: |
[nongnu] elpa/geiser-kawa 02cd922 046/119: Add workaround to free port when a Kawa server is already using it through ssocket.close() |
Date: |
Sun, 1 Aug 2021 18:30:36 -0400 (EDT) |
branch: elpa/geiser-kawa
commit 02cd922876f4b20bb9ae5b09a2b84022a7c58781
Author: spellcard199 <spellcard199@protonmail.com>
Commit: spellcard199 <spellcard199@protonmail.com>
Add workaround to free port when a Kawa server is already using it through
ssocket.close()
---
.../kawageiser/StartKawaWithGeiserSupport.java | 90 ++++++++++++++++++----
1 file changed, 77 insertions(+), 13 deletions(-)
diff --git a/src/main/java/kawageiser/StartKawaWithGeiserSupport.java
b/src/main/java/kawageiser/StartKawaWithGeiserSupport.java
index cc558e8..433b0b1 100644
--- a/src/main/java/kawageiser/StartKawaWithGeiserSupport.java
+++ b/src/main/java/kawageiser/StartKawaWithGeiserSupport.java
@@ -5,19 +5,41 @@
package kawageiser;
+import gnu.expr.Language;
+import kawa.TelnetRepl;
+import kawa.standard.Scheme;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+
public class StartKawaWithGeiserSupport {
- public static void main(String[] args) {
+ public static void main(String[] args) throws IOException {
+ Scheme scheme = new Scheme();
if (args.length == 0) {
int defaultPort = 37146;
System.out.println(
String.format(
"No port specified. Starting kawa server on
default port (%d)...",
defaultPort));
- startKawaServerWithGeiserSupport(defaultPort);
+ // NOTE: You can use ssocket.close() to forcefully free port:
+ // that's why we are passing around a ServerSocket instead
of an int port
+ // NOTE:
+ // 1. You can't reuse address if you use the constructor new
ServerSocket(int port);
+ // 2. That's why we used the following 3 lines here
+ ServerSocket ssocket =
makeServerSocketWithReusableAddress(defaultPort);
+ Thread kawaServerThread =
makeThreadForKawaServerWithGeiserSupport(ssocket, scheme);
+ kawaServerThread.start();
+
} else if (args.length == 1 && args[0].matches("[0-9]+")) {
+ // NOTE: You can use ssocket.close() to forcefully free port:
+ // that's why we are passing around a ServerSocket instead
of an int port
int port = Integer.parseInt(args[0]);
- startKawaServerWithGeiserSupport(port);
+ ServerSocket ssocket = makeServerSocketWithReusableAddress(port);
+ Thread kawaServerThread =
makeThreadForKawaServerWithGeiserSupport(ssocket, scheme);
+ kawaServerThread.start();
+
} else if (args.length == 1 && args[0].equals("--no-server")) {
System.out.println("Starting kawa repl in current terminal...");
startKawaReplWithGeiserSupport();
@@ -30,24 +52,66 @@ public class StartKawaWithGeiserSupport {
}
}
- public static void startKawaReplWithGeiserSupport() {
+ public static Scheme startKawaReplWithGeiserSupport() {
String[] interpArgs = new String[]{
"-e", "(require <kawageiser.Geiser>)",
"--",
};
- runSchemeAsApplication(interpArgs);
+ return runSchemeAsApplication(interpArgs);
}
- public static void startKawaServerWithGeiserSupport(int port) {
- String[] interpArgs = new String[]{
- "-e", "(require <kawageiser.Geiser>)",
- "--server", String.valueOf(port)};
- runSchemeAsApplication(interpArgs);
- }
-
- public static void runSchemeAsApplication(String[] args) {
+ public static Scheme runSchemeAsApplication(String[] args) {
kawa.standard.Scheme scheme = kawa.standard.Scheme.getInstance();
scheme.runAsApplication(args);
+ return scheme;
+
+ }
+
+ public static Thread
+ makeThreadForKawaServerWithGeiserSupport(ServerSocket ssocket, Scheme
scheme) {
+ return new Thread(() -> {
+ try {
+ startKawaServerWithGeiserSupport(ssocket, scheme);
+ } catch (Throwable throwable) {
+ throwable.printStackTrace();
+ }
+ });
+ }
+
+ public static ServerSocket
+ makeServerSocketWithReusableAddress(int port) throws IOException {
+ ServerSocket ssocket = new ServerSocket();
+ ssocket.setReuseAddress(true);
+ ssocket.bind(new InetSocketAddress(port));
+ return ssocket;
+ }
+
+ private static void
+ startKawaServerWithGeiserSupport(ServerSocket ssocket,
+ Scheme scheme) throws Throwable {
+ scheme.eval("(require <kawageiser.Geiser>)");
+ startKawaServer(ssocket, scheme);
+ }
+
+ private static void
+ startKawaServer(ServerSocket ssocket, Scheme scheme) throws IOException {
+ // Adapted from Kawa's code in kawa/repl.java
+ System.err.println("Listening on port " + ssocket.getLocalPort());
+ for (; ; ) {
+ System.err.print("waiting ... ");
+ System.err.flush();
+ java.net.Socket client = ssocket.accept();
+ System.err.println("got connection from "
+ + client.getInetAddress()
+ + " port:" + client.getPort());
+ Language saveLang = Language.getDefaultLanguage();
+ try {
+ Language.setCurrentLanguage(scheme);
+ TelnetRepl.serve(scheme, client);
+ } finally {
+ Language.setCurrentLanguage(saveLang);
+ }
+ }
}
}
- [nongnu] elpa/geiser-kawa c1f9e3f 087/119: Fix unsupported add to gnu.lists.EmptyList, (continued)
- [nongnu] elpa/geiser-kawa c1f9e3f 087/119: Fix unsupported add to gnu.lists.EmptyList, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 1e15f01 091/119: Refactor 'geiser-kawa-util--eval...' + related fixes, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa e864cf6 108/119: Fix docstring wording, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 46ab237 102/119: melpa/melpa#6858 - Replace license boilerplates with SPDX-License-Identifier, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 506bf81 095/119: Add section about embedding kawa-geiser in a java application, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 322a40f 093/119: README.org: add heading about manual lookup, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 6009d95 100/119: Rename test-geiser-kawa.el to geiser-kawa-test.el + fix flycheck issues, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa cd39deb 116/119: Update geiser version in Package-Requires, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 989485b 118/119: README.org - update instructions with new geiser-kawa's repo, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 3d999a3 119/119: README.org - update Geiser's repo url after new repo, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 02cd922 046/119: Add workaround to free port when a Kawa server is already using it through ssocket.close(),
Philip Kaludercic <=
- [nongnu] elpa/geiser-kawa 0d86c7b 048/119: Add java package completion caching at module load time, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 6ff146d 077/119: pom.xml: update kawa-devutil version, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa e63a8b4 088/119: Improve message when manual is not found., Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 47f195f 094/119: README.org: add heading about extending geiser-kawa, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa b96c008 113/119: pom.xml - update kawa-devutil version, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 7e060a4 115/119: Update package's Homepage, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 95573c2 019/119: Add initial support for colon-notation completion, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa eb83483 070/119: Add+Refactor: add support for generic kawa-devutil completion + refactor, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa 3a02f86 078/119: Add first line to elisp files: description + lexical binding, Philip Kaludercic, 2021/08/01
- [nongnu] elpa/geiser-kawa d744274 080/119: Cask file: add development dependencies, Philip Kaludercic, 2021/08/01