/* * Copyright 2022 Young Mei * * SPDX-License-Identifier: Apache-2.0 */ #ifdef __ZEPHYR__ #include #endif #include #include #include #include #include #include #include #include #include #include "Hello.h" #include "HelloHandler.h" using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; #ifndef IS_ENABLED #define IS_ENABLED(flag) flag #endif #ifndef CONFIG_THRIFT_COMPACT_PROTOCOL #define CONFIG_THRIFT_COMPACT_PROTOCOL 0 #endif #ifndef CONFIG_THRIFT_SSL_SOCKET #define CONFIG_THRIFT_SSL_SOCKET 0 #endif #ifdef __ZEPHYR__ int main(void) #else int main(int argc, char **argv) #endif { std::string my_addr; #ifdef __ZEPHYR__ my_addr = CONFIG_NET_CONFIG_MY_IPV4_ADDR; #else if (IS_ENABLED(CONFIG_THRIFT_SSL_SOCKET)) { if (argc != 5) { printf("usage: %s " "\n", argv[0]); return EXIT_FAILURE; } } else { if (argc != 2) { printf("usage: %s \n", argv[0]); return EXIT_FAILURE; } } my_addr = std::string(argv[1]); #endif const int port = 4242; std::shared_ptr serverTransport; std::shared_ptr transportFactory; std::shared_ptr protocolFactory; std::shared_ptr handler(new HelloHandler()); std::shared_ptr processor(new HelloProcessor(handler)); if (IS_ENABLED(CONFIG_THRIFT_SSL_SOCKET)) { std::shared_ptr socketFactory(new TSSLSocketFactory()); socketFactory->server(true); #ifdef __ZEPHYR__ static const char qemu_cert_pem[] = { #include "qemu_cert.pem.inc" '\0' }; static const char qemu_key_pem[] = { #include "qemu_key.pem.inc" '\0' }; static const char native_cert_pem[] = { #include "native_cert.pem.inc" '\0' }; socketFactory->loadCertificateFromBuffer(qemu_cert_pem); socketFactory->loadPrivateKeyFromBuffer(qemu_key_pem); socketFactory->loadTrustedCertificatesFromBuffer(native_cert_pem); #else socketFactory->loadCertificate(argv[2]); socketFactory->loadPrivateKey(argv[3]); socketFactory->loadTrustedCertificates(argv[4]); #endif serverTransport = std::make_shared("0.0.0.0", port, socketFactory); } else { serverTransport = std::make_shared(my_addr, port); } transportFactory = std::make_shared(); if (IS_ENABLED(CONFIG_THRIFT_COMPACT_PROTOCOL)) { protocolFactory = std::make_shared(); } else { protocolFactory = std::make_shared(); } TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); try { server.serve(); } catch (std::exception &e) { printf("caught exception: %s\n", e.what()); return EXIT_FAILURE; } return EXIT_SUCCESS; }