Skip to content

Getting Started

Thorben Kuck edited this page Sep 19, 2018 · 6 revisions

How to quickstart:

You can start using this framework by simply creating 2 Classes, let's call them Client and Server

public class Client {
    public static void main(String[] args) {
        ClientStart clientStart = ClientStart.at("localhost", 4444);
        try {
            clientStart.launch();
        } catch(NetComException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
public class Server {
    public static void main(String[] args) {
        ServerStart serverStart = ServerStart.at(4444);
        try {
            serverStart.launch();
	    serverStart.acceptAllNextClients();
	} catch (NetComExcetpion e) {
	    e.printStackTrace();
            System.exit(1);
	}
    }
}

What happened here? With the first line, we create the container ServerStart and ClientStart. With .launch(); we create the Sockets internaly. This means, at this point they try to connect. The ServerStart tries to reserve the port and the ClientStart tries to connect to the Server. The extra line serverStart.acceptAllNextClients(); at the Server now accepts the next Clients, that trie to connect to the Server.

Now, first run the Sever class and then the Client class.

Done. You have just connected.

However, those classes do not communicate. To accomplish that, we have to alter both existing, and add another class. Let's call the Class "TestObject".

public class TestObject implements Serializable {
    private String string;

    public TestObject(String s) {
        this.string = s;
    }

    public String getString() {
        return this.string;
    }
}

This Class is now our Message- and Data-Object. Both, the Client and the Server can specify, how to react to this Object. Let's implement that now.

public class Client {
    public static void main(String[] args) {
        ClientStart clientStart = ClientStart.at("localhost", 4444);
        clientStart.getCommunicationRegistration()
                .register(TestObject.class)
                .addLast((session, o) -> System.out.println("Received from Server: " + o.getString()));

        try {
            clientStart.launch();
        } catch(NetComException e) {
            e.printStackTrace();
            System.exit(1);
        }
        Sender sender  = Sender.open(clientStart);
        sender.objectToServer(new TestObject("Hello"));
    }
}
public class Server {
    public static void main(String[] args) {
        ServerStart serverStart = ServerStart.at(4444);

        serverStart.getCommunicationRegistration()
                .register(TestObject.class)
                .addLast((session, o) ->  { 
                    System.out.println("Received from Client: " + o.getString());
                    session.send(new TestObject("World"));
                });

        try {
            serverStart.launch();
	    serverStart.acceptAllNextClients();
	} catch (NetComExcetpion e) {
	    e.printStackTrace();
            System.exit(1);
	}
    }
}

We now did 2 Things:

  1. We told the Server and the Client how to React to our TestObject. We did this with this line:
serverStart.getCommunicationRegistration()
                .register(TestObject.class)
                .addLast((session, o) ->  { 
                    // Stuff
                });

The .register() returnes a Pipeline, for a specific Message-Object. Since this is a Pipeline, you could add multiple Handlers for one Message Object.

so we could change the Server-CommunicationSpecification for the TestObject-Communication into 2 seperate actions like this:

serverStart.getCommunicationRegistration()
                .register(TestObject.class)
                .addFirst((session, o) ->  System.out.println("Received " + o.getString() + " from Client"));

serverStart.getCommunicationRegistration()
                .register(TestObject.class)
                .addLast((session, o) ->  session.send(new TestObject("World")));

Of course you could add as many handlers as you like to this Pipeline.

If you are wondering, the Lambda (session, o) -> {} is replacing an OnReceive interface.

  1. We send an TestObject from the Client to the Server. With the line sender.objectToServer(new TestObject("Hello")); we send a new instance of TestObject to the Server.

That's it. With that you can get a head start.