]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/call.go
nncp-call -pkts option
[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, 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 package nncp
20
21 import (
22         "net"
23         "strconv"
24
25         "github.com/gorhill/cronexpr"
26 )
27
28 type Call struct {
29         Cron           *cronexpr.Expression
30         Nice           uint8
31         Xx             TRxTx
32         RxRate         int
33         TxRate         int
34         Addr           *string
35         OnlineDeadline uint
36         MaxOnlineTime  uint
37 }
38
39 func (ctx *Ctx) CallNode(
40         node *Node,
41         addrs []string,
42         nice uint8,
43         xxOnly TRxTx,
44         rxRate, txRate int,
45         onlineDeadline, maxOnlineTime uint,
46         listOnly bool,
47         onlyPkts map[[32]byte]bool) (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, err := ctx.StartI(
58                         conn,
59                         node.Id,
60                         nice,
61                         xxOnly,
62                         rxRate,
63                         txRate,
64                         onlineDeadline,
65                         maxOnlineTime,
66                         listOnly,
67                         onlyPkts,
68                 )
69                 if 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 }