|
| 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__ |
| 41 | +int 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 | + }; |
| 82 | + |
| 83 | + static const char qemu_key_pem[] = { |
| 84 | +#include "qemu_key.pem.inc" |
| 85 | + }; |
| 86 | + |
| 87 | + static const char native_cert_pem[] = { |
| 88 | +#include "native_cert.pem.inc" |
| 89 | + }; |
| 90 | + |
| 91 | + socketFactory->loadCertificateFromBuffer(qemu_cert_pem); |
| 92 | + socketFactory->loadPrivateKeyFromBuffer(qemu_key_pem); |
| 93 | + socketFactory->loadTrustedCertificatesFromBuffer(native_cert_pem); |
| 94 | +#else |
| 95 | + socketFactory->loadCertificate(argv[2]); |
| 96 | + socketFactory->loadPrivateKey(argv[3]); |
| 97 | + socketFactory->loadTrustedCertificates(argv[4]); |
| 98 | +#endif |
| 99 | + trans = socketFactory->createSocket(my_addr, port); |
| 100 | + } else { |
| 101 | + trans = std::make_shared<TSocket>(my_addr, port); |
| 102 | + } |
| 103 | + |
| 104 | + transport = std::make_shared<TBufferedTransport>(trans); |
| 105 | + |
| 106 | + if (IS_ENABLED(CONFIG_THRIFT_COMPACT_PROTOCOL)) { |
| 107 | + protocol = std::make_shared<TCompactProtocol>(transport); |
| 108 | + } else { |
| 109 | + protocol = std::make_shared<TBinaryProtocol>(transport); |
| 110 | + } |
| 111 | + |
| 112 | + HelloClient client(protocol); |
| 113 | + |
| 114 | + try { |
| 115 | + transport->open(); |
| 116 | + client.ping(); |
| 117 | + std::string s; |
| 118 | + client.echo(s, "Hello, world!"); |
| 119 | + for (int i = 0; i < 5; ++i) { |
| 120 | + client.counter(); |
| 121 | + } |
| 122 | + |
| 123 | + transport->close(); |
| 124 | + } catch (std::exception &e) { |
| 125 | + printf("caught exception: %s\n", e.what()); |
| 126 | + return EXIT_FAILURE; |
| 127 | + } |
| 128 | + |
| 129 | + return EXIT_SUCCESS; |
| 130 | +} |
0 commit comments