feat: implement ListFiles and DownloadFile RPCs with documentation updates in GRPC.md

This commit is contained in:
2026-07-17 18:14:51 +09:00
parent 27d5b59106
commit 8f183d597e
6 changed files with 569 additions and 65 deletions
+62
View File
@@ -4,7 +4,9 @@ import (
"context"
"fmt"
"grpccanary/examples/grpcentity/protoapi"
"io"
"math/rand"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
@@ -64,6 +66,31 @@ func AskUploadFile(ctx context.Context, m protoapi.IoTServiceClient, fileName st
return stream.CloseAndRecv()
}
func AskListFiles(ctx context.Context, m protoapi.IoTServiceClient) (*protoapi.FileList, error) {
return m.ListFiles(ctx, &protoapi.EmptyRequest{})
}
func AskDownloadFile(ctx context.Context, m protoapi.IoTServiceClient, fileName string) ([]byte, error) {
stream, err := m.DownloadFile(ctx, &protoapi.DownloadRequest{FileName: fileName})
if err != nil {
return nil, err
}
var buffer []byte
for {
chunk, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
buffer = append(buffer, chunk.GetContent()...)
}
return buffer, nil
}
func ClientRun(addr string) {
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
@@ -109,4 +136,39 @@ func ClientRun(addr string) {
fmt.Println("Upload Success:", status.Success)
fmt.Println("Upload Message:", status.Message)
fmt.Printf("Uploaded Bytes: %d bytes\n", status.BytesUploaded)
// 5단계: 파일 리스트 조회 실행 예제
fmt.Println("Querying uploaded files metadata from Server...")
list, err := AskListFiles(context.Background(), client)
if err != nil {
fmt.Println("Failed to list files:", err)
return
}
for i, f := range list.GetFiles() {
fmt.Printf("[%d] Name: %s, Size: %d bytes, UploadedAt: %s\n",
i+1, f.GetFileName(), f.GetFileSize(), time.Unix(f.GetUploadedAt(), 0).Format("2006-01-02 15:04:05"))
}
// 6단계: 파일 다운로드 스트리밍 실행 예제
fmt.Println("Downloading file 'firmware.bin' via Server Streaming...")
downloadedData, err := AskDownloadFile(context.Background(), client, "firmware.bin")
if err != nil {
fmt.Println("Failed to download file:", err)
return
}
fmt.Printf("Download completed. Received %d bytes.\n", len(downloadedData))
// 데이터 정합성(Integrity) 검증
isMatch := true
if len(dummyData) != len(downloadedData) {
isMatch = false
} else {
for i := range dummyData {
if dummyData[i] != downloadedData[i] {
isMatch = false
break
}
}
}
fmt.Printf("Data Integrity Checked (Upload vs Download matches?): %t\n", isMatch)
}