]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-caller/main.go
24a1a8033788969154a28762f464cc1b31d30206
[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         ctx.Quiet = *quiet
71         ctx.Debug = *debug
72
73         var nodes []*nncp.Node
74         if flag.NArg() > 0 {
75                 for _, nodeId := range flag.Args() {
76                         node, err := ctx.FindNode(nodeId)
77                         if err != nil {
78                                 log.Fatalln("Invalid NODE specified:", err)
79                         }
80                         if len(node.Calls) == 0 {
81                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
82                                 continue
83                         }
84                         nodes = append(nodes, node)
85                 }
86         } else {
87                 for _, node := range ctx.Neigh {
88                         if len(node.Calls) == 0 {
89                                 ctx.LogD("caller", nncp.SDS{"node": node.Id}, "has no calls, skipping")
90                                 continue
91                         }
92                         nodes = append(nodes, node)
93                 }
94         }
95
96         var wg sync.WaitGroup
97         for _, node := range nodes {
98                 for i, call := range node.Calls {
99                         wg.Add(1)
100                         go func(node *nncp.Node, i int, call *nncp.Call) {
101                                 defer wg.Done()
102                                 var addrs []string
103                                 if call.Addr == nil {
104                                         for _, addr := range node.Addrs {
105                                                 addrs = append(addrs, addr)
106                                         }
107                                 } else {
108                                         addrs = append(addrs, *call.Addr)
109                                 }
110                                 sds := nncp.SDS{"node": node.Id, "callindex": strconv.Itoa(i)}
111                                 for {
112                                         n := time.Now()
113                                         t := call.Cron.Next(n)
114                                         ctx.LogD("caller", sds, t.String())
115                                         if t.IsZero() {
116                                                 ctx.LogE("caller", sds, "got zero time")
117                                                 return
118                                         }
119                                         time.Sleep(t.Sub(n))
120                                         node.Lock()
121                                         if node.Busy {
122                                                 node.Unlock()
123                                                 ctx.LogD("caller", sds, "busy")
124                                                 continue
125                                         } else {
126                                                 node.Busy = true
127                                                 node.Unlock()
128                                                 ctx.CallNode(node, addrs, call.Nice, call.Xx, call.OnlineDeadline)
129                                                 node.Lock()
130                                                 node.Busy = false
131                                                 node.Unlock()
132                                         }
133                                 }
134                         }(node, i, call)
135                 }
136         }
137         wg.Wait()
138 }