ICE 3.4.2
[php5-ice-freebsdport.git] / rb / demo / book / simple_filesystem / Client.rb
blob8d7af903c774191b877f85ca89266cbab0a36b54
1 #!/usr/bin/env ruby
2 # **********************************************************************
4 # Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
6 # This copy of Ice is licensed to you under the terms described in the
7 # ICE_LICENSE file included in this distribution.
9 # **********************************************************************
11 require 'Ice'
12 Ice::loadSlice('Filesystem.ice')
14 # Recursively print the contents of directory "dir"
15 # in tree fashion. For files, show the contents of
16 # each file. The "depth" parameter is the current
17 # nesting level (for indentation).
19 def listRecursive(dir, depth)
20     indent = ''
21     depth = depth + 1
22     for i in (0...depth)
23         indent += "\t"
24     end
26     contents = dir.list()
28     for node in contents
29         subdir = Filesystem::DirectoryPrx::checkedCast(node)
30         file = Filesystem::FilePrx::uncheckedCast(node)
31         print indent + node.name()
32         if subdir
33             puts "(directory):"
34             listRecursive(subdir, depth)
35         else
36             puts "(file):"
37             text = file.read()
38             for line in text
39                 puts indent + "\t" + line
40             end
41         end
42     end
43 end
45 status = 0
46 ic = nil
47 begin
48     # Create a communicator
49     #
50     ic = Ice::initialize(ARGV)
52     # Create a proxy for the root directory
53     #
54     obj = ic.stringToProxy("RootDir:default -p 10000")
56     # Down-cast the proxy to a Directory proxy
57     #
58     rootDir = Filesystem::DirectoryPrx::checkedCast(obj)
60     # Recursively list the contents of the root directory
61     #
62     puts "Contents of root directory:"
63     listRecursive(rootDir, 0)
64 rescue => ex
65     puts ex
66     print ex.backtrace.join("\n")
67     status = 1
68 end
70 if ic
71     # Clean up
72     #
73     begin
74         ic.destroy()
75     rescue => ex
76         puts ex
77         print ex.backtrace.join("\n")
78         status = 1
79     end
80 end
82 exit(status)