|
|
|
Copyright © 2003-2010 ZeroC, Inc. |
19.2 The Objective‑C Client
We now have seen enough of the client-side Objective‑C mapping to develop a complete client to access our remote file system. For reference, here is the Slice definition once more:["objc:prefix:FS"]
module Filesystem {
exception GenericError {
string reason;
};
interface Node {
idempotent string name();
};
sequence<string> Lines;
interface File extends Node {
idempotent Lines read();
idempotent void write(Lines text) throws GenericError;
};
sequence<Node*> NodeSeq;
interface Directory extends Node {
idempotent NodeSeq list();
};
};To exercise the file system, the client does a recursive listing of the file system, starting at the root directory. For each node in the file system, the client shows the name of the node and whether that node is a file or directory. If the node is a file, the client retrieves the contents of the file and prints them.#import <Ice/Ice.h>
#import <Filesystem.h>
#import <Foundation/NSAutoreleasePool.h>
#import <stdio.h>
static void
listRecursive(id<FSDirectoryPrx> dir, int depth)
{
// ...
}
int
main(int argc, char* argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int status = 1;
id<ICECommunicator> communicator;
@try
{
communicator = [ICEUtil createCommunicator:&argc
argv:argv];
// Create a proxy for the root directory
//
id<FSDirectoryPrx> rootDir = [FSDirectoryPrx checkedCast:
[communicator stringToProxy:
@"RootDir:default ‑p 10000"]];
if (!rootDir)
[NSException raise:@"invalid proxy" format:@"nil"];
// Recursively list the contents of the root directory
//
printf("Contents of root directory:\n");
listRecursive(rootDir, 0);
status = 0;
} @catch (NSException *ex) {
NSLog(@"%@\n", [ex name]);
}
@try {
[communicator destroy];
} @catch (NSException* ex) {
NSLog(@"%@\n", [ex name]);
}
[pool release];
return status;
}1. Ice/Ice.hThis file is always included in both client and server source files. It provides definitions that are necessary for accessing the Ice run time.2. Filesystem.hThis is the header that is generated by the Slice compiler from the Slice definitions in Filesystem.ice.4. stdio.h2. The structure of the code in main follows what we saw in Chapter 3. After initializing the run time, the client creates a proxy to the root directory of the file system. For this example, we assume that the server runs on the local host and listens using the default protocol (TCP/IP) at port 10000. The object identity of the root directory is known to be RootDir.3. The client down-casts the proxy to DirectoryPrx and passes that proxy to listRecursive, which prints the contents of the file system.Most of the work happens in listRecursive:// Print the specified number of tabs.
static void
printIndent(int depth)
{
while (depth‑‑ > 0)
putchar('\t');
}
// 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(id<FSDirectoryPrx> dir, int depth)
{
++depth;
FSNodeSeq *contents = [dir list];
for (id<FSNodePrx> node in contents) {
id<FSDirectoryPrx> dir =
[FSDirectoryPrx checkedCast:node];
id<FSFilePrx> file = [FSFilePrx uncheckedCast:node];
printIndent(depth);
printf("%s%s\n", [[node name] UTF8String],
(dir ? " (directory):" : " (file):"));
if (dir) {
listRecursive(dir, depth);
} else {
FSLines *text = [file read];
for (NSString *line in text) {
printIndent(depth);
printf("\t%s\n", [line UTF8String]);
}
}
}
}The function is passed a proxy to a directory to list, and an indent level. (The indent level increments with each recursive call and allows the code to print the name of each node at an indent level that corresponds to the depth of the tree at that node.) listRecursive calls the list operation on the directory and iterates over the returned sequence of nodes:1. The code does a checkedCast to narrow the Node proxy to a Directory proxy, as well as an uncheckedCast to narrow the Node proxy to a File proxy. Exactly one of those casts will succeed, so there is no need to call checkedCast twice: if the Node is-a Directory, the code uses the id<FSDirectoryPrx> returned by the checkedCast; if the checkedCast fails, we know that the Node is-a File and, therefore, an uncheckedCast is sufficient to get an id<FSFilePrx>.In general, if you know that a down-cast to a specific type will succeed, it is preferable to use an uncheckedCast instead of a checkedCast because an uncheckedCast does not incur any network traffic.2. The code prints the name of the file or directory and then, depending on which cast succeeded, prints "(directory)" or "(file)" following the name.• If it is a file, the code calls the read operation on the file to retrieve the file contents and then iterates over the returned sequence of lines, printing each line.Figure 19.1. A small file system.Contents of root directory:
README (file):
This file system contains a collection of poetry.
Coleridge (directory):
Kubla_Khan (file):
In Xanadu did Kubla Khan
A stately pleasure‑dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.• The client makes more remote procedure calls than strictly necessary; with minor redesign of the Slice definitions, many of these calls can be avoided.
|
|