Skip to content

marcluque/Hydra

Repository files navigation

default-cropped

Maven Central BCH compliance Build Status

SonarCloud
Quality Gate Status Coverage Code Smells Bugs Vulnerabilities Reliability Rating Security Rating Maintainability Rating Duplicated Lines (%) Lines of Code

License

Description

Hydra is built upon Netty. It is supposed to simplify the process of socket setup in Java. Netty allows high performances and good maintainability of programs built upon it. And here comes Hydra in. Hydra uses the builder-pattern in order to make the process of socket setup even simpler. It comes with a handy packet system that allows you to easily create your own packets and send them via the session Hydra creates for you. Furthermore, you have the ability to create packets and listener which just need a simple annotation and will be invoked by Hydra when a packet is received. Convince yourself by taking a look at the client and server examples.

Wiki

In case you would like to have an in-depth introduction to Hydra, please take a look at the wiki. The wiki takes you step-by-step through the setup of a server and a client. Furthermore, the wiki features example usages, like a simple chat application and a key-value store.

Quantitative benefits over netty

Netty code for server setup:

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup)
     .channel(NioServerSocketChannel.class)
     .childHandler(new ChannelInitializer<SocketChannel>() {
         @Override
         public void initChannel(SocketChannel ch) throws Exception {
             ch.pipeline().addLast(new ServerHandler());
         }
     })
     .option(ChannelOption.SO_BACKLOG, 128);
     .childOption(ChannelOption.SO_KEEPALIVE, true);
     
    ChannelFuture f = b.bind("localhost", 8888).sync();
    f.channel().closeFuture().sync();
} finally {
    workerGroup.shutdownGracefully();
    bossGroup.shutdownGracefully();
}

Hydra code for server setup:

HydraServer server = new Server.Builder("localhost", 8888, new SampleProtocol())
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .build();

Note that ServerHandler and SampleProtocol have approximately the same size/complexity.

Installing

  • Install Maven 3
  • Clone/Download this repo
  • Install it with: mvn -U -Dmaven.test.skip=true clean compile install

Maven

Local Maven dependency

<dependency>
    <groupId>com.marcluque</groupId>
    <artifactId>hydra-all</artifactId>
    <version>1.6.5</version>
</dependency>

Maven central

<dependency>
    <groupId>com.marcluque</groupId>
    <artifactId>hydra-all</artifactId>
    <version>1.6.5</version>
</dependency>

If you would like to just have the client or server, use hydra-client or hydra-server instead of hydra-all as artifact id.

And if you don't use maven you can download a release version and include it in your project the way you prefer.

Examples

Client

HydraClient client = new Client.Builder("localhost", 8888, new SampleProtocol())
                .workerThreads(4)
                .option(ChannelOption.TCP_NODELAY, true)
                .build();

This is an easy-to-understand example of how to create a client socket. In order to make the packet system work, you have to register your created packets and listeners. For detailed information on how to do that and examples see the client example.

Server

HydraServer server = new Server.Builder("localhost", 8888, new SampleProtocol())
                .bossThreads(2)
                .workerThreads(4)
                .option(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .build();

This here is an example of how to create a server socket. In order to make the packet system work, you have to register your created packets and listeners. For detailed information on how to do that and examples see the server example.

Future features

  • Log4J configuration
  • SSL support
  • Finish UDP support

Javadoc

The javadoc is always up-to-date and can be found on marcluque.com/hydra/javadoc/

License

Licensed under the BSD 2-Clause License - see the LICENSE file for details.