blob: fa911b732e77dc8bf742f4d8798451a1dff6c44d [file] [log] [blame]
Alexei Frolov5ceb57e2022-03-28 11:21:03 -07001// Copyright 2022 The Pigweed Authors
Wyatt Hepler22530402021-07-30 23:40:40 -07002//
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 Frolovfad8e852021-08-16 13:58:51 -070016// with RPC packets through a socket. The transfer service reads and writes to
Alexei Frolov5ceb57e2022-03-28 11:21:03 -070017// files within a given directory. The name of a file is its resource ID.
Wyatt Hepler22530402021-07-30 23:40:40 -070018
19#include <cstddef>
Alexei Frolovfad8e852021-08-16 13:58:51 -070020#include <filesystem>
21#include <string>
Wyatt Hepler22530402021-07-30 23:40:40 -070022#include <thread>
23#include <variant>
Alexei Frolovfad8e852021-08-16 13:58:51 -070024#include <vector>
Wyatt Hepler22530402021-07-30 23:40:40 -070025
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 Frolovfad8e852021-08-16 13:58:51 -070030#include "pw_stream/std_file_stream.h"
Alexei Frolov4fd8c802021-11-09 09:49:25 -080031#include "pw_thread/detached_thread.h"
32#include "pw_thread_stl/options.h"
Moussa Traore7662a932022-08-23 17:00:23 +000033#include "pw_transfer/atomic_file_transfer_handler.h"
Wyatt Hepler22530402021-07-30 23:40:40 -070034#include "pw_transfer/transfer.h"
Alexei Frolovfad8e852021-08-16 13:58:51 -070035#include "pw_transfer_test/test_server.raw_rpc.pb.h"
Wyatt Hepler22530402021-07-30 23:40:40 -070036
37namespace pw::transfer {
38namespace {
Alexei Frolov39d8c5c2021-11-24 10:16:31 -080039class TestServerService
40 : public pw_rpc::raw::TestServer::Service<TestServerService> {
Alexei Frolovfad8e852021-08-16 13:58:51 -070041 public:
42 TestServerService(TransferService& transfer_service)
43 : transfer_service_(transfer_service) {}
44
Moussa Traore7662a932022-08-23 17:00:23 +000045 ~TestServerService() { UnregisterHandlers(); }
46
47 void UnregisterHandlers() {
48 for (auto handler : file_transfer_handlers_) {
49 transfer_service_.UnregisterHandler(*handler);
50 }
51 }
52
Alexei Frolovfad8e852021-08-16 13:58:51 -070053 void set_directory(const char* directory) { directory_ = directory; }
54
Wyatt Heplerb15c55b2022-02-11 09:26:05 -080055 void ReloadTransferFiles(ConstByteSpan, rpc::RawUnaryResponder&) {
Alexei Frolovfad8e852021-08-16 13:58:51 -070056 LoadFileHandlers();
Alexei Frolovfad8e852021-08-16 13:58:51 -070057 }
58
59 void LoadFileHandlers() {
60 PW_LOG_INFO("Reloading file handlers from %s", directory_.c_str());
Moussa Traore7662a932022-08-23 17:00:23 +000061 UnregisterHandlers();
Alexei Frolovfad8e852021-08-16 13:58:51 -070062 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 Frolov5ceb57e2022-03-28 11:21:03 -070069 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 Traore7662a932022-08-23 17:00:23 +000072 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 Frolovfad8e852021-08-16 13:58:51 -070076 }
77 }
78 }
79
80 private:
81 TransferService& transfer_service_;
82 std::string directory_;
Moussa Traore7662a932022-08-23 17:00:23 +000083 std::vector<std::shared_ptr<AtomicFileTransferHandler>>
84 file_transfer_handlers_;
Wyatt Hepler22530402021-07-30 23:40:40 -070085};
86
Alexei Frolov563946f2021-08-05 18:58:48 -070087constexpr size_t kChunkSizeBytes = 256;
88constexpr size_t kMaxReceiveSizeBytes = 1024;
89
Alexei Frolov22ee1142022-02-03 13:59:01 -080090std::array<std::byte, kChunkSizeBytes> chunk_buffer;
91std::array<std::byte, kChunkSizeBytes> encode_buffer;
92transfer::Thread<4, 4> transfer_thread(chunk_buffer, encode_buffer);
93TransferService transfer_service(transfer_thread, kMaxReceiveSizeBytes);
Alexei Frolovfad8e852021-08-16 13:58:51 -070094TestServerService test_server_service(transfer_service);
Wyatt Hepler22530402021-07-30 23:40:40 -070095
Alexei Frolovfad8e852021-08-16 13:58:51 -070096void RunServer(int socket_port, const char* directory) {
Wyatt Hepler82d499b2021-08-23 09:13:16 -070097 rpc::system_server::set_socket_port(socket_port);
Wyatt Hepler22530402021-07-30 23:40:40 -070098
Alexei Frolovfad8e852021-08-16 13:58:51 -070099 test_server_service.set_directory(directory);
100 test_server_service.LoadFileHandlers();
Wyatt Hepler22530402021-07-30 23:40:40 -0700101
102 rpc::system_server::Init();
Wyatt Hepler7566bc82022-02-15 15:09:00 -0800103 rpc::system_server::Server().RegisterService(test_server_service,
104 transfer_service);
Wyatt Hepler22530402021-07-30 23:40:40 -0700105
Alexei Frolov22ee1142022-02-03 13:59:01 -0800106 thread::DetachedThread(thread::stl::Options(), transfer_thread);
Alexei Frolov4fd8c802021-11-09 09:49:25 -0800107
Wyatt Hepler22530402021-07-30 23:40:40 -0700108 PW_LOG_INFO("Starting pw_rpc server");
Wyatt Hepler82d499b2021-08-23 09:13:16 -0700109 PW_CHECK_OK(rpc::system_server::Start());
Wyatt Hepler22530402021-07-30 23:40:40 -0700110}
111
112} // namespace
113} // namespace pw::transfer
114
Wyatt Hepler82d499b2021-08-23 09:13:16 -0700115int main(int argc, char* argv[]) {
Alexei Frolovfad8e852021-08-16 13:58:51 -0700116 if (argc != 3) {
117 PW_LOG_ERROR("Usage: %s PORT DIR", argv[0]);
Wyatt Hepler82d499b2021-08-23 09:13:16 -0700118 return 1;
119 }
Alexei Frolovfad8e852021-08-16 13:58:51 -0700120
121 pw::transfer::RunServer(std::atoi(argv[1]), argv[2]);
Wyatt Hepler22530402021-07-30 23:40:40 -0700122 return 0;
123}