1 #pragma once
2 
3 #include "blobs.hpp"
4 
5 #include <string>
6 #include <unordered_map>
7 #include <vector>
8 
9 namespace blobs
10 {
11 
12 constexpr int kBufferSize = 1024;
13 
14 struct ExampleBlob
15 {
16     ExampleBlob() = default;
17     ExampleBlob(uint16_t id, uint16_t flags) :
18         sessionId(id), flags(flags), length(0)
19     {
20     }
21 
22     /* The blob handler session id. */
23     uint16_t sessionId;
24 
25     /* The flags passed into open. */
26     uint16_t flags;
27 
28     /* The buffer is a fixed size, but length represents the number of bytes
29      * expected to be used contiguously from offset 0.
30      */
31     uint32_t length;
32 
33     /* The staging buffer. */
34     uint8_t buffer[kBufferSize];
35 };
36 
37 class ExampleBlobHandler : public GenericBlobInterface
38 {
39   public:
40     /* We want everything explicitly default. */
41     ExampleBlobHandler() = default;
42     ~ExampleBlobHandler() = default;
43     ExampleBlobHandler(const ExampleBlobHandler&) = default;
44     ExampleBlobHandler& operator=(const ExampleBlobHandler&) = default;
45     ExampleBlobHandler(ExampleBlobHandler&&) = default;
46     ExampleBlobHandler& operator=(ExampleBlobHandler&&) = default;
47 
48     bool canHandleBlob(const std::string& path) override;
49     std::vector<std::string> getBlobIds() override;
50     bool deleteBlob(const std::string& path) override;
51     bool stat(const std::string& path, struct BlobMeta* meta) override;
52     bool open(uint16_t session, uint16_t flags,
53               const std::string& path) override;
54     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
55                               uint32_t requestedSize) override;
56     bool write(uint16_t session, uint32_t offset,
57                const std::vector<uint8_t>& data) override;
58     bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
59     bool close(uint16_t session) override;
60     bool stat(uint16_t session, struct BlobMeta* meta) override;
61     bool expire(uint16_t session) override;
62 
63     constexpr static char supportedPath[] = "/dev/fake/command";
64 
65   private:
66     ExampleBlob* getSession(uint16_t id);
67 
68     std::unordered_map<uint16_t, ExampleBlob> sessions;
69 };
70 
71 } // namespace blobs
72