• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / JavaSpiNNaker / 6233274834

19 Sep 2023 08:46AM UTC coverage: 36.409% (-0.6%) from 36.982%
6233274834

Pull #658

github

dkfellows
Merge branch 'master' into java-17
Pull Request #658: Update Java version to 17

1656 of 1656 new or added lines in 260 files covered. (100.0%)

8373 of 22997 relevant lines covered (36.41%)

0.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/SpiNNaker-utils/src/main/java/uk/ac/manchester/spinnaker/utils/Ping.java
1
/*
2
 * Copyright (c) 2018 The University of Manchester
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package uk.ac.manchester.spinnaker.utils;
17

18
import static java.lang.System.getProperty;
19
import static java.lang.Thread.sleep;
20

21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.net.InetAddress;
24

25
import com.google.errorprone.annotations.CheckReturnValue;
26

27
/**
28
 * How to ping a host to test for (ICMP) network connectivity.
29
 *
30
 * @author Donal Fellows
31
 */
32
public abstract class Ping {
33
        private static final int PING_DELAY = 500;
34

35
        private static final int PING_COUNT = 10;
36

37
        /**
38
         * Are we running in Github Actions? That network environment is hostile to
39
         * this class.
40
         *
41
         * @see <a href=
42
         *      "https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables">Github
43
         *      Actions Documentation &rarr; Variables &rarr; Default environment
44
         *      variables</a>
45
         */
46
        private static final boolean IN_GITHUB_ACTION =
×
47
                        Boolean.getBoolean("GITHUB_ACTIONS");
×
48

49
        private Ping() {
50
        }
51

52
        private static ProcessBuilder pingCmd(String address, int count, int wait) {
53
                if (getProperty("os.name").toLowerCase().contains("win")) {
×
54
                        return new ProcessBuilder("ping", "-n", Integer.toString(count),
×
55
                                        "-w", Integer.toString(wait), address);
×
56
                } else {
57
                        return new ProcessBuilder("ping", "-c", Integer.toString(count),
×
58
                                        "-W", Integer.toString(wait), address);
×
59
                }
60
        }
61

62
        /**
63
         * Core ping operation.
64
         *
65
         * @param address
66
         *            Where to ping
67
         * @return Return code, or -1 on total failure
68
         */
69
        private static int ping1(String address) {
70
                var cmd = pingCmd(address, 1, 1);
×
71
                cmd.redirectErrorStream(true);
×
72
                try {
73
                        var process = cmd.start();
×
74
                        var input = process.getInputStream();
×
75
                        new Daemon(() -> drain(input)).start();
×
76
                        return process.waitFor();
×
77
                } catch (Exception e) {
×
78
                        return -1;
×
79
                }
80
        }
81

82
        private static void drain(InputStream is) {
83
                try (is) {
×
84
                        is.skip(Long.MAX_VALUE);
×
85
                } catch (IOException e) {
×
86
                        // Ignore this exception
87
                }
×
88
        }
×
89

90
        /**
91
         * Pings to detect if a host or IP address is reachable. May wait for up to
92
         * about five seconds (or longer <i>in extremis</i>). Technically, it only
93
         * detects if a host is reachable by ICMP ECHO requests; there are
94
         * environments (such as Microsoft Azure) where ICMP ECHO is blocked but it
95
         * is possible to route ordinary UDP packets.
96
         *
97
         * @param address
98
         *            Where should be pinged.
99
         * @return 0 on success, other values on failure (reflecting the result of
100
         *         the OS subprocess).
101
         */
102
        @CheckReturnValue
103
        public static int ping(String address) {
104
                if (IN_GITHUB_ACTION) {
×
105
                        // Github runs in Azure, which blocks ping; pretend success
106
                        return 0;
×
107
                }
108
                int i = 0;
×
109
                while (true) {
110
                        int result = ping1(address);
×
111
                        if (result == 0 || ++i >= PING_COUNT) {
×
112
                                return result;
×
113
                        }
114
                        try {
115
                                sleep(PING_DELAY);
×
116
                        } catch (InterruptedException e) {
×
117
                                return result;
×
118
                        }
×
119
                }
×
120
        }
121

122
        /**
123
         * Pings to detect if a host or IP address is reachable. May wait for up to
124
         * about five seconds (or longer <i>in extremis</i>). Technically, it only
125
         * detects if a host is reachable by ICMP ECHO requests; there are
126
         * environments (such as Microsoft Azure) where ICMP ECHO is blocked but it
127
         * is possible to route ordinary UDP packets.
128
         *
129
         * @param address
130
         *            Where should be pinged.
131
         * @return 0 on success, other values on failure (reflecting the result of
132
         *         the OS subprocess).
133
         */
134
        @CheckReturnValue
135
        public static int ping(InetAddress address) {
136
                return ping(address.getHostAddress());
×
137
        }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc