@@ -285,6 +285,7 @@ func (t *Transport) SetEventHandler(events datatransfer.EventsHandler) error {
285285 }
286286 t .events = events
287287
288+ t .unregisterFuncs = append (t .unregisterFuncs , t .gs .RegisterIncomingRequestQueuedHook (t .gsReqQueuedHook ))
288289 t .unregisterFuncs = append (t .unregisterFuncs , t .gs .RegisterIncomingRequestHook (t .gsReqRecdHook ))
289290 t .unregisterFuncs = append (t .unregisterFuncs , t .gs .RegisterCompletedResponseListener (t .gsCompletedResponseListener ))
290291 t .unregisterFuncs = append (t .unregisterFuncs , t .gs .RegisterIncomingBlockHook (t .gsIncomingBlockHook ))
@@ -448,6 +449,40 @@ func (t *Transport) gsOutgoingBlockHook(p peer.ID, request graphsync.RequestData
448449 }
449450}
450451
452+ // gsReqQueuedHook is called when graphsync enqueues an incoming request for data
453+ func (t * Transport ) gsReqQueuedHook (p peer.ID , request graphsync.RequestData ) {
454+ msg , err := extension .GetTransferData (request , t .supportedExtensions )
455+ if err != nil {
456+ log .Errorf ("failed GetTransferData, req=%+v, err=%s" , request , err )
457+ }
458+ // extension not found; probably not our request.
459+ if msg == nil {
460+ return
461+ }
462+
463+ var chid datatransfer.ChannelID
464+ if msg .IsRequest () {
465+ // when a data transfer request comes in on graphsync, the remote peer
466+ // initiated a pull
467+ chid = datatransfer.ChannelID {ID : msg .TransferID (), Initiator : p , Responder : t .peerID }
468+ request := msg .(datatransfer.Request )
469+ if request .IsNew () {
470+ log .Infof ("%s, pull request queued, req=%+v" , chid , request )
471+ t .events .OnTransferQueued (chid )
472+ }
473+ } else {
474+ // when a data transfer response comes in on graphsync, this node
475+ // initiated a push, and the remote peer responded with a request
476+ // for data
477+ chid = datatransfer.ChannelID {ID : msg .TransferID (), Initiator : t .peerID , Responder : p }
478+ response := msg .(datatransfer.Response )
479+ if response .IsNew () {
480+ log .Infof ("%s, GS pull request queued in response to our push, req=%+v" , chid , request )
481+ t .events .OnTransferQueued (chid )
482+ }
483+ }
484+ }
485+
451486// gsReqRecdHook is called when graphsync receives an incoming request for data
452487func (t * Transport ) gsReqRecdHook (p peer.ID , request graphsync.RequestData , hookActions graphsync.IncomingRequestHookActions ) {
453488 // if this is a push request the sender is us.
0 commit comments