]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go
Ability to create and deal with .seen files after tossing
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-toss / main.go
index 5914d03f0d3a0a1f1199b3d814032615884ba6cb..12dc214d8a4b7e77029dcfb6034640ae8edbaffe 100644 (file)
@@ -25,6 +25,7 @@ import (
        "io/ioutil"
        "log"
        "os"
+       "time"
 
        "cypherpunks.ru/nncp"
 )
@@ -32,7 +33,7 @@ import (
 func usage() {
        fmt.Fprintf(os.Stderr, nncp.UsageHeader())
        fmt.Fprintln(os.Stderr, "nncp-toss -- process inbound packets\n")
-       fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:", os.Args[0])
+       fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
        flag.PrintDefaults()
 }
 
@@ -41,7 +42,11 @@ func main() {
                cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
                nodeRaw  = flag.String("node", "", "Process only that node")
                niceRaw  = flag.Int("nice", 255, "Minimal required niceness")
-               debug    = flag.Bool("debug", false, "Enable debugging information")
+               dryRun   = flag.Bool("dryrun", false, "Do not actually write any tossed data")
+               doSeen   = flag.Bool("seen", false, "Create .seen files")
+               cycle    = flag.Uint("cycle", 0, "Repeat tossing after N seconds in infinite loop")
+               quiet    = flag.Bool("quiet", false, "Print only errors")
+               debug    = flag.Bool("debug", false, "Print debug messages")
                version  = flag.Bool("version", false, "Print version information")
                warranty = flag.Bool("warranty", false, "Print warranty information")
        )
@@ -60,7 +65,7 @@ func main() {
        }
        nice := uint8(*niceRaw)
 
-       cfgRaw, err := ioutil.ReadFile(*cfgPath)
+       cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
        if err != nil {
                log.Fatalln("Can not read config:", err)
        }
@@ -68,6 +73,10 @@ func main() {
        if err != nil {
                log.Fatalln("Can not parse config:", err)
        }
+       if ctx.Self == nil {
+               log.Fatalln("Config lacks private keys")
+       }
+       ctx.Quiet = *quiet
        ctx.Debug = *debug
 
        var nodeOnly *nncp.Node
@@ -78,10 +87,19 @@ func main() {
                }
        }
 
+Cycle:
+       isBad := false
        for nodeId, node := range ctx.Neigh {
                if nodeOnly != nil && nodeId != *nodeOnly.Id {
                        continue
                }
-               ctx.Toss(node.Id, nice)
+               isBad = ctx.Toss(node.Id, nice, *dryRun, *doSeen)
+       }
+       if *cycle > 0 {
+               time.Sleep(time.Duration(*cycle) * time.Second)
+               goto Cycle
+       }
+       if isBad {
+               os.Exit(1)
        }
 }