]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/call.go
Forbid any later GNU GPL versions autousage
[nncp.git] / src / cypherpunks.ru / nncp / 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         "strconv"
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  uint
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, maxOnlineTime uint,
45         listOnly bool,
46         onlyPkts map[[32]byte]bool,
47 ) (isGood bool) {
48         for _, addr := range addrs {
49                 sds := SDS{"node": node.Id, "addr": addr}
50                 ctx.LogD("call", sds, "dialing")
51                 conn, err := net.Dial("tcp", addr)
52                 if err != nil {
53                         ctx.LogD("call", SdsAdd(sds, SDS{"err": err}), "dialing")
54                         continue
55                 }
56                 ctx.LogD("call", sds, "connected")
57                 state := SPState{
58                         Ctx:            ctx,
59                         Node:           node,
60                         Nice:           nice,
61                         onlineDeadline: onlineDeadline,
62                         maxOnlineTime:  maxOnlineTime,
63                         xxOnly:         xxOnly,
64                         rxRate:         rxRate,
65                         txRate:         txRate,
66                         listOnly:       listOnly,
67                         onlyPkts:       onlyPkts,
68                 }
69                 if err = state.StartI(conn); err == nil {
70                         ctx.LogI("call-start", sds, "connected")
71                         state.Wait()
72                         ctx.LogI("call-finish", SDS{
73                                 "node":     state.Node.Id,
74                                 "duration": strconv.FormatInt(int64(state.Duration.Seconds()), 10),
75                                 "rxbytes":  strconv.FormatInt(state.RxBytes, 10),
76                                 "txbytes":  strconv.FormatInt(state.TxBytes, 10),
77                                 "rxspeed":  strconv.FormatInt(state.RxSpeed, 10),
78                                 "txspeed":  strconv.FormatInt(state.TxSpeed, 10),
79                         }, "")
80                         isGood = true
81                         conn.Close()
82                         break
83                 } else {
84                         ctx.LogE("call-start", SdsAdd(sds, SDS{"err": err}), "")
85                         conn.Close()
86                 }
87         }
88         return
89 }