]> Cypherpunks.ru repositories - nncp.git/blob - src/call.go
Use typed time.Duration instead of raw uint for maxOnlineTime
[nncp.git] / src / call.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2020 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 uint
35         MaxOnlineTime  time.Duration
36 }
37
38 func (ctx *Ctx) CallNode(
39         node *Node,
40         addrs []string,
41         nice uint8,
42         xxOnly TRxTx,
43         rxRate, txRate int,
44         onlineDeadline uint,
45         maxOnlineTime time.Duration,
46         listOnly bool,
47         onlyPkts map[[32]byte]bool,
48 ) (isGood bool) {
49         for _, addr := range addrs {
50                 sds := SDS{"node": node.Id, "addr": addr}
51                 ctx.LogD("call", sds, "dialing")
52                 var conn ConnDeadlined
53                 var err error
54                 if addr[0] == '|' {
55                         conn, err = NewPipeConn(addr[1:])
56                 } else {
57                         conn, err = net.Dial("tcp", addr)
58                 }
59                 if err != nil {
60                         ctx.LogD("call", SdsAdd(sds, SDS{"err": err}), "dialing")
61                         continue
62                 }
63                 ctx.LogD("call", sds, "connected")
64                 state := SPState{
65                         Ctx:            ctx,
66                         Node:           node,
67                         Nice:           nice,
68                         onlineDeadline: onlineDeadline,
69                         maxOnlineTime:  maxOnlineTime,
70                         xxOnly:         xxOnly,
71                         rxRate:         rxRate,
72                         txRate:         txRate,
73                         listOnly:       listOnly,
74                         onlyPkts:       onlyPkts,
75                 }
76                 if err = state.StartI(conn); err == nil {
77                         ctx.LogI("call-start", sds, "connected")
78                         state.Wait()
79                         ctx.LogI("call-finish", SDS{
80                                 "node":     state.Node.Id,
81                                 "duration": int64(state.Duration.Seconds()),
82                                 "rxbytes":  state.RxBytes,
83                                 "txbytes":  state.TxBytes,
84                                 "rxspeed":  state.RxSpeed,
85                                 "txspeed":  state.TxSpeed,
86                         }, "")
87                         isGood = true
88                         conn.Close()
89                         break
90                 } else {
91                         ctx.LogE("call-start", sds, err, "")
92                         conn.Close()
93                 }
94         }
95         return
96 }