summaryrefslogtreecommitdiff
path: root/cpp/demo/book/freeze_filesystem/Client.cpp
blob: 9fa07c9898eb84beba018695d4c44a4eae51b8b5 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/Ice.h>
#include <Filesystem.h>

using namespace std;
using namespace Filesystem;

// Recursively print the contents of directory "dir" in tree fashion.
// For files, show the contents of each file. The "depth"
// parameter is the current nesting level (for indentation).

static void
listRecursive(const DirectoryPrx & dir, int depth = 0)
{
    string indent(++depth, '\t');

    NodeDescSeq contents = dir->list();

    for (NodeDescSeq::const_iterator i = contents.begin(); i != contents.end(); ++i) {
        DirectoryPrx dir = DirectoryPrx::checkedCast(i->proxy);
        FilePrx file = FilePrx::uncheckedCast(i->proxy);
        cout << indent << i->name << (dir ? " (directory):" : " (file):") << endl;
        if (dir) {
            listRecursive(dir, depth);
        } else {
            Lines text = file->read();
            for (Lines::const_iterator j = text.begin(); j != text.end(); ++j)
                cout << indent << "\t" << *j << endl;
        }
    }
}

class FilesystemClient : public Ice::Application
{
public:

    FilesystemClient();
    virtual int run(int argc, char* argv[]);
};

int
main(int argc, char* argv[])
{
    FilesystemClient app;
    return app.main(argc, argv, "config.client");
}

FilesystemClient::FilesystemClient() :
    //
    // Since this is an interactive demo we don't want any signal
    // handling.
    //
    Ice::Application(Ice::NoSignalHandling)
{
}

int
FilesystemClient::run(int argc, char* argv[])
{
    //
    // Create a proxy for the root directory.
    //
    DirectoryPrx rootDir = DirectoryPrx::checkedCast(communicator()->propertyToProxy("RootDir.Proxy"));
    if(!rootDir)
    {
        cerr << argv[0] << ": invalid proxy" << endl;
        return EXIT_FAILURE;
    }

    //
    // Create a file called "README" in the root directory.
    //
    try
    {
        FilePrx readme = rootDir->createFile("README");
        Lines text;
        text.push_back("This file system contains a collection of poetry.");
        readme->write(text);
        cout << "Created README." << endl;
    }
    catch(const NameInUse&)
    {
        //
        // Ignore - file already exists.
        //
    }

    //
    // Create a directory called "Coleridge" in the root directory.
    //
    DirectoryPrx coleridge;
    try
    {
        coleridge = rootDir->createDirectory("Coleridge");
        cout << "Created Coleridge." << endl;
    }
    catch(const NameInUse&)
    {
        NodeDesc desc = rootDir->find("Coleridge");
        coleridge = DirectoryPrx::checkedCast(desc.proxy);
        assert(coleridge);
    }

    //
    // Create a file called "Kubla_Khan" in the Coleridge directory.
    //
    try
    {
        FilePrx file = coleridge->createFile("Kubla_Khan");
        Lines text;
        text.push_back("In Xanadu did Kubla Khan");
        text.push_back("A stately pleasure-dome decree:");
        text.push_back("Where Alph, the sacred river, ran");
        text.push_back("Through caverns measureless to man");
        text.push_back("Down to a sunless sea.");
        file->write(text);
        cout << "Created Coleridge/Kubla_Khan." << endl;
    }
    catch(const NameInUse&)
    {
        //
        // Ignore - file already exists.
        //
    }

    cout << "Contents of filesystem:" << endl;
    listRecursive(rootDir);

    //
    // Destroy the filesystem.
    //
    NodeDescSeq contents = rootDir->list();
    for(NodeDescSeq::iterator i = contents.begin(); i != contents.end(); ++i)
    {
        cout << "Destroying " << i->name << endl;
        i->proxy->destroy();
    }

    return EXIT_SUCCESS;
}