implement conn manager api

This commit is contained in:
vyzo 2018-10-28 14:21:30 +02:00
parent 7cb1607b18
commit 35595dfca7
2 changed files with 66 additions and 0 deletions

View File

@ -114,6 +114,14 @@ func (d *Daemon) handleConn(c net.Conn) {
return
}
case pb.Request_CONNMANAGER:
res := d.doConnManager(&req)
err := w.WriteMsg(res)
if err != nil {
log.Debugf("Error writing response: %s", err.Error())
return
}
default:
log.Debugf("Unexpected request type: %d", *req.Type)
return

58
connmgr.go Normal file
View File

@ -0,0 +1,58 @@
package p2pd
import (
"context"
"time"
pb "github.com/libp2p/go-libp2p-daemon/pb"
peer "github.com/libp2p/go-libp2p-peer"
)
func (d *Daemon) doConnManager(req *pb.Request) *pb.Response {
if req.ConnManager == nil {
return errorResponseString("Malformed request; missing parameters")
}
switch *req.ConnManager.Type {
case pb.ConnManagerRequest_TAG_PEER:
p, err := peer.IDFromBytes(req.ConnManager.GetPeer())
if err != nil {
return errorResponse(err)
}
tag := req.ConnManager.GetTag()
if tag == "" {
return errorResponseString("Malformed request; missing tag parameter")
}
weight := req.ConnManager.GetWeight()
d.host.ConnManager().TagPeer(p, tag, int(weight))
return okResponse()
case pb.ConnManagerRequest_UNTAG_PEER:
p, err := peer.IDFromBytes(req.ConnManager.GetPeer())
if err != nil {
return errorResponse(err)
}
tag := req.ConnManager.GetTag()
if tag == "" {
return errorResponseString("Malformed request; missing tag parameter")
}
d.host.ConnManager().UntagPeer(p, tag)
return okResponse()
case pb.ConnManagerRequest_TRIM:
ctx, cancel := context.WithTimeout(d.ctx, 60*time.Second)
defer cancel()
d.host.ConnManager().TrimOpenConns(ctx)
return okResponse()
default:
log.Debugf("Unexpected ConnManager request type: %d", *req.ConnManager.Type)
return errorResponseString("Unexpected request")
}
}