diff options
Diffstat (limited to 'java/demo/Ice/serialize')
-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 | 37 | ||||
-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 | 28 | ||||
-rw-r--r-- | java/demo/Ice/serialize/config.server | 29 | ||||
-rwxr-xr-x | java/demo/Ice/serialize/expect.py | 30 |
10 files changed, 429 insertions, 0 deletions
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..c472c81bef0 --- /dev/null +++ b/java/demo/Ice/serialize/README @@ -0,0 +1,37 @@ +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 required you to explicitly serialize +your class into a byte sequence and then pass that sequence to your +operations to be deserialized by the receiver. You can now accomplish +the same thing more conveniently via metadata. + +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 or +data types, you can supply an instance of the designated Java class +and Ice automatically converts it to and from the byte sequence that +is passed over the wire. (The Java class you pass must derive from +java.io.Serializable.) + +With the "java:serializable" metadata, if you have a serializable +class as an out-parameter, the out-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..cdf6f093e41 --- /dev/null +++ b/java/demo/Ice/serialize/config.client @@ -0,0 +1,28 @@ +# +# 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 + +# +# 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..41bfe6caa46 --- /dev/null +++ b/java/demo/Ice/serialize/config.server @@ -0,0 +1,29 @@ +# +# 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 + +# +# 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/expect.py b/java/demo/Ice/serialize/expect.py new file mode 100755 index 00000000000..76bc8e43408 --- /dev/null +++ b/java/demo/Ice/serialize/expect.py @@ -0,0 +1,30 @@ +#!/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.Ice import serialize + +server = Util.spawn('java Server --Ice.PrintAdapterReady --Ice.Warn.Connections=0') +server.expect('.* ready') +client = Util.spawn('java Client --Ice.Warn.Connections=0') +client.expect('.*==>') + +serialize.run(client, server) |