@@ -3,17 +3,26 @@ package message
33import (
44 "bytes"
55 "fmt"
6+ "io"
67 "strings"
78
89 blocks "github.com/ipfs/go-block-format"
910 cid "github.com/ipfs/go-cid"
1011 "github.com/ipld/go-ipld-prime"
1112 "github.com/ipld/go-ipld-prime/codec/dagjson"
1213 "github.com/ipld/go-ipld-prime/datamodel"
14+ "github.com/libp2p/go-libp2p-core/peer"
15+ "github.com/libp2p/go-msgio"
1316
1417 "github.com/ipfs/go-graphsync"
1518)
1619
20+ type MessageHandler interface {
21+ FromNet (peer.ID , io.Reader ) (GraphSyncMessage , error )
22+ FromMsgReader (peer.ID , msgio.Reader ) (GraphSyncMessage , error )
23+ ToNet (peer.ID , GraphSyncMessage , io.Writer ) error
24+ }
25+
1726// GraphSyncRequest is a struct to capture data on a request contained in a
1827// GraphSyncMessage.
1928type GraphSyncRequest struct {
@@ -34,14 +43,19 @@ func (gsr GraphSyncRequest) String() string {
3443 dagjson .Encode (gsr .selector , & buf )
3544 sel = buf .String ()
3645 }
46+ extStr := strings.Builder {}
47+ for _ , name := range gsr .ExtensionNames () {
48+ extStr .WriteString (string (name ))
49+ extStr .WriteString ("|" )
50+ }
3751 return fmt .Sprintf ("GraphSyncRequest<root=%s, selector=%s, priority=%d, id=%s, cancel=%v, update=%v, exts=%s>" ,
3852 gsr .root .String (),
3953 sel ,
4054 gsr .priority ,
4155 gsr .id .String (),
4256 gsr .isCancel ,
4357 gsr .isUpdate ,
44- strings . Join ( gsr . ExtensionNames (), "|" ),
58+ extStr . String ( ),
4559 )
4660}
4761
@@ -55,10 +69,15 @@ type GraphSyncResponse struct {
5569
5670// String returns a human-readable form of a GraphSyncResponse
5771func (gsr GraphSyncResponse ) String () string {
72+ extStr := strings.Builder {}
73+ for _ , name := range gsr .ExtensionNames () {
74+ extStr .WriteString (string (name ))
75+ extStr .WriteString ("|" )
76+ }
5877 return fmt .Sprintf ("GraphSyncResponse<id=%s, status=%d, exts=%s>" ,
5978 gsr .requestID .String (),
6079 gsr .status ,
61- strings . Join ( gsr . ExtensionNames (), "|" ),
80+ extStr . String ( ),
6281 )
6382}
6483
@@ -70,6 +89,14 @@ type GraphSyncMessage struct {
7089 blocks map [cid.Cid ]blocks.Block
7190}
7291
92+ func NewMessage (
93+ requests map [graphsync.RequestID ]GraphSyncRequest ,
94+ responses map [graphsync.RequestID ]GraphSyncResponse ,
95+ blocks map [cid.Cid ]blocks.Block ,
96+ ) GraphSyncMessage {
97+ return GraphSyncMessage {requests , responses , blocks }
98+ }
99+
73100// String returns a human-readable (multi-line) form of a GraphSyncMessage and
74101// its contents
75102func (gsm GraphSyncMessage ) String () string {
@@ -87,7 +114,7 @@ func (gsm GraphSyncMessage) String() string {
87114 return fmt .Sprintf ("GraphSyncMessage<\n \t %s\n >" , strings .Join (cts , ",\n \t " ))
88115}
89116
90- // NewRequest builds a new Graphsync request
117+ // NewRequest builds a new GraphSyncRequest
91118func NewRequest (id graphsync.RequestID ,
92119 root cid.Cid ,
93120 selector ipld.Node ,
@@ -97,13 +124,13 @@ func NewRequest(id graphsync.RequestID,
97124 return newRequest (id , root , selector , priority , false , false , toExtensionsMap (extensions ))
98125}
99126
100- // CancelRequest request generates a request to cancel an in progress request
101- func CancelRequest (id graphsync.RequestID ) GraphSyncRequest {
127+ // NewCancelRequest request generates a request to cancel an in progress request
128+ func NewCancelRequest (id graphsync.RequestID ) GraphSyncRequest {
102129 return newRequest (id , cid.Cid {}, nil , 0 , true , false , nil )
103130}
104131
105- // UpdateRequest generates a new request to update an in progress request with the given extensions
106- func UpdateRequest (id graphsync.RequestID , extensions ... graphsync.ExtensionData ) GraphSyncRequest {
132+ // NewUpdateRequest generates a new request to update an in progress request with the given extensions
133+ func NewUpdateRequest (id graphsync.RequestID , extensions ... graphsync.ExtensionData ) GraphSyncRequest {
107134 return newRequest (id , cid.Cid {}, nil , 0 , false , true , toExtensionsMap (extensions ))
108135}
109136
@@ -247,10 +274,10 @@ func (gsr GraphSyncRequest) Extension(name graphsync.ExtensionName) (datamodel.N
247274}
248275
249276// ExtensionNames returns the names of the extensions included in this request
250- func (gsr GraphSyncRequest ) ExtensionNames () []string {
251- var extNames []string
277+ func (gsr GraphSyncRequest ) ExtensionNames () []graphsync. ExtensionName {
278+ var extNames []graphsync. ExtensionName
252279 for ext := range gsr .extensions {
253- extNames = append (extNames , ext )
280+ extNames = append (extNames , graphsync . ExtensionName ( ext ) )
254281 }
255282 return extNames
256283}
@@ -281,10 +308,10 @@ func (gsr GraphSyncResponse) Extension(name graphsync.ExtensionName) (datamodel.
281308}
282309
283310// ExtensionNames returns the names of the extensions included in this request
284- func (gsr GraphSyncResponse ) ExtensionNames () []string {
285- var extNames []string
311+ func (gsr GraphSyncResponse ) ExtensionNames () []graphsync. ExtensionName {
312+ var extNames []graphsync. ExtensionName
286313 for ext := range gsr .extensions {
287- extNames = append (extNames , ext )
314+ extNames = append (extNames , graphsync . ExtensionName ( ext ) )
288315 }
289316 return extNames
290317}
0 commit comments