Skip to content

Commit

Permalink
v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo-MG committed May 11, 2024
2 parents 9457613 + a8333f2 commit 9385b46
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 116 deletions.
44 changes: 0 additions & 44 deletions .github/workflows/doc_deployment.yml

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>com.bernardomg.example</groupId>
<artifactId>netty-tcp-server-example</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<packaging>jar</packaging>

<name>Netty TCP Server Example</name>
Expand Down
34 changes: 6 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mvn clean package
The JAR will be a runnable Java file. It can be executed like this:

```
java -jar target/server.jar start --port=8080
java -jar target/server.jar start --response=Acknowledged --port=8080
```

To show other commands:
Expand All @@ -20,12 +20,6 @@ To show other commands:
java -jar target/server.jar -h
```

[![Release docs](https://img.shields.io/badge/docs-release-blue.svg)][site-release]
[![Development docs](https://img.shields.io/badge/docs-develop-blue.svg)][site-develop]

[![Release javadocs](https://img.shields.io/badge/javadocs-release-blue.svg)][javadoc-release]
[![Development javadocs](https://img.shields.io/badge/javadocs-develop-blue.svg)][javadoc-develop]

## Other Netty examples

### TCP
Expand All @@ -40,10 +34,10 @@ java -jar target/server.jar -h
- [Reactor Netty TCP Server Example](https://github.com/Bernardo-MG/reactor-netty-tcp-server-example)
- [Reactor Netty TCP Proxy Example](https://github.com/Bernardo-MG/reactor-netty-tcp-proxy-example)

### HTTP
### HTTP Reactive

- [Netty HTTP Client Example](https://github.com/Bernardo-MG/reactor-netty-http-client-example)
- [Netty HTTP Server Example](https://github.com/Bernardo-MG/reactor-netty-http-server-example)
- [Reactor Netty HTTP Client Example](https://github.com/Bernardo-MG/reactor-netty-http-client-example)
- [Reactor Netty HTTP Server Example](https://github.com/Bernardo-MG/reactor-netty-http-server-example)

## Features

Expand All @@ -56,25 +50,13 @@ java -jar target/server.jar -h

## Documentation

Documentation is always generated for the latest release, kept in the 'master' branch:

- The [latest release documentation page][site-release].
- The [latest release Javadoc site][javadoc-release].

Documentation is also generated from the latest snapshot, taken from the 'develop' branch:

- The [the latest snapshot documentation page][site-develop].
- The [latest snapshot Javadoc site][javadoc-develop].

### Building the docs

The documentation site is actually a Maven site, and its sources are included in the project. If required it can be generated by using the following Maven command:
The documentation site is actually a Maven site, its sources are included in the project. Can be generated by using the following Maven command:

```
mvn verify site
```

The verify phase is required, otherwise some of the reports won't be generated.
The verify phase is required, otherwise some of the reports won't be built.

## Collaborate

Expand All @@ -96,9 +78,5 @@ If you wish to fork or modify the code, visit the [GitHub project page][scm], wh
The project has been released under the [MIT License][license].

[issues]: https://github.com/bernardo-mg/netty-tcp-server-example/issues
[javadoc-develop]: https://docs.bernardomg.com/development/maven/netty-tcp-server-example/apidocs
[javadoc-release]: https://docs.bernardomg.com/maven/netty-tcp-server-example/apidocs
[license]: https://www.opensource.org/licenses/mit-license.php
[scm]: https://github.com/bernardo-mg/netty-tcp-server-example
[site-develop]: https://docs.bernardomg.com/development/maven/netty-tcp-server-example
[site-release]: https://docs.bernardomg.com/maven/netty-tcp-server-example
11 changes: 11 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,16 @@
Cleaned up code.
</action>
</release>
<release version="1.0.5" date="2024-03-28" description="Cleaned up docs">
<action dev="bmg" type="update">
Cleaned up readme.
</action>
<action dev="bmg" type="update">
Allows not setting a response.
</action>
<action dev="bmg" type="remove">
Docs are no longer deployed.
</action>
</release>
</body>
</document>
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@
import com.bernardomg.example.netty.tcp.server.TransactionListener;

/**
* Transaction listener which will write the context of each step into the CLI console.
* Transaction listener which will write the context of each step into a {@link PrintWriter}.
*
* @author Bernardo Mart&iacute;nez Garrido
*
*/
public final class CliWriterTransactionListener implements TransactionListener {
public final class TransactionPrinterListener implements TransactionListener {

/**
* Port which the server will listen to.
*/
private final Integer port;

/**
* CLI writer, to print console messages.
* Print writer, where the messages will be sent.
*/
private final PrintWriter writer;

public CliWriterTransactionListener(final Integer prt, final PrintWriter wrt) {
public TransactionPrinterListener(final Integer prt, final PrintWriter wrt) {
super();

port = Objects.requireNonNull(prt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;

import com.bernardomg.example.netty.tcp.cli.CliWriterTransactionListener;
import com.bernardomg.example.netty.tcp.cli.TransactionPrinterListener;
import com.bernardomg.example.netty.tcp.cli.version.ManifestVersionProvider;
import com.bernardomg.example.netty.tcp.server.NettyTcpServer;
import com.bernardomg.example.netty.tcp.server.Server;
import com.bernardomg.example.netty.tcp.server.TransactionListener;
import com.bernardomg.example.netty.tcp.server.channel.ListenAndAnswerChannelHandler;
import com.bernardomg.example.netty.tcp.server.channel.SinkChannelHandler;

import io.netty.channel.ChannelInboundHandlerAdapter;
import picocli.CommandLine.Command;
import picocli.CommandLine.Help;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;

/**
* Start server command.
* Start server. This creates a server which listens for requests, if the response is defined it will also answer them.
*
* @author Bernardo Mart&iacute;nez Garrido
*
Expand All @@ -57,7 +60,7 @@ public final class StartServerCommand implements Runnable {
* Debug flag. Shows debug logs.
*/
@Option(names = { "--debug" }, paramLabel = "flag", description = "Enable debug logs.", defaultValue = "false")
private Boolean debug;
private boolean debug;

/**
* Port to listen.
Expand All @@ -69,7 +72,7 @@ public final class StartServerCommand implements Runnable {
* Response to return.
*/
@Option(names = { "-r", "--response" }, paramLabel = "response",
description = "Response to send back after receiving a request.", defaultValue = "Acknowledged")
description = "Response to send back after receiving a request.")
private String response;

/**
Expand All @@ -83,7 +86,7 @@ public final class StartServerCommand implements Runnable {
*/
@Option(names = { "--verbose" }, paramLabel = "flag", description = "Print information to console.",
defaultValue = "true", showDefaultValue = Help.Visibility.ALWAYS)
private Boolean verbose;
private boolean verbose;

/**
* Default constructor.
Expand All @@ -94,9 +97,10 @@ public StartServerCommand() {

@Override
public final void run() {
final PrintWriter writer;
final Server server;
final TransactionListener listener;
final PrintWriter writer;
final Server server;
final TransactionListener listener;
final ChannelInboundHandlerAdapter adapter;

if (debug) {
activateDebugLog();
Expand All @@ -112,8 +116,14 @@ public final void run() {
}

// Create server
listener = new CliWriterTransactionListener(port, writer);
server = new NettyTcpServer(port, response, listener);
listener = new TransactionPrinterListener(port, writer);
if (response == null) {
// Missing response, will just sink requests
adapter = new SinkChannelHandler(listener);
} else {
adapter = new ListenAndAnswerChannelHandler(response, listener);
}
server = new NettyTcpServer(port, listener, adapter);

// Start server
server.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@

import java.util.Objects;

import com.bernardomg.example.netty.tcp.server.channel.ListenAndAnswerChannelInitializer;
import com.bernardomg.example.netty.tcp.server.channel.HandlerChannelInitializer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -51,49 +54,51 @@ public final class NettyTcpServer implements Server {
/**
* Group storing the server channel.
*/
private ChannelGroup channelGroup;
private ChannelGroup channelGroup;

/**
* Server secondary event loop group.
* Channel initializer.
*/
private EventLoopGroup childGroup;
private final ChannelInitializer<SocketChannel> channelInitializer;

/**
* Transaction listener. Extension hook which allows reacting to the transaction events.
* Server secondary event loop group.
*/
private final TransactionListener listener;
private EventLoopGroup childGroup;

/**
* Response to send after a request.
* Transaction listener. Extension hook which allows reacting to the transaction events.
*/
private final String messageForClient;
private final TransactionListener listener;

/**
* Server main event loop group.
*/
private EventLoopGroup parentGroup;
private EventLoopGroup parentGroup;

/**
* Port which the server will listen to.
*/
private final Integer port;
private final Integer port;

/**
* Constructs a server for the given port. The transaction listener will react to events when calling the server.
*
* @param prt
* port to listen for
* @param resp
* response to return
* @param lst
* transaction listener
* @param adapter
* channel handler adapter
*/
public NettyTcpServer(final Integer prt, final String resp, final TransactionListener lst) {
public NettyTcpServer(final Integer prt, final TransactionListener lst,
final ChannelInboundHandlerAdapter adapter) {
super();

port = Objects.requireNonNull(prt);
messageForClient = Objects.requireNonNull(resp);
listener = Objects.requireNonNull(lst);

channelInitializer = new HandlerChannelInitializer(adapter);
}

@Override
Expand Down Expand Up @@ -121,7 +126,7 @@ public final void start() {
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true)
// Child handler
.childHandler(new ListenAndAnswerChannelInitializer(messageForClient, listener));
.childHandler(channelInitializer);

try {
// Binds to the port
Expand Down
Loading

0 comments on commit 9385b46

Please sign in to comment.