]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cmd/nncp-call/main.go
nncp-caller command
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-call / main.go
index a3861dc68d9ea7a99c953cc36843831125943dd9..a0875440114adfe04d779d6a29fdd15d6e0d7157 100644 (file)
@@ -24,9 +24,7 @@ import (
        "fmt"
        "io/ioutil"
        "log"
-       "net"
        "os"
-       "strconv"
        "strings"
 
        "cypherpunks.ru/nncp"
@@ -50,6 +48,8 @@ func main() {
                debug    = flag.Bool("debug", false, "Print debug messages")
                version  = flag.Bool("version", false, "Print version information")
                warranty = flag.Bool("warranty", false, "Print warranty information")
+
+               onlineDeadline = flag.Int("onlinedeadline", 0, "Override onlinedeadline option")
        )
        flag.Usage = usage
        flag.Parse()
@@ -93,6 +93,10 @@ func main() {
                log.Fatalln("Node does not have online communication capability")
        }
 
+       if *onlineDeadline == 0 {
+               onlineDeadline = &node.OnlineDeadline
+       }
+
        var xxOnly nncp.TRxTx
        if *rxOnly {
                xxOnly = nncp.TRx
@@ -115,36 +119,7 @@ func main() {
                }
        }
 
-       isGood := false
-       for _, addr := range addrs {
-               ctx.LogD("call", nncp.SDS{"addr": addr}, "dialing")
-               conn, err := net.Dial("tcp", addr)
-               if err != nil {
-                       log.Println("Can not connect:", err)
-                       continue
-               }
-               ctx.LogD("call", nncp.SDS{"addr": addr}, "connected")
-               state, err := ctx.StartI(conn, node.Id, nice, &xxOnly)
-               if err == nil {
-                       ctx.LogI("call-start", nncp.SDS{"node": state.NodeId}, "connected")
-                       state.Wait()
-                       ctx.LogI("call-finish", nncp.SDS{
-                               "node":     state.NodeId,
-                               "duration": strconv.FormatInt(int64(state.Duration.Seconds()), 10),
-                               "rxbytes":  strconv.FormatInt(state.RxBytes, 10),
-                               "txbytes":  strconv.FormatInt(state.TxBytes, 10),
-                               "rxspeed":  strconv.FormatInt(state.RxSpeed, 10),
-                               "txspeed":  strconv.FormatInt(state.TxSpeed, 10),
-                       }, "")
-                       isGood = true
-                       conn.Close()
-                       break
-               } else {
-                       ctx.LogE("call-start", nncp.SDS{"node": state.NodeId, "err": err}, "")
-                       conn.Close()
-               }
-       }
-       if !isGood {
+       if !ctx.CallNode(node, addrs, nice, &xxOnly, *onlineDeadline) {
                os.Exit(1)
        }
 }