Skip to content

Commit 9e8edcf

Browse files
committed
Removed setMaxPortShift(int) method, added setPort(IntStream) instead
1 parent 9fe527e commit 9e8edcf

File tree

6 files changed

+47
-29
lines changed

6 files changed

+47
-29
lines changed

.github/workflows/build.yml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,39 @@ jobs:
1313
steps:
1414
- name: Checkout
1515
uses: actions/checkout@v3
16+
1617
- name: Set up JDK 21
1718
uses: actions/setup-java@v3
1819
with:
1920
distribution: 'temurin'
2021
java-version: '21'
2122
cache: gradle
23+
2224
- name: Grant execute permission for gradlew
2325
run: chmod +x gradlew
26+
2427
- name: Validate Gradle Wrapper
2528
uses: gradle/wrapper-validation-action@v1
26-
- name: Build and Publish with Gradle
29+
30+
- name: Build with Gradle
31+
uses: gradle/gradle-build-action@v2
32+
with:
33+
arguments: build -x test --stacktrace --no-daemon
34+
35+
- name: Publish to latvian.dev Maven
2736
uses: gradle/gradle-build-action@v2
2837
env:
38+
MAVEN_URL: 'https://maven.latvian.dev/releases'
39+
MAVEN_USERNAME: 'lat'
2940
MAVEN_TOKEN: ${{ secrets.MAVEN_TOKEN }}
30-
SAPS_TOKEN: ${{ secrets.SAPS_TOKEN }}
3141
with:
32-
arguments: build -x test publish --stacktrace --no-daemon
42+
arguments: publish --stacktrace --no-daemon
43+
44+
- name: Publish to saps.dev Maven
45+
uses: gradle/gradle-build-action@v2
46+
env:
47+
MAVEN_URL: 'https://maven.saps.dev/releases'
48+
MAVEN_USERNAME: 'latvian'
49+
MAVEN_TOKEN: ${{ secrets.SAPS_TOKEN }}
50+
with:
51+
arguments: publish --stacktrace --no-daemon

build.gradle

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,12 @@ publishing {
5151
}
5252

5353
repositories {
54-
if (ENV.MAVEN_TOKEN) {
54+
if (ENV.MAVEN_URL && ENV.MAVEN_USERNAME && ENV.MAVEN_TOKEN) {
5555
maven {
56-
url "https://maven.latvian.dev/releases"
56+
url = ENV.MAVEN_URL
5757
credentials {
58-
username = "lat"
59-
password = "${ENV.MAVEN_TOKEN}"
60-
}
61-
}
62-
}
63-
64-
if (ENV.SAPS_TOKEN) {
65-
maven {
66-
url "https://maven.saps.dev/releases"
67-
credentials {
68-
username = "latvian"
69-
password = "${ENV.SAPS_TOKEN}"
58+
username = ENV.MAVEN_USERNAME
59+
password = ENV.MAVEN_TOKEN
7060
}
7161
}
7262
}

src/main/java/dev/latvian/apps/tinyserver/HTTPConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected void closed(StatusCode reason) {
121121
}
122122

123123
protected void error(Throwable error) {
124-
if (!(error instanceof SocketTimeoutException || error instanceof ClosedChannelException)) {
124+
if (!(error instanceof SocketTimeoutException || error instanceof ClosedChannelException || error instanceof IOException io && "Broken pipe".equals(io.getMessage()))) {
125125
error.printStackTrace();
126126
}
127127
}

src/main/java/dev/latvian/apps/tinyserver/HTTPServer.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
import java.util.Set;
3535
import java.util.function.Supplier;
3636
import java.util.stream.Collectors;
37+
import java.util.stream.IntStream;
3738
import java.util.stream.Stream;
3839

3940
public class HTTPServer<REQ extends HTTPRequest> implements Runnable, ServerRegistry<REQ> {
4041
private static final Object DUMMY = new Object();
42+
private static final int[] DEFAULT_PORTS = {8080};
4143

4244
private final Supplier<REQ> requestFactory;
4345
private final Map<HTTPMethod, HandlerList<REQ>> handlers;
@@ -48,8 +50,7 @@ public class HTTPServer<REQ extends HTTPRequest> implements Runnable, ServerRegi
4850
//private Selector selector;
4951
private ServerSocketChannel serverSocketChannel;
5052
private String address;
51-
private int port = 8080;
52-
private int maxPortShift = 0;
53+
private int[] ports = DEFAULT_PORTS;
5354
private boolean daemon = false;
5455
private int bufferSize = 0;
5556
private int maxKeepAliveConnections = 100;
@@ -74,11 +75,11 @@ public void setAddress(String address) {
7475
}
7576

7677
public void setPort(int port) {
77-
this.port = port;
78+
this.ports = new int[]{port};
7879
}
7980

80-
public void setMaxPortShift(int maxPortShift) {
81-
this.maxPortShift = Math.max(maxPortShift, 0);
81+
public void setPort(IntStream range) {
82+
this.ports = range.toArray();
8283
}
8384

8485
public void setDaemon(boolean daemon) {
@@ -104,12 +105,17 @@ public boolean isRunning() {
104105
public int start() {
105106
if (serverSocketChannel != null) {
106107
throw new IllegalStateException("Server is already running");
108+
} else if (ports.length == 0) {
109+
return -1;
107110
}
108111

109112
int boundPort = -1;
110113

111-
try {
114+
Arrays.sort(ports);
115+
int minPort = ports[0];
116+
int maxPort = ports[ports.length - 1];
112117

118+
try {
113119
serverSocketChannel = ServerSocketChannel.open();
114120
serverSocketChannel.configureBlocking(false);
115121

@@ -121,7 +127,7 @@ public int start() {
121127

122128
var inetAddress = address == null ? null : InetAddress.getByName(address);
123129

124-
for (int i = port; i <= port + maxPortShift; i++) {
130+
for (int i : ports) {
125131
try {
126132
socket.bind(new InetSocketAddress(inetAddress, i));
127133
boundPort = i;
@@ -137,7 +143,7 @@ public int start() {
137143
}
138144

139145
if (boundPort == -1) {
140-
throw new BindFailedException(port, port + maxPortShift);
146+
throw new BindFailedException(minPort, maxPort);
141147
}
142148

143149
startThread();

src/test/java/dev/latvian/apps/tinyserver/test/TestConnection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.latvian.apps.tinyserver.HTTPServer;
55
import dev.latvian.apps.tinyserver.StatusCode;
66

7+
import java.io.IOException;
78
import java.net.SocketTimeoutException;
89
import java.nio.channels.ClosedChannelException;
910
import java.nio.channels.SocketChannel;
@@ -28,6 +29,8 @@ protected void closed(StatusCode reason) {
2829
protected void error(Throwable error) {
2930
if (error instanceof SocketTimeoutException || error instanceof ClosedChannelException) {
3031
System.out.println("\u001B[31mConnection " + this + " timed out\u001B[0m");
32+
} else if (error instanceof IOException io && "Broken pipe".equals(io.getMessage())) {
33+
System.out.println("\u001B[31mConnection " + this + " terminated\u001B[0m");
3134
} else {
3235
error.printStackTrace();
3336
}

src/test/java/dev/latvian/apps/tinyserver/test/TinyServerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.time.Duration;
1414
import java.util.ArrayList;
1515
import java.util.stream.Collectors;
16+
import java.util.stream.IntStream;
1617

1718
public class TinyServerTest {
1819
public static TestServer server;
@@ -22,8 +23,7 @@ public static void main(String[] args) throws IOException {
2223
server = new TestServer();
2324
server.setServerName("TinyServer Test");
2425
server.setAddress("127.0.0.1");
25-
server.setPort(8080);
26-
server.setMaxPortShift(10);
26+
server.setPort(IntStream.range(8080, 8080 + 10));
2727
server.setDaemon(false);
2828
server.setKeepAliveTimeout(Duration.ofSeconds(5L));
2929
server.setMaxKeepAliveConnections(5);
@@ -51,7 +51,7 @@ public static void main(String[] args) throws IOException {
5151

5252
wsHandler = server.ws("/console/{console-type}", TestWSSession::new);
5353

54-
System.out.println("Started server at https://localhost:" + server.start());
54+
System.out.println("Started server at http://localhost:" + server.start());
5555

5656
while (server.isRunning()) {
5757
var sb = new StringBuilder();

0 commit comments

Comments
 (0)