]> Cypherpunks.ru repositories - nncp.git/blob - src/call.go
On demand calling
[nncp.git] / src / call.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package nncp
19
20 import (
21         "net"
22         "time"
23
24         "github.com/gorhill/cronexpr"
25 )
26
27 type Call struct {
28         Cron           *cronexpr.Expression
29         Nice           uint8
30         Xx             TRxTx
31         RxRate         int
32         TxRate         int
33         Addr           *string
34         OnlineDeadline time.Duration
35         MaxOnlineTime  time.Duration
36         WhenTxExists   bool
37
38         AutoToss       bool
39         AutoTossDoSeen bool
40         AutoTossNoFile bool
41         AutoTossNoFreq bool
42         AutoTossNoExec bool
43         AutoTossNoTrns bool
44 }
45
46 func (ctx *Ctx) CallNode(
47         node *Node,
48         addrs []string,
49         nice uint8,
50         xxOnly TRxTx,
51         rxRate, txRate int,
52         onlineDeadline, maxOnlineTime time.Duration,
53         listOnly bool,
54         onlyPkts map[[32]byte]bool,
55 ) (isGood bool) {
56         for _, addr := range addrs {
57                 sds := SDS{"node": node.Id, "addr": addr}
58                 ctx.LogD("call", sds, "dialing")
59                 var conn ConnDeadlined
60                 var err error
61                 if addr[0] == '|' {
62                         conn, err = NewPipeConn(addr[1:])
63                 } else {
64                         conn, err = net.Dial("tcp", addr)
65                 }
66                 if err != nil {
67                         ctx.LogD("call", SdsAdd(sds, SDS{"err": err}), "dialing")
68                         continue
69                 }
70                 ctx.LogD("call", sds, "connected")
71                 state := SPState{
72                         Ctx:            ctx,
73                         Node:           node,
74                         Nice:           nice,
75                         onlineDeadline: onlineDeadline,
76                         maxOnlineTime:  maxOnlineTime,
77                         xxOnly:         xxOnly,
78                         rxRate:         rxRate,
79                         txRate:         txRate,
80                         listOnly:       listOnly,
81                         onlyPkts:       onlyPkts,
82                 }
83                 if err = state.StartI(conn); err == nil {
84                         ctx.LogI("call-start", sds, "connected")
85                         state.Wait()
86                         ctx.LogI("call-finish", SDS{
87                                 "node":     state.Node.Id,
88                                 "duration": int64(state.Duration.Seconds()),
89                                 "rxbytes":  state.RxBytes,
90                                 "txbytes":  state.TxBytes,
91                                 "rxspeed":  state.RxSpeed,
92                                 "txspeed":  state.TxSpeed,
93                         }, "")
94                         isGood = true
95                         conn.Close() // #nosec G104
96                         break
97                 } else {
98                         ctx.LogE("call-start", sds, err, "")
99                         conn.Close() // #nosec G104
100                 }
101         }
102         return
103 }