Search in sources :

Example 1 with Client

use of de.datasecs.hydra.client.Client in project Hydra by DataSecs.

the class ExampleSerializationClient method main.

public static void main(String[] args) {
    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();
    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 SampleSerializationPacket(customClass));
}
Also used : HydraClient(de.datasecs.hydra.client.HydraClient) SampleSerializationPacket(client.packets.SampleSerializationPacket) ArrayList(java.util.ArrayList) HydraClient(de.datasecs.hydra.client.HydraClient) Client(de.datasecs.hydra.client.Client) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) SampleProtocol(client.SampleProtocol) Session(de.datasecs.hydra.shared.handler.Session)

Example 2 with Client

use of de.datasecs.hydra.client.Client 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();
}
Also used : HydraClient(de.datasecs.hydra.client.HydraClient) ExamplePacket(de.datasecs.hydra.example.shared.ExamplePacket) HydraClient(de.datasecs.hydra.client.HydraClient) Client(de.datasecs.hydra.client.Client) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) Session(de.datasecs.hydra.shared.handler.Session)

Example 3 with Client

use of de.datasecs.hydra.client.Client 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));
}
Also used : HydraClient(de.datasecs.hydra.client.HydraClient) ExampleSerializationPacket(de.datasecs.hydra.example.shared.serialization.ExampleSerializationPacket) ArrayList(java.util.ArrayList) HydraClient(de.datasecs.hydra.client.HydraClient) Client(de.datasecs.hydra.client.Client) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) CustomClassExtended(de.datasecs.hydra.example.shared.serialization.CustomClassExtended) Session(de.datasecs.hydra.shared.handler.Session) CustomClass(de.datasecs.hydra.example.shared.serialization.CustomClass)

Example 4 with Client

use of de.datasecs.hydra.client.Client in project Hydra by DataSecs.

the class UdpClient method main.

public static void main(String[] args) {
    HydraClient client = new Client.Builder("localhost", 8888, new UdpClientProtocol()).useUDP(true).option(ChannelOption.SO_BROADCAST, true).build();
    if (client.isConnected()) {
        session = client.getSession();
        System.out.println("\nClient is online!");
        System.out.printf("Socket address: %s%n", session.getAddress());
    }
    System.out.println(session);
    try {
        session.getChannel().writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), SocketUtils.socketAddress("localhost", 8888))).sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // Send something simple
    session.send("This is a String and dealt with as object by Hydra");
}
Also used : HydraClient(de.datasecs.hydra.client.HydraClient) DatagramPacket(io.netty.channel.socket.DatagramPacket) HydraClient(de.datasecs.hydra.client.HydraClient) Client(de.datasecs.hydra.client.Client)

Example 5 with Client

use of de.datasecs.hydra.client.Client 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();
}
Also used : SamplePacket(client.packets.SamplePacket) HydraClient(de.datasecs.hydra.client.HydraClient) HydraClient(de.datasecs.hydra.client.HydraClient) Client(de.datasecs.hydra.client.Client) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) Session(de.datasecs.hydra.shared.handler.Session)

Aggregations

Client (de.datasecs.hydra.client.Client)5 HydraClient (de.datasecs.hydra.client.HydraClient)5 Session (de.datasecs.hydra.shared.handler.Session)4 HydraSessionListener (de.datasecs.hydra.shared.handler.listener.HydraSessionListener)4 ArrayList (java.util.ArrayList)2 SampleProtocol (client.SampleProtocol)1 SamplePacket (client.packets.SamplePacket)1 SampleSerializationPacket (client.packets.SampleSerializationPacket)1 ExamplePacket (de.datasecs.hydra.example.shared.ExamplePacket)1 CustomClass (de.datasecs.hydra.example.shared.serialization.CustomClass)1 CustomClassExtended (de.datasecs.hydra.example.shared.serialization.CustomClassExtended)1 ExampleSerializationPacket (de.datasecs.hydra.example.shared.serialization.ExampleSerializationPacket)1 DatagramPacket (io.netty.channel.socket.DatagramPacket)1