|
| 1 | +// Command-line client for server-streaming sample services |
| 2 | +#include <grpcpp/grpcpp.h> |
| 3 | +#include <iostream> |
| 4 | +#include <glog/logging.h> |
| 5 | +#include <fstream> |
| 6 | +#include <vector> |
| 7 | +#include <string> |
| 8 | +#include "server_streaming_sample.pb.h" |
| 9 | +#include "server_streaming_sample.grpc.pb.h" |
| 10 | + |
| 11 | +void print_usage(const char* prog_name) { |
| 12 | + std::cout << "Usage: " << prog_name << " <service> <server_address> [options]\n" |
| 13 | + << " service: file_size | random_bytes\n" |
| 14 | + << " server_address: host:port\n" |
| 15 | + << " file_size options: <file_path>\n" |
| 16 | + << " random_bytes options: <size>\n"; |
| 17 | +} |
| 18 | + |
| 19 | +int main(int argc, char* argv[]) { |
| 20 | + if (argc < 3) { |
| 21 | + print_usage(argv[0]); |
| 22 | + return 1; |
| 23 | + } |
| 24 | + std::string service = argv[1]; |
| 25 | + std::string server_address = argv[2]; |
| 26 | + grpc::ChannelArguments args; |
| 27 | + args.SetMaxSendMessageSize(64 * 1024 * 1024); // 64MB |
| 28 | + args.SetMaxReceiveMessageSize(64 * 1024 * 1024); // 64MB |
| 29 | + auto channel = grpc::CreateCustomChannel(server_address, grpc::InsecureChannelCredentials(), args); |
| 30 | + |
| 31 | + if (service == "file_size") { |
| 32 | + using namespace std::chrono; |
| 33 | + LOG(INFO) << "[file_size] start"; |
| 34 | + auto t0 = steady_clock::now(); |
| 35 | + if (argc < 4) { |
| 36 | + std::cerr << "Missing file_path argument for file_size service\n"; |
| 37 | + return 1; |
| 38 | + } |
| 39 | + std::string file_path = argv[3]; |
| 40 | + std::ifstream file(file_path, std::ios::binary); |
| 41 | + if (!file) { |
| 42 | + std::cerr << "Failed to open file: " << file_path << "\n"; |
| 43 | + return 1; |
| 44 | + } |
| 45 | + limestone::grpc::FileSizeService::Stub stub(channel); |
| 46 | + grpc::ClientContext context; |
| 47 | + limestone::grpc::FileSizeResponse response; |
| 48 | + std::unique_ptr<grpc::ClientWriter<limestone::grpc::FileChunk>> writer( |
| 49 | + stub.GetFileSize(&context, &response)); |
| 50 | + constexpr size_t buffer_size = 32 * 1024 * 1024; // 32MB |
| 51 | + std::vector<char> buffer(buffer_size); |
| 52 | + while (file.read(buffer.data(), buffer.size()) || file.gcount() > 0) { |
| 53 | + limestone::grpc::FileChunk chunk; |
| 54 | + chunk.set_data(std::string(buffer.data(), file.gcount())); |
| 55 | + if (!writer->Write(chunk)) { |
| 56 | + std::cerr << "Failed to write chunk to server\n"; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + writer->WritesDone(); |
| 61 | + grpc::Status status = writer->Finish(); |
| 62 | + auto t1 = steady_clock::now(); |
| 63 | + auto ms = duration_cast<milliseconds>(t1 - t0).count(); |
| 64 | + if (status.ok()) { |
| 65 | + std::cout << "File size: " << response.size() << " bytes\n"; |
| 66 | + } else { |
| 67 | + std::cerr << "RPC failed: " << status.error_message() << "\n"; |
| 68 | + } |
| 69 | + LOG(INFO) << "[file_size] end: elapsed " << ms << " ms"; |
| 70 | + } else if (service == "random_bytes") { |
| 71 | + using namespace std::chrono; |
| 72 | + LOG(INFO) << "[random_bytes] start"; |
| 73 | + auto t0 = steady_clock::now(); |
| 74 | + if (argc < 4) { |
| 75 | + std::cerr << "Missing size argument for random_bytes service\n"; |
| 76 | + return 1; |
| 77 | + } |
| 78 | + int64_t size = std::stoll(argv[3]); |
| 79 | + limestone::grpc::RandomBytesService::Stub stub(channel); |
| 80 | + grpc::ClientContext context; |
| 81 | + limestone::grpc::RandomBytesRequest request; |
| 82 | + request.set_size(size); |
| 83 | + std::unique_ptr<grpc::ClientReader<limestone::grpc::RandomBytesChunk>> reader( |
| 84 | + stub.GenerateRandomBytes(&context, request)); |
| 85 | + int64_t received = 0; |
| 86 | + limestone::grpc::RandomBytesChunk chunk; |
| 87 | + while (reader->Read(&chunk)) { |
| 88 | + received += chunk.data().size(); |
| 89 | + // For demonstration, do not print the data |
| 90 | + } |
| 91 | + grpc::Status status = reader->Finish(); |
| 92 | + auto t1 = steady_clock::now(); |
| 93 | + auto ms = duration_cast<milliseconds>(t1 - t0).count(); |
| 94 | + if (status.ok()) { |
| 95 | + std::cout << "Received " << received << " bytes of random data\n"; |
| 96 | + } else { |
| 97 | + std::cerr << "RPC failed: " << status.error_message() << "\n"; |
| 98 | + } |
| 99 | + LOG(INFO) << "[random_bytes] end: elapsed " << ms << " ms"; |
| 100 | + } else { |
| 101 | + std::cerr << "Unknown service: " << service << "\n"; |
| 102 | + print_usage(argv[0]); |
| 103 | + return 1; |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
0 commit comments