]> Cypherpunks.ru repositories - nncp.git/blob - src/call.go
Operations progress
[nncp.git] / src / call.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, 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
23         "github.com/gorhill/cronexpr"
24 )
25
26 type Call struct {
27         Cron           *cronexpr.Expression
28         Nice           uint8
29         Xx             TRxTx
30         RxRate         int
31         TxRate         int
32         Addr           *string
33         OnlineDeadline uint
34         MaxOnlineTime  uint
35 }
36
37 func (ctx *Ctx) CallNode(
38         node *Node,
39         addrs []string,
40         nice uint8,
41         xxOnly TRxTx,
42         rxRate, txRate int,
43         onlineDeadline, maxOnlineTime uint,
44         listOnly bool,
45         onlyPkts map[[32]byte]bool,
46 ) (isGood bool) {
47         for _, addr := range addrs {
48                 sds := SDS{"node": node.Id, "addr": addr}
49                 ctx.LogD("call", sds, "dialing")
50                 var conn ConnDeadlined
51                 var err error
52                 if addr[0] == '|' {
53                         conn, err = NewPipeConn(addr[1:])
54                 } else {
55                         conn, err = net.Dial("tcp", addr)
56                 }
57                 if err != nil {
58                         ctx.LogD("call", SdsAdd(sds, SDS{"err": err}), "dialing")
59                         continue
60                 }
61                 ctx.LogD("call", sds, "connected")
62                 state := SPState{
63                         Ctx:            ctx,
64                         Node:           node,
65                         Nice:           nice,
66                         onlineDeadline: onlineDeadline,
67                         maxOnlineTime:  maxOnlineTime,
68                         xxOnly:         xxOnly,
69                         rxRate:         rxRate,
70                         txRate:         txRate,
71                         listOnly:       listOnly,
72                         onlyPkts:       onlyPkts,
73                 }
74                 if err = state.StartI(conn); err == nil {
75                         ctx.LogI("call-start", sds, "connected")
76                         state.Wait()
77                         ctx.LogI("call-finish", SDS{
78                                 "node":     state.Node.Id,
79                                 "duration": int64(state.Duration.Seconds()),
80                                 "rxbytes":  state.RxBytes,
81                                 "txbytes":  state.TxBytes,
82                                 "rxspeed":  state.RxSpeed,
83                                 "txspeed":  state.TxSpeed,
84                         }, "")
85                         isGood = true
86                         conn.Close()
87                         break
88                 } else {
89                         ctx.LogE("call-start", sds, err, "")
90                         conn.Close()
91                 }
92         }
93         return
94 }