diff options
author | Mark Spruiell <mes@zeroc.com> | 2009-12-12 06:15:33 -0800 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2009-12-12 06:15:33 -0800 |
commit | 2b0101c6ff41eda1139634f67a4d003f2f9cbff6 (patch) | |
tree | c5eebb0b6782650d5efde894698300ffd2bdb710 /java | |
parent | Minor fixes to several test scripts. (diff) | |
download | ice-2b0101c6ff41eda1139634f67a4d003f2f9cbff6.tar.bz2 ice-2b0101c6ff41eda1139634f67a4d003f2f9cbff6.tar.xz ice-2b0101c6ff41eda1139634f67a4d003f2f9cbff6.zip |
Renaming demo/book/freeze_filesystem to evictor_filesystem.
Adding demo/book/map_filesystem.
Diffstat (limited to 'java')
42 files changed, 2502 insertions, 181 deletions
diff --git a/java/allDemos.py b/java/allDemos.py index e07b2b109bc..3e50fe49f4f 100755 --- a/java/allDemos.py +++ b/java/allDemos.py @@ -48,7 +48,8 @@ demos = [ "Freeze/library", "Freeze/transform", "Freeze/casino", - "book/freeze_filesystem", + "book/evictor_filesystem", + "book/map_filesystem", "book/simple_filesystem", "book/printer", "book/lifecycle", diff --git a/java/demo/book/README b/java/demo/book/README index 3d58a2ba06f..d1653f804c4 100644 --- a/java/demo/book/README +++ b/java/demo/book/README @@ -6,10 +6,15 @@ Demos in this directory: the Ice Run Time chapter. You can copy and modify this code to suit your needs. -- freeze_filesystem +- evictor_filesystem An implementation of the persistent version of the file system - example described in the Freeze chapter. + example described in the Freeze chapter using a Freeze evictor. + +- map_filesystem + + An implementation of the persistent version of the file system + example described in the Freeze chapter using a Freeze map. - lifecycle diff --git a/java/demo/book/build.xml b/java/demo/book/build.xml index 997d62a507b..bb2c8d93a67 100644 --- a/java/demo/book/build.xml +++ b/java/demo/book/build.xml @@ -14,7 +14,8 @@ <target name="all"> <ant dir="printer"/> <ant dir="simple_filesystem"/> - <ant dir="freeze_filesystem"/> + <ant dir="evictor_filesystem"/> + <ant dir="map_filesystem"/> <ant dir="lifecycle"/> </target> @@ -22,7 +23,8 @@ <ant dir="printer" target="clean"/> <ant dir="simple_filesystem" target="clean"/> <ant dir="lifecycle" target="clean"/> - <ant dir="freeze_filesystem" target="clean"/> + <ant dir="evictor_filesystem" target="clean"/> + <ant dir="map_filesystem" target="clean"/> </target> </project> diff --git a/java/demo/book/freeze_filesystem/.gitignore b/java/demo/book/evictor_filesystem/.gitignore index 9c39416c539..9c39416c539 100644 --- a/java/demo/book/freeze_filesystem/.gitignore +++ b/java/demo/book/evictor_filesystem/.gitignore diff --git a/java/demo/book/evictor_filesystem/Client.java b/java/demo/book/evictor_filesystem/Client.java new file mode 100644 index 00000000000..c24556388ce --- /dev/null +++ b/java/demo/book/evictor_filesystem/Client.java @@ -0,0 +1,47 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Filesystem.*; + +public class Client extends Ice.Application +{ + public int + run(String[] args) + { + // Terminate cleanly on receipt of a signal. + // + shutdownOnInterrupt(); + + // Create a proxy for the root directory + // + DirectoryPrx rootDir = DirectoryPrxHelper.checkedCast(communicator().propertyToProxy("RootDir.Proxy")); + if(rootDir == null) + { + throw new Error("Client: invalid proxy"); + } + + Parser p = new Parser(rootDir); + return p.parse(); + } + + static public void + main(String[] args) + { + Client app = new Client(); + app.main("demo.book.evictor_filesystem.Client", args, "config.client"); + } + + static private class Error extends RuntimeException + { + public Error(String msg) + { + super(msg); + } + } +} diff --git a/java/demo/book/freeze_filesystem/DirectoryI.java b/java/demo/book/evictor_filesystem/DirectoryI.java index 59e5e4c3ed6..c58f1c64013 100644 --- a/java/demo/book/freeze_filesystem/DirectoryI.java +++ b/java/demo/book/evictor_filesystem/DirectoryI.java @@ -42,7 +42,7 @@ public final class DirectoryI extends PersistentDirectory { if(parent == null) { - throw new PermissionDenied("cannot destroy root directory"); + throw new PermissionDenied("Cannot destroy root directory"); } synchronized(this) @@ -53,7 +53,7 @@ public final class DirectoryI extends PersistentDirectory } if(!nodes.isEmpty()) { - throw new PermissionDenied("cannot destroy non-empty directory"); + throw new PermissionDenied("Cannot destroy non-empty directory"); } _destroyed = true; } diff --git a/java/demo/book/freeze_filesystem/FileI.java b/java/demo/book/evictor_filesystem/FileI.java index 8b84ae13b34..8b84ae13b34 100644 --- a/java/demo/book/freeze_filesystem/FileI.java +++ b/java/demo/book/evictor_filesystem/FileI.java diff --git a/java/demo/book/freeze_filesystem/Filesystem.ice b/java/demo/book/evictor_filesystem/Filesystem.ice index 2e48dece6e3..2e48dece6e3 100644 --- a/java/demo/book/freeze_filesystem/Filesystem.ice +++ b/java/demo/book/evictor_filesystem/Filesystem.ice diff --git a/java/demo/book/evictor_filesystem/Grammar.java b/java/demo/book/evictor_filesystem/Grammar.java new file mode 100644 index 00000000000..e9e94969364 --- /dev/null +++ b/java/demo/book/evictor_filesystem/Grammar.java @@ -0,0 +1,202 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +class Grammar +{ + Grammar(Parser p) + { + _parser = p; + _scanner = new Scanner(_parser); + } + + void + parse() + { + while(true) + { + try + { + _token = _scanner.nextToken(); + if(_token == null) + { + return; + } + else if(_token.type == Token.TOK_SEMI) + { + // Continue + } + else if(_token.type == Token.TOK_HELP) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.usage(); + } + else if(_token.type == Token.TOK_EXIT) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + return; + } + else if(_token.type == Token.TOK_LIST) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.list(false); + } + else if(_token.type == Token.TOK_LIST_RECURSIVE) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.list(true); + } + else if(_token.type == Token.TOK_CREATE_FILE) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: mkfile FILE [FILE...]"); + } + _parser.createFile(s); + } + else if(_token.type == Token.TOK_CREATE_DIR) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: mkdir DIR [DIR...]"); + } + _parser.createDir(s); + } + else if(_token.type == Token.TOK_PWD) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.pwd(); + } + else if(_token.type == Token.TOK_CD) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() > 1) + { + throw new ParseError("usage: cd [DIR]"); + } + else if(s.size() == 0) + { + _parser.cd("/"); + } + else + { + _parser.cd((String)s.get(0)); + } + } + else if(_token.type == Token.TOK_CAT) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() != 1) + { + throw new ParseError("usage: cat FILE"); + } + _parser.cat((String)s.get(0)); + } + else if(_token.type == Token.TOK_WRITE) + { + java.util.LinkedList<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: write FILE [STRING...]"); + } + _parser.write(s); + } + else if(_token.type == Token.TOK_RM) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: rm NAME [NAME...]"); + } + _parser.destroy(s); + } + else + { + _parser.error("parse error"); + } + } + catch(ParseError e) + { + _parser.error("Parse error: " + e.getMessage()); + } + } + } + + private java.util.LinkedList<String> + strings() + { + java.util.LinkedList<String> l = new java.util.LinkedList<String>(); + while(true) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_STRING) + { + return l; + } + l.add(_token.value); + } + } + + static private class ParseError extends RuntimeException + { + ParseError(String msg) + { + super(msg); + } + } + + private Parser _parser; + private Scanner _scanner; + private Token _token; +} diff --git a/java/demo/book/freeze_filesystem/NodeFactory.java b/java/demo/book/evictor_filesystem/NodeFactory.java index 9dd3ab922ed..9dd3ab922ed 100644 --- a/java/demo/book/freeze_filesystem/NodeFactory.java +++ b/java/demo/book/evictor_filesystem/NodeFactory.java diff --git a/java/demo/book/freeze_filesystem/NodeInitializer.java b/java/demo/book/evictor_filesystem/NodeInitializer.java index e09f39c1db5..e09f39c1db5 100644 --- a/java/demo/book/freeze_filesystem/NodeInitializer.java +++ b/java/demo/book/evictor_filesystem/NodeInitializer.java diff --git a/java/demo/book/evictor_filesystem/Parser.java b/java/demo/book/evictor_filesystem/Parser.java new file mode 100644 index 00000000000..9a2889b7473 --- /dev/null +++ b/java/demo/book/evictor_filesystem/Parser.java @@ -0,0 +1,349 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Filesystem.*; + +class Parser +{ + Parser(DirectoryPrx root) + { + _dirs = new java.util.LinkedList<DirectoryPrx>(); + _dirs.addFirst(root); + } + + void + usage() + { + System.err.print( + "help Print this message.\n" + + "pwd Print current directory (/ = root).\n" + + "cd [DIR] Change directory (/ or empty = root).\n" + + "ls List current directory.\n" + + "lr Recursively list current directory.\n" + + "mkdir DIR [DIR...] Create directories DIR in current directory.\n" + + "mkfile FILE [FILE...] Create files FILE in current directory.\n" + + "rm NAME [NAME...] Delete directory or file NAME (rm * to delete all).\n" + + "cat FILE List the contents of FILE.\n" + + "write FILE [STRING...] Write STRING to FILE.\n" + + "exit, quit Exit this program.\n"); + } + + void + list(boolean recursive) + { + try + { + list(_dirs.get(0), recursive, 0); + } + catch(Ice.LocalException ex) + { + error(ex.toString()); + } + } + + void + list(Filesystem.DirectoryPrx dir, boolean recursive, int depth) + { + StringBuilder b = new StringBuilder(); + for(int i = 0; i < depth; ++i) + { + b.append('\t'); + } + String indent = b.toString(); + + NodeDesc[] contents = dir.list(); + + for(int i = 0; i < contents.length; ++i) + { + DirectoryPrx d + = contents[i].type == NodeType.DirType + ? DirectoryPrxHelper.uncheckedCast(contents[i].proxy) + : null; + System.out.print(indent + contents[i].name + (d != null ? " (directory)" : " (file)")); + if(d != null && recursive) + { + System.out.println(":"); + list(d, true, ++depth); + } + else + { + System.out.println(); + } + } + } + + void + createFile(java.util.List<String> names) + { + DirectoryPrx dir = _dirs.getFirst(); + + for(String name : names) + { + if(name.equals("..")) + { + System.out.println("Cannot create a file named `..'"); + continue; + } + + try + { + dir.createFile(name); + } + catch(NameInUse ex) + { + System.out.println("`" + name + "' exists already"); + } + } + } + + void + createDir(java.util.List<String> names) + { + DirectoryPrx dir = _dirs.getFirst(); + + for(String name : names) + { + if(name.equals("..")) + { + System.out.println("Cannot create a directory named `..'"); + continue; + } + + try + { + dir.createDirectory(name); + } + catch(NameInUse ex) + { + System.out.println("`" + name + "' exists already"); + } + } + } + + void + pwd() + { + if(_dirs.size() == 1) + { + System.out.print("/"); + } + else + { + java.util.ListIterator<DirectoryPrx> i = _dirs.listIterator(_dirs.size()); + i.previous(); + while(i.hasPrevious()) + { + System.out.print("/" + i.previous().name()); + } + } + System.out.println(); + } + + void + cd(String name) + { + if(name.equals("/")) + { + while(_dirs.size() > 1) + { + _dirs.removeFirst(); + } + return; + } + + if(name.equals("..")) + { + if(_dirs.size() > 1) + { + _dirs.removeFirst(); + } + return; + } + + DirectoryPrx dir = _dirs.getFirst(); + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such directory"); + return; + } + if(d.type == NodeType.FileType) + { + System.out.println("`" + name + "': not a directory"); + return; + } + _dirs.addFirst(DirectoryPrxHelper.uncheckedCast(d.proxy)); + } + + void + cat(String name) + { + DirectoryPrx dir = _dirs.getFirst(); + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such file"); + return; + } + if(d.type == NodeType.DirType) + { + System.out.println("`" + name + "': not a file"); + return; + } + FilePrx f = FilePrxHelper.uncheckedCast(d.proxy); + String[] l = f.read(); + for(int i = 0; i < l.length; ++i) + { + System.out.println(l[i]); + } + } + + void + write(java.util.LinkedList<String> args) + { + DirectoryPrx dir = _dirs.getFirst(); + String name = args.getFirst(); + args.removeFirst(); + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such file"); + return; + } + if(d.type == NodeType.DirType) + { + System.out.println("`" + name + "': not a file"); + return; + } + FilePrx f = FilePrxHelper.uncheckedCast(d.proxy); + + String[] l = args.toArray(new String[0]); + try + { + f.write(l); + } + catch(GenericError ex) + { + System.out.println("`" + name + "': cannot write to file: " + ex.reason); + } + } + + void + destroy(java.util.List<String> names) + { + DirectoryPrx dir = _dirs.getFirst(); + + for(String name : names) + { + if(name.equals("*")) + { + NodeDesc[] nodes = dir.list(); + for(NodeDesc node : nodes) + { + try + { + node.proxy.destroy(); + } + catch(PermissionDenied ex) + { + System.out.println("cannot remove `" + node.name + "': " + ex.reason); + } + } + return; + } + else + { + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such file or directory"); + return; + } + try + { + d.proxy.destroy(); + } + catch(PermissionDenied ex) + { + System.out.println("cannot remove `" + name + "': " + ex.reason); + } + } + } + } + + void + error(String s) + { + System.err.println("error: " + s); + } + + void + warning(String s) + { + System.err.println("warning: " + s); + } + + String + getInput() + { + System.out.print("> "); + System.out.flush(); + + try + { + return _in.readLine(); + } + catch(java.io.IOException e) + { + return null; + } + } + + int + parse() + { + _in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); + + Grammar g = new Grammar(this); + g.parse(); + + return 0; + } + + int + parse(java.io.BufferedReader in) + { + _in = in; + + Grammar g = new Grammar(this); + g.parse(); + + return 0; + } + + private java.util.LinkedList<DirectoryPrx> _dirs; + + private java.io.BufferedReader _in; +} diff --git a/java/demo/book/freeze_filesystem/PersistentFilesystem.ice b/java/demo/book/evictor_filesystem/PersistentFilesystem.ice index fe0216eb4d2..fe0216eb4d2 100644 --- a/java/demo/book/freeze_filesystem/PersistentFilesystem.ice +++ b/java/demo/book/evictor_filesystem/PersistentFilesystem.ice diff --git a/java/demo/book/evictor_filesystem/README b/java/demo/book/evictor_filesystem/README new file mode 100644 index 00000000000..36892b4cfed --- /dev/null +++ b/java/demo/book/evictor_filesystem/README @@ -0,0 +1,11 @@ +This demo presents an alternate implementation of the filesystem +application using a Freeze evictor, as discussed in the Freeze chapter +of the Ice manual. + +To run it, start the server in a window: + +$ java Server + +Then run the client in a separate window: + +$ java Client diff --git a/java/demo/book/evictor_filesystem/Scanner.java b/java/demo/book/evictor_filesystem/Scanner.java new file mode 100644 index 00000000000..c4c695fcd8b --- /dev/null +++ b/java/demo/book/evictor_filesystem/Scanner.java @@ -0,0 +1,283 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +class Scanner +{ + Scanner(Parser p) + { + _parser = p; + } + + Token + nextToken() + { + String s = next(); + if(s == null) + { + return null; + } + + if(s.equals(";")) + { + return new Token(Token.TOK_SEMI); + } + else if(s.equals("help")) + { + return new Token(Token.TOK_HELP); + } + else if(s.equals("exit") || s.equals("quit")) + { + return new Token(Token.TOK_EXIT); + } + else if(s.equals("ls")) + { + return new Token(Token.TOK_LIST); + } + else if(s.equals("lr")) + { + return new Token(Token.TOK_LIST_RECURSIVE); + } + else if(s.equals("mkfile")) + { + return new Token(Token.TOK_CREATE_FILE); + } + else if(s.equals("mkdir")) + { + return new Token(Token.TOK_CREATE_DIR); + } + else if(s.equals("pwd")) + { + return new Token(Token.TOK_PWD); + } + else if(s.equals("cd")) + { + return new Token(Token.TOK_CD); + } + else if(s.equals("cat")) + { + return new Token(Token.TOK_CAT); + } + else if(s.equals("write")) + { + return new Token(Token.TOK_WRITE); + } + else if(s.equals("rm")) + { + return new Token(Token.TOK_RM); + } + else + { + return new Token(Token.TOK_STRING, s); + } + } + + static private class EndOfInput extends Exception + { + } + + private char + get() + throws EndOfInput + { + // + // If there is an character in the unget buffer, return it. + // + if(_unget) + { + _unget = false; + return _ungetChar; + } + + // + // No current buffer? + // + if(_buf == null) + { + _buf = _parser.getInput(); + _pos = 0; + if(_buf == null) + { + throw new EndOfInput(); + } + } + + // + // At the end-of-buffer? + // + while(_pos >= _buf.length()) + { + _buf = null; + _pos = 0; + return '\n'; + } + + return _buf.charAt(_pos++); + } + + // + // unget only works with one character. + // + private void + unget(char c) + { + assert(!_unget); + _unget = true; + _ungetChar = c; + } + + private String + next() + { + // + // Eat any whitespace. + // + char c; + try + { + do + { + c = get(); + } + while(Character.isWhitespace(c) && c != '\n'); + } + catch(EndOfInput ignore) + { + return null; + } + + StringBuilder buf = new StringBuilder(128); + + if(c == ';' || c == '\n') + { + buf.append(';'); + } + else if(c == '\'') + { + try + { + while(true) + { + c = get(); + if(c == '\'') + { + break; + } + else + { + buf.append(c); + } + } + } + catch(EndOfInput e) + { + _parser.warning("EOF in string"); + } + } + else if(c == '\"') + { + try + { + while(true) + { + c = get(); + if(c == '\"') + { + break; + } + else if(c == '\\') + { + try + { + char next = get(); + switch(next) + { + case '\\': + case '"': + { + buf.append(next); + break; + } + + case 'n': + { + buf.append('\n'); + break; + } + + case 'r': + { + buf.append('\r'); + break; + } + + case 't': + { + buf.append('\t'); + break; + } + + case 'f': + { + buf.append('\f'); + break; + } + + default: + { + buf.append(c); + unget(next); + } + } + } + catch(EndOfInput e) + { + buf.append(c); + } + } + else + { + buf.append(c); + } + } + } + catch(EndOfInput e) + { + _parser.warning("EOF in string"); + } + } + else + { + // + // Otherwise it's a string. + // + try + { + do + { + buf.append(c); + c = get(); + } + while(!Character.isWhitespace(c) && c != ';' && c != '\n'); + + unget(c); + } + catch(EndOfInput ignore) + { + } + } + + return buf.toString(); + } + + private Parser _parser; + private boolean _unget = false; + private char _ungetChar; + private String _buf = null; + private int _pos; +} diff --git a/java/demo/book/freeze_filesystem/Server.java b/java/demo/book/evictor_filesystem/Server.java index d774785a1c3..dfdf5e109c3 100644 --- a/java/demo/book/freeze_filesystem/Server.java +++ b/java/demo/book/evictor_filesystem/Server.java @@ -31,7 +31,7 @@ public class Server extends Ice.Application // Create an object adapter. // Ice.ObjectAdapter adapter = - communicator().createObjectAdapterWithEndpoints("FreezeFilesystem", "default -p 10000"); + communicator().createObjectAdapter("EvictorFilesystem"); // // Create the Freeze evictor (stored in the _evictor @@ -74,7 +74,7 @@ public class Server extends Ice.Application main(String[] args) { Server app = new Server("db"); - int status = app.main("Server", args, "config.server"); + int status = app.main("demo.book.evictor_filesystem.Server", args, "config.server"); System.exit(status); } diff --git a/java/demo/book/evictor_filesystem/Token.java b/java/demo/book/evictor_filesystem/Token.java new file mode 100644 index 00000000000..e9b76961ea7 --- /dev/null +++ b/java/demo/book/evictor_filesystem/Token.java @@ -0,0 +1,40 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +class Token +{ + public static final int TOK_HELP = 0; + public static final int TOK_EXIT = 1; + public static final int TOK_SEMI = 2; + public static final int TOK_LIST = 3; + public static final int TOK_LIST_RECURSIVE = 4; + public static final int TOK_CREATE_FILE = 5; + public static final int TOK_CREATE_DIR = 6; + public static final int TOK_PWD = 7; + public static final int TOK_CD = 8; + public static final int TOK_CAT = 9; + public static final int TOK_WRITE = 10; + public static final int TOK_RM = 11; + public static final int TOK_STRING = 12; + + int type; + String value; + + Token(int t) + { + type = t; + value = null; + } + + Token(int t, String v) + { + type = t; + value = v; + } +} diff --git a/java/demo/book/freeze_filesystem/build.xml b/java/demo/book/evictor_filesystem/build.xml index 8010bd22527..cfe9cd29c5f 100644 --- a/java/demo/book/freeze_filesystem/build.xml +++ b/java/demo/book/evictor_filesystem/build.xml @@ -9,7 +9,7 @@ ********************************************************************** --> -<project name="demo_book_freeze_filesystem" default="all" basedir="."> +<project name="demo_book_evictor_filesystem" default="all" basedir="."> <!-- set global properties for this build --> <property name="top.dir" value="../../.."/> diff --git a/java/demo/book/freeze_filesystem/config.client b/java/demo/book/evictor_filesystem/config.client index 6f0ba50b98f..6f0ba50b98f 100644 --- a/java/demo/book/freeze_filesystem/config.client +++ b/java/demo/book/evictor_filesystem/config.client diff --git a/java/demo/book/evictor_filesystem/config.server b/java/demo/book/evictor_filesystem/config.server new file mode 100644 index 00000000000..20049e1abd9 --- /dev/null +++ b/java/demo/book/evictor_filesystem/config.server @@ -0,0 +1,49 @@ +# +# Configure the server endpoints. +# +EvictorFilesystem.Endpoints=default -h 127.0.0.1 -p 10000 + +# +# Freeze Map Tracing. +# +# 0 = No map activity trace (default). +# 1 = Trace database open and close. +# 2 = Also trace iterator and transaction operations, and reference +# counting of the underlying database. +# +Freeze.Trace.Map=1 + +# +# Freeze Evictor Tracing. +# +# 0 = No evictor activity trace (default). +# 1 = Trace Ice object and facet creation and destruction, facet +# streaming time, facet saving time, object eviction (every 50 +# objects) and evictor deactivation. +# 2 = Also trace object lookups, and all object evictions. +# 3 = Also trace object retrieval from the database. +# +Freeze.Trace.Evictor=2 + +# +# Warn about connection exceptions +# +Ice.Warn.Connections=1 + +# +# Network Tracing +# +# 0 = no network tracing +# 1 = trace connection establishment and closure +# 2 = like 1, but more detailed +# 3 = like 2, but also trace data transfer +# +#Ice.Trace.Network=1 + +# +# Protocol Tracing +# +# 0 = no protocol tracing +# 1 = trace protocol messages +# +#Ice.Trace.Protocol=1 diff --git a/java/demo/book/freeze_filesystem/db/.gitignore b/java/demo/book/evictor_filesystem/db/.gitignore index 39af5887579..39af5887579 100644 --- a/java/demo/book/freeze_filesystem/db/.gitignore +++ b/java/demo/book/evictor_filesystem/db/.gitignore diff --git a/java/demo/book/evictor_filesystem/expect.py b/java/demo/book/evictor_filesystem/expect.py new file mode 100755 index 00000000000..91b19af32e6 --- /dev/null +++ b/java/demo/book/evictor_filesystem/expect.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# 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. +# +# ********************************************************************** + +import sys, os + +path = [ ".", "..", "../..", "../../..", "../../../.." ] +head = os.path.dirname(sys.argv[0]) +if len(head) > 0: + path = [os.path.join(head, p) for p in path] +path = [os.path.abspath(p) for p in path if os.path.exists(os.path.join(p, "demoscript")) ] +if len(path) == 0: + raise "can't find toplevel directory!" +sys.path.append(path[0]) + +from demoscript import * +from demoscript.book import evictor_filesystem + +print "cleaning databases...", +sys.stdout.flush() +Util.cleanDbDir("db") +print "ok" + +server = Util.spawn('java Server --Ice.PrintAdapterReady') +server.expect('.* ready') +client = Util.spawn('java Client') + +evictor_filesystem.run(client, server) diff --git a/java/demo/book/freeze_filesystem/Client.java b/java/demo/book/freeze_filesystem/Client.java deleted file mode 100644 index 2c0056e1451..00000000000 --- a/java/demo/book/freeze_filesystem/Client.java +++ /dev/null @@ -1,165 +0,0 @@ -// ********************************************************************** -// -// 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. -// -// ********************************************************************** - -import Filesystem.*; - -public class Client extends Ice.Application -{ - // 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(DirectoryPrx dir, int depth) - { - char[] indentCh = new char[++depth]; - java.util.Arrays.fill(indentCh, '\t'); - String indent = new String(indentCh); - - NodeDesc[] contents = dir.list(); - - for (int i = 0; i < contents.length; ++i) { - DirectoryPrx subdir = DirectoryPrxHelper.checkedCast(contents[i].proxy); - FilePrx file = FilePrxHelper.uncheckedCast(contents[i].proxy); - System.out.println(indent + contents[i].name + (subdir != null ? " (directory):" : " (file):")); - if (subdir != null) { - listRecursive(subdir, depth); - } else { - String[] text = file.read(); - for (int j = 0; j < text.length; ++j) - System.out.println(indent + "\t" + text[j]); - } - } - } - - class ShutdownHook extends Thread - { - public void - run() - { - try - { - communicator().destroy(); - } - catch(Ice.LocalException ex) - { - ex.printStackTrace(); - } - } - } - - public int - run(String[] args) - { - // - // Since this is an interactive demo we want to clear the - // Application installed interrupt callback and install our - // own shutdown hook. - // - setInterruptHook(new ShutdownHook()); - - // - // Create a proxy for the root directory. - // - DirectoryPrx rootDir = DirectoryPrxHelper.checkedCast(communicator().propertyToProxy("RootDir.Proxy")); - if(rootDir == null) - { - System.err.println("Client: invalid proxy"); - return 1; - } - - try - { - // - // Create a file called "README" in the root directory. - // - FilePrx readme = null; - try - { - readme = rootDir.createFile("README"); - String[] text = new String[1]; - text[0] = "This file system contains a collection of poetry."; - readme.write(text); - System.out.println("Created README."); - } - catch(NameInUse ex) - { - NodeDesc desc = rootDir.find("README"); - readme = FilePrxHelper.checkedCast(desc.proxy); - assert(readme != null); - } - - // - // Create a directory called "Coleridge" in the root directory. - // - DirectoryPrx coleridge = null; - try - { - coleridge = rootDir.createDirectory("Coleridge"); - System.out.println("Created Coleridge."); - } - catch(NameInUse ex) - { - NodeDesc desc = rootDir.find("Coleridge"); - coleridge = DirectoryPrxHelper.checkedCast(desc.proxy); - assert(coleridge != null); - } - - // - // Create a file called "Kubla_Khan" in the Coleridge directory. - // - FilePrx file = null; - try - { - file = coleridge.createFile("Kubla_Khan"); - String[] text = new String[5]; - text[0] = "In Xanadu did Kubla Khan"; - text[1] = "A stately pleasure-dome decree:"; - text[2] = "Where Alph, the sacred river, ran"; - text[3] = "Through caverns measureless to man"; - text[4] = "Down to a sunless sea."; - file.write(text); - System.out.println("Created Coleridge/Kubla_Khan."); - } - catch(NameInUse ex) - { - NodeDesc desc = coleridge.find("Kubla_Khan"); - file = FilePrxHelper.checkedCast(desc.proxy); - assert(file != null); - } - - System.out.println("Contents of filesystem:"); - listRecursive(rootDir, 0); - - // - // Destroy the filesystem. - // - System.out.println("Destroying " + file.name()); - file.destroy(); - System.out.println("Destroying " + readme.name()); - readme.destroy(); - System.out.println("Destroying " + coleridge.name()); - coleridge.destroy(); - } - catch(Ice.UserException ex) - { - ex.printStackTrace(); - } - - return 0; - } - - public static void - main(String[] args) - { - Client app = new Client(); - int status = app.main("Client", args, "config.client"); - System.exit(status); - } -} diff --git a/java/demo/book/lifecycle/Server.java b/java/demo/book/lifecycle/Server.java index 6e3286ebafb..54bc2d2526f 100644 --- a/java/demo/book/lifecycle/Server.java +++ b/java/demo/book/lifecycle/Server.java @@ -22,7 +22,7 @@ class Server extends Ice.Application // Create an object adapter // Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpoints( - "LifecycleFilesystem", "default -p 10000"); + "LifecycleFilesystem", "default -h 127.0.0.1 -p 10000"); // Create the root directory. // diff --git a/java/demo/book/map_filesystem/.gitignore b/java/demo/book/map_filesystem/.gitignore new file mode 100644 index 00000000000..9c39416c539 --- /dev/null +++ b/java/demo/book/map_filesystem/.gitignore @@ -0,0 +1 @@ +db/* diff --git a/java/demo/book/map_filesystem/Client.java b/java/demo/book/map_filesystem/Client.java new file mode 100644 index 00000000000..33542ecf941 --- /dev/null +++ b/java/demo/book/map_filesystem/Client.java @@ -0,0 +1,47 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Filesystem.*; + +public class Client extends Ice.Application +{ + public int + run(String[] args) + { + // Terminate cleanly on receipt of a signal. + // + shutdownOnInterrupt(); + + // Create a proxy for the root directory + // + DirectoryPrx rootDir = DirectoryPrxHelper.checkedCast(communicator().propertyToProxy("RootDir.Proxy")); + if(rootDir == null) + { + throw new Error("Client: invalid proxy"); + } + + Parser p = new Parser(rootDir); + return p.parse(); + } + + static public void + main(String[] args) + { + Client app = new Client(); + app.main("demo.book.map_filesystem.Client", args, "config.client"); + } + + static private class Error extends RuntimeException + { + public Error(String msg) + { + super(msg); + } + } +} diff --git a/java/demo/book/map_filesystem/DirectoryI.java b/java/demo/book/map_filesystem/DirectoryI.java new file mode 100644 index 00000000000..7b5a164ccf5 --- /dev/null +++ b/java/demo/book/map_filesystem/DirectoryI.java @@ -0,0 +1,201 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Filesystem.*; + +public class DirectoryI extends _DirectoryDisp +{ + public synchronized void + destroy(Ice.Current c) + throws PermissionDenied + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(c.id, c.facet, c.operation); + } + if(_parent == null) + { + throw new PermissionDenied("Cannot destroy root directory"); + } + if(!_dir.nodes.isEmpty()) + { + throw new PermissionDenied("Cannot destroy non-empty directory"); + } + _destroyed = true; + _parent.removeEntry(_dir.name); + _map.remove(c.id); + c.adapter.remove(c.id); + } + + public synchronized String + name(Ice.Current c) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(); + } + return _dir.name; + } + + public synchronized NodeDesc[] + list(Ice.Current c) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(); + } + NodeDesc[] result = new NodeDesc[_dir.nodes.size()]; + java.util.Iterator<NodeDesc> p = _dir.nodes.values().iterator(); + for(int i = 0; i < _dir.nodes.size(); ++i) + { + result[i] = p.next(); + } + return result; + } + + public synchronized NodeDesc + find(String name, Ice.Current c) + throws NoSuchName + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(); + } + NodeDesc nd = _dir.nodes.get(name); + if(nd == null) + { + throw new NoSuchName(name); + } + return nd; + } + + public synchronized FilePrx + createFile(String name, Ice.Current c) + throws NameInUse + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(); + } + + if(name.isEmpty() || _dir.nodes.get(name) != null) + { + throw new NameInUse(name); + } + + PersistentFile persistentFile = new PersistentFile(); + persistentFile.name = name; + Ice.Identity id = _communicator.stringToIdentity(java.util.UUID.randomUUID().toString()); + FileI file = new FileI(persistentFile, this); + _map.put(id, persistentFile); + + FilePrx proxy = FilePrxHelper.uncheckedCast(c.adapter.createProxy(id)); + + NodeDesc nd = new NodeDesc(); + nd.name = name; + nd.type = NodeType.FileType; + nd.proxy = proxy; + _dir.nodes.put(name, nd); + + _map.put(c.id, _dir); + + _adapter.add(file, id); + + return proxy; + } + + public synchronized DirectoryPrx + createDirectory(String name, Ice.Current c) + throws NameInUse + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(); + } + + if(name.isEmpty() || _dir.nodes.get(name) != null) + { + throw new NameInUse(name); + } + + PersistentDirectory persistentDir = new PersistentDirectory(); + persistentDir.name = name; + persistentDir.nodes = new java.util.HashMap<String, NodeDesc>(); + Ice.Identity id = _communicator.stringToIdentity(java.util.UUID.randomUUID().toString()); + DirectoryI dir = new DirectoryI(id, persistentDir, this); + _map.put(id, persistentDir); + + DirectoryPrx proxy = DirectoryPrxHelper.uncheckedCast(c.adapter.createProxy(id)); + + NodeDesc nd = new NodeDesc(); + nd.name = name; + nd.type = NodeType.DirType; + nd.proxy = proxy; + _dir.nodes.put(name, nd); + + _map.put(c.id, _dir); + + _adapter.add(dir, id); + + return proxy; + } + + // + // Called by the child to remove itself from the parent's node map when the child is destroyed. + // + public synchronized void + removeEntry(String name) + { + _dir.nodes.remove(name); + _map.put(_id, _dir); + } + + public + DirectoryI(Ice.Identity pid, PersistentDirectory dir, DirectoryI parent) + { + _id = pid; + _dir = dir; + _parent = parent; + _destroyed = false; + + // Instantiate the child nodes + // + java.util.Iterator<NodeDesc> p = _dir.nodes.values().iterator(); + while(p.hasNext()) + { + NodeDesc desc = p.next(); + Ice.Identity id = desc.proxy.ice_getIdentity(); + PersistentNode node = _map.get(id); + assert(node != null); + if(desc.type == NodeType.DirType) + { + PersistentDirectory pDir = (PersistentDirectory)node; + assert(pDir != null); + DirectoryI d = new DirectoryI(id, pDir, this); + _adapter.add(d, id); + } + else + { + PersistentFile pFile = (PersistentFile)node; + assert(pFile != null); + FileI f = new FileI(pFile, this); + _adapter.add(f, id); + } + } + } + + public static Ice.Communicator _communicator; + public static Ice.ObjectAdapter _adapter; + public static IdentityNodeMap _map; + + private Ice.Identity _id; + private PersistentDirectory _dir; + private DirectoryI _parent; + private boolean _destroyed; +} diff --git a/java/demo/book/map_filesystem/FileI.java b/java/demo/book/map_filesystem/FileI.java new file mode 100644 index 00000000000..3099da800cb --- /dev/null +++ b/java/demo/book/map_filesystem/FileI.java @@ -0,0 +1,73 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Filesystem.*; + +public class FileI extends _FileDisp +{ + public synchronized String + name(Ice.Current c) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(); + } + return _file.name; + } + + public synchronized void + destroy(Ice.Current c) + throws PermissionDenied + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(c.id, c.facet, c.operation); + } + _destroyed = true; + _parent.removeEntry(_file.name); + _map.remove(c.id); + c.adapter.remove(c.id); + } + + public synchronized String[] + read(Ice.Current c) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(c.id, c.facet, c.operation); + } + return _file.text; + } + + public synchronized void + write(String[] text, Ice.Current c) + throws GenericError + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(c.id, c.facet, c.operation); + } + _file.text = text; + _map.put(c.id, _file); + } + + public + FileI(PersistentFile file, DirectoryI parent) + { + _file = file; + _parent = parent; + _destroyed = false; + } + + public static IdentityNodeMap _map; + + private PersistentFile _file; + private DirectoryI _parent; + private boolean _destroyed; +} diff --git a/java/demo/book/map_filesystem/Filesystem.ice b/java/demo/book/map_filesystem/Filesystem.ice new file mode 100644 index 00000000000..746148fba73 --- /dev/null +++ b/java/demo/book/map_filesystem/Filesystem.ice @@ -0,0 +1,52 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +module Filesystem +{ + exception GenericError + { + string reason; + }; + exception PermissionDenied extends GenericError {}; + exception NameInUse extends GenericError {}; + exception NoSuchName extends GenericError {}; + + interface Node + { + idempotent string name(); + void destroy() throws PermissionDenied; + }; + + sequence<string> Lines; + + interface File extends Node + { + idempotent Lines read(); + idempotent void write(Lines text) throws GenericError; + }; + + enum NodeType { DirType, FileType }; + + struct NodeDesc + { + string name; + NodeType type; + Node* proxy; + }; + + sequence<NodeDesc> NodeDescSeq; + + interface Directory extends Node + { + idempotent NodeDescSeq list(); + idempotent NodeDesc find(string name) throws NoSuchName; + File* createFile(string name) throws NameInUse; + Directory* createDirectory(string name) throws NameInUse; + }; +}; diff --git a/java/demo/book/map_filesystem/Grammar.java b/java/demo/book/map_filesystem/Grammar.java new file mode 100644 index 00000000000..e9e94969364 --- /dev/null +++ b/java/demo/book/map_filesystem/Grammar.java @@ -0,0 +1,202 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +class Grammar +{ + Grammar(Parser p) + { + _parser = p; + _scanner = new Scanner(_parser); + } + + void + parse() + { + while(true) + { + try + { + _token = _scanner.nextToken(); + if(_token == null) + { + return; + } + else if(_token.type == Token.TOK_SEMI) + { + // Continue + } + else if(_token.type == Token.TOK_HELP) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.usage(); + } + else if(_token.type == Token.TOK_EXIT) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + return; + } + else if(_token.type == Token.TOK_LIST) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.list(false); + } + else if(_token.type == Token.TOK_LIST_RECURSIVE) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.list(true); + } + else if(_token.type == Token.TOK_CREATE_FILE) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: mkfile FILE [FILE...]"); + } + _parser.createFile(s); + } + else if(_token.type == Token.TOK_CREATE_DIR) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: mkdir DIR [DIR...]"); + } + _parser.createDir(s); + } + else if(_token.type == Token.TOK_PWD) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + _parser.pwd(); + } + else if(_token.type == Token.TOK_CD) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() > 1) + { + throw new ParseError("usage: cd [DIR]"); + } + else if(s.size() == 0) + { + _parser.cd("/"); + } + else + { + _parser.cd((String)s.get(0)); + } + } + else if(_token.type == Token.TOK_CAT) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() != 1) + { + throw new ParseError("usage: cat FILE"); + } + _parser.cat((String)s.get(0)); + } + else if(_token.type == Token.TOK_WRITE) + { + java.util.LinkedList<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: write FILE [STRING...]"); + } + _parser.write(s); + } + else if(_token.type == Token.TOK_RM) + { + java.util.List<String> s = strings(); + if(_token.type != Token.TOK_SEMI) + { + throw new ParseError("Expected ';'"); + } + if(s.size() == 0) + { + throw new ParseError("usage: rm NAME [NAME...]"); + } + _parser.destroy(s); + } + else + { + _parser.error("parse error"); + } + } + catch(ParseError e) + { + _parser.error("Parse error: " + e.getMessage()); + } + } + } + + private java.util.LinkedList<String> + strings() + { + java.util.LinkedList<String> l = new java.util.LinkedList<String>(); + while(true) + { + _token = _scanner.nextToken(); + if(_token.type != Token.TOK_STRING) + { + return l; + } + l.add(_token.value); + } + } + + static private class ParseError extends RuntimeException + { + ParseError(String msg) + { + super(msg); + } + } + + private Parser _parser; + private Scanner _scanner; + private Token _token; +} diff --git a/java/demo/book/map_filesystem/Parser.java b/java/demo/book/map_filesystem/Parser.java new file mode 100644 index 00000000000..9a2889b7473 --- /dev/null +++ b/java/demo/book/map_filesystem/Parser.java @@ -0,0 +1,349 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Filesystem.*; + +class Parser +{ + Parser(DirectoryPrx root) + { + _dirs = new java.util.LinkedList<DirectoryPrx>(); + _dirs.addFirst(root); + } + + void + usage() + { + System.err.print( + "help Print this message.\n" + + "pwd Print current directory (/ = root).\n" + + "cd [DIR] Change directory (/ or empty = root).\n" + + "ls List current directory.\n" + + "lr Recursively list current directory.\n" + + "mkdir DIR [DIR...] Create directories DIR in current directory.\n" + + "mkfile FILE [FILE...] Create files FILE in current directory.\n" + + "rm NAME [NAME...] Delete directory or file NAME (rm * to delete all).\n" + + "cat FILE List the contents of FILE.\n" + + "write FILE [STRING...] Write STRING to FILE.\n" + + "exit, quit Exit this program.\n"); + } + + void + list(boolean recursive) + { + try + { + list(_dirs.get(0), recursive, 0); + } + catch(Ice.LocalException ex) + { + error(ex.toString()); + } + } + + void + list(Filesystem.DirectoryPrx dir, boolean recursive, int depth) + { + StringBuilder b = new StringBuilder(); + for(int i = 0; i < depth; ++i) + { + b.append('\t'); + } + String indent = b.toString(); + + NodeDesc[] contents = dir.list(); + + for(int i = 0; i < contents.length; ++i) + { + DirectoryPrx d + = contents[i].type == NodeType.DirType + ? DirectoryPrxHelper.uncheckedCast(contents[i].proxy) + : null; + System.out.print(indent + contents[i].name + (d != null ? " (directory)" : " (file)")); + if(d != null && recursive) + { + System.out.println(":"); + list(d, true, ++depth); + } + else + { + System.out.println(); + } + } + } + + void + createFile(java.util.List<String> names) + { + DirectoryPrx dir = _dirs.getFirst(); + + for(String name : names) + { + if(name.equals("..")) + { + System.out.println("Cannot create a file named `..'"); + continue; + } + + try + { + dir.createFile(name); + } + catch(NameInUse ex) + { + System.out.println("`" + name + "' exists already"); + } + } + } + + void + createDir(java.util.List<String> names) + { + DirectoryPrx dir = _dirs.getFirst(); + + for(String name : names) + { + if(name.equals("..")) + { + System.out.println("Cannot create a directory named `..'"); + continue; + } + + try + { + dir.createDirectory(name); + } + catch(NameInUse ex) + { + System.out.println("`" + name + "' exists already"); + } + } + } + + void + pwd() + { + if(_dirs.size() == 1) + { + System.out.print("/"); + } + else + { + java.util.ListIterator<DirectoryPrx> i = _dirs.listIterator(_dirs.size()); + i.previous(); + while(i.hasPrevious()) + { + System.out.print("/" + i.previous().name()); + } + } + System.out.println(); + } + + void + cd(String name) + { + if(name.equals("/")) + { + while(_dirs.size() > 1) + { + _dirs.removeFirst(); + } + return; + } + + if(name.equals("..")) + { + if(_dirs.size() > 1) + { + _dirs.removeFirst(); + } + return; + } + + DirectoryPrx dir = _dirs.getFirst(); + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such directory"); + return; + } + if(d.type == NodeType.FileType) + { + System.out.println("`" + name + "': not a directory"); + return; + } + _dirs.addFirst(DirectoryPrxHelper.uncheckedCast(d.proxy)); + } + + void + cat(String name) + { + DirectoryPrx dir = _dirs.getFirst(); + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such file"); + return; + } + if(d.type == NodeType.DirType) + { + System.out.println("`" + name + "': not a file"); + return; + } + FilePrx f = FilePrxHelper.uncheckedCast(d.proxy); + String[] l = f.read(); + for(int i = 0; i < l.length; ++i) + { + System.out.println(l[i]); + } + } + + void + write(java.util.LinkedList<String> args) + { + DirectoryPrx dir = _dirs.getFirst(); + String name = args.getFirst(); + args.removeFirst(); + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such file"); + return; + } + if(d.type == NodeType.DirType) + { + System.out.println("`" + name + "': not a file"); + return; + } + FilePrx f = FilePrxHelper.uncheckedCast(d.proxy); + + String[] l = args.toArray(new String[0]); + try + { + f.write(l); + } + catch(GenericError ex) + { + System.out.println("`" + name + "': cannot write to file: " + ex.reason); + } + } + + void + destroy(java.util.List<String> names) + { + DirectoryPrx dir = _dirs.getFirst(); + + for(String name : names) + { + if(name.equals("*")) + { + NodeDesc[] nodes = dir.list(); + for(NodeDesc node : nodes) + { + try + { + node.proxy.destroy(); + } + catch(PermissionDenied ex) + { + System.out.println("cannot remove `" + node.name + "': " + ex.reason); + } + } + return; + } + else + { + NodeDesc d; + try + { + d = dir.find(name); + } + catch(NoSuchName ex) + { + System.out.println("`" + name + "': no such file or directory"); + return; + } + try + { + d.proxy.destroy(); + } + catch(PermissionDenied ex) + { + System.out.println("cannot remove `" + name + "': " + ex.reason); + } + } + } + } + + void + error(String s) + { + System.err.println("error: " + s); + } + + void + warning(String s) + { + System.err.println("warning: " + s); + } + + String + getInput() + { + System.out.print("> "); + System.out.flush(); + + try + { + return _in.readLine(); + } + catch(java.io.IOException e) + { + return null; + } + } + + int + parse() + { + _in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); + + Grammar g = new Grammar(this); + g.parse(); + + return 0; + } + + int + parse(java.io.BufferedReader in) + { + _in = in; + + Grammar g = new Grammar(this); + g.parse(); + + return 0; + } + + private java.util.LinkedList<DirectoryPrx> _dirs; + + private java.io.BufferedReader _in; +} diff --git a/java/demo/book/map_filesystem/PersistentFilesystem.ice b/java/demo/book/map_filesystem/PersistentFilesystem.ice new file mode 100644 index 00000000000..356c0e10454 --- /dev/null +++ b/java/demo/book/map_filesystem/PersistentFilesystem.ice @@ -0,0 +1,35 @@ +// ********************************************************************** +// +// 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 <Filesystem.ice> + +module Filesystem +{ + class PersistentDirectory; + + class PersistentNode + { + string name; + PersistentDirectory* parent; + }; + + ["cpp:virtual"] + class PersistentFile extends PersistentNode + { + Lines text; + }; + + dictionary<string, NodeDesc> NodeDict; + + ["cpp:virtual"] + class PersistentDirectory extends PersistentNode + { + NodeDict nodes; + }; +}; diff --git a/java/demo/book/freeze_filesystem/README b/java/demo/book/map_filesystem/README index 32f3381a068..4cf93946329 100644 --- a/java/demo/book/freeze_filesystem/README +++ b/java/demo/book/map_filesystem/README @@ -1,5 +1,6 @@ This demo presents an alternate implementation of the filesystem -application, as discussed in the Freeze chapter of the Ice manual. +application using a Freeze map, as discussed in the Freeze chapter of +the Ice manual. To run it, start the server in a window: diff --git a/java/demo/book/map_filesystem/Scanner.java b/java/demo/book/map_filesystem/Scanner.java new file mode 100644 index 00000000000..c4c695fcd8b --- /dev/null +++ b/java/demo/book/map_filesystem/Scanner.java @@ -0,0 +1,283 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +class Scanner +{ + Scanner(Parser p) + { + _parser = p; + } + + Token + nextToken() + { + String s = next(); + if(s == null) + { + return null; + } + + if(s.equals(";")) + { + return new Token(Token.TOK_SEMI); + } + else if(s.equals("help")) + { + return new Token(Token.TOK_HELP); + } + else if(s.equals("exit") || s.equals("quit")) + { + return new Token(Token.TOK_EXIT); + } + else if(s.equals("ls")) + { + return new Token(Token.TOK_LIST); + } + else if(s.equals("lr")) + { + return new Token(Token.TOK_LIST_RECURSIVE); + } + else if(s.equals("mkfile")) + { + return new Token(Token.TOK_CREATE_FILE); + } + else if(s.equals("mkdir")) + { + return new Token(Token.TOK_CREATE_DIR); + } + else if(s.equals("pwd")) + { + return new Token(Token.TOK_PWD); + } + else if(s.equals("cd")) + { + return new Token(Token.TOK_CD); + } + else if(s.equals("cat")) + { + return new Token(Token.TOK_CAT); + } + else if(s.equals("write")) + { + return new Token(Token.TOK_WRITE); + } + else if(s.equals("rm")) + { + return new Token(Token.TOK_RM); + } + else + { + return new Token(Token.TOK_STRING, s); + } + } + + static private class EndOfInput extends Exception + { + } + + private char + get() + throws EndOfInput + { + // + // If there is an character in the unget buffer, return it. + // + if(_unget) + { + _unget = false; + return _ungetChar; + } + + // + // No current buffer? + // + if(_buf == null) + { + _buf = _parser.getInput(); + _pos = 0; + if(_buf == null) + { + throw new EndOfInput(); + } + } + + // + // At the end-of-buffer? + // + while(_pos >= _buf.length()) + { + _buf = null; + _pos = 0; + return '\n'; + } + + return _buf.charAt(_pos++); + } + + // + // unget only works with one character. + // + private void + unget(char c) + { + assert(!_unget); + _unget = true; + _ungetChar = c; + } + + private String + next() + { + // + // Eat any whitespace. + // + char c; + try + { + do + { + c = get(); + } + while(Character.isWhitespace(c) && c != '\n'); + } + catch(EndOfInput ignore) + { + return null; + } + + StringBuilder buf = new StringBuilder(128); + + if(c == ';' || c == '\n') + { + buf.append(';'); + } + else if(c == '\'') + { + try + { + while(true) + { + c = get(); + if(c == '\'') + { + break; + } + else + { + buf.append(c); + } + } + } + catch(EndOfInput e) + { + _parser.warning("EOF in string"); + } + } + else if(c == '\"') + { + try + { + while(true) + { + c = get(); + if(c == '\"') + { + break; + } + else if(c == '\\') + { + try + { + char next = get(); + switch(next) + { + case '\\': + case '"': + { + buf.append(next); + break; + } + + case 'n': + { + buf.append('\n'); + break; + } + + case 'r': + { + buf.append('\r'); + break; + } + + case 't': + { + buf.append('\t'); + break; + } + + case 'f': + { + buf.append('\f'); + break; + } + + default: + { + buf.append(c); + unget(next); + } + } + } + catch(EndOfInput e) + { + buf.append(c); + } + } + else + { + buf.append(c); + } + } + } + catch(EndOfInput e) + { + _parser.warning("EOF in string"); + } + } + else + { + // + // Otherwise it's a string. + // + try + { + do + { + buf.append(c); + c = get(); + } + while(!Character.isWhitespace(c) && c != ';' && c != '\n'); + + unget(c); + } + catch(EndOfInput ignore) + { + } + } + + return buf.toString(); + } + + private Parser _parser; + private boolean _unget = false; + private char _ungetChar; + private String _buf = null; + private int _pos; +} diff --git a/java/demo/book/map_filesystem/Server.java b/java/demo/book/map_filesystem/Server.java new file mode 100644 index 00000000000..4bc2a397c42 --- /dev/null +++ b/java/demo/book/map_filesystem/Server.java @@ -0,0 +1,86 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Filesystem.*; + +public class Server extends Ice.Application +{ + public + Server(String envName) + { + _envName = envName; + } + + public int + run(String[] args) + { + // Install object factories + // + communicator().addObjectFactory(PersistentFile.ice_factory(), PersistentFile.ice_staticId()); + communicator().addObjectFactory(PersistentDirectory.ice_factory(), PersistentDirectory.ice_staticId()); + + // Create an object adapter (stored in the _adapter static member) + // + Ice.ObjectAdapter adapter = + communicator().createObjectAdapter("MapFilesystem"); + + // Create a Freeze connection and the map + // + Freeze.Connection connection = Freeze.Util.createConnection(communicator(), _envName); + IdentityNodeMap persistentMap = new IdentityNodeMap(connection, "mapfs", true); + + // Set static members + // + FileI._map = persistentMap; + DirectoryI._map = persistentMap; + DirectoryI._communicator = communicator(); + DirectoryI._adapter = adapter; + + // Find the persistent node for the root directory, or create it if not found + // + Ice.Identity rootId = Ice.Util.stringToIdentity("RootDir"); + PersistentDirectory pRoot = (PersistentDirectory)persistentMap.get(rootId); + if(pRoot == null) + { + pRoot = new PersistentDirectory(); + pRoot.name = "/"; + pRoot.nodes = new java.util.HashMap<String, NodeDesc>(); + persistentMap.put(rootId, pRoot); + } + + // Create the root directory (with name "/" and no parent) + // + DirectoryI root = new DirectoryI(rootId, pRoot, null); + adapter.add(root, rootId); + + // Ready to accept requests now + // + adapter.activate(); + + // Wait until we are done + // + communicator().waitForShutdown(); + + // Clean up + // + connection.close(); + + return 0; + } + + public static void + main(String[] args) + { + Server app = new Server("db"); + app.main("demo.book.map_filesystem.Server", args, "config.server"); + System.exit(0); + } + + private String _envName; +} diff --git a/java/demo/book/map_filesystem/Token.java b/java/demo/book/map_filesystem/Token.java new file mode 100644 index 00000000000..e9b76961ea7 --- /dev/null +++ b/java/demo/book/map_filesystem/Token.java @@ -0,0 +1,40 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +class Token +{ + public static final int TOK_HELP = 0; + public static final int TOK_EXIT = 1; + public static final int TOK_SEMI = 2; + public static final int TOK_LIST = 3; + public static final int TOK_LIST_RECURSIVE = 4; + public static final int TOK_CREATE_FILE = 5; + public static final int TOK_CREATE_DIR = 6; + public static final int TOK_PWD = 7; + public static final int TOK_CD = 8; + public static final int TOK_CAT = 9; + public static final int TOK_WRITE = 10; + public static final int TOK_RM = 11; + public static final int TOK_STRING = 12; + + int type; + String value; + + Token(int t) + { + type = t; + value = null; + } + + Token(int t, String v) + { + type = t; + value = v; + } +} diff --git a/java/demo/book/map_filesystem/build.xml b/java/demo/book/map_filesystem/build.xml new file mode 100644 index 00000000000..937733fe13f --- /dev/null +++ b/java/demo/book/map_filesystem/build.xml @@ -0,0 +1,64 @@ +<!-- + ********************************************************************** + + 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. + + ********************************************************************** +--> + +<project name="demo_book_map_filesystem" default="all" basedir="."> + + <!-- set global properties for this build --> + <property name="top.dir" value="../../.."/> + + <!-- import common definitions --> + <import file="${top.dir}/config/common.xml"/> + + <target name="generate" depends="init"> + <!-- Create the output directory for generated code --> + <mkdir dir="${generated.dir}"/> + <slice2java outputdir="${generated.dir}"> + <includepath> + <pathelement path="." /> + </includepath> + <fileset dir="." includes="Filesystem.ice"/> + <fileset dir="." includes="PersistentFilesystem.ice"/> + </slice2java> + + <slice2freezej ice="on" outputdir="${generated.dir}"> + <includepath> + <pathelement path="." /> + <pathelement path="${slice.dir}" /> + </includepath> + <fileset dir="." includes="Filesystem.ice"/> + <fileset dir="." includes="PersistentFilesystem.ice"/> + <fileset dir="." includes="Filesystem.ice,PersistentFilesystem.ice"/> + <fileset dir="${slice.dir}/Ice" includes="Identity.ice"/> + <dict name="Filesystem.IdentityNodeMap" key="Ice::Identity" value="Filesystem::PersistentNode"/> + </slice2freezej> + + </target> + + <target name="compile" depends="generate"> + <mkdir dir="${class.dir}"/> + <javac srcdir=".:${generated.dir}" destdir="${class.dir}" debug="${debug}"> + <exclude name="${generated.dir}/**"/> + <classpath> + <path refid="ice.classpath"/> + <path refid="freeze.classpath"/> + </classpath> + <compilerarg value="${javac.lint}"/> + </javac> + </target> + + <target name="all" depends="compile"/> + + <target name="clean"> + <delete dir="${generated.dir}"/> + <delete dir="${class.dir}"/> + </target> + +</project> diff --git a/java/demo/book/map_filesystem/config.client b/java/demo/book/map_filesystem/config.client new file mode 100644 index 00000000000..6f0ba50b98f --- /dev/null +++ b/java/demo/book/map_filesystem/config.client @@ -0,0 +1,28 @@ +# +# The client reads this property to create the reference to the root +# "directory" object in the server. +# +RootDir.Proxy=RootDir:default -p 10000 + +# +# Warn about connection exceptions +# +Ice.Warn.Connections=1 + +# +# Network Tracing +# +# 0 = no network tracing +# 1 = trace connection establishment and closure +# 2 = like 1, but more detailed +# 3 = like 2, but also trace data transfer +# +#Ice.Trace.Network=1 + +# +# Protocol Tracing +# +# 0 = no protocol tracing +# 1 = trace protocol messages +# +#Ice.Trace.Protocol=1 diff --git a/java/demo/book/freeze_filesystem/config.server b/java/demo/book/map_filesystem/config.server index 8ee5723e944..1b56c7c588a 100644 --- a/java/demo/book/freeze_filesystem/config.server +++ b/java/demo/book/map_filesystem/config.server @@ -1,7 +1,7 @@ # # Configure the server endpoints. # -FreezeFilesystem.Endpoints=default -p 10000 +MapFilesystem.Endpoints=default -h 127.0.0.1 -p 10000 # # Freeze Map Tracing. diff --git a/java/demo/book/map_filesystem/db/.gitignore b/java/demo/book/map_filesystem/db/.gitignore new file mode 100644 index 00000000000..39af5887579 --- /dev/null +++ b/java/demo/book/map_filesystem/db/.gitignore @@ -0,0 +1 @@ +# Dummy file, so that git retains this otherwise empty directory. diff --git a/java/demo/book/freeze_filesystem/expect.py b/java/demo/book/map_filesystem/expect.py index dcb76d36540..a5afc074999 100755 --- a/java/demo/book/freeze_filesystem/expect.py +++ b/java/demo/book/map_filesystem/expect.py @@ -20,7 +20,7 @@ if len(path) == 0: sys.path.append(path[0]) from demoscript import * -from demoscript.book import freeze_filesystem +from demoscript.book import map_filesystem print "cleaning databases...", sys.stdout.flush() @@ -31,4 +31,4 @@ server = Util.spawn('java Server --Ice.PrintAdapterReady') server.expect('.* ready') client = Util.spawn('java Client') -freeze_filesystem.run(client, server) +map_filesystem.run(client, server) diff --git a/java/demo/book/simple_filesystem/Server.java b/java/demo/book/simple_filesystem/Server.java index 5d4101af9cc..aefca791431 100644 --- a/java/demo/book/simple_filesystem/Server.java +++ b/java/demo/book/simple_filesystem/Server.java @@ -20,7 +20,7 @@ public class Server extends Ice.Application { // Create an object adapter. // Ice.ObjectAdapter adapter = communicator().createObjectAdapterWithEndpoints( - "SimpleFilesystem", "default -p 10000"); + "SimpleFilesystem", "default -h 127.0.0.1 -p 10000"); // Create the root directory (with name "/" and no parent) // |