Search in sources :

Example 1 with HydraServer

use of de.datasecs.hydra.server.HydraServer 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 SampleProtocol()).bossThreads(2).workerThreads(4).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).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();
}
Also used : HydraServer(de.datasecs.hydra.server.HydraServer) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) Session(de.datasecs.hydra.shared.handler.Session)

Example 2 with HydraServer

use of de.datasecs.hydra.server.HydraServer in project Hydra by DataSecs.

the class ExampleSerializationServer method main.

public static void main(String[] args) {
    HydraServer server = new Server.Builder("localhost", 8888, new SampleProtocol()).bossThreads(2).workerThreads(4).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).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());
    }
}
Also used : HydraServer(de.datasecs.hydra.server.HydraServer) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) SampleProtocol(server.SampleProtocol) Session(de.datasecs.hydra.shared.handler.Session)

Example 3 with HydraServer

use of de.datasecs.hydra.server.HydraServer in project Hydra by DataSecs.

the class ChatServer method main.

public static void main(String[] args) {
    HydraServer hydraServer = new Server.Builder("localhost", 8888, new ChatServerProtocol()).addListener(new HydraSessionListener() {

        @Override
        public void onConnected(Session session) {
            System.out.printf("User with ip %s connected!%n", session.getAddress());
            session.send(new ServerPacket("Welcome at localhost user!"));
            for (Session s : sessions) {
                if (!s.equals(session)) {
                    s.send(new ServerPacket("Client with ip " + session.getAddress() + " connected to the chat!"));
                }
            }
            sessions.add(session);
        }

        @Override
        public void onDisconnected(Session session) {
            System.out.printf("User with ip %s disconnected!%n", session.getAddress());
            for (Session s : sessions) {
                if (!s.equals(session)) {
                    s.send(new ServerPacket("Client with ip " + session.getAddress() + " left the chat!"));
                }
            }
            sessions.remove(session);
        }
    }).option(ChannelOption.SO_BACKLOG, 200).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).build();
    System.out.println("Server started!");
}
Also used : ServerPacket(de.datasecs.hydra.example.shared.chat.ServerPacket) Server(de.datasecs.hydra.server.Server) HydraServer(de.datasecs.hydra.server.HydraServer) HydraServer(de.datasecs.hydra.server.HydraServer) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) Session(de.datasecs.hydra.shared.handler.Session)

Example 4 with HydraServer

use of de.datasecs.hydra.server.HydraServer 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();
}
Also used : HydraServer(de.datasecs.hydra.server.HydraServer) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) Session(de.datasecs.hydra.shared.handler.Session)

Example 5 with HydraServer

use of de.datasecs.hydra.server.HydraServer in project Hydra by DataSecs.

the class UdpServer method main.

public static void main(String[] args) {
    HydraServer server = new Server.Builder("localhost", 8888, new UdpServerProtocol()).useUDP(true).childOption(ChannelOption.SO_BROADCAST, true).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());
}
Also used : Server(de.datasecs.hydra.server.Server) HydraServer(de.datasecs.hydra.server.HydraServer) HydraServer(de.datasecs.hydra.server.HydraServer)

Aggregations

HydraServer (de.datasecs.hydra.server.HydraServer)6 Session (de.datasecs.hydra.shared.handler.Session)5 HydraSessionListener (de.datasecs.hydra.shared.handler.listener.HydraSessionListener)5 Server (de.datasecs.hydra.server.Server)2 ServerPacket (de.datasecs.hydra.example.shared.chat.ServerPacket)1 SampleProtocol (server.SampleProtocol)1