@@ -32,6 +32,7 @@ import (
3232
3333/* the url paths of supernode APIs*/
3434const (
35+ pingPath = "/_ping"
3536 peerRegisterPath = "/peer/registry"
3637 peerPullPieceTaskPath = "/peer/task"
3738 peerReportPiecePath = "/peer/piece/suc"
@@ -53,6 +54,7 @@ func NewSupernodeAPI() SupernodeAPI {
5354
5455// SupernodeAPI defines the communication methods between supernode and dfget.
5556type SupernodeAPI interface {
57+ Ping (node string ) (reqIP string , e error )
5658 Register (node string , req * types.RegisterRequest ) (resp * types.RegisterResponse , e error )
5759 PullPieceTask (node string , req * types.PullPieceTaskRequest ) (resp * types.PullPieceTaskResponse , e error )
5860 ReportPiece (node string , req * types.ReportPieceRequest ) (resp * types.BaseResponse , e error )
@@ -74,6 +76,24 @@ type supernodeAPI struct {
7476
7577var _ SupernodeAPI = & supernodeAPI {}
7678
79+ // Ping sends a request to the supernode to check if suppernode is ok
80+ // and get request ip from supernode.
81+ func (api * supernodeAPI ) Ping (node string ) (reqIP string , e error ) {
82+ var (
83+ code int
84+ body []byte
85+ )
86+ url := fmt .Sprintf ("%s://%s%s" ,
87+ api .Scheme , node , pingPath )
88+ if code , body , e = api .HTTPClient .Get (url , api .Timeout ); e != nil {
89+ return "" , e
90+ }
91+ if ! httputils .HTTPStatusOk (code ) {
92+ return "" , fmt .Errorf ("%d:%s" , code , body )
93+ }
94+ return string (body ), e
95+ }
96+
7797// Register sends a request to the supernode to register itself as a peer
7898// and create downloading task.
7999func (api * supernodeAPI ) Register (node string , req * types.RegisterRequest ) (
0 commit comments