Search in sources :

Example 1 with PingService

use of org.terasology.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.valueOf(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.network.Server) UnknownHostException(java.net.UnknownHostException) PingService(org.terasology.network.PingService) IOException(java.io.IOException) Command(org.terasology.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.logic.console.commandSystem.ConsoleCommand)

Example 2 with PingService

use of org.terasology.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();
    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}");
            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.rendering.nui.widgets.UILabel) PingService(org.terasology.network.PingService) IOException(java.io.IOException) GameThread(org.terasology.engine.GameThread)

Aggregations

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