-
Notifications
You must be signed in to change notification settings - Fork 0
Example: Echo Server
Thorben Kuck edited this page Sep 19, 2018
·
3 revisions
Example:
Following, you see a 3-Class echo-Server-example.
import com.github.thorbenkuck.netcom2.exceptions.StartFailedException;
import com.github.thorbenkuck.netcom2.network.client.ClientStart;
import com.github.thorbenkuck.netcom2.network.client.Sender;
import com.github.thorbenkuck.netcom2.network.shared.CommunicationRegistration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class EchoClientTest {
/**
* Used, to send TestObject every second to the Server
*/
private static ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
private static ClientStart clientStart;
public static void main(String[] args) {
// Create the ClientStart
clientStart = ClientStart.at("localhost", 8888);
// Simply print out, what you received from the Server
register(clientStart.getCommunicationRegistration());
try {
// Launch it
clientStart.launch();
} catch (StartFailedException e) {
e.printStackTrace();
System.exit(1);
}
// If launched, schedule the sending every 1 second
schedule(Sender.open(clientStart));
}
private static void register(CommunicationRegistration communicationRegistration) {
communicationRegistration.register(TestObject.class)
.addFirst((testObject -> System.out.println("Server send: " + testObject.getContent())));
}
private static void schedule(Sender send) {
scheduledExecutorService.scheduleAtFixedRate(() -> {
send.objectToServer(new TestObject("Hello!"));
System.out.println("Sending Hello! to server ...");
}, 1, 1, TimeUnit.SECONDS);
}
}import com.github.thorbenkuck.netcom2.exceptions.ClientConnectionFailedException;
import com.github.thorbenkuck.netcom2.exceptions.StartFailedException;
import com.github.thorbenkuck.netcom2.integration.TestObject;
import com.github.thorbenkuck.netcom2.network.server.ServerStart;
import com.github.thorbenkuck.netcom2.network.shared.CommunicationRegistration;
import com.github.thorbenkuck.netcom2.network.shared.Session;
public class EchoServerTest {
private static ServerStart serverStart;
public static void main(String[] args) {
// Create the Server at Port 8888
serverStart = ServerStart.at(8888);
// And tell the Server, to always send back the Command, he received
register(serverStart.getCommunicationRegistration());
try {
// Than launch the Server, connecting him to the specified port
serverStart.launch();
//And accept the connecting clients
serverStart.acceptAllNextClients();
} catch (ClientConnectionFailedException | StartFailedException e) {
e.printStackTrace();
System.exit(1);
}
}
private static void register(CommunicationRegistration communicationRegistration) {
// Alternative:
// This will echo anything to all Clients.
// communicationRegistration.addDefaultCommunicationHandler(o -> serverStart.distribute().toAll(o));
communicationRegistration.register(TestObject.class)
.addFirst(Session::send);
communicationRegistration.register(TestObject.class)
.addFirst((session, o) -> System.out.println("sending back: " + o.getContent()));
}
}import java.io.Serializable;
public class TestObject implements Serializable {
private String hello;
public TestObject(String hello) {
this.hello = hello;
}
public String getContent() {
return hello;
}
}Introduction
Advanced
Outdated
Further Reading
Versions explained
Examples