use of de.datasecs.hydra.shared.handler.listener.HydraSessionListener in project Hydra by DataSecs.
the class ExampleServer method main.
public static void main(String[] args) {
/*
* The session listener adds a listener to the server that is supposed to be called when
* a session is created (in this case, when a client connects). For demonstration purposes
* this is done via a direct instantiation (anonymous class). It is advised to do this in a separate class
* for clearness, especially when there are other methods than just the two small from the
* SessionListener interface.
*/
// The builder returns a server which you can use for several things
HydraServer server = new Server.Builder("localhost", 8888, new ExampleServerProtocol()).bossThreads(2).workerThreads(4).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).addListener(new HydraSessionListener() {
@Override
public void onConnected(Session session) {
System.out.println("\nClient connected!");
}
@Override
public void onDisconnected(Session session) {
System.out.println("\nClient disconnected!");
}
}).build();
// Check if server is actively running (not obligatory)
if (server.isActive()) {
System.out.println("Server is online!");
// Returns the local address of the server that was set in the constructor
System.out.printf("Socket address: %s%n", server.getLocalAdress());
}
// As soon as a channel with a client is initialized it is added to the set of sessions
// If no clients are connected the set is empty
System.out.println("Sessions: " + server.getSessions());
// Closes the server and releases the occupied resources
// server.close();
}
use of de.datasecs.hydra.shared.handler.listener.HydraSessionListener in project Hydra by DataSecs.
the class ExampleSerializationServer method main.
public static void main(String[] args) {
HydraServer server = new Server.Builder("localhost", 8888, new ExampleSerializationServerProtocol()).bossThreads(2).workerThreads(4).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).addListener(new HydraSessionListener() {
@Override
public void onConnected(Session session) {
System.out.println("\nClient connected!");
}
@Override
public void onDisconnected(Session session) {
System.out.println("\nClient disconnected!");
}
}).build();
if (server.isActive()) {
System.out.println("Server is online!");
// Returns the local address of the server that was set in the constructor
System.out.printf("Socket address: %s%n", server.getLocalAdress());
}
}
use of de.datasecs.hydra.shared.handler.listener.HydraSessionListener in project Hydra by DataSecs.
the class ExampleClient method main.
public static void main(String[] args) {
/*
* The session listener is optional, that's why it's a method that may be called in the builder.
* It adds a listener to the client and is supposed to be called when
* a session is created (in this case, when the client connects to a server). For demonstration purposes
* this is done via a direct instantiation (anonymous class). It's advised to do this in a separate class
* for clearness, especially when there are other methods than just the two small from the
* SessionListener interface.
*/
// The builder returns a session which you can use for several things
HydraClient client = new Client.Builder("localhost", 8888, new SampleProtocol()).workerThreads(4).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).addSessionListener(new HydraSessionListener() {
@Override
public void onConnected(Session session) {
System.out.println("Connected to server!");
}
@Override
public void onDisconnected(Session session) {
System.out.println("\nDisconnected from server!");
}
}).build();
// Checks if the client is connected to its remote host (not obligatory)
if (client.isConnected()) {
// Returns the session that was created for the client and its remote host
session = client.getSession();
System.out.println("\nClient is online!");
System.out.printf("Socket address: %s%n", session.getAddress());
}
/* Send a packet to the server via the session the client has saved */
// Sends a String, that is converted to a Object and an array, the type of the array is defined in SamplePacket.class
session.send(new SamplePacket("This is a message", new String[] { "This", "is", "a", "message" }));
// Sends a list, that is converted to a Object and the array, like above
session.send(new SamplePacket(Arrays.asList("This", "is", "a", "message", "2"), new String[] { "This", "is", "a", "message", "2" }));
// Closes the connection and releases all occupied resources
// client.close();
}
use of de.datasecs.hydra.shared.handler.listener.HydraSessionListener in project Hydra by DataSecs.
the class HydraTest method init.
public static void init() {
server = new Server.Builder("localhost", 8888, new TestServerProtocol()).addListener(new HydraSessionListener() {
@Override
public void onConnected(Session session) {
System.out.println("TestClient connected!");
synchronized (LOCK) {
clientConnected = true;
LOCK.notify();
}
}
@Override
public void onDisconnected(Session session) {
System.out.println("TestClient disconnected!");
}
}).option(ChannelOption.SO_BACKLOG, 200).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).build();
System.out.println("Server started successfully!");
client = new Client.Builder("localhost", 8888, new TestClientProtocol()).workerThreads(4).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).addSessionListener(new HydraSessionListener() {
@Override
public void onConnected(Session session) {
System.out.println("Connected to server!");
}
@Override
public void onDisconnected(Session session) {
System.out.println("\nDisconnected from server!");
}
}).build();
System.out.println("Client started successfully!");
}
use of de.datasecs.hydra.shared.handler.listener.HydraSessionListener in project Hydra by DataSecs.
the class ExampleSerializationClient method main.
public static void main(String[] args) {
HydraClient client = new Client.Builder("localhost", 8888, new ExampleSerializationClientProtocol()).workerThreads(4).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).addSessionListener(new HydraSessionListener() {
@Override
public void onConnected(Session session) {
System.out.println("Connected to server!");
}
@Override
public void onDisconnected(Session session) {
System.out.println("\nDisconnected from server!");
}
}).build();
if (client.isConnected()) {
session = client.getSession();
System.out.println("\nClient is online!");
System.out.printf("Socket address: %s%n", session.getAddress());
}
// Create custom classes and necessary stuff for example serialization
List<String> testStringList = new ArrayList<>();
testStringList.add("Hydra");
testStringList.add("Serialization");
testStringList.add("Test");
CustomClassExtended customClassExtended = new CustomClassExtended("testStringExtended", UUID.randomUUID(), 5L, Integer.class);
CustomClass customClass = new CustomClass("testString", 1, new String[] { "Hydra", "serialization" }, testStringList, "this is a random object", customClassExtended);
// Sends the instance of a custom class, that is create and filled with data above
session.send(new ExampleSerializationPacket(customClass));
}
Aggregations