blob: 02a02c78d7b12bff2ba1901322aca0f157a0677a [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// Copyright 2017 the original author or authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "fmt"
19 "github.com/tatsushid/go-fastping"
20 "net"
21 "time"
22)
23
24// VerifyConnection verify the connection by attempt to connect to the host
25func VerifyNodes(nodes []string, port int) error {
26
27 var err error
28
29 pinger := fastping.NewPinger()
30 //pinger.Network("udp")
31 pinger.MaxRTT, err = time.ParseDuration("1s")
32 if err != nil {
33 return err
34 }
35
36 count := len(nodes)
37 pinger.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
38 count -= 1
39 }
40 for _, node := range nodes {
41 pinger.AddIPAddr(&net.IPAddr{IP: net.ParseIP(node)})
42 }
43 if err := pinger.Run(); err != nil {
44 return err
45 }
46 if count != 0 {
47 return fmt.Errorf("Unable to verify all nodes, %d unverified", count)
48 }
49
50 return nil
51}