use of de.datasecs.hydra.example.shared.ExamplePacket in project Hydra by DataSecs.
the class ExampleServerPacketListener method onSamplePacket.
/* Use the @PacketHandler annotation on methods that are supposed to handle packets.
* The amount of parameters has always to be equal 2 and in the given order. (Packet class and then the session)
*/
@PacketHandler
public void onSamplePacket(ExamplePacket examplePacket, Session session) {
System.out.println("\n---PACKET-LISTENER OUTPUT---");
// Process received packet
System.out.printf("Received from client: %s%n", examplePacket);
// Send response
session.send(new ExamplePacket("This is a response", new String[] { "This", "is", "a", "response" }));
// Returns whether the session is active
System.out.println("\nIs session active?: " + session.isConnected());
// Returns the local or remote address, depending if it's the server or the client
System.out.println("Local server address: " + session.getAddress());
// TODO: See if it can be checked whether the session is already closed and return a boolean for that
// Closes the session, this does not stop the server. It just closes the channel!
session.close();
System.out.println("\nSession closed!");
// Check again if session is active
System.out.println("Is session active?: " + session.isConnected());
}
use of de.datasecs.hydra.example.shared.ExamplePacket 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 ExampleClientProtocol()).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 ExamplePacket.class
session.send(new ExamplePacket("This is a message", new String[] { "This", "is", "a", "message in an array" }));
// Sends a list, that is converted to a Object and the array, like above
session.send(new ExamplePacket(Arrays.asList("This", "is", "a", "message", "in a list"), new String[] { "This", "is", "a", "message", "in an array" }));
/* Sends an object the user wants to send with the limitation that the object has to be serializable.
* Hydra internally uses a standard packet that comes ready out of the box. The only thing that is important to notice
* is the fact, that the Handler for the packet still has to be created by the user itself. Therefore see
* the ExampleClientPacketListener of the server example classes.
*/
session.send("This is a String and dealt with as object by Hydra");
// Closes the connection and releases all occupied resources
// client.close();
}
Aggregations