A simple and lightweight network library for Java.
The libs are hosted on the Sonatype Nexus Repository. You can add the following dependency to your project.
<repository>
<id>netline-central-snapshot</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
<dependency>
<groupId>dev.httpmarco</groupId>
<artifactId>netline</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
compile "dev.httpmarco:netline:1.0.0-SNAPSHOT"
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
implementation("dev.httpmarco:netline:1.0.0-SNAPSHOT")
Below is an example of a simple server component. The API is similar for a client or, in the future, a node.
Net.line()
// Set the type of the component.
// You can also use: client or, in the future, a node.
.server()
// If you want to configure your component, you can do the following:
.config(it -> {
// Set the hostname for binding. The default is '0.0.0.0'.
it.hostname("xx.xx.xx.xx");
// Set the port for binding. The default is '9091'.
it.port(1234);
})
// Register a new channel tracking. See the tracking table for more details.
.track(YourExamplePacket.class, (channel, packet) -> {
// Add your logic here when 'YourExamplePacket' is received.
System.out.println("Packet received! Hello!");
});
With Netline, you can easily send a request packet to a component (comp) and receive a response. This can be used either asynchronously or synchronously.
// For every request, you need a responder to handle the request and send back a response.
// You can create it as follows:
// Important: The request ID and responder ID must match for each request-response pair!
server.responderOf("your_custom_request_id", (channel, id) -> new ResponsePacketType());
var request = client.request("your_custom_request_id", ResponsePacketType.class);
// Get the response of your request synchronously.
// The default timeout limit is 5 seconds. You can customize this in your component configuration.
ResponsePacketType response = request.sync();
// The same request but handled asynchronously.
request.async().whenComplete((result, throwable) -> {
if (throwable != null) {
// The request failed. You must check the cause of the exception!
throwable.printStackTrace();
return;
}
// The request was successfully answered.
ResponsePacketType asyncResponse = result;
});
// send a packet to all clients and servers
client.generateBroadcast().send(new YourBroadcastPacket());
// send a packet only to selected clients
client.generateBroadcast()
.to("clientA", "clientB")
.send(new YourBroadcastPacket());
// send a packet to all, but skip a specific client(s). Here 'clientA'
client.generateBroadcast()
.exclude("clientA")
.send(new YourBroadcastPacket());
// send a packet to all clients and servers, but also to his own client
client.generateBroadcast()
.generateBroadcast()
.send(new YourBroadcastPacket());
// send a packet to the first 2 endpoints. Left clients are be ignored!
client.generateBroadcast()
.limt(2)
.send(new YourBroadcastPacket());
// All this options can be merged together in one broadcast option.
// Important: All options are optional!
client.generateBroadcast()
.limit(10) // maximum client amount
.exclude("trash-service") // ignore the trash services
.to("rich-service", "babo-service") // we select the best services
.deselect(Receiver.SERVER) // send only to all clients
.send(new YourBroadcastPacket()); // finally sending the broadcast
You can send a packet from a client connected to the server, routing it through the server to a specific client. In this example, the packet is sent to client A.
client.send("clientA", new YourRedirectPacket());
var client = Net.line().client();
var server = Net.line().server();
server.bootSync();
// alert a packet to all connected clients
server.broadcast(new YourPacketType());
// alert a packet to a specific client
server.send("clientA", new YourPacketType());
// alert a packet to all clients with an id starting with "stats"
server.send(it -> it.id().startWith("stats"), new YourPacketType());
// get all connected clients
server.clients();
var server = Net.line().server();
server.config(it -> {
// blacklist a specific address
it.blacklist().add("xx.xx.xx.xx");
// whitelist a specific address
it.whitelist().add("xx.xx.xx.xx");
});
server.bootSync();
The security provider allow you, to manage your clients with a custom security policy. Block or allow clients, based on your custom logic. A simpler way are the blacklist and whitelist.
// set your custom security provider -> child of @SecurityProvider
server.withSecurityPolicy(new YourCustomSecurityProvider());
Example of a simple security provider:
public class YourCustomSecurityProvider implements SecurityProvider {
@Override
public void detectUnauthorizedAccess(NetChannel netChannel) {
// alert if a client tries to connect without permission
// Channel closed automatically after this method.
System.err.println("Unauthorized access detected");
}
@Override
public boolean authenticate(NetChannel netChannel) {
// check the id of the client. Here you can implement your custom logic.
return netChannel.id().equals("testA");
}
}
Tracking | Description |
---|---|
ClientConnectedTracking.class | If the client connects to the server, this tracking will be triggered. |
ClientDisconnectedTracking.class | If the client disconnects from the server, this tracking will be triggered. |
- Node implementation
- test for bad response answer
- Add a better packet base logic
- message channels