Search in sources :

Example 1 with PacketHandler

use of de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler in project Hydra by DataSecs.

the class SamplePacketListener 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(SamplePacket samplePacket, Session session) {
    System.out.println("\n---PACKET-LISTENER OUTPUT---");
    // Process received packet
    System.out.printf("Received from client: %s%n", samplePacket);
    // Send response
    session.send(new SamplePacket("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());
}
Also used : SamplePacket(server.packets.SamplePacket) PacketHandler(de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler)

Example 2 with PacketHandler

use of de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler 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());
}
Also used : ExamplePacket(de.datasecs.hydra.example.shared.ExamplePacket) PacketHandler(de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler)

Example 3 with PacketHandler

use of de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler in project Hydra by DataSecs.

the class TestServerPacketListener method onStandardPacket.

@PacketHandler
public void onStandardPacket(StandardPacket standardPacket, Session session) {
    Assertions.assertTrue(session.isConnected());
    session.send(new StandardPacket("#Received!"));
}
Also used : StandardPacket(de.datasecs.hydra.shared.protocol.packets.StandardPacket) PacketHandler(de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler)

Example 4 with PacketHandler

use of de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler in project Hydra by DataSecs.

the class TestServerPacketListener method onTestPacket.

@PacketHandler
public void onTestPacket(TestPacket examplePacket, Session session) {
    Assertions.assertTrue(session.isConnected());
    switch(examplePacket.getNumber()) {
        case 0:
            Assertions.assertEquals("Test", examplePacket.getObject().toString());
            break;
    }
    session.send(new StandardPacket("#Received!"));
    session.send(new TestPacket(examplePacket.getNumber(), "test"));
}
Also used : TestPacket(de.datasecs.hydra.shared.TestPacket) StandardPacket(de.datasecs.hydra.shared.protocol.packets.StandardPacket) PacketHandler(de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler)

Aggregations

PacketHandler (de.datasecs.hydra.shared.protocol.packets.listener.PacketHandler)4 StandardPacket (de.datasecs.hydra.shared.protocol.packets.StandardPacket)2 ExamplePacket (de.datasecs.hydra.example.shared.ExamplePacket)1 TestPacket (de.datasecs.hydra.shared.TestPacket)1 SamplePacket (server.packets.SamplePacket)1