]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go
19307153a7e57c3f02eebf6f584f338a0ff31e4d
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-caller / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Croned NNCP TCP daemon caller.
20 package main
21
22 import (
23         "flag"
24         "fmt"
25         "log"
26         "os"
27         "strconv"
28         "sync"
29         "time"
30
31         "cypherpunks.ru/nncp"
32 )
33
34 func usage() {
35         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
36         fmt.Fprintf(os.Stderr, "nncp-caller -- croned NNCP TCP daemon caller\n\n")
37         fmt.Fprintf(os.Stderr, "Usage: %s [options] [NODE ...]\n", os.Args[0])
38         fmt.Fprintln(os.Stderr, "Options:")
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
44                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45                 spoolPath = flag.String("spool", "", "Override path to spool")
46                 logPath   = flag.String("log", "", "Override path to logfile")
47                 quiet     = flag.Bool("quiet", false, "Print only errors")
48                 debug     = flag.Bool("debug", false, "Print debug messages")
49                 version   = flag.Bool("version", false, "Print version information")
50                 warranty  = flag.Bool("warranty", false, "Print warranty information")
51         )
52         flag.Usage = usage
53         flag.Parse()
54         if *warranty {
55                 fmt.Println(nncp.Warranty)
56                 return
57         }
58         if *version {
59                 fmt.Println(nncp.VersionGet())
60                 return
61         }
62
63         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
64         if err != nil {
65                 log.Fatalln("Error during initialization:", err)
66         }
67         if ctx.Self == nil {
68                 log.Fatalln("Config lacks private keys")
69         }
70
71         var nodes []*nncp.Node
72         if flag.NArg() > 0 {
73                 for _, nodeId := range flag.Args() {
74                         node, err := ctx.FindNode(nodeId)
75                         if err != nil {
76                                 log.Fatalln("Invalid NODE specified:", err)
77                         }
78                         if len(node.Calls) == 0 {
79                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
80                                 continue
81                         }
82                         nodes = append(nodes, node)
83                 }
84         } else {
85                 for _, node := range ctx.Neigh {
86                         if len(node.Calls) == 0 {
87                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
88                                 continue
89                         }
90                         nodes = append(nodes, node)
91                 }
92         }
93
94         var wg sync.WaitGroup
95         for _, node := range nodes {
96                 for i, call := range node.Calls {
97                         wg.Add(1)
98                         go func(node *nncp.Node, i int, call *nncp.Call) {
99                                 defer wg.Done()
100                                 var addrs []string
101                                 if call.Addr == nil {
102                                         for _, addr := range node.Addrs {
103                                                 addrs = append(addrs, addr)
104                                         }
105                                 } else {
106                                         addrs = append(addrs, *call.Addr)
107                                 }
108                                 sds := nncp.SDS{"node": node.Id, "callindex": strconv.Itoa(i)}
109                                 for {
110                                         n := time.Now()
111                                         t := call.Cron.Next(n)
112                                         ctx.LogD("caller", sds, t.String())
113                                         if t.IsZero() {
114                                                 ctx.LogE("caller", sds, "got zero time")
115                                                 return
116                                         }
117                                         time.Sleep(t.Sub(n))
118                                         node.Lock()
119                                         if node.Busy {
120                                                 node.Unlock()
121                                                 ctx.LogD("caller", sds, "busy")
122                                                 continue
123                                         } else {
124                                                 node.Busy = true
125                                                 node.Unlock()
126                                                 ctx.CallNode(
127                                                         node,
128                                                         addrs,
129                                                         call.Nice,
130                                                         call.Xx,
131                                                         call.RxRate,
132                                                         call.TxRate,
133                                                         call.OnlineDeadline,
134                                                         call.MaxOnlineTime,
135                                                         false,
136                                                         nil,
137                                                 )
138                                                 node.Lock()
139                                                 node.Busy = false
140                                                 node.Unlock()
141                                         }
142                                 }
143                         }(node, i, call)
144                 }
145         }
146         wg.Wait()
147 }