Search in sources :

Example 1 with PingService

use of org.terasology.engine.network.PingService in project Terasology by MovingBlocks.

the class CoreCommands method ping.

/**
 * Your ping to the server
 *
 * @param sender Sender of command
 * @return String containing ping or error message
 */
@Command(shortDescription = "Your ping to the server", helpText = "The time it takes the packet " + "to reach the server and back", requiredPermission = PermissionManager.NO_PERMISSION)
public String ping(@Sender EntityRef sender) {
    Server server = networkSystem.getServer();
    if (server == null) {
        // TODO: i18n
        if (networkSystem.getMode().isServer()) {
            return "Your player is running on the server";
        } else {
            return "Please make sure you are connected to an online server (singleplayer doesn't count)";
        }
    }
    String[] remoteAddress = server.getRemoteAddress().split("-");
    String address = remoteAddress[1];
    int port = Integer.parseInt(remoteAddress[2]);
    try {
        PingService pingService = new PingService(address, port);
        long delay = pingService.call();
        return String.format("%d ms", delay);
    } catch (UnknownHostException e) {
        return String.format("Error: Unknown host \"%s\" at %s:%s -- %s", remoteAddress[0], remoteAddress[1], remoteAddress[2], e);
    } catch (IOException e) {
        return String.format("Error: Failed to ping server \"%s\" at %s:%s -- %s", remoteAddress[0], remoteAddress[1], remoteAddress[2], e);
    }
}
Also used : Server(org.terasology.engine.network.Server) UnknownHostException(java.net.UnknownHostException) PingService(org.terasology.engine.network.PingService) IOException(java.io.IOException) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand)

Example 2 with PingService

use of org.terasology.engine.network.PingService in project Terasology by MovingBlocks.

the class JoinGameScreen method refreshPing.

private void refreshPing() {
    String address = visibleList.getSelection().getAddress();
    int port = visibleList.getSelection().getPort();
    String name = visibleList.getSelection().getName();
    UILabel ping = find("ping", UILabel.class);
    ping.setText("Requested");
    Thread getPing = new Thread(() -> {
        PingService pingService = new PingService(address, port);
        // we're not on the game thread, so we cannot modify GUI elements directly
        try {
            long responseTime = pingService.call();
            if (visibleList.getSelection().getAddress().equals(address)) {
                GameThread.asynch(() -> ping.setText(responseTime + " ms."));
            }
        } catch (IOException e) {
            String text = translationSystem.translate("${engine:menu#connection-failed}");
            // Check if selection name is same as earlier when response is received before updating ping field
            if (name.equals(visibleList.getSelection().getName())) {
                GameThread.asynch(() -> ping.setText(FontColor.getColored(text, Color.RED)));
            }
        }
    });
    // TODO: once the common thread pool is in place this could be posted there and the
    // returned Future could be kept and cancelled as soon the selected menu entry changes
    getPing.start();
}
Also used : UILabel(org.terasology.nui.widgets.UILabel) PingService(org.terasology.engine.network.PingService) IOException(java.io.IOException) GameThread(org.terasology.engine.core.GameThread)

Aggregations

IOException (java.io.IOException)2 PingService (org.terasology.engine.network.PingService)2 UnknownHostException (java.net.UnknownHostException)1 GameThread (org.terasology.engine.core.GameThread)1 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)1 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)1 Server (org.terasology.engine.network.Server)1 UILabel (org.terasology.nui.widgets.UILabel)1