]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/call.go
Fix invalid -rx/-tx arguments processing
[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-2017 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
26 func (ctx *Ctx) CallNode(node *Node, addrs []string, nice uint8, xxOnly TRxTx, onlineDeadline, maxOnlineTime uint) (isGood bool) {
27         for _, addr := range addrs {
28                 sds := SDS{"node": node.Id, "addr": addr}
29                 ctx.LogD("call", sds, "dialing")
30                 conn, err := net.Dial("tcp", addr)
31                 if err != nil {
32                         ctx.LogD("call", SdsAdd(sds, SDS{"err": err}), "dialing")
33                         continue
34                 }
35                 ctx.LogD("call", sds, "connected")
36                 state, err := ctx.StartI(
37                         conn,
38                         node.Id,
39                         nice,
40                         xxOnly,
41                         onlineDeadline,
42                         maxOnlineTime,
43                 )
44                 if err == nil {
45                         ctx.LogI("call-start", sds, "connected")
46                         state.Wait()
47                         ctx.LogI("call-finish", SDS{
48                                 "node":     state.Node.Id,
49                                 "duration": strconv.FormatInt(int64(state.Duration.Seconds()), 10),
50                                 "rxbytes":  strconv.FormatInt(state.RxBytes, 10),
51                                 "txbytes":  strconv.FormatInt(state.TxBytes, 10),
52                                 "rxspeed":  strconv.FormatInt(state.RxSpeed, 10),
53                                 "txspeed":  strconv.FormatInt(state.TxSpeed, 10),
54                         }, "")
55                         isGood = true
56                         conn.Close()
57                         break
58                 } else {
59                         ctx.LogE("call-start", SdsAdd(sds, SDS{"err": err}), "")
60                         conn.Close()
61                 }
62         }
63         return
64 }