@@ -3,21 +3,22 @@ package peer
33import "encoding/json"
44import "fmt"
55import "io"
6+ import "lbry/daemon/blob"
67import "net"
78import "time"
89
9- func StartServer (listener net.Listener ) {
10+ func StartServer (blobManager blob. BlobManager , listener net.Listener ) {
1011 for {
1112 conn , err := listener .Accept ()
1213 if err != nil {
1314 fmt .Println ("Error accepting:" , err )
1415 continue
1516 }
16- go handleConnection (conn )
17+ go handleConnection (conn , blobManager )
1718 }
1819}
1920
20- func handleConnection (conn net.Conn ) {
21+ func handleConnection (conn net.Conn , blobManager blob. BlobManager ) {
2122 defer conn .Close ()
2223 conn .SetReadDeadline (time .Now ().Add (10 * time .Second )) // Prevent hanging
2324
@@ -40,7 +41,7 @@ func handleConnection(conn net.Conn) {
4041
4142 requestedBlobsValue , hasRequestedBlobs := data ["requested_blobs" ]
4243 if hasRequestedBlobs {
43- responseData ["available_blobs" ] = getAvailableBlobs (requestedBlobsValue .([]string ))
44+ responseData ["available_blobs" ] = getAvailableBlobs (blobManager , requestedBlobsValue .([]string ))
4445 }
4546
4647 blobDataPaymentRateValue , hasBlobDataPaymentRate := data ["blob_data_payment_rate" ]
@@ -53,7 +54,7 @@ func handleConnection(conn net.Conn) {
5354
5455 requestedBlobValue , hasRequestedBlob := data ["requested_blob" ]
5556 if hasRequestedBlob {
56- incomingBlob , blobData = getRequestedBlob (requestedBlobValue .(string ))
57+ incomingBlob , blobData = getRequestedBlob (blobManager , requestedBlobValue .(string ))
5758 responseData ["incoming_blob" ] = incomingBlob
5859 }
5960
@@ -64,17 +65,35 @@ func handleConnection(conn net.Conn) {
6465 }
6566}
6667
67- func getAvailableBlobs (requestedBlobs []string ) []string {
68- // TODO
69- return []string {}
68+ func getAvailableBlobs (blobManager blob.BlobManager , requestedBlobs []string ) []string {
69+ var availableBlobs []string
70+
71+ for _ , requestedBlob := range requestedBlobs {
72+ if blobManager .Has (requestedBlob ) {
73+ availableBlobs = append (availableBlobs , requestedBlob )
74+ }
75+ }
76+
77+ return availableBlobs
7078}
7179
7280func getBlobDataPaymentRate (blobDataPaymentRate float64 ) string {
7381 // TODO
7482 return "RATE_UNSET"
7583}
7684
77- func getRequestedBlob (requestedBlob string ) (map [string ]any , []byte ) {
78- // TODO
79- return map [string ]any {}, []byte {}
85+ func getRequestedBlob (blobManager blob.BlobManager , requestedBlob string ) (map [string ]any , []byte ) {
86+ if blobManager .Has (requestedBlob ) {
87+ blobData := blobManager .Get (requestedBlob )
88+ return map [string ]any {
89+ "blob_hash" : requestedBlob ,
90+ "length" : len (blobData ),
91+ }, blobData
92+ }
93+
94+ return map [string ]any {
95+ "blob_hash" : "" ,
96+ "length" : 0 ,
97+ "error" : "Blob not found" ,
98+ }, nil
8099}
0 commit comments