forked from reactive-ipc/reactive-ipc-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpServer.java
39 lines (27 loc) · 974 Bytes
/
TcpServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package io.ripc.protocol.tcp;
import java.util.concurrent.atomic.AtomicBoolean;
public abstract class TcpServer<R, W> {
protected final TcpHandler<R, W> thisHandler;
protected final AtomicBoolean started;
protected TcpServer() {
thisHandler = null;
this.started = new AtomicBoolean();
}
public final TcpServer<R, W> start(TcpHandler<R, W> handler) {
if (!started.compareAndSet(false, true)) {
throw new IllegalStateException("Server already started");
}
doStart(handler);
return this;
}
public final void startAndAwait(TcpHandler<R, W> handler) {
start(handler);
awaitShutdown();
}
public final boolean shutdown() {
return !started.compareAndSet(true, false) || doShutdown();
}
public abstract void awaitShutdown();
public abstract boolean doShutdown();
protected abstract TcpServer<R, W> doStart(TcpHandler<R, W> handler);
}