summaryrefslogtreecommitdiff
path: root/cpp/demo/book/freeze_filesystem/Client.cpp
blob: e73351b6777f409e9ae279e7bb22783dda7a4d64 (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
149
150
151
// **********************************************************************
//
// Copyright (c) 2003-2008 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;

class FilesystemClient : public Ice::Application
{
    virtual int run(int argc, char* argv[]);
    virtual void interruptCallback(int);
};

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

int
FilesystemClient::run(int argc, char* argv[])
{
    //
    // Since this is an interactive demo we want the custom interrupt
    // callback to be called when the process is interrupted.
    //
    callbackOnInterrupt();

    //
    // 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->resolve("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;
    NodeDict contents = rootDir->list(RecursiveList);
    NodeDict::iterator p;
    for(p = contents.begin(); p != contents.end(); ++p)
    {
        cout << "  " << p->first << endl;
    }

    NodeDesc desc = rootDir->resolve("Coleridge/Kubla_Khan");
    FilePrx file = FilePrx::checkedCast(desc.proxy);
    assert(file);
    Lines text = file->read();
    cout << "Contents of file Coleridge/Kubla_Khan:" << endl;
    for(Lines::iterator i = text.begin(); i != text.end(); ++i)
    {
        cout << "  " << *i << endl;
    }

    //
    // Destroy the filesystem.
    //
    contents = rootDir->list(NormalList);
    for(p = contents.begin(); p != contents.end(); ++p)
    {
        cout << "Destroying " << p->first << "..." << endl;
        p->second.proxy->destroy();
    }

    return EXIT_SUCCESS;
}

void
FilesystemClient::interruptCallback(int)
{
    try
    {
        communicator()->destroy();
    }
    catch(const IceUtil::Exception& ex)
    {
        cerr << appName() << ": " << ex << endl;
    }
    catch(...)
    {
        cerr << appName() << ": unknown exception" << endl;
    }
    exit(EXIT_SUCCESS);
}