mirror of
https://github.com/taigrr/go-fastping
synced 2025-01-18 05:03:15 -08:00
Clean up codes to pass golint checks
This commit is contained in:
parent
be887297cb
commit
be67e41b89
54
fastping.go
54
fastping.go
@ -1,6 +1,6 @@
|
|||||||
// go-fastping is a Go language port of Marc Lehmann's AnyEvent::FastPing Perl
|
// Package fastping is a Go language port of Marc Lehmann's AnyEvent::FastPing
|
||||||
// module to send ICMP ECHO REQUEST packets quickly. Original Perl module is
|
// Perl module to send ICMP ECHO REQUEST packets quickly. Original Perl module
|
||||||
// available at
|
// is available at
|
||||||
// http://search.cpan.org/~mlehmann/AnyEvent-FastPing-2.01/
|
// http://search.cpan.org/~mlehmann/AnyEvent-FastPing-2.01/
|
||||||
//
|
//
|
||||||
// It hasn't been fully implemented original functions yet and only for IPv4
|
// It hasn't been fully implemented original functions yet and only for IPv4
|
||||||
@ -108,7 +108,7 @@ type Pinger struct {
|
|||||||
Debug bool
|
Debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// It returns a new Pinger
|
// NewPinger returns a new Pinger
|
||||||
func NewPinger() *Pinger {
|
func NewPinger() *Pinger {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
p := &Pinger{
|
p := &Pinger{
|
||||||
@ -122,11 +122,12 @@ func NewPinger() *Pinger {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add an IP address to Pinger. ipaddr arg should be a string like "192.0.2.1".
|
// AddIP adds an IP address to Pinger. ipaddr arg should be a string like
|
||||||
|
// "192.0.2.1".
|
||||||
func (p *Pinger) AddIP(ipaddr string) error {
|
func (p *Pinger) AddIP(ipaddr string) error {
|
||||||
addr := net.ParseIP(ipaddr)
|
addr := net.ParseIP(ipaddr)
|
||||||
if addr == nil {
|
if addr == nil {
|
||||||
return errors.New(fmt.Sprintf("%s is not a valid textual representation of an IP address", ipaddr))
|
return fmt.Errorf("%s is not a valid textual representation of an IP address", ipaddr)
|
||||||
}
|
}
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
p.addrs[addr.String()] = &net.IPAddr{IP: addr}
|
p.addrs[addr.String()] = &net.IPAddr{IP: addr}
|
||||||
@ -134,14 +135,16 @@ func (p *Pinger) AddIP(ipaddr string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add an IP address to Pinger. ip arg should be a net.IPAddr pointer.
|
// AddIPAddr adds an IP address to Pinger. ip arg should be a net.IPAddr
|
||||||
|
// pointer.
|
||||||
func (p *Pinger) AddIPAddr(ip *net.IPAddr) {
|
func (p *Pinger) AddIPAddr(ip *net.IPAddr) {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
p.addrs[ip.String()] = ip
|
p.addrs[ip.String()] = ip
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add event handler to Pinger. event arg should be "receive" or "idle" string.
|
// AddHandler adds event handler to Pinger. event arg should be "receive" or
|
||||||
|
// "idle" string.
|
||||||
//
|
//
|
||||||
// "receive" handler should be
|
// "receive" handler should be
|
||||||
//
|
//
|
||||||
@ -164,25 +167,23 @@ func (p *Pinger) AddHandler(event string, handler interface{}) error {
|
|||||||
p.handlers[event] = hdl
|
p.handlers[event] = hdl
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
return errors.New(fmt.Sprintf("Receive event handler should be `func(*net.IPAddr, time.Duration)`"))
|
|
||||||
}
|
}
|
||||||
|
return errors.New("Receive event handler should be `func(*net.IPAddr, time.Duration)`")
|
||||||
case "idle":
|
case "idle":
|
||||||
if hdl, ok := handler.(func()); ok {
|
if hdl, ok := handler.(func()); ok {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
p.handlers[event] = hdl
|
p.handlers[event] = hdl
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
} else {
|
|
||||||
return errors.New(fmt.Sprintf("Idle event handler should be `func()`"))
|
|
||||||
}
|
}
|
||||||
|
return errors.New("Idle event handler should be `func()`")
|
||||||
}
|
}
|
||||||
return errors.New(fmt.Sprintf("No such event: %s", event))
|
return errors.New("No such event: " + event)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke a single send/receive procedure. It sends packets to all hosts which
|
// Run invokes a single send/receive procedure. It sends packets to all hosts
|
||||||
// have already been added by AddIP() etc. and wait those responses. When it
|
// which have already been added by AddIP() etc. and wait those responses. When
|
||||||
// receives a response, it calls "receive" handler registered by AddHander().
|
// it receives a response, it calls "receive" handler registered by AddHander().
|
||||||
// After MaxRTT seconds, it calls "idle" handler and returns to caller with
|
// After MaxRTT seconds, it calls "idle" handler and returns to caller with
|
||||||
// an error value. It means it blocks until MaxRTT seconds passed. For the
|
// an error value. It means it blocks until MaxRTT seconds passed. For the
|
||||||
// purpose of sending/receiving packets over and over, use RunLoop().
|
// purpose of sending/receiving packets over and over, use RunLoop().
|
||||||
@ -196,11 +197,11 @@ func (p *Pinger) Run() error {
|
|||||||
return p.ctx.err
|
return p.ctx.err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke send/receive procedure repeatedly. It sends packets to all hosts which
|
// RunLoop invokes send/receive procedure repeatedly. It sends packets to all
|
||||||
// have already been added by AddIP() etc. and wait those responses. When it
|
// hosts which have already been added by AddIP() etc. and wait those responses.
|
||||||
// receives a response, it calls "receive" handler registered by AddHander().
|
// When it receives a response, it calls "receive" handler registered by
|
||||||
// After MaxRTT seconds, it calls "idle" handler, resend packets and wait those
|
// AddHander(). After MaxRTT seconds, it calls "idle" handler, resend packets
|
||||||
// response. MaxRTT works as an interval time.
|
// and wait those response. MaxRTT works as an interval time.
|
||||||
//
|
//
|
||||||
// This is a non-blocking method so immediately returns. If you want to monitor
|
// This is a non-blocking method so immediately returns. If you want to monitor
|
||||||
// and stop sending packets, use Done() and Stop() methods. For example,
|
// and stop sending packets, use Done() and Stop() methods. For example,
|
||||||
@ -226,13 +227,14 @@ func (p *Pinger) RunLoop() {
|
|||||||
go p.run(false)
|
go p.run(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a channel that is closed when RunLoop() is stopped by an error or
|
// Done returns a channel that is closed when RunLoop() is stopped by an error
|
||||||
// Stop(). It must be called after RunLoop() call. If not, it causes panic.
|
// or Stop(). It must be called after RunLoop() call. If not, it causes panic.
|
||||||
func (p *Pinger) Done() <-chan bool {
|
func (p *Pinger) Done() <-chan bool {
|
||||||
return p.ctx.done
|
return p.ctx.done
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop RunLoop(). It must be called after RunLoop(). If not, it causes panic.
|
// Stop stops RunLoop(). It must be called after RunLoop(). If not, it causes
|
||||||
|
// panic.
|
||||||
func (p *Pinger) Stop() {
|
func (p *Pinger) Stop() {
|
||||||
p.debugln("Stop(): close(p.ctx.stop)")
|
p.debugln("Stop(): close(p.ctx.stop)")
|
||||||
close(p.ctx.stop)
|
close(p.ctx.stop)
|
||||||
@ -240,8 +242,8 @@ func (p *Pinger) Stop() {
|
|||||||
<-p.ctx.done
|
<-p.ctx.done
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return an error that is set by RunLoop(). It must be called after RunLoop().
|
// Err returns an error that is set by RunLoop(). It must be called after
|
||||||
// If not, it causes panic.
|
// RunLoop(). If not, it causes panic.
|
||||||
func (p *Pinger) Err() error {
|
func (p *Pinger) Err() error {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user