All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xxl.mathematica.network.PingTime Maven / Gradle / Ivy

package xxl.mathematica.network;

import io.vavr.control.Try;
import xxl.mathematica.Rule;
import xxl.mathematica.external.External;
import xxl.mathematica.list.Last;
import xxl.mathematica.string.StringCases;
import xxl.mathematica.system.Os;

import java.nio.charset.Charset;
import java.util.List;

public class PingTime {
    /**
     * ping时间
     *
     * @return
     */
    public static Double pingTime(String dst) {
        return Try.ofCallable(() -> {
            if (Os.isWindows()) {
                Rule rule = External.runProcess("ping " + dst);
                if (rule.getKey() == 0) {
                    String res = new String(rule.getValue(), Charset.forName("GBK"));
                    return Double.valueOf(Last.last(StringCases.stringCases(res, "= (\\d*)ms", 1)));
                } else {
                    return null;
                }
            } else if (Os.isLinux()) {
                Rule rule = External.runProcess("ping -c 5 " + dst);
                if (rule.getKey() == 0) {
                    String res = new String(rule.getValue(), Charset.forName("GBK"));
                    return Double.valueOf(Last.last(StringCases.stringCases(res, "= .*/(.*)/.*/.* ms", 1)));
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }).getOrNull();
    }

    /**
     * 一次性ping多次
     *
     * @param dst
     * @param times
     * @return
     */
    public static List pingTime(String dst, long times) {
        return io.vavr.collection.List.range(0, times)
                .map(aLong -> pingTime(dst))
                .asJava();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy