diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..eeb6993 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,10 @@ +name: build +on: [push, pull_request] +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: { distribution: temurin, java-version: '17', cache: maven } + - run: mvn -B -f JSC/pom.xml test diff --git a/.gitignore b/.gitignore index 9ca7801..1846529 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# Maven build output +target/ diff --git a/JSC/JConnection/pom.xml b/JSC/JConnection/pom.xml index 9758c8e..56c318f 100644 --- a/JSC/JConnection/pom.xml +++ b/JSC/JConnection/pom.xml @@ -7,29 +7,35 @@ org.example JConnection 1.0-SNAPSHOT + + + 5.10.2 + UTF-8 + + org.apache.maven.plugins maven-compiler-plugin + 3.13.0 8 8 + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + org.junit.jupiter junit-jupiter - RELEASE - test - - - org.junit.jupiter - junit-jupiter - RELEASE + ${junit.version} test diff --git a/JSC/JConnection/src/test/java/jsc/jConnection/JConnectionTest.java b/JSC/JConnection/src/test/java/jsc/jConnection/JConnectionTest.java index 64c8b3e..cd87476 100644 --- a/JSC/JConnection/src/test/java/jsc/jConnection/JConnectionTest.java +++ b/JSC/JConnection/src/test/java/jsc/jConnection/JConnectionTest.java @@ -3,6 +3,7 @@ import java.io.IOException; +@org.junit.jupiter.api.Disabled("Manual: needs EchoServer running on :5656. Automated coverage: JSERVER EchoRoundTripTest") class JConnectionTest { JConnection jConnection; diff --git a/JSC/JSERVER/pom.xml b/JSC/JSERVER/pom.xml index 9cc43d2..ee34f65 100644 --- a/JSC/JSERVER/pom.xml +++ b/JSC/JSERVER/pom.xml @@ -7,18 +7,44 @@ org.example JSERVER 1.0-SNAPSHOT + + + 5.10.2 + UTF-8 + + org.apache.maven.plugins maven-compiler-plugin + 3.13.0 8 8 + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + + + org.example + JConnection + 1.0-SNAPSHOT + + + org.junit.jupiter + junit-jupiter + ${junit.version} + test + + + \ No newline at end of file diff --git a/JSC/JSERVER/src/test/java/jsc/jServer/EchoRoundTripTest.java b/JSC/JSERVER/src/test/java/jsc/jServer/EchoRoundTripTest.java new file mode 100644 index 0000000..e075f7a --- /dev/null +++ b/JSC/JSERVER/src/test/java/jsc/jServer/EchoRoundTripTest.java @@ -0,0 +1,67 @@ +package jsc.jServer; + +import jsc.jConnection.JConnection; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** End-to-end: real JServer on an ephemeral port, real JConnection client, messages echoed back in order. */ +class EchoRoundTripTest { + + public static class Echo extends JRequestManager { + @Override + public void accept(String req) { write(req); } + } + + private JServer server; + private JConnection client; + private final BlockingQueue received = new LinkedBlockingQueue<>(); + + @BeforeEach + void setUp() throws Exception { + server = new JServer(0, Echo.class); // port 0 = OS picks a free port + Thread t = new Thread(server::start, "jserver"); + t.setDaemon(true); + t.start(); + client = new JConnection("127.0.0.1", server.getServerSocket().getLocalPort(), received::add); + client.run(); + } + + @AfterEach + void tearDown() { + client.close(); + server.setRunning(false); + server.close(); + } + + @Test + void echoesMessagesInOrder() throws Exception { + String[] msgs = {"hello", "1/2~3>4;5,6", "unicode โœ“"}; + for (String m : msgs) client.write(m); + for (String m : msgs) assertEquals(m, received.poll(5, TimeUnit.SECONDS)); + } + + @Test + void pausedReadHoldsMessagesUntilResume() throws Exception { + client.write("before"); + assertEquals("before", received.poll(5, TimeUnit.SECONDS)); + client.setReadInterrupt(true); + client.write("m1"); + client.write("m2"); + client.write("m3"); + Thread.sleep(500); + // The pause is checked before each blocking readUTF(), so a read already in flight + // can still deliver one message (documented limitation); the rest must be held. + int leaked = received.size(); + assertTrue(leaked <= 1, "at most one in-flight message may arrive while paused, got " + leaked); + client.setReadInterrupt(false); + for (String m : new String[]{"m1", "m2", "m3"}) assertEquals(m, received.poll(5, TimeUnit.SECONDS)); + } +} diff --git a/JSC/pom.xml b/JSC/pom.xml new file mode 100644 index 0000000..b8c67f2 --- /dev/null +++ b/JSC/pom.xml @@ -0,0 +1,15 @@ + + + + 4.0.0 + org.example + JSC + 1.0-SNAPSHOT + pom + + JConnection + JSERVER + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..22e965e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Prashant Joshi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index f895428..fe0034d 100644 --- a/README.md +++ b/README.md @@ -1,263 +1,74 @@ -# ๐Ÿ“จ JavaServerClientAPI +# JavaServerClientAPI (JSC) -![COMMIT_STATUS](https://img.shields.io/github/last-commit/pj-25/JavaServerClientAPI?style=for-the-badge) +[![build](https://github.com/pj-25/JavaServerClientAPI/actions/workflows/build.yml/badge.svg)](https://github.com/pj-25/JavaServerClientAPI/actions/workflows/build.yml) ยท [JavaDocs](https://pj-25.github.io/JavaServerClientAPI/docs) -๐Ÿ“– [Jump to JavaDocs --->](https://pj-25.github.io/JavaServerClientAPI/docs) +JSC is a small, dependency-free Java library for building TCP client/server applications around **events and typed messages** instead of raw sockets. I wrote it in June 2021, as a student, so my multiplayer Java projects could share one networking layer. Its first real consumer was **[Gameholic](https://github.com/pj-25/Gameholic)**, a platform for building and sharing multiplayer games. -## ๐Ÿ“‘ Library Structure +> **Status:** historical project, kept for reference. It was built in 2021 on Java 8 and is not maintained as a production library. The build and tests were restored in 2026 so the code can be run and read. The design is unchanged. -```javascript -jsc -โ”œโ”€โ”€ jConnection -โ”‚ย ย  โ”œโ”€โ”€ JCloseEventConsumer.java -โ”‚ย ย  โ”œโ”€โ”€ JConnection.java -โ”‚ย ย  โ””โ”€โ”€ JConnectionManager.java -โ”œโ”€โ”€ jEventManager -โ”‚ย ย  โ”œโ”€โ”€ JEventCode.java -โ”‚ย ย  โ”œโ”€โ”€ JEventConsumer.java -โ”‚ย ย  โ”œโ”€โ”€ JEventDataSender.java -โ”‚ย ย  โ”œโ”€โ”€ JEventManager.java -โ”‚ย ย  โ””โ”€โ”€ JEventType.java -โ”œโ”€โ”€ jMessageHandler -โ”‚ย ย  โ”œโ”€โ”€ JFileConsumer.java -โ”‚ย ย  โ”œโ”€โ”€ JMessageCode.java -โ”‚ย ย  โ”œโ”€โ”€ JMessageConsumer.java -โ”‚ย ย  โ”œโ”€โ”€ JMessageDelimiter.java -โ”‚ย ย  โ”œโ”€โ”€ JMessageFormatHandler.java -โ”‚ย ย  โ”œโ”€โ”€ JMessageSender.java -โ”‚ย ย  โ””โ”€โ”€ JMessageType.java -โ”œโ”€โ”€ jObjectParser -โ”‚ย ย  โ”œโ”€โ”€ JObjectParseException.java -โ”‚ย ย  โ””โ”€โ”€ JObjectParser.java -โ””โ”€โ”€ jServer - โ”œโ”€โ”€ JClientConnectionsHandler.java - โ”œโ”€โ”€ JConnectionHandler.java - โ”œโ”€โ”€ JRequestManager.java - โ”œโ”€โ”€ JResponseSender.java - โ”œโ”€โ”€ JServer.java - โ””โ”€โ”€ JSocketConsumer.java +## The problem +Every networked app I wrote, whether a chat app, a game lobby or a file transfer, repeated the same plumbing: +- accept connections and give each client its own handler; +- frame messages; +- tell *what kind* of message arrived; +- notify interested parts of the app when something happened, such as a connection, a close or an entity update. -5 directories, 23 files -``` - -> *click any of the above highlighted file to navigate to its source code - -## ๐Ÿ—๏ธ JSC Architecture -* ### System Design - ![JSC system architecture](https://github.com/pj-25/JavaServerClientAPI/blob/main/res/JSC-JSC_System_Architecture.png) -* ### JSC Integration - ![JSC system integration](https://github.com/pj-25/JavaServerClientAPI/blob/main/res/JSC-JSC_Integration_System_Design.png) - -## ๐ŸŽข Getting Started - -* ### Server Side: - * #### Server implementation: - - * ```EchoServer.java``` - ```java - import jsc.jServer.JServer; - import java.io.IOException; - - public class EchoServer { - public static void main(String []s){ - try{ - JServer echoServer = new JServer(EchoJRequestManagerImpl.class); - echoServer.start(); - }catch (IOException e){ - System.out.println("Unable to start server :("); - } - } - } - ``` - * ```EchoJRequestManagerImpl.java``` - ```java - import jsc.jServer.JRequestManager; - - public class EchoJRequestManagerImpl extends JRequestManager { - @Override - public void accept(String req){ - //write your logic here to serve the request - - System.out.println("Received Request: "+req); - write(req); - } - } - ``` - * Default server port: ```5656``` - - * #### Run Server: - ```bash - javac -cp JSERVER.jar EchoJRequestManagerImpl.java - javac -cp JSERVER.jar:. EchoServer.java - java -cp JSERVER.jar:. EchoServer - ``` -* ### Client Side: - * #### Client implementation: - * ```TestRelayEchoClient.java``` - ```java - import jsc.jConnection.JConnection; +JSC pulls that plumbing out once. - import java.io.IOException; - import java.util.Scanner; +## Design +![JSC architecture](res/JSC-JSC_System_Architecture.png) - public class TestRelayEchoClient { +| Package | Responsibility | +|---|---| +| `jConnection` | `JConnection` wraps a socket. It owns a reader thread that hands each message to a `JMessageConsumer` (a lambda), and it can **pause and resume** reading so the socket can be temporarily handed to a different consumer, for example during a file transfer. | +| `jServer` | `JServer` runs an accept loop with **one thread per client**. Each client gets a fresh instance of your `JRequestManager` subclass, created by reflection, so per-connection state needs no locking. | +| `jMessageHandler` | Encodes and decodes a small text protocol: `TYPE / data`, `TYPE ~ EVENT > data`, attributes split by `;` and lists by `,`. | +| `jEventManager` | An observer-style event bus keyed by numeric event codes (`CONNECT`, `CLOSE`, `ENTITY_TRANSFER`, `UPDATED`, โ€ฆ). | +| `jObjectParser` | Lets domain objects serialise to and from the attribute format, so lists of entities can be sent in one message. | - public static Scanner scanner = new Scanner(System.in); - public static void main(String []s){ - try{ - JConnection jConnection = new JConnection("127.0.0.1", 5656, (res)->{ - //write your logic here to consume the response - System.out.println(res); - }); - jConnection.run(); - System.out.println("Write your message and press enter...(write \"exit\" to stop)"); - String msg; - while(jConnection.isConnected()){ - msg = scanner.nextLine(); - if(msg.equals("exit")){ - break; - } - jConnection.write(msg); - } - jConnection.close(); - System.out.println("----------- Bye :) -----------"); - }catch (IOException ioE){ - System.out.println("Server connection lost, check your network connection :("); - } - } +Messages are framed with `DataOutputStream.writeUTF`, which writes a 2-byte length prefix followed by modified UTF-8. - } - ``` - * Output: - ``` - Write your message and press enter...(write "exit" to stop) - Hello, Server! - Hello, Server! - JSC is perfectly working :) - JSC is perfectly working :) - exit - ----------- Bye :) ----------- - anonymous disconnected! - ``` - -## ๐Ÿš€ Proof of Concept: +### Trade-offs I made, and what I'd do differently now +- **Thread per connection.** Simple and correct for tens of game clients, but it doesn't scale to thousands. Today I'd use NIO, Netty or virtual threads (Java 21). +- **Human-readable delimiter protocol.** Easy to debug with `telnet`. But **delimiters are not escaped**, so data containing `/ ~ > ; ,` can be decoded wrongly. The transport is safe (it is length-prefixed and covered by a test); the problem is in field decoding. Today I'd use length-prefixed binary or JSON, with a versioned envelope. +- **`writeUTF` framing.** Each message is limited to 64 KB. +- **Pause and resume reading** checks the pause flag *before* each blocking read. A read already in flight can therefore still deliver one message after `setReadInterrupt(true)`. The behaviour is pinned down by `EchoRoundTripTest`. +- **Concurrency hygiene.** The connection flags aren't `volatile`, and `write()` isn't synchronised across threads that write at the same time. Both are fine for the original single-writer use, and both would be the first fixes in a v2. +- No TLS, authentication or back-pressure. -1. [Gameholic](https://github.com/pj-25/Gameholic) - -## ๐Ÿ—ƒ๏ธ Project Structure - -![STATUS](https://img.shields.io/badge/status-under%20development-important) -![IDE](https://img.shields.io/badge/IDE-IntellijIDEA-blueviolet) - -```javascript -JSC -โ”œโ”€โ”€ JConnection -โ”‚ย ย  โ”œโ”€โ”€ JConnection.iml -โ”‚ย ย  โ”œโ”€โ”€ pom.xml -โ”‚ย ย  โ”œโ”€โ”€ src -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ main -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jsc -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jConnection -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JCloseEventConsumer.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JConnection.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JConnectionManager.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jEventManager -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventCode.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventConsumer.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventDataSender.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventManager.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JEventType.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jMessageHandler -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JFileConsumer.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageCode.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageConsumer.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageDelimiter.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageFormatHandler.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageSender.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JMessageType.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jObjectParser -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JObjectParseException.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JObjectParser.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ resources -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ test -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ java -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jsc -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jConnection -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JConnectionTest.java -โ”‚ย ย  โ””โ”€โ”€ target -โ”‚ย ย  โ”œโ”€โ”€ classes -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jsc -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jConnection -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JCloseEventConsumer.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JConnection.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JConnectionManager.class -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jEventManager -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventCode.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventConsumer.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventDataSender.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JEventManager.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JEventType.class -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ jMessageHandler -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JFileConsumer.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageCode.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageConsumer.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageDelimiter.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageFormatHandler.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JMessageSender.class -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JMessageType.class -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jObjectParser -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JObjectParseException.class -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JObjectParser.class -โ”‚ย ย  โ”œโ”€โ”€ generated-sources -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ annotations -โ”‚ย ย  โ”œโ”€โ”€ generated-test-sources -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ test-annotations -โ”‚ย ย  โ””โ”€โ”€ test-classes -โ”‚ย ย  โ””โ”€โ”€ jsc -โ”‚ย ย  โ””โ”€โ”€ jConnection -โ”‚ย ย  โ””โ”€โ”€ JConnectionTest.class -โ”œโ”€โ”€ JSERVER -โ”‚ย ย  โ”œโ”€โ”€ JSERVER.iml -โ”‚ย ย  โ”œโ”€โ”€ pom.xml -โ”‚ย ย  โ”œโ”€โ”€ src -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ main -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jsc -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jServer -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JClientConnectionsHandler.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JConnectionHandler.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JRequestManager.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JResponseSender.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JServer.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JSocketConsumer.java -โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ resources -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ test -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ java -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ EchoJRequestManagerImpl.java -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ EchoServer.java -โ”‚ย ย  โ””โ”€โ”€ target -โ”‚ย ย  โ”œโ”€โ”€ classes -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jsc -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ jServer -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JClientConnectionsHandler.class -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JConnectionHandler.class -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JRequestManager.class -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JResponseSender.class -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JServer$RequestHandlerNotFound.class -โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ JServer.class -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ JSocketConsumer.class -โ”‚ย ย  โ”œโ”€โ”€ generated-sources -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ annotations -โ”‚ย ย  โ”œโ”€โ”€ generated-test-sources -โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ test-annotations -โ”‚ย ย  โ””โ”€โ”€ test-classes -โ”‚ย ย  โ”œโ”€โ”€ EchoJRequestManagerImpl.class -โ”‚ย ย  โ””โ”€โ”€ EchoServer.class -โ””โ”€โ”€ libs - โ”œโ”€โ”€ JConnection.jar - โ””โ”€โ”€ JSERVER.jar +## Run it +You need JDK 17+ and Maven. The code targets Java 8. +```sh +mvn -f JSC/pom.xml test # builds JConnection, then JSERVER; runs the end-to-end echo tests +``` +`EchoRoundTripTest` starts a real `JServer` on an ephemeral port, connects a real `JConnection`, and checks: +- messages are echoed in order, including ones that contain delimiters and non-ASCII characters; +- pause and resume behave as described above. + +### Minimal echo server and client +```java +public class Echo extends JRequestManager { // one instance per connected client + @Override public void accept(String req) { write(req); } +} +new JServer(5656, Echo.class).start(); // blocks; one thread per client + +JConnection c = new JConnection("127.0.0.1", 5656, System.out::println); +c.run(); // background reader thread +c.write("Hello, server!"); +``` +Larger examples are in `JSC/JSERVER/src/test/java` (`EchoServer`) and `JSC/JConnection/src/test/java` (`TestRelayEchoClient`). -47 directories, 59 files +## Repository layout +``` +JSC/JConnection connection, message and event library (Maven module) +JSC/JSERVER server and per-client request managers (depends on JConnection) +JSC/libs prebuilt jars from 2021 +sourceCode/ flat copy of the library sources (added 2021-06-19; identical to JSC/) +docs/ generated JavaDocs (GitHub Pages) ``` -> *click any of the above highlighted file to navigate to its source code + +## History +- 2021-06-10: initial implementation. +- 2021-06-11: JavaDocs. +- 2021-06 โ†’ 2021-11: used by Gameholic. +- 2026: Maven build repaired (duplicate JUnit dependency; the JSERVER โ†’ JConnection dependency had only been declared in the IDE); end-to-end tests and CI added.