-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate abstract server class for server and node
- Loading branch information
Showing
7 changed files
with
154 additions
and
60 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
src/main/java/dev/httpmarco/netline/channel/NetClientChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/dev/httpmarco/netline/node/NetNodeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package dev.httpmarco.netline.node; | ||
|
||
import dev.httpmarco.netline.config.CompConfig; | ||
|
||
public final class NetNodeConfig extends CompConfig { | ||
|
||
public NetNodeConfig() { | ||
super("0.0.0.0", 9091, true, 5); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/dev/httpmarco/netline/node/NetNodeState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dev.httpmarco.netline.node; | ||
|
||
public enum NetNodeState { | ||
|
||
// if node is present, but not started booting | ||
INITIALIZING, | ||
// if node is booting and failed to bind to cluster | ||
FAILED, | ||
// if node is booting and binding to cluster (ip pool) | ||
BIND_CLUSTER, | ||
// if node is booting and syncing with cluster | ||
SYNC_CLUSTER, | ||
// if node is booting and ready to accept connections | ||
READY, | ||
// if node is shutting down | ||
CLOSED | ||
|
||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/dev/httpmarco/netline/server/AbstractNetServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.httpmarco.netline.server; | ||
|
||
import dev.httpmarco.netline.NetChannel; | ||
import dev.httpmarco.netline.NetCompHandler; | ||
import dev.httpmarco.netline.channel.NetChannelInitializer; | ||
import dev.httpmarco.netline.config.CompConfig; | ||
import dev.httpmarco.netline.impl.AbstractNetCompImpl; | ||
import dev.httpmarco.netline.utils.NetworkUtils; | ||
import io.netty5.bootstrap.ServerBootstrap; | ||
import io.netty5.channel.ChannelOption; | ||
import io.netty5.channel.EventLoopGroup; | ||
import io.netty5.channel.epoll.Epoll; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public abstract class AbstractNetServer<C extends CompConfig> extends AbstractNetCompImpl<C> { | ||
|
||
private final EventLoopGroup workerGroup = NetworkUtils.createEventLoopGroup(0); | ||
|
||
public AbstractNetServer(C config) { | ||
super(1, config); | ||
} | ||
|
||
|
||
@Override | ||
public CompletableFuture<Void> boot() { | ||
var future = new CompletableFuture<Void>(); | ||
|
||
var bootstrap = new ServerBootstrap() | ||
.group(bossGroup(), workerGroup) | ||
.childHandler(new NetChannelInitializer(handler())) | ||
.channelFactory(NetworkUtils.generateChannelFactory()) | ||
.childOption(ChannelOption.TCP_NODELAY, true) | ||
.childOption(ChannelOption.IP_TOS, 24) | ||
.childOption(ChannelOption.SO_KEEPALIVE, true); | ||
|
||
if (config().tryTcpFastOpen() && Epoll.isTcpFastOpenServerSideAvailable()) { | ||
bootstrap.childOption(ChannelOption.TCP_FASTOPEN_CONNECT, true); | ||
} | ||
|
||
bootstrap.bind(config().hostname(), config().port()).addListener(it -> { | ||
if(it.isSuccess()) { | ||
this.onBindSuccess(); | ||
future.complete(null); | ||
return; | ||
} | ||
this.onBindFail(it.cause()); | ||
future.completeExceptionally(it.cause()); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
@Override | ||
public @NotNull CompletableFuture<Void> close() { | ||
var future = new CompletableFuture<Void>(); | ||
this.bossGroup().shutdownGracefully().addListener(it -> onClose().whenComplete((unused, throwable) -> future.complete(null))); | ||
return future; | ||
} | ||
|
||
public abstract CompletableFuture<Void> onClose(); | ||
|
||
public abstract void onBindFail(Throwable throwable); | ||
|
||
public abstract void onBindSuccess(); | ||
|
||
public abstract NetCompHandler handler(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.httpmarco.netline.tests; | ||
|
||
import dev.httpmarco.netline.node.NetNodeState; | ||
import org.junit.jupiter.api.*; | ||
|
||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
@DisplayName("5 - Node cluster test") | ||
public class NodeTest { | ||
|
||
/* | ||
private static NetNode node; | ||
@BeforeAll | ||
public static void beforeHandling() { | ||
node = new NetNode(); | ||
} | ||
@Test | ||
@Order(1) | ||
@DisplayName("5.1 Start first node") | ||
public void testState() { | ||
assert node.state() == NetNodeState.INITIALIZING; | ||
node.bootSync(); | ||
assert node.state() == NetNodeState.READY; | ||
} | ||
*/ | ||
} |