1 /* 2 * Copyright 2022 Young Mei 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifdef __ZEPHYR__ 8 #include <zephyr/kernel.h> 9 #endif 10 11 #include <cstdlib> 12 #include <iostream> 13 14 #include <fcntl.h> 15 16 #include <thrift/protocol/TBinaryProtocol.h> 17 #include <thrift/protocol/TCompactProtocol.h> 18 #include <thrift/transport/TBufferTransports.h> 19 #include <thrift/transport/TSSLSocket.h> 20 #include <thrift/transport/TSocket.h> 21 22 #include "Hello.h" 23 24 using namespace apache::thrift; 25 using namespace apache::thrift::protocol; 26 using namespace apache::thrift::transport; 27 28 #ifndef IS_ENABLED 29 #define IS_ENABLED(flag) flag 30 #endif 31 32 #ifndef CONFIG_THRIFT_COMPACT_PROTOCOL 33 #define CONFIG_THRIFT_COMPACT_PROTOCOL 0 34 #endif 35 36 #ifndef CONFIG_THRIFT_SSL_SOCKET 37 #define CONFIG_THRIFT_SSL_SOCKET 0 38 #endif 39 40 #ifdef __ZEPHYR__ main(void)41int main(void) 42 #else 43 int main(int argc, char **argv) 44 #endif 45 { 46 std::string my_addr; 47 48 #ifdef __ZEPHYR__ 49 my_addr = CONFIG_NET_CONFIG_PEER_IPV4_ADDR; 50 #else 51 if (IS_ENABLED(CONFIG_THRIFT_SSL_SOCKET)) { 52 if (argc != 5) { 53 printf("usage: %s <ip> <native-cert.pem> <native-key.pem> " 54 "<qemu-cert.pem>\n", 55 argv[0]); 56 return EXIT_FAILURE; 57 } 58 } 59 60 if (argc >= 2) { 61 my_addr = std::string(argv[1]); 62 } else { 63 my_addr = "192.0.2.1"; 64 } 65 #endif 66 67 int port = 4242; 68 std::shared_ptr<TProtocol> protocol; 69 std::shared_ptr<TTransport> transport; 70 std::shared_ptr<TSSLSocketFactory> socketFactory; 71 std::shared_ptr<TTransport> trans; 72 73 if (IS_ENABLED(CONFIG_THRIFT_SSL_SOCKET)) { 74 const int port = 4242; 75 socketFactory = std::make_shared<TSSLSocketFactory>(); 76 socketFactory->authenticate(true); 77 78 #ifdef __ZEPHYR__ 79 static const char qemu_cert_pem[] = { 80 #include "qemu_cert.pem.inc" 81 '\0' 82 }; 83 84 static const char qemu_key_pem[] = { 85 #include "qemu_key.pem.inc" 86 '\0' 87 }; 88 89 static const char native_cert_pem[] = { 90 #include "native_cert.pem.inc" 91 '\0' 92 }; 93 94 socketFactory->loadCertificateFromBuffer(qemu_cert_pem); 95 socketFactory->loadPrivateKeyFromBuffer(qemu_key_pem); 96 socketFactory->loadTrustedCertificatesFromBuffer(native_cert_pem); 97 #else 98 socketFactory->loadCertificate(argv[2]); 99 socketFactory->loadPrivateKey(argv[3]); 100 socketFactory->loadTrustedCertificates(argv[4]); 101 #endif 102 trans = socketFactory->createSocket(my_addr, port); 103 } else { 104 trans = std::make_shared<TSocket>(my_addr, port); 105 } 106 107 transport = std::make_shared<TBufferedTransport>(trans); 108 109 if (IS_ENABLED(CONFIG_THRIFT_COMPACT_PROTOCOL)) { 110 protocol = std::make_shared<TCompactProtocol>(transport); 111 } else { 112 protocol = std::make_shared<TBinaryProtocol>(transport); 113 } 114 115 HelloClient client(protocol); 116 117 try { 118 transport->open(); 119 client.ping(); 120 std::string s; 121 client.echo(s, "Hello, world!"); 122 for (int i = 0; i < 5; ++i) { 123 client.counter(); 124 } 125 126 transport->close(); 127 } catch (std::exception &e) { 128 printf("caught exception: %s\n", e.what()); 129 return EXIT_FAILURE; 130 } 131 132 return EXIT_SUCCESS; 133 } 134