]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go
nncp-mincfg command
[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-2017 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         "io/ioutil"
26         "log"
27         "os"
28         "strconv"
29         "sync"
30         "time"
31
32         "cypherpunks.ru/nncp"
33 )
34
35 func usage() {
36         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
37         fmt.Fprintln(os.Stderr, "nncp-caller -- croned NNCP TCP daemon caller\n")
38         fmt.Fprintf(os.Stderr, "Usage: %s [options] [NODE ...]\n", os.Args[0])
39         fmt.Fprintln(os.Stderr, "Options:")
40         flag.PrintDefaults()
41 }
42
43 func main() {
44         var (
45                 cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
46                 quiet    = flag.Bool("quiet", false, "Print only errors")
47                 debug    = flag.Bool("debug", false, "Print debug messages")
48                 version  = flag.Bool("version", false, "Print version information")
49                 warranty = flag.Bool("warranty", false, "Print warranty information")
50         )
51         flag.Usage = usage
52         flag.Parse()
53         if *warranty {
54                 fmt.Println(nncp.Warranty)
55                 return
56         }
57         if *version {
58                 fmt.Println(nncp.VersionGet())
59                 return
60         }
61
62         cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
63         if err != nil {
64                 log.Fatalln("Can not read config:", err)
65         }
66         ctx, err := nncp.CfgParse(cfgRaw)
67         if err != nil {
68                 log.Fatalln("Can not parse config:", err)
69         }
70         if ctx.Self == nil {
71                 log.Fatalln("Config lacks private keys")
72         }
73         ctx.Quiet = *quiet
74         ctx.Debug = *debug
75
76         var nodes []*nncp.Node
77         if flag.NArg() > 0 {
78                 for _, nodeId := range flag.Args() {
79                         node, err := ctx.FindNode(nodeId)
80                         if err != nil {
81                                 log.Fatalln("Invalid NODE specified:", err)
82                         }
83                         if len(node.Calls) == 0 {
84                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
85                                 continue
86                         }
87                         nodes = append(nodes, node)
88                 }
89         } else {
90                 for _, node := range ctx.Neigh {
91                         if len(node.Calls) == 0 {
92                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
93                                 continue
94                         }
95                         nodes = append(nodes, node)
96                 }
97         }
98
99         var wg sync.WaitGroup
100         for _, node := range nodes {
101                 for i, call := range node.Calls {
102                         wg.Add(1)
103                         go func(node *nncp.Node, i int, call *nncp.Call) {
104                                 defer wg.Done()
105                                 var addrs []string
106                                 if call.Addr == nil {
107                                         for _, addr := range node.Addrs {
108                                                 addrs = append(addrs, addr)
109                                         }
110                                 } else {
111                                         addrs = append(addrs, *call.Addr)
112                                 }
113                                 sds := nncp.SDS{"node": node.Id, "callindex": strconv.Itoa(i)}
114                                 for {
115                                         n := time.Now()
116                                         t := call.Cron.Next(n)
117                                         ctx.LogD("caller", sds, t.String())
118                                         if t.IsZero() {
119                                                 ctx.LogE("caller", sds, "got zero time")
120                                                 return
121                                         }
122                                         time.Sleep(t.Sub(n))
123                                         node.Lock()
124                                         if node.Busy {
125                                                 node.Unlock()
126                                                 ctx.LogD("caller", sds, "busy")
127                                                 continue
128                                         } else {
129                                                 node.Busy = true
130                                                 node.Unlock()
131                                                 ctx.CallNode(
132                                                         node,
133                                                         addrs,
134                                                         call.Nice,
135                                                         call.Xx,
136                                                         call.OnlineDeadline,
137                                                         call.MaxOnlineTime,
138                                                 )
139                                                 node.Lock()
140                                                 node.Busy = false
141                                                 node.Unlock()
142                                         }
143                                 }
144                         }(node, i, call)
145                 }
146         }
147         wg.Wait()
148 }