-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e5350b
commit 3271c51
Showing
18 changed files
with
434 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"goa.design/clue/log" | ||
|
||
"douglasthrift.net/presence" | ||
"douglasthrift.net/presence/neighbors" | ||
) | ||
|
||
type ( | ||
Detect struct { | ||
Interface string `arg:""` | ||
HardwareAddrs []string `arg:""` | ||
Iterations uint `help:"Only detect for N iterations." placeholder:"N" short:"i"` | ||
} | ||
) | ||
|
||
func (d *Detect) Run(cli *CLI) error { | ||
ctx := cli.Context() | ||
|
||
ifs := neighbors.Interfaces{d.Interface: true} | ||
hws := make(neighbors.HardwareAddrStates, len(d.HardwareAddrs)) | ||
for _, hw := range d.HardwareAddrs { | ||
hws[hw] = neighbors.NewState() | ||
config, err := presence.ParseConfigWithContext(ctx, cli.Config, wNet) | ||
if err != nil { | ||
log.Fatal(ctx, err, log.KV{K: "msg", V: "error parsing config"}, log.KV{K: "config", V: cli.Config}) | ||
} | ||
|
||
a, err := neighbors.NewARP(1) | ||
arp, err := neighbors.NewARP(config.PingCount) | ||
if err != nil { | ||
return err | ||
log.Fatal(ctx, err, log.KV{K: "msg", V: "error finding dependencies"}) | ||
} | ||
|
||
ok, err := a.Present(ctx, ifs, hws) | ||
var ( | ||
detector = presence.NewDetector(config, arp) | ||
ticker = time.NewTicker(config.Interval) | ||
stop = make(chan os.Signal, 1) | ||
reload = make(chan os.Signal, 1) | ||
i uint | ||
) | ||
|
||
err = detector.Detect(ctx) | ||
if err != nil { | ||
return err | ||
log.Error(ctx, err, log.KV{K: "msg", V: "error detecting presence"}) | ||
} | ||
log.Info(ctx, log.KV{K: "present", V: ok}) | ||
for hw, state := range hws { | ||
log.Info(ctx, log.KV{K: "hw", V: hw}, log.KV{K: "present", V: state.Present()}, log.KV{K: "changed", V: state.Changed()}) | ||
|
||
if d.Iterations != 0 { | ||
i++ | ||
if i >= d.Iterations { | ||
ticker.Stop() | ||
return nil | ||
} | ||
} | ||
|
||
signal.Ignore(syscall.SIGHUP) | ||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) | ||
signal.Notify(reload, syscall.SIGUSR1) | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
err = detector.Detect(ctx) | ||
if err != nil { | ||
log.Error(ctx, err, log.KV{K: "msg", V: "error detecting presence"}) | ||
} | ||
|
||
if d.Iterations != 0 { | ||
i++ | ||
if i >= d.Iterations { | ||
ticker.Stop() | ||
return nil | ||
} | ||
} | ||
case s := <-stop: | ||
log.Print(ctx, log.Fields{"msg": "received stop signal"}, log.Fields{"signal": s}) | ||
ticker.Stop() | ||
return nil | ||
case s := <-reload: | ||
log.Print(ctx, log.Fields{"msg": "received reload signal"}, log.Fields{"signal": s}) | ||
config, err = presence.ParseConfigWithContext(ctx, cli.Config, wNet) | ||
if err != nil { | ||
log.Error(ctx, err, log.KV{K: "msg", V: "error parsing config"}, log.KV{K: "config", V: cli.Config}) | ||
} else { | ||
arp.Count(config.PingCount) | ||
detector.Config(config) | ||
ticker.Reset(config.Interval) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.