Skip to content

Commit

Permalink
Merge pull request #23 from libp2p/feat/disconnect-peer
Browse files Browse the repository at this point in the history
Add API endpoint to disconnect peers
  • Loading branch information
vyzo authored Nov 16, 2018
2 parents 813e269 + 25f6394 commit 0542092
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 97 deletions.
26 changes: 26 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func (d *Daemon) handleConn(c net.Conn) {
return
}

case pb.Request_DISCONNECT:
res := d.doDisconnect(&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
Expand Down Expand Up @@ -179,6 +187,24 @@ func (d *Daemon) doConnect(req *pb.Request) *pb.Response {
return okResponse()
}

func (d *Daemon) doDisconnect(req *pb.Request) *pb.Response {
if req.Disconnect == nil {
return errorResponseString("Malformed request; missing parameters")
}

p, err := peer.IDFromBytes(req.Disconnect.GetPeer())
if err != nil {
return errorResponse(err)
}

err = d.host.Network().ClosePeer(p)
if err != nil {
return errorResponse(err)
}

return okResponse()
}

func (d *Daemon) doStreamOpen(req *pb.Request) (*pb.Response, inet.Stream) {
ctx, cancel := context.WithTimeout(d.ctx, DefaultTimeout)
defer cancel()
Expand Down
Loading

0 comments on commit 0542092

Please sign in to comment.