Alexei Frolov | 5ceb57e | 2022-03-28 11:21:03 -0700 | [diff] [blame] | 1 | // Copyright 2022 The Pigweed Authors |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | // Simple RPC server with the transfer service registered. Reads HDLC frames |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 16 | // with RPC packets through a socket. The transfer service reads and writes to |
Alexei Frolov | 5ceb57e | 2022-03-28 11:21:03 -0700 | [diff] [blame] | 17 | // files within a given directory. The name of a file is its resource ID. |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 18 | |
| 19 | #include <cstddef> |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 20 | #include <filesystem> |
| 21 | #include <string> |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 22 | #include <thread> |
| 23 | #include <variant> |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 24 | #include <vector> |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 25 | |
| 26 | #include "pw_assert/check.h" |
| 27 | #include "pw_log/log.h" |
| 28 | #include "pw_rpc_system_server/rpc_server.h" |
| 29 | #include "pw_rpc_system_server/socket.h" |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 30 | #include "pw_stream/std_file_stream.h" |
Alexei Frolov | 4fd8c80 | 2021-11-09 09:49:25 -0800 | [diff] [blame] | 31 | #include "pw_thread/detached_thread.h" |
| 32 | #include "pw_thread_stl/options.h" |
Moussa Traore | 7662a93 | 2022-08-23 17:00:23 +0000 | [diff] [blame] | 33 | #include "pw_transfer/atomic_file_transfer_handler.h" |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 34 | #include "pw_transfer/transfer.h" |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 35 | #include "pw_transfer_test/test_server.raw_rpc.pb.h" |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 36 | |
| 37 | namespace pw::transfer { |
| 38 | namespace { |
Alexei Frolov | 39d8c5c | 2021-11-24 10:16:31 -0800 | [diff] [blame] | 39 | class TestServerService |
| 40 | : public pw_rpc::raw::TestServer::Service<TestServerService> { |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 41 | public: |
| 42 | TestServerService(TransferService& transfer_service) |
| 43 | : transfer_service_(transfer_service) {} |
| 44 | |
Moussa Traore | 7662a93 | 2022-08-23 17:00:23 +0000 | [diff] [blame] | 45 | ~TestServerService() { UnregisterHandlers(); } |
| 46 | |
| 47 | void UnregisterHandlers() { |
| 48 | for (auto handler : file_transfer_handlers_) { |
| 49 | transfer_service_.UnregisterHandler(*handler); |
| 50 | } |
| 51 | } |
| 52 | |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 53 | void set_directory(const char* directory) { directory_ = directory; } |
| 54 | |
Wyatt Hepler | b15c55b | 2022-02-11 09:26:05 -0800 | [diff] [blame] | 55 | void ReloadTransferFiles(ConstByteSpan, rpc::RawUnaryResponder&) { |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 56 | LoadFileHandlers(); |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void LoadFileHandlers() { |
| 60 | PW_LOG_INFO("Reloading file handlers from %s", directory_.c_str()); |
Moussa Traore | 7662a93 | 2022-08-23 17:00:23 +0000 | [diff] [blame] | 61 | UnregisterHandlers(); |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 62 | file_transfer_handlers_.clear(); |
| 63 | |
| 64 | for (const auto& entry : std::filesystem::directory_iterator(directory_)) { |
| 65 | if (!entry.is_regular_file()) { |
| 66 | continue; |
| 67 | } |
| 68 | |
Alexei Frolov | 5ceb57e | 2022-03-28 11:21:03 -0700 | [diff] [blame] | 69 | int resource_id = std::atoi(entry.path().filename().c_str()); |
| 70 | if (resource_id > 0) { |
| 71 | PW_LOG_DEBUG("Found transfer file %d", resource_id); |
Moussa Traore | 7662a93 | 2022-08-23 17:00:23 +0000 | [diff] [blame] | 72 | auto handler = std::make_shared<AtomicFileTransferHandler>( |
| 73 | resource_id, entry.path().c_str()); |
| 74 | transfer_service_.RegisterHandler(*handler); |
| 75 | file_transfer_handlers_.emplace_back(handler); |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | TransferService& transfer_service_; |
| 82 | std::string directory_; |
Moussa Traore | 7662a93 | 2022-08-23 17:00:23 +0000 | [diff] [blame] | 83 | std::vector<std::shared_ptr<AtomicFileTransferHandler>> |
| 84 | file_transfer_handlers_; |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Alexei Frolov | 563946f | 2021-08-05 18:58:48 -0700 | [diff] [blame] | 87 | constexpr size_t kChunkSizeBytes = 256; |
| 88 | constexpr size_t kMaxReceiveSizeBytes = 1024; |
| 89 | |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 90 | std::array<std::byte, kChunkSizeBytes> chunk_buffer; |
| 91 | std::array<std::byte, kChunkSizeBytes> encode_buffer; |
| 92 | transfer::Thread<4, 4> transfer_thread(chunk_buffer, encode_buffer); |
| 93 | TransferService transfer_service(transfer_thread, kMaxReceiveSizeBytes); |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 94 | TestServerService test_server_service(transfer_service); |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 95 | |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 96 | void RunServer(int socket_port, const char* directory) { |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 97 | rpc::system_server::set_socket_port(socket_port); |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 98 | |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 99 | test_server_service.set_directory(directory); |
| 100 | test_server_service.LoadFileHandlers(); |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 101 | |
| 102 | rpc::system_server::Init(); |
Wyatt Hepler | 7566bc8 | 2022-02-15 15:09:00 -0800 | [diff] [blame] | 103 | rpc::system_server::Server().RegisterService(test_server_service, |
| 104 | transfer_service); |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 105 | |
Alexei Frolov | 22ee114 | 2022-02-03 13:59:01 -0800 | [diff] [blame] | 106 | thread::DetachedThread(thread::stl::Options(), transfer_thread); |
Alexei Frolov | 4fd8c80 | 2021-11-09 09:49:25 -0800 | [diff] [blame] | 107 | |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 108 | PW_LOG_INFO("Starting pw_rpc server"); |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 109 | PW_CHECK_OK(rpc::system_server::Start()); |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace |
| 113 | } // namespace pw::transfer |
| 114 | |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 115 | int main(int argc, char* argv[]) { |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 116 | if (argc != 3) { |
| 117 | PW_LOG_ERROR("Usage: %s PORT DIR", argv[0]); |
Wyatt Hepler | 82d499b | 2021-08-23 09:13:16 -0700 | [diff] [blame] | 118 | return 1; |
| 119 | } |
Alexei Frolov | fad8e85 | 2021-08-16 13:58:51 -0700 | [diff] [blame] | 120 | |
| 121 | pw::transfer::RunServer(std::atoi(argv[1]), argv[2]); |
Wyatt Hepler | 2253040 | 2021-07-30 23:40:40 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |