|
| 1 | +// |
| 2 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// |
| 5 | + |
| 6 | +#ifndef OCVSMD_SDK_FILE_SERVER_HPP_INCLUDED |
| 7 | +#define OCVSMD_SDK_FILE_SERVER_HPP_INCLUDED |
| 8 | + |
| 9 | +#include "execution.hpp" |
| 10 | + |
| 11 | +#include <cetl/pf17/cetlpf.hpp> |
| 12 | + |
| 13 | +#include <chrono> |
| 14 | +#include <memory> |
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace ocvsmd |
| 19 | +{ |
| 20 | +namespace sdk |
| 21 | +{ |
| 22 | + |
| 23 | +/// Defines client side interface of the OCVSMD File Server component. |
| 24 | +/// |
| 25 | +class FileServer |
| 26 | +{ |
| 27 | +public: |
| 28 | + /// Defines the shared pointer type for the interface. |
| 29 | + /// |
| 30 | + using Ptr = std::shared_ptr<FileServer>; |
| 31 | + |
| 32 | + // No copy/move semantics. |
| 33 | + FileServer(FileServer&&) = delete; |
| 34 | + FileServer(const FileServer&) = delete; |
| 35 | + FileServer& operator=(FileServer&&) = delete; |
| 36 | + FileServer& operator=(const FileServer&) = delete; |
| 37 | + |
| 38 | + virtual ~FileServer() = default; |
| 39 | + |
| 40 | + /// Makes async sender which emits a current list of the File Server root paths. |
| 41 | + /// |
| 42 | + /// @return An execution sender which emits the async result of the operation. |
| 43 | + /// The returned paths are the same values as they were added by `pushRoot`. |
| 44 | + /// The entries are not unique, and the order is preserved. |
| 45 | + /// |
| 46 | + struct ListRoots final |
| 47 | + { |
| 48 | + using Success = std::vector<std::string>; |
| 49 | + using Failure = int; // `errno`-like error code |
| 50 | + using Result = cetl::variant<Success, Failure>; |
| 51 | + }; |
| 52 | + virtual SenderOf<ListRoots::Result>::Ptr listRoots() = 0; |
| 53 | + |
| 54 | + /// Does nothing if such root path does not exist (no error reported). |
| 55 | + /// If such a path is listed more than once, only one copy is removed. |
| 56 | + /// The `back` flag determines whether the path is searched from the front or the back of the list. |
| 57 | + /// The flag has no effect if there are no duplicates. |
| 58 | + /// The changed list of paths is persisted by the daemon (in its configuration; on its exit), |
| 59 | + /// so the list will be automatically restored on the next daemon start. |
| 60 | + /// |
| 61 | + struct PopRoot final |
| 62 | + { |
| 63 | + using Success = cetl::monostate; // like `void` |
| 64 | + using Failure = int; // `errno`-like error code |
| 65 | + using Result = cetl::variant<Success, Failure>; |
| 66 | + }; |
| 67 | + /// Removes a root directory from the list of directories that the file server will serve. |
| 68 | + /// |
| 69 | + /// @return An execution sender which emits the async result of the operation. |
| 70 | + /// |
| 71 | + virtual SenderOf<PopRoot::Result>::Ptr popRoot(const std::string& path, const bool back) = 0; |
| 72 | + |
| 73 | + /// When the file server handles a request, it will attempt to locate the path relative to each of its root |
| 74 | + /// directories. See Yakut for a hands-on example. |
| 75 | + /// The `path` could be a relative or an absolute path. In case of a relative path |
| 76 | + /// (without leading `/`), the daemon will resolve it against its current working directory. |
| 77 | + /// The daemon will internally canonicalize the path and resolve symlinks, |
| 78 | + /// and use real the file system path when matching and serving Cyphal network 'File' requests. |
| 79 | + /// The same path may be added multiple times to avoid interference across different clients. |
| 80 | + /// Currently, the path should be a directory (later we might support a direct file as well). |
| 81 | + /// The `back` flag determines whether the path is added to the front or the back of the list. |
| 82 | + /// The changed list of paths is persisted by the daemon (in its configuration; on its exit), |
| 83 | + /// so the list will be automatically restored on the next daemon start. |
| 84 | + /// |
| 85 | + struct PushRoot final |
| 86 | + { |
| 87 | + using Success = cetl::monostate; // like `void` |
| 88 | + using Failure = int; // `errno`-like error code |
| 89 | + using Result = cetl::variant<Success, Failure>; |
| 90 | + }; |
| 91 | + /// Adds a new root directory to the list of directories that the file server will serve. |
| 92 | + /// |
| 93 | + /// @return An execution sender which emits the async result of the operation. |
| 94 | + /// |
| 95 | + virtual SenderOf<PushRoot::Result>::Ptr pushRoot(const std::string& path, const bool back) = 0; |
| 96 | + |
| 97 | +protected: |
| 98 | + FileServer() = default; |
| 99 | + |
| 100 | +}; // FileServer |
| 101 | + |
| 102 | +} // namespace sdk |
| 103 | +} // namespace ocvsmd |
| 104 | + |
| 105 | +#endif // OCVSMD_SDK_FILE_SERVER_HPP_INCLUDED |
0 commit comments