diff options
author | Benoit Foucher <benoit@zeroc.com> | 2009-02-09 19:20:38 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2009-02-09 19:20:38 +0100 |
commit | 124b43c7bd718655556bd3cba2a554293bf63d16 (patch) | |
tree | 5c3138e3c96e9f3a4763e1b6eea92018d892b241 /java/demo | |
parent | Bug 3519 - fix use of tolower in slice2freeze (diff) | |
download | ice-124b43c7bd718655556bd3cba2a554293bf63d16.tar.bz2 ice-124b43c7bd718655556bd3cba2a554293bf63d16.tar.xz ice-124b43c7bd718655556bd3cba2a554293bf63d16.zip |
Support for serializable and protobuf metadata (from cs_serial branch)
Diffstat (limited to 'java/demo')
-rw-r--r-- | java/demo/Ice/build.xml | 2 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/Client.java | 127 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/Hello.ice | 27 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/HelloI.java | 26 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/Person.proto | 18 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/README | 41 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/Server.java | 37 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/ant/ProtocTask.java | 367 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/build.xml | 63 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/config.client | 23 | ||||
-rw-r--r-- | java/demo/Ice/protobuf/config.server | 24 | ||||
-rw-r--r-- | java/demo/Ice/serialize/Client.java | 142 | ||||
-rw-r--r-- | java/demo/Ice/serialize/Demo/MyGreeting.java | 15 | ||||
-rw-r--r-- | java/demo/Ice/serialize/Greet.ice | 26 | ||||
-rw-r--r-- | java/demo/Ice/serialize/GreetI.java | 33 | ||||
-rw-r--r-- | java/demo/Ice/serialize/README | 36 | ||||
-rw-r--r-- | java/demo/Ice/serialize/Server.java | 37 | ||||
-rw-r--r-- | java/demo/Ice/serialize/build.xml | 52 | ||||
-rw-r--r-- | java/demo/Ice/serialize/config.client | 36 | ||||
-rw-r--r-- | java/demo/Ice/serialize/config.server | 37 |
20 files changed, 1169 insertions, 0 deletions
diff --git a/java/demo/Ice/build.xml b/java/demo/Ice/build.xml index a8f43061344..c101f7c9627 100644 --- a/java/demo/Ice/build.xml +++ b/java/demo/Ice/build.xml @@ -22,6 +22,7 @@ <ant dir="minimal"/> <ant dir="multicast"/> <ant dir="nested"/> + <ant dir="serialize"/> <ant dir="session"/> <ant dir="throughput"/> <ant dir="value"/> @@ -38,6 +39,7 @@ <ant dir="minimal" target="clean"/> <ant dir="multicast" target="clean"/> <ant dir="nested" target="clean"/> + <ant dir="serialize" target="clean"/> <ant dir="session" target="clean"/> <ant dir="throughput" target="clean"/> <ant dir="value" target="clean"/> diff --git a/java/demo/Ice/protobuf/Client.java b/java/demo/Ice/protobuf/Client.java new file mode 100644 index 00000000000..cbdbb7aca2b --- /dev/null +++ b/java/demo/Ice/protobuf/Client.java @@ -0,0 +1,127 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Demo.*; + +public class Client extends Ice.Application +{ + class ShutdownHook extends Thread + { + public void + run() + { + try + { + communicator().destroy(); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + } + } + } + + private static void + menu() + { + System.out.println( + "usage:\n" + + "t: send greeting\n" + + "s: shutdown server\n" + + "x: exit\n" + + "?: help\n"); + } + + public int + run(String[] args) + { + if(args.length > 0) + { + System.err.println(appName() + ": too many arguments"); + return 1; + } + + // + // Since this is an interactive demo we want to clear the + // Application installed interrupt callback and install our + // own shutdown hook. + // + setInterruptHook(new ShutdownHook()); + + HelloPrx hello = HelloPrxHelper.checkedCast(communicator().propertyToProxy("Hello.Proxy")); + if(hello == null) + { + System.err.println("invalid proxy"); + return 1; + } + + tutorial.PersonPB.Person p = tutorial.PersonPB.Person.newBuilder(). + setId(1).setName("Fred Jones").setEmail("fred@jones.com").build(); + + menu(); + + java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); + + String line = null; + do + { + try + { + System.out.print("==> "); + System.out.flush(); + line = in.readLine(); + if(line == null) + { + break; + } + if(line.equals("t")) + { + hello.sayHello(p); + } + else if(line.equals("s")) + { + hello.shutdown(); + } + else if(line.equals("x")) + { + // Nothing to do + } + else if(line.equals("?")) + { + menu(); + } + else + { + System.out.println("unknown command `" + line + "'"); + menu(); + } + } + catch(java.io.IOException ex) + { + ex.printStackTrace(); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + } + } + while(!line.equals("x")); + + 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/Ice/protobuf/Hello.ice b/java/demo/Ice/protobuf/Hello.ice new file mode 100644 index 00000000000..8ddbbbd34ae --- /dev/null +++ b/java/demo/Ice/protobuf/Hello.ice @@ -0,0 +1,27 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +#ifndef HELLO_ICE +#define HELLO_ICE + +module Demo +{ + +["cpp:protobuf:tutorial::Person", "java:protobuf:tutorial.PersonPB.Person", "python:protobuf:Person_pb2.Person"] +sequence<byte> Person; + +interface Hello +{ + idempotent void sayHello(Person p); + void shutdown(); +}; + +}; + +#endif diff --git a/java/demo/Ice/protobuf/HelloI.java b/java/demo/Ice/protobuf/HelloI.java new file mode 100644 index 00000000000..e3fcf7008bb --- /dev/null +++ b/java/demo/Ice/protobuf/HelloI.java @@ -0,0 +1,26 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Demo.*; + +public class HelloI extends _HelloDisp +{ + public void + sayHello(tutorial.PersonPB.Person p, Ice.Current current) + { + System.out.println("Hello World from " + p); + } + + public void + shutdown(Ice.Current current) + { + System.out.println("Shutting down..."); + current.adapter.getCommunicator().shutdown(); + } +} diff --git a/java/demo/Ice/protobuf/Person.proto b/java/demo/Ice/protobuf/Person.proto new file mode 100644 index 00000000000..6ee58a3f246 --- /dev/null +++ b/java/demo/Ice/protobuf/Person.proto @@ -0,0 +1,18 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +package tutorial; + +option java_outer_classname = "PersonPB"; + +message Person { + required int32 id = 1; + required string name = 2; + optional string email = 3; +} diff --git a/java/demo/Ice/protobuf/README b/java/demo/Ice/protobuf/README new file mode 100644 index 00000000000..a431af0e734 --- /dev/null +++ b/java/demo/Ice/protobuf/README @@ -0,0 +1,41 @@ +This demo shows how to integrate Google Protocol Buffers with Ice. + +The Protocol Buffers distribution and documentation can be found at + + http://code.google.com/apis/protocolbuffers/ + +This demo was tested with Protocol Buffers version 2.0.2. + +We have added new metadata that makes it possible for you to specify +protocol buffers message types in your Slice definitions, with Ice +handling the serialization chores for you automatically. The metadata, +which may only be used on a sequence<byte> type, has the following +syntax in Java: + + java:protobuf:protoc-generated-class + +For example: + + ["java:protobuf:tutorial.PersonPB.Person"] sequence<byte> Person; + +The type name specified in this example, tutorial.PersonPB.Person, +corresponds to the Java class generated by the Protocol Buffers +compiler (protoc) for the definition shown below: + + package tutorial; + option java_outer_classname = "PersonPB"; + message Person { ... }; + +This demo uses an ant build task, located in the "ant" subdirectory, +for compiling .proto files. + +Prior to building the demo, you should add protoc to your PATH and +protobuf-java-2.0.2 to your CLASSPATH. + +To run the demo, first start the server: + + $ java Server + +In a separate window, start the client: + + $ java Client diff --git a/java/demo/Ice/protobuf/Server.java b/java/demo/Ice/protobuf/Server.java new file mode 100644 index 00000000000..6ffab5d02fb --- /dev/null +++ b/java/demo/Ice/protobuf/Server.java @@ -0,0 +1,37 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Demo.*; + +public class Server extends Ice.Application +{ + public int + run(String[] args) + { + if(args.length > 0) + { + System.err.println(appName() + ": too many arguments"); + return 1; + } + + Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Hello"); + adapter.add(new HelloI(), communicator().stringToIdentity("hello")); + adapter.activate(); + communicator().waitForShutdown(); + return 0; + } + + public static void + main(String[] args) + { + Server app = new Server(); + int status = app.main("Server", args, "config.server"); + System.exit(status); + } +} diff --git a/java/demo/Ice/protobuf/ant/ProtocTask.java b/java/demo/Ice/protobuf/ant/ProtocTask.java new file mode 100644 index 00000000000..ba981a27db0 --- /dev/null +++ b/java/demo/Ice/protobuf/ant/ProtocTask.java @@ -0,0 +1,367 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +//package Ice.Ant; + +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.taskdefs.ExecTask; +import org.apache.tools.ant.taskdefs.Execute; +import org.apache.tools.ant.taskdefs.PumpStreamHandler; +import org.apache.tools.ant.types.Commandline; +import org.apache.tools.ant.types.Commandline.Argument; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.Reference; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.StringReader; +import java.io.BufferedReader; +import java.io.BufferedWriter; + +/** + * An ant task for protoc. + * + * Attributes specific to protoc: + * + * translator - The pathname of the translator (default: "protoc"). + * protocpath - The value for the --proto_path translator option. + * outputdir - The value for the --java_out translator option. + * dependencyfile - The file in which dependencies are stored (default: ".pbdepend"). + * + * Example: + * + * <project ...> + * <taskdef name="protoc" classname="ProtocTask" /> + * <property name="protoc.dir" value="../include/protoc"/> + * <target name="generate"> + * <mkdir dir="tags" /> + * <protoc tagdir="tags" outputdir="out"> + * <fileset dir="${protoc.dir}"> + * <include name="*.ice" /> + * </fileset> + * </protoc> + * </target> + * </project> + * + * The <taskdef> element installs the protoctask task. + */ +public class ProtocTask extends org.apache.tools.ant.Task +{ + public + ProtocTask() + { + _translator = null; + _outputDir = null; + _protocPath = null; + _outputDirString = null; + } + + public void + setDependencyFile(File file) + { + _dependencyFile = file; + } + + public void + setOutputdir(File dir) + { + _outputDir = dir; + _outputDirString = _outputDir.toString(); + if(_outputDirString.indexOf(' ') != -1) + { + _outputDirString = '"' + _outputDirString + '"'; + } + } + + public void + setProtocpath(File dir) + { + _protocPath = dir.toString(); + } + + public void + setTranslator(File prog) + { + _translator = prog; + } + + public FileSet + createFileset() + { + FileSet fileset = new FileSet(); + _fileSets.add(fileset); + + return fileset; + } + + public void + execute() + throws BuildException + { + if(_fileSets.isEmpty()) + { + throw new BuildException("No fileset specified"); + } + + // + // Read the set of dependencies for this task. + // + java.util.HashMap dependencies = readDependencies(); + + // + // Compose a list of the files that need to be translated. A + // file needs to translated if we can't find a dependency in + // the dependency table or if its dependency is not up-to-date + // anymore (the proto file changed since the dependency was + // last updated) + // + java.util.Vector buildList = new java.util.Vector(); + java.util.Vector skipList = new java.util.Vector(); + java.util.Iterator p = _fileSets.iterator(); + while(p.hasNext()) + { + FileSet fileset = (FileSet)p.next(); + + DirectoryScanner scanner = fileset.getDirectoryScanner(getProject()); + scanner.scan(); + String[] files = scanner.getIncludedFiles(); + for(int i = 0; i < files.length; i++) + { + File proto = new File(fileset.getDir(getProject()), files[i]); + + ProtoDependency depend = (ProtoDependency)dependencies.get(getTargetKey(proto.toString())); + if(depend == null || !depend.isUpToDate()) + { + buildList.addElement(proto); + } + else + { + skipList.addElement(proto); + } + } + + java.util.Iterator i = skipList.iterator(); + while(i.hasNext()) + { + File file = (File)i.next(); + log("skipping " + file.getName()); + } + } + + // + // Run the translator + // + if(!buildList.isEmpty()) + { + String translator; + if(_translator == null) + { + translator = "protoc"; + } + else + { + translator = _translator.toString(); + } + + StringBuffer cmd = new StringBuffer(); + + // + // Add --java_out. + // + if(_outputDir != null) + { + cmd.append(" --java_out="); + cmd.append(stripDriveLetter(_outputDirString)); + } + + // + // Add --proto_path + // + if(_protocPath != null) + { + cmd.append(" --proto_path="); + cmd.append(stripDriveLetter(_protocPath)); + } + + // + // Add files to be translated + // + for(int i = 0; i < buildList.size(); i++) + { + File f = (File)buildList.elementAt(i); + cmd.append(" "); + String s = stripDriveLetter(f.toString()); + if(s.indexOf(' ') != -1) + { + cmd.append('"' + s + '"'); + } + else + { + cmd.append(s); + } + } + + // + // Execute + // + log(translator + " " + cmd); + ExecTask task = (ExecTask)getProject().createTask("exec"); + task.setFailonerror(true); + Argument arg = task.createArg(); + arg.setLine(cmd.toString()); + task.setExecutable(translator); + task.execute(); + + // + // Update dependency file. + // + for(int i = 0; i < buildList.size(); i++) + { + ProtoDependency depend = new ProtoDependency(); + depend._timeStamp = new java.util.Date().getTime(); + depend._dependency = ((File)buildList.elementAt(i)).toString(); + dependencies.put(getTargetKey(depend._dependency), depend); + } + + writeDependencies(dependencies); + } + } + + private String + getTargetKey(String proto) + { + // + // Since the dependency file can be shared by several proto + // tasks we need to make sure that each dependency has a + // unique key. We use the name of the task, the output + // directory and the name of the proto file to be compiled. + // + // If there's two protoc tasks using the same dependency + // file, with the same output dir and which compiles the same + // protoc file they'll use the same dependency. + // + return "protoc " + _outputDir.toString() + " " + proto; + } + + // This is to work around a bug with protoc, where it does not + // accept drive letters in path names. See + // http://bugzilla/bugzilla/show_bug.cgi?id=3349 + // + private String + stripDriveLetter(String s) + { + if(s.length() > 1 && s.charAt(1) == ':') + { + return s.substring(2); + } + return s; + } + + // + // Read the dependency file. + // + private java.util.HashMap + readDependencies() + { + if(_dependencyFile == null) + { + if(_outputDir != null) + { + _dependencyFile = new File(_outputDir, ".pbdepend"); + } + else + { + _dependencyFile = new File(".pbdepend"); + } + } + + try + { + java.io.ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(_dependencyFile)); + java.util.HashMap dependencies = (java.util.HashMap)in.readObject(); + in.close(); + return dependencies; + } + catch(java.io.IOException ex) + { + } + catch(java.lang.ClassNotFoundException ex) + { + } + + return new java.util.HashMap(); + } + + private void + writeDependencies(java.util.HashMap dependencies) + { + try + { + java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(new FileOutputStream(_dependencyFile)); + out.writeObject(dependencies); + out.close(); + } + catch(java.io.IOException ex) + { + throw new BuildException("Unable to write dependencies in file " + _dependencyFile.getPath() + ": " + ex); + } + } + + // + // A proto dependency. + // + // * the _timeStamp attribute contains the last time the proto + // file was compiled. + // + // * the _dependency attribute contains the .proto file. + // + private class ProtoDependency implements java.io.Serializable + { + private void writeObject(java.io.ObjectOutputStream out) + throws java.io.IOException + { + out.writeObject(_dependency); + out.writeLong(_timeStamp); + } + + private void readObject(java.io.ObjectInputStream in) + throws java.io.IOException, java.lang.ClassNotFoundException + { + _dependency = (String)in.readObject(); + _timeStamp = in.readLong(); + } + + public boolean + isUpToDate() + { + File dep = new File(_dependency); + if(!dep.exists() || _timeStamp < dep.lastModified()) + { + return false; + } + + return true; + } + + public String _dependency; + public long _timeStamp; + } + + private File _translator; + private File _dependencyFile; + private File _outputDir; + private String _outputDirString; + private String _protocPath; + private java.util.List _fileSets = new java.util.LinkedList(); +} diff --git a/java/demo/Ice/protobuf/build.xml b/java/demo/Ice/protobuf/build.xml new file mode 100644 index 00000000000..aa336420300 --- /dev/null +++ b/java/demo/Ice/protobuf/build.xml @@ -0,0 +1,63 @@ +<!-- + ********************************************************************** + + 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. + + ********************************************************************** +--> + +<project name="demo_Ice_protobuf" 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="protoc" depends="init"> + <!-- Build protoc task --> + <mkdir dir="${class.dir}"/> + <javac srcdir="ant" destdir="${class.dir}" debug="${debug}"> + <compilerarg value="${javac.lint}"/> + </javac> + </target> + + <target name="generate" depends="protoc"> + <!-- Add the protoc task --> + <taskdef name="protoc" classpath="${class.dir}" classname="ProtocTask" /> + <!-- Create the output directory for generated code --> + <mkdir dir="${generated.dir}"/> + <slice2java outputdir="${generated.dir}"> + <meta value="${java2metadata}"/> + <fileset dir="." includes="Hello.ice"/> + </slice2java> + <protoc outputdir="${generated.dir}" protocpath="."> + <fileset dir="." includes="Person.proto"/> + </protoc> + </target> + + <target name="compile" depends="generate"> + <mkdir dir="${class.dir}"/> + <javac srcdir="${generated.dir}" destdir="${class.dir}" + debug="${debug}"> + <classpath refid="ice.classpath"/> + <compilerarg value="${javac.lint}"/> + </javac> + <javac srcdir="." destdir="${class.dir}" + excludes="generated/**, ant/**" debug="${debug}"> + <classpath refid="ice.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/Ice/protobuf/config.client b/java/demo/Ice/protobuf/config.client new file mode 100644 index 00000000000..a02cab0f4cb --- /dev/null +++ b/java/demo/Ice/protobuf/config.client @@ -0,0 +1,23 @@ +# +# The client reads this property to create the reference to the +# "hello" object in the server. +# +Hello.Proxy=hello:tcp -p 10000 + +# +# 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/Ice/protobuf/config.server b/java/demo/Ice/protobuf/config.server new file mode 100644 index 00000000000..309db9731b2 --- /dev/null +++ b/java/demo/Ice/protobuf/config.server @@ -0,0 +1,24 @@ +# +# The server creates one single object adapter with the name +# "Hello". The following line sets the endpoints for this +# adapter. +# +Hello.Endpoints=tcp -p 10000 + +# +# 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/Ice/serialize/Client.java b/java/demo/Ice/serialize/Client.java new file mode 100644 index 00000000000..e5c6ce82fb0 --- /dev/null +++ b/java/demo/Ice/serialize/Client.java @@ -0,0 +1,142 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Demo.*; + +public class Client extends Ice.Application +{ + class ShutdownHook extends Thread + { + public void + run() + { + try + { + communicator().destroy(); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + } + } + } + + private static void + menu() + { + System.out.println( + "usage:\n" + + "g: send greeting\n" + + "t: toggle null greeting\n" + + "s: shutdown server\n" + + "x: exit\n" + + "?: help\n"); + } + + public int + run(String[] args) + { + if(args.length > 0) + { + System.err.println(appName() + ": too many arguments"); + return 1; + } + + // + // Since this is an interactive demo we want to clear the + // Application installed interrupt callback and install our + // own shutdown hook. + // + setInterruptHook(new ShutdownHook()); + + GreetPrx greet = GreetPrxHelper.checkedCast(communicator().propertyToProxy("Greet.Proxy")); + if(greet == null) { + + System.err.println("invalid proxy"); + return 1; + } + + MyGreeting greeting = new MyGreeting(); + greeting.text = "Hello there!"; + MyGreeting nullGreeting = null; + + boolean sendNull = false; + + menu(); + + java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); + + String line = null; + do + { + try + { + System.out.print("==> "); + System.out.flush(); + line = in.readLine(); + if(line == null) + { + break; + } + if(line.equals("g")) + { + if(sendNull) + { + greet.sendGreeting(nullGreeting); + } + else + { + greet.sendGreeting(greeting); + } + } + else if(line.equals("t")) + { + sendNull = !sendNull; + } + else if(line.equals("s")) + { + greet.shutdown(); + } + else if(line.equals("x")) + { + // Nothing to do + } + else if(line.equals("?")) + { + menu(); + } + else + { + System.out.println("unknown command `" + line + "'"); + menu(); + } + } + catch(java.io.IOException ex) + { + ex.printStackTrace(); + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + } + } + while(!line.equals("x")); + + 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/Ice/serialize/Demo/MyGreeting.java b/java/demo/Ice/serialize/Demo/MyGreeting.java new file mode 100644 index 00000000000..bbefc3e687b --- /dev/null +++ b/java/demo/Ice/serialize/Demo/MyGreeting.java @@ -0,0 +1,15 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +package Demo; + +public class MyGreeting implements java.io.Serializable +{ + public String text; +} diff --git a/java/demo/Ice/serialize/Greet.ice b/java/demo/Ice/serialize/Greet.ice new file mode 100644 index 00000000000..e06d9d04f6d --- /dev/null +++ b/java/demo/Ice/serialize/Greet.ice @@ -0,0 +1,26 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +#ifndef GREET_ICE +#define GREET_ICE + +module Demo +{ + +["java:serializable:Demo.MyGreeting"] sequence<byte> Greeting; + +interface Greet +{ + idempotent void sendGreeting(Greeting g); + void shutdown(); +}; + +}; + +#endif diff --git a/java/demo/Ice/serialize/GreetI.java b/java/demo/Ice/serialize/GreetI.java new file mode 100644 index 00000000000..855af5173f5 --- /dev/null +++ b/java/demo/Ice/serialize/GreetI.java @@ -0,0 +1,33 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Demo.*; + +public class GreetI extends _GreetDisp +{ + public void + sendGreeting(MyGreeting greeting, Ice.Current current) + { + if(greeting != null) + { + System.out.println(greeting.text); + } + else + { + System.out.println("Received null greeting"); + } + } + + public void + shutdown(Ice.Current current) + { + System.out.println("Shutting down..."); + current.adapter.getCommunicator().shutdown(); + } +} diff --git a/java/demo/Ice/serialize/README b/java/demo/Ice/serialize/README new file mode 100644 index 00000000000..b6e3828c445 --- /dev/null +++ b/java/demo/Ice/serialize/README @@ -0,0 +1,36 @@ +This demo illustrates how to transfer serializable Java classes +with Ice. + +The Java classes are transferred as byte sequences by Ice. It was +always possible to do this, but it would have required that you +explicitly serialize your class into a byte sequence and then pass +that sequence to your operations. Through the use of Slice metadata, +Ice now makes this much more convenient to accomplish. + +In your Slice definitions you must declare a byte sequence using the +"java:serializable" metadata and specify the Java class name, as shown +below: + +["java:serializable:JavaClassName"] sequence<byte> SliceType; + +Now, wherever you use the declared Slice type in your operations, you +will be able to supply an instance of the designated Java class and +Ice will automatically convert it to and from the byte sequence that +is passed over the wire. + +With the "java:serializable" metadata, if you have a serializable +class as an out-parameter, the our-parameter is passed as +Ice.Holder<JavaClassName>. For example, if you have a java class +MyPackage.Car as an out-parameter, the out-parameter is passed as +Ice.Holder<MyPackage.Car>. + +To run the demo, first start the server: + +$ java Server + +In a separate window, start the client: + +$ java Client + +The client allows you to toggle between sending a real class instance +and sending a null value, to show that passing null is supported. diff --git a/java/demo/Ice/serialize/Server.java b/java/demo/Ice/serialize/Server.java new file mode 100644 index 00000000000..6550ea79a6d --- /dev/null +++ b/java/demo/Ice/serialize/Server.java @@ -0,0 +1,37 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +import Demo.*; + +public class Server extends Ice.Application +{ + public int + run(String[] args) + { + if(args.length > 0) + { + System.err.println(appName() + ": too many arguments"); + return 1; + } + + Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Greet"); + adapter.add(new GreetI(), communicator().stringToIdentity("greet")); + adapter.activate(); + communicator().waitForShutdown(); + return 0; + } + + public static void + main(String[] args) + { + Server app = new Server(); + int status = app.main("Server", args, "config.server"); + System.exit(status); + } +} diff --git a/java/demo/Ice/serialize/build.xml b/java/demo/Ice/serialize/build.xml new file mode 100644 index 00000000000..96b3470fb12 --- /dev/null +++ b/java/demo/Ice/serialize/build.xml @@ -0,0 +1,52 @@ +<!-- + ********************************************************************** + + 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. + + ********************************************************************** +--> + +<project name="demo_Ice_serialize" 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}"> + <meta value="${java2metadata}"/> + <fileset dir="." includes="Greet.ice"/> + </slice2java> + </target> + + <target name="compile" depends="generate"> + <mkdir dir="${class.dir}"/> + <javac srcdir="." destdir="${class.dir}" includes="Demo/**" debug="${debug}"> + <classpath refid="ice.classpath"/> + <compilerarg value="${javac.lint}"/> + </javac> + <javac srcdir="${generated.dir}" destdir="${class.dir}" debug="${debug}"> + <classpath refid="ice.classpath"/> + <compilerarg value="${javac.lint}"/> + </javac> + <javac srcdir="." destdir="${class.dir}" excludes="generated/**,Serialize/**" debug="${debug}"> + <classpath refid="ice.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/Ice/serialize/config.client b/java/demo/Ice/serialize/config.client new file mode 100644 index 00000000000..f0cff951933 --- /dev/null +++ b/java/demo/Ice/serialize/config.client @@ -0,0 +1,36 @@ +# +# The client reads this property to create the reference to the +# "greet" object in the server. +# +Greet.Proxy=greet:tcp -p 10000 + +# +# Warn about connection exceptions. +# +Ice.Warn.Connections=1 + +# +# Client-side ACM is enabled by default, with an interval of 60 +# seconds. For this demo, we want to use a short timeout of 10 +# seconds. By enabling network tracing below, you can see ACM +# automatically close idle connections. +# +Ice.ACM.Client=10 + +# +# 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/Ice/serialize/config.server b/java/demo/Ice/serialize/config.server new file mode 100644 index 00000000000..a2227d3a224 --- /dev/null +++ b/java/demo/Ice/serialize/config.server @@ -0,0 +1,37 @@ +# +# The server creates one single object adapter with the name +# "Greet". The following line sets the endpoints for this +# adapter. +# +Greet.Endpoints=tcp -p 10000 + +# +# Warn about connection exceptions. +# +Ice.Warn.Connections=1 + +# +# Server-side ACM is disabled by default. For this demo, we want it +# enabled and set to a short timeout of 10 seconds. By enabling +# network tracing below, you can see ACM automatically close idle +# connections. +# +Ice.ACM.Server=10 + +# +# 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 |