]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-caller/main.go
Do not keep files opened
[nncp.git] / src / cmd / nncp-caller / main.go
index e97ea8555f4e6d91dda10d5500f529cde4c59fdc..245c6183689a075c3e596a5b5ca1bc43c5f604da 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -19,11 +19,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "errors"
        "flag"
        "fmt"
        "log"
        "os"
-       "strconv"
        "sync"
        "time"
 
@@ -44,6 +44,8 @@ func main() {
                spoolPath = flag.String("spool", "", "Override path to spool")
                logPath   = flag.String("log", "", "Override path to logfile")
                quiet     = flag.Bool("quiet", false, "Print only errors")
+               showPrgrs = flag.Bool("progress", false, "Force progress showing")
+               omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
                debug     = flag.Bool("debug", false, "Print debug messages")
                version   = flag.Bool("version", false, "Print version information")
                warranty  = flag.Bool("warranty", false, "Print warranty information")
@@ -59,7 +61,15 @@ func main() {
                return
        }
 
-       ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
+       ctx, err := nncp.CtxFromCmdline(
+               *cfgPath,
+               *spoolPath,
+               *logPath,
+               *quiet,
+               *showPrgrs,
+               *omitPrgrs,
+               *debug,
+       )
        if err != nil {
                log.Fatalln("Error during initialization:", err)
        }
@@ -76,7 +86,7 @@ func main() {
                                log.Fatalln("Invalid NODE specified:", err)
                        }
                        if len(node.Calls) == 0 {
-                               ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
+                               ctx.LogD("caller", nncp.LEs{{K: "Node", V: node.Id}}, "has no calls, skipping")
                                continue
                        }
                        nodes = append(nodes, node)
@@ -84,7 +94,7 @@ func main() {
        } else {
                for _, node := range ctx.Neigh {
                        if len(node.Calls) == 0 {
-                               ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
+                               ctx.LogD("caller", nncp.LEs{{K: "Node", V: node.Id}}, "has no calls, skipping")
                                continue
                        }
                        nodes = append(nodes, node)
@@ -105,24 +115,57 @@ func main() {
                                } else {
                                        addrs = append(addrs, *call.Addr)
                                }
-                               sds := nncp.SDS{"node": node.Id, "callindex": strconv.Itoa(i)}
+                               les := nncp.LEs{{K: "Node", V: node.Id}, {K: "CallIndex", V: i}}
                                for {
                                        n := time.Now()
                                        t := call.Cron.Next(n)
-                                       ctx.LogD("caller", sds, t.String())
+                                       ctx.LogD("caller", les, t.String())
                                        if t.IsZero() {
-                                               ctx.LogE("caller", sds, "got zero time")
+                                               ctx.LogE("caller", les, errors.New("got zero time"), "")
                                                return
                                        }
                                        time.Sleep(t.Sub(n))
                                        node.Lock()
                                        if node.Busy {
                                                node.Unlock()
-                                               ctx.LogD("caller", sds, "busy")
+                                               ctx.LogD("caller", les, "busy")
                                                continue
                                        } else {
                                                node.Busy = true
                                                node.Unlock()
+
+                                               if call.WhenTxExists && call.Xx != "TRx" {
+                                                       ctx.LogD("caller", les, "checking tx existence")
+                                                       txExists := false
+                                                       for job := range ctx.Jobs(node.Id, nncp.TTx) {
+                                                               if job.PktEnc.Nice > call.Nice {
+                                                                       continue
+                                                               }
+                                                               txExists = true
+                                                       }
+                                                       if !txExists {
+                                                               ctx.LogD("caller", les, "no tx")
+                                                               node.Lock()
+                                                               node.Busy = false
+                                                               node.Unlock()
+                                                               continue
+                                                       }
+                                               }
+
+                                               var autoTossFinish chan struct{}
+                                               var autoTossBadCode chan bool
+                                               if call.AutoToss {
+                                                       autoTossFinish, autoTossBadCode = ctx.AutoToss(
+                                                               node.Id,
+                                                               call.Nice,
+                                                               call.AutoTossDoSeen,
+                                                               call.AutoTossNoFile,
+                                                               call.AutoTossNoFreq,
+                                                               call.AutoTossNoExec,
+                                                               call.AutoTossNoTrns,
+                                                       )
+                                               }
+
                                                ctx.CallNode(
                                                        node,
                                                        addrs,
@@ -135,6 +178,12 @@ func main() {
                                                        false,
                                                        nil,
                                                )
+
+                                               if call.AutoToss {
+                                                       close(autoTossFinish)
+                                                       <-autoTossBadCode
+                                               }
+
                                                node.Lock()
                                                node.Busy = false
                                                node.Unlock()