Intial commit

This commit is contained in:
Willem Cazander 2023-09-24 12:41:43 +02:00
parent 7329fb8dea
commit 73f201bdac
268 changed files with 11220 additions and 5 deletions

10
no2all-wire/pom.xml Normal file
View file

@ -0,0 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>love.distributedrebirth.no2all</groupId>
<artifactId>no2all</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>no2all-wire</artifactId>
<name>No2All-Wire</name>
</project>

View file

@ -0,0 +1,14 @@
package love.distributedrebirth.no2all.wire;
import java.nio.ByteBuffer;
public interface WireClient {
void connect();
void close(int code, String message);
void sendMessage(String message);
void sendBinary(ByteBuffer message);
}

View file

@ -0,0 +1,8 @@
package love.distributedrebirth.no2all.wire;
public interface WireClientEndpoint {
String wireId();
WireClient wireClient(WireClientHandler handler);
}

View file

@ -0,0 +1,18 @@
package love.distributedrebirth.no2all.wire;
import java.nio.ByteBuffer;
public interface WireClientHandler {
void onStart();
void onOpen();
void onClose(int code, String reason, boolean remote);
void onError(Throwable error);
void onMessage(String message);
void onBinary(ByteBuffer message);
}

View file

@ -0,0 +1,10 @@
package love.distributedrebirth.no2all.wire;
import java.nio.ByteBuffer;
public interface WireServer {
void broadcastMessage(String message);
void broadcastBinary(ByteBuffer message);
}

View file

@ -0,0 +1,8 @@
package love.distributedrebirth.no2all.wire;
public interface WireServerEndpoint {
String wireId();
WireServer wireServer(WireServerHandler handler);
}

View file

@ -0,0 +1,18 @@
package love.distributedrebirth.no2all.wire;
import java.nio.ByteBuffer;
public interface WireServerHandler {
void onStart();
void onOpen(WireServerSocket conn);
void onClose(WireServerSocket conn, int code, String reason, boolean remote);
void onError(WireServerSocket conn, Throwable error);
void onMessage(WireServerSocket conn, String message);
void onBinary(WireServerSocket conn, ByteBuffer message);
}

View file

@ -0,0 +1,14 @@
package love.distributedrebirth.no2all.wire;
import java.nio.ByteBuffer;
public interface WireServerSocket {
String getRemoteAddress();
void sendMessage(String message);
void sendBinary(ByteBuffer message);
void close(int code, String message);
}

View file

@ -0,0 +1,4 @@
package love.distributedrebirth.no2all.wire.fetch;
public interface WireFetch extends Runnable {
}

View file

@ -0,0 +1,6 @@
package love.distributedrebirth.no2all.wire.fetch;
public interface WireFetchFactory {
WireFetch buildFetcher(WireFetchResource resource, WireFetchHandler handler);
}

View file

@ -0,0 +1,84 @@
package love.distributedrebirth.no2all.wire.fetch;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.Map.Entry;
public final class WireFetchFactoryDefault implements WireFetchFactory {
@Override
public WireFetch buildFetcher(WireFetchResource resource, WireFetchHandler handler) {
return new HttpDownloader(resource, handler);
}
static protected final class HttpDownloader implements WireFetch {
private final WireFetchResource resource;
private final WireFetchHandler handler;
protected HttpDownloader(WireFetchResource resource, WireFetchHandler handler) {
this.resource = resource;
this.handler = handler;
}
@Override
public void run() {
try {
handler.onStart();
try (InputStream conn = openInputStream(resource.getUrId().toURL(), resource.getHeaders(), resource.getTimeoutMs(), 5)) {
try (ByteArrayOutputStream result = new ByteArrayOutputStream()) {
int totalBytes = 0;
byte[] buffer = new byte[4096];
int length;
while ((length = conn.read(buffer)) != -1) {
result.write(buffer, 0, length);
totalBytes += length;
handler.onProgress(totalBytes);
}
handler.onReady(ByteBuffer.wrap(result.toByteArray()));
}
}
} catch (Exception error) {
handler.onError(error);
}
}
private InputStream openInputStream(URL url, Map<String, String> args, int timeoutMs, int redirectLimit) throws IOException {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(timeoutMs);
con.setReadTimeout(timeoutMs);
if (args != null) {
for (Entry<String, String> e : args.entrySet()) {
con.setRequestProperty(e.getKey(), e.getValue());
}
}
con.connect();
int responseCode = con.getResponseCode();
if (responseCode < 400 && responseCode > 299) {
// for redirects but breaks switch protocols
redirectLimit--;
if (redirectLimit == 0) {
throw new IllegalStateException("Max redirect limit reached");
}
String redirectLocation = con.getHeaderField("Location");
if (redirectLocation == null) {
throw new IllegalStateException("No redirect location header");
}
URL redirectUrl = null;
try {
redirectUrl = new URL(redirectLocation);
} catch (MalformedURLException e) {
redirectUrl = new URL(url.getProtocol() + "://" + url.getHost() + redirectLocation);
}
return openInputStream(redirectUrl, args, timeoutMs, redirectLimit);
}
return con.getInputStream();
}
}
}

View file

@ -0,0 +1,14 @@
package love.distributedrebirth.no2all.wire.fetch;
import java.nio.ByteBuffer;
public interface WireFetchHandler {
void onStart();
void onReady(ByteBuffer result);
void onError(Throwable error);
void onProgress(int totalBytes);
}

View file

@ -0,0 +1,39 @@
package love.distributedrebirth.no2all.wire.fetch;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
public final class WireFetchResource {
private final Map<String, String> headers = new HashMap<>();
private final URI uri;
private int timeoutMs = 10;
public WireFetchResource(URI uri) {
this.uri = uri;
this.timeoutMs = 10;
}
public URI getUrId() {
return uri;
}
public int getTimeoutMs() {
return timeoutMs;
}
public Map<String, String> getHeaders() {
return headers;
}
public WireFetchResource withTimeoutMs(int timeoutMs) {
this.timeoutMs = timeoutMs;
return this;
}
public WireFetchResource withHeader(String key, String value) {
headers.put(key, value);
return this;
}
}