summaryrefslogtreecommitdiffstats
path: root/example/example.hpp
blob: 0ea95721e344dd8ea55d06a96300c9eadd6833a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include "blobs.hpp"

#include <string>
#include <unordered_map>
#include <vector>

namespace blobs
{

constexpr int kBufferSize = 1024;

struct ExampleBlob
{
    ExampleBlob() = default;
    ExampleBlob(uint16_t id, uint16_t flags) :
        sessionId(id), flags(flags), length(0)
    {
    }

    /* The blob handler session id. */
    uint16_t sessionId;

    /* The flags passed into open. */
    uint16_t flags;

    /* The buffer is a fixed size, but length represents the number of bytes
     * expected to be used contiguously from offset 0.
     */
    uint32_t length;

    /* The staging buffer. */
    uint8_t buffer[kBufferSize];
};

class ExampleBlobHandler : public GenericBlobInterface
{
  public:
    /* We want everything explicitly default. */
    ExampleBlobHandler() = default;
    ~ExampleBlobHandler() = default;
    ExampleBlobHandler(const ExampleBlobHandler&) = default;
    ExampleBlobHandler& operator=(const ExampleBlobHandler&) = default;
    ExampleBlobHandler(ExampleBlobHandler&&) = default;
    ExampleBlobHandler& operator=(ExampleBlobHandler&&) = default;

    bool canHandleBlob(const std::string& path) override;
    std::vector<std::string> getBlobIds() override;
    bool deleteBlob(const std::string& path) override;
    bool stat(const std::string& path, struct BlobMeta* meta) override;
    bool open(uint16_t session, uint16_t flags,
              const std::string& path) override;
    std::vector<uint8_t> read(uint16_t session, uint32_t offset,
                              uint32_t requestedSize) override;
    bool write(uint16_t session, uint32_t offset,
               const std::vector<uint8_t>& data) override;
    bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
    bool close(uint16_t session) override;
    bool stat(uint16_t session, struct BlobMeta* meta) override;
    bool expire(uint16_t session) override;

    constexpr static char supportedPath[] = "/dev/fake/command";

  private:
    ExampleBlob* getSession(uint16_t id);

    std::unordered_map<uint16_t, ExampleBlob> sessions;
};

} // namespace blobs
OpenPOWER on IntegriCloud