From 35595dfca7b09729dd9aa93ce489a30288e145a2 Mon Sep 17 00:00:00 2001 From: vyzo Date: Sun, 28 Oct 2018 14:21:30 +0200 Subject: [PATCH] implement conn manager api --- conn.go | 8 ++++++++ connmgr.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 connmgr.go diff --git a/conn.go b/conn.go index 6bd6249..14b6068 100644 --- a/conn.go +++ b/conn.go @@ -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 diff --git a/connmgr.go b/connmgr.go new file mode 100644 index 0000000..b61b137 --- /dev/null +++ b/connmgr.go @@ -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") + } +}