~unity-api-team/storage-framework/vivid

« back to all changes in this revision

Viewing changes to demo/provider-test.cpp

  • Committer: James Henstridge
  • Date: 2016-05-10 09:38:09 UTC
  • mto: (8.1.4 storage-framework)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james@jamesh.id.au-20160510093809-uywupy44te62qe91
Add demo script, and fix up bugs preventing method calls from being handled.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <unity/storage/provider/ProviderBase.h>
 
3
#include <unity/storage/provider/Server.h>
 
4
 
 
5
#include <stdexcept>
 
6
 
 
7
using namespace unity::storage::provider;
 
8
 
 
9
class MyProvider : public ProviderBase
 
10
{
 
11
public:
 
12
    MyProvider();
 
13
 
 
14
    boost::future<std::vector<Item>> roots() override;
 
15
    boost::future<std::tuple<std::vector<Item>,std::string>> list(
 
16
        std::string const& item_id, std::string const& page_token) override;
 
17
    boost::future<std::vector<Item>> lookup(
 
18
        std::string const& parent_id, std::string const& name) override;
 
19
    boost::future<Item> metadata(std::string const& item_id) override;
 
20
};
 
21
 
 
22
MyProvider::MyProvider()
 
23
{
 
24
}
 
25
 
 
26
boost::future<std::vector<Item>> MyProvider::roots()
 
27
{
 
28
    std::vector<Item> roots = {
 
29
        {"root_id", "", "Root", "etag", ItemType::root, {}},
 
30
    };
 
31
    return boost::make_ready_future<std::vector<Item>>(roots);
 
32
}
 
33
 
 
34
boost::future<std::tuple<std::vector<Item>,std::string>> MyProvider::list(
 
35
    std::string const& item_id, std::string const& page_token)
 
36
{
 
37
    if (item_id != "root_id")
 
38
    {
 
39
        return boost::make_exceptional_future<std::tuple<std::vector<Item>,std::string>>(std::runtime_error("unknown folder"));
 
40
    }
 
41
    if (page_token != "")
 
42
    {
 
43
        return boost::make_exceptional_future<std::tuple<std::vector<Item>,std::string>>(std::runtime_error("unknown page token"));
 
44
    }
 
45
    std::vector<Item> children = {
 
46
        {"child_id", "root_id", "Child", "etag", ItemType::file, {}}
 
47
    };
 
48
    boost::promise<std::tuple<std::vector<Item>,std::string>> p;
 
49
    p.set_value(std::make_tuple(children, std::string()));
 
50
    return p.get_future();
 
51
}
 
52
 
 
53
boost::future<std::vector<Item>> MyProvider::lookup(
 
54
    std::string const& parent_id, std::string const& name)
 
55
{
 
56
    if (parent_id != "root_id" || name != "Child")
 
57
    {
 
58
        return boost::make_exceptional_future<std::vector<Item>>(std::runtime_error("file not found"));
 
59
    }
 
60
    std::vector<Item> children = {
 
61
        {"child_id", "root_id", "Child", "etag", ItemType::file, {}}
 
62
    };
 
63
    return boost::make_ready_future<std::vector<Item>>(children);
 
64
}
 
65
 
 
66
boost::future<Item> MyProvider::metadata(std::string const& item_id)
 
67
{
 
68
    if (item_id == "root_id")
 
69
    {
 
70
        Item metadata{"root_id", "", "Root", "etag", ItemType::root, {}};
 
71
        return boost::make_ready_future<Item>(metadata);
 
72
    }
 
73
    else if (item_id == "child_id")
 
74
    {
 
75
        Item metadata{"child_id", "root_id", "Child", "etag", ItemType::file, {}};
 
76
        return boost::make_ready_future<Item>(metadata);
 
77
    }
 
78
    return boost::make_exceptional_future<Item>(std::runtime_error("no such file"));
 
79
}
 
80
 
 
81
int main(int argc, char **argv)
 
82
{
 
83
    Server<MyProvider> server;
 
84
    server.init(argc, argv);
 
85
    server.run();
 
86
}