diff options
author | Benoit Foucher <benoit@zeroc.com> | 2014-04-22 17:44:12 +0200 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2014-04-22 17:44:12 +0200 |
commit | 873b482a66e5914ba3dcf85d17fdc94a62457c6a (patch) | |
tree | 18211a7e8f13d3f4322df9479025ce5f5a74d72c /java | |
parent | Remove Qt and VC90 settings from Makefiles. (diff) | |
download | ice-873b482a66e5914ba3dcf85d17fdc94a62457c6a.tar.bz2 ice-873b482a66e5914ba3dcf85d17fdc94a62457c6a.tar.xz ice-873b482a66e5914ba3dcf85d17fdc94a62457c6a.zip |
Added IceDiscovery service plugin
Diffstat (limited to 'java')
42 files changed, 2424 insertions, 78 deletions
diff --git a/java/allTests.py b/java/allTests.py index 8b9a0509525..069c3e307a9 100755 --- a/java/allTests.py +++ b/java/allTests.py @@ -79,6 +79,7 @@ tests = [ ("Freeze/fileLock", ["once"]), ("Glacier2/router", ["service"]), ("Glacier2/sessionHelper", ["service", "nossl", "noipv6"]), + ("IceDiscovery/simple", ["service"]), ("IceGrid/simple", ["service"]), ("IceSSL/configuration", ["once"]) ] diff --git a/java/build.xml b/java/build.xml index 924c5669ee5..0e22cdecb97 100644 --- a/java/build.xml +++ b/java/build.xml @@ -79,6 +79,9 @@ <include name="Instrumentation.ice" /> <include name="Metrics.ice" /> </fileset> + <fileset dir="${slice.dir}/IceDiscovery"> + <include name="IceDiscovery.ice" /> + </fileset> <fileset dir="${slice.dir}/Freeze"> <include name="DB.ice" /> <include name="Connection.ice" /> @@ -242,6 +245,34 @@ </jar> </target> + <target name="icediscovery-compile" depends="ice-compile" unless="ice.bin.dist"> + <mkdir dir="${lib.dir}"/> + <mkdir dir="${cache.dir}"/> + <depend srcdir=".:${src.dir}:${generated.dir}:${generated.test.dir}" destdir="${lib.dir}" cache="${cache.dir}"> + <include name="IceDiscovery/**"/> + </depend> + <javac srcdir="${src.dir}:${generated.dir}" destdir="${lib.dir}" source="1.6" target="1.6" debug="${debug}" + deprecation="on"> + <include name="IceDiscovery/**"/> + <compilerarg value="${javac.lint}"/> + </javac> + </target> + + <target name="icediscovery-jar" depends="icediscovery-compile, ice-jar" unless="ice.bin.dist"> + <jar jarfile="${lib.dir}/${icediscovery.jar.name}" basedir="${lib.dir}"> + <include name="IceDiscovery/*.class"/> + <manifest> + <attribute name="Built-By" value="ZeroC, Inc."/> + </manifest> + </jar> + <jar jarfile="${lib.dir}/${icediscovery.jar.name}" basedir="${src.dir}" update="yes"> + <include name="IceDiscovery/*.java"/> + </jar> + <jar jarfile="${lib.dir}/${icediscovery.jar.name}" basedir="${generated.dir}" update="yes"> + <include name="IceDiscovery/*.java"/> + </jar> + </target> + <target name="freeze-compile" depends="ice-compile" unless="ice.bin.dist"> <mkdir dir="${lib.dir}"/> <depend srcdir=".:${src.dir}:${generated.dir}:${generated.test.dir}" destdir="${lib.dir}" cache="${cache.dir}"> @@ -466,6 +497,7 @@ <include name="test/Freeze/complex/Complex.ice" /> <include name="test/Glacier2/router/Callback.ice" /> <include name="test/Glacier2/sessionHelper/Callback.ice" /> + <include name="test/IceDiscovery/simple/Test.ice" /> <include name="test/IceGrid/simple/Test.ice" /> <include name="test/IceBox/admin/Test.ice" /> <include name="test/IceBox/configuration/Test.ice" /> @@ -573,7 +605,9 @@ </target> <target name="test-compile" depends="ice-compile, freeze-compile, glacier2-compile, icebox-compile, - icestorm-compile, icegrid-compile, test-generate"> + icestorm-compile, + icegrid-compile, + icediscovery-compile, test-generate"> <mkdir dir="${lib.dir}"/> <mkdir dir="${cache.dir}"/> <depend srcdir=".:${generated.test.dir}" destdir="${lib.dir}" cache="${cache.dir}"> @@ -901,11 +935,14 @@ </target> <target name="compile" depends="ice-compile, icebox-compile, glacier2-compile, icestorm-compile, icegrid-compile, - freeze-compile, icepatch2-compile, icegridadmin-compile"/> + icediscovery-compile, freeze-compile, icepatch2-compile, icegridadmin-compile"/> <target name="dist-jar" - depends="ice-jar, icebox-jar, glacier2-jar, icestorm-jar, icegrid-jar, freeze-jar, icepatch2-jar, - icegridadmin-plain-jar, icegridadmin-pro-jar, icegridadmin-javafx-jar, icegridadmin-bundle"/> + depends="ice-jar, icebox-jar, glacier2-jar, icestorm-jar, + icegrid-jar, freeze-jar, icepatch2-jar, + icediscovery-jar, icegridadmin-plain-jar, + icegridadmin-pro-jar, + icegridadmin-javafx-jar, icegridadmin-bundle"/> <target name="jar" depends="dist-jar, test-jar, test-android-jar"/> @@ -938,7 +975,8 @@ <target name="install-icegridgui-bundle" if="build-icegridadmin-bundle"> <mkdir dir="${prefix}/bin"/> - <!-- Don't use the copy task, it will lost the folder flags --> + <!-- Don't use the copy task, it will lost the folder flags + --> <exec executable="cp"> <arg value="-rf"/> <arg value="${lib.dir}/IceGrid Admin.app"/> diff --git a/java/config/common.xml b/java/config/common.xml index 24afa396092..0c02b47914e 100644 --- a/java/config/common.xml +++ b/java/config/common.xml @@ -37,6 +37,7 @@ </condition> <property name="ant-ice.jar.name" value="ant-ice${version.suffix}.jar"/> + <property name="icediscovery.jar.name" value="IceDiscovery${version.suffix}.jar"/> <property name="freeze.jar.name" value="Freeze${version.suffix}.jar"/> <property name="glacier2.jar.name" value="Glacier2${version.suffix}.jar"/> <property name="icebox.jar.name" value="IceBox${version.suffix}.jar"/> @@ -501,6 +502,25 @@ <pathelement location="${ice.jar.file}"/> </path> + <!-- IceDiscovery jar file --> + <condition property="icediscovery.jar.file" value="${dist.lib.dir}/IceDiscovery-${ice.version}.jar"> + <and> + <not><isset property="icediscovery.jar.file"/></not> + <available file="${dist.lib.dir}/IceDiscovery-${ice.version}.jar"/> + </and> + </condition> + + <condition property="icediscovery.jar.file" value="${dist.lib.dir}/IceDiscovery.jar"> + <and> + <not><isset property="icediscovery.jar.file"/></not> + <available file="${dist.lib.dir}/IceDiscovery.jar"/> + </and> + </condition> + + <path id="icediscovery.classpath"> + <pathelement location="${icediscovery.jar.file}"/> + </path> + <!-- Glacier2 jar file --> <condition property="glacier2.jar.file" value="${dist.lib.dir}/Glacier2-${ice.version}.jar"> <and> diff --git a/java/demo/IceDiscovery/build.xml b/java/demo/IceDiscovery/build.xml new file mode 100644 index 00000000000..a181b6bb5ae --- /dev/null +++ b/java/demo/IceDiscovery/build.xml @@ -0,0 +1,24 @@ +<!-- + ********************************************************************** + + Copyright (c) 2003-2013 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_IceDiscovery" default="all" basedir="."> + + <target name="all"> + <ant dir="hello"/> + <ant dir="replication"/> + </target> + + <target name="clean"> + <ant dir="hello" target="clean"/> + <ant dir="replication" target="clean"/> + </target> + +</project> diff --git a/java/demo/IceDiscovery/hello/Client.java b/java/demo/IceDiscovery/hello/Client.java new file mode 100644 index 00000000000..31d55f70c8c --- /dev/null +++ b/java/demo/IceDiscovery/hello/Client.java @@ -0,0 +1,237 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 as twoway\n" + + "o: send greeting as oneway\n" + + "O: send greeting as batch oneway\n" + + "d: send greeting as datagram\n" + + "D: send greeting as batch datagram\n" + + "f: flush all batch requests\n" + + "T: set a timeout\n" + + "P: set a server delay\n" + + "S: switch secure mode on/off\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 twoway = HelloPrxHelper.checkedCast(communicator().stringToProxy("hello").ice_timeout(-1)); + if(twoway == null) + { + System.err.println("invalid proxy"); + return 1; + } + HelloPrx oneway = (HelloPrx)twoway.ice_oneway(); + HelloPrx batchOneway = (HelloPrx)twoway.ice_batchOneway(); + HelloPrx datagram = (HelloPrx)twoway.ice_datagram(); + HelloPrx batchDatagram = (HelloPrx)twoway.ice_batchDatagram(); + + boolean secure = false; + int timeout = -1; + int delay = 0; + + 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")) + { + twoway.sayHello(delay); + } + else if(line.equals("o")) + { + oneway.sayHello(delay); + } + else if(line.equals("O")) + { + batchOneway.sayHello(delay); + } + else if(line.equals("d")) + { + if(secure) + { + System.out.println("secure datagrams are not supported"); + } + else + { + datagram.sayHello(delay); + } + } + else if(line.equals("D")) + { + if(secure) + { + System.out.println("secure datagrams are not supported"); + } + else + { + batchDatagram.sayHello(delay); + } + } + else if(line.equals("f")) + { + communicator().flushBatchRequests(); + } + else if(line.equals("T")) + { + if(timeout == -1) + { + timeout = 2000; + } + else + { + timeout = -1; + } + + twoway = (HelloPrx)twoway.ice_timeout(timeout); + oneway = (HelloPrx)oneway.ice_timeout(timeout); + batchOneway = (HelloPrx)batchOneway.ice_timeout(timeout); + + if(timeout == -1) + { + System.out.println("timeout is now switched off"); + } + else + { + System.out.println("timeout is now set to 2000ms"); + } + } + else if(line.equals("P")) + { + if(delay == 0) + { + delay = 2500; + } + else + { + delay = 0; + } + + if(delay == 0) + { + System.out.println("server delay is now deactivated"); + } + else + { + System.out.println("server delay is now set to 2500ms"); + } + } + else if(line.equals("S")) + { + secure = !secure; + + twoway = (HelloPrx)twoway.ice_secure(secure); + oneway = (HelloPrx)oneway.ice_secure(secure); + batchOneway = (HelloPrx)batchOneway.ice_secure(secure); + datagram = (HelloPrx)datagram.ice_secure(secure); + batchDatagram = (HelloPrx)batchDatagram.ice_secure(secure); + + if(secure) + { + System.out.println("secure mode is now on"); + } + else + { + System.out.println("secure mode is now off"); + } + } + else if(line.equals("s")) + { + twoway.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/IceDiscovery/hello/Hello.ice b/java/demo/IceDiscovery/hello/Hello.ice new file mode 100644 index 00000000000..aca54020837 --- /dev/null +++ b/java/demo/IceDiscovery/hello/Hello.ice @@ -0,0 +1,21 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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. +// +// ********************************************************************** + +#pragma once + +module Demo +{ + +interface Hello +{ + idempotent void sayHello(int delay); + void shutdown(); +}; + +}; diff --git a/java/demo/IceDiscovery/hello/HelloI.java b/java/demo/IceDiscovery/hello/HelloI.java new file mode 100644 index 00000000000..416ee68e93b --- /dev/null +++ b/java/demo/IceDiscovery/hello/HelloI.java @@ -0,0 +1,36 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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(int delay, Ice.Current current) + { + if(delay > 0) + { + try + { + Thread.currentThread().sleep(delay); + } + catch(InterruptedException ex1) + { + } + } + System.out.println("Hello World!"); + } + + public void + shutdown(Ice.Current current) + { + System.out.println("Shutting down..."); + current.adapter.getCommunicator().shutdown(); + } +} diff --git a/java/demo/IceDiscovery/hello/README b/java/demo/IceDiscovery/hello/README new file mode 100644 index 00000000000..0cc7c5b7cc9 --- /dev/null +++ b/java/demo/IceDiscovery/hello/README @@ -0,0 +1,18 @@ +This demo illustrates how to invoke ordinary (twoway) operations, as +well as how to make oneway, datagram, secure, and batched invocations. + +To run the demo, first start the server: + +$ java Server + +In a separate window, start the client: + +$ java Client + +To test timeouts you can use 'T' to set a timeout on the client proxy +and 'P' to set a delayed response in the server to cause a timeout. +You will notice that two "Hello World!" messages will be printed by +the server in this case. This is because the sayHello method is marked +as idempotent in the slice, meaning that Ice does not need to follow +the at-most-once retry semantics. See the manual for more information +about retry behavior. diff --git a/java/demo/IceDiscovery/hello/Server.java b/java/demo/IceDiscovery/hello/Server.java new file mode 100644 index 00000000000..809282f6b25 --- /dev/null +++ b/java/demo/IceDiscovery/hello/Server.java @@ -0,0 +1,37 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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/IceDiscovery/hello/build.xml b/java/demo/IceDiscovery/hello/build.xml new file mode 100644 index 00000000000..4b771656bca --- /dev/null +++ b/java/demo/IceDiscovery/hello/build.xml @@ -0,0 +1,44 @@ +<!-- + ********************************************************************** + + Copyright (c) 2003-2013 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_IceDiscovery_hello" 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}"> + <fileset dir="." includes="Hello.ice"/> + </slice2java> + </target> + + <target name="compile" depends="generate"> + <mkdir dir="${class.dir}"/> + <javac srcdir=".:${generated.dir}" destdir="${class.dir}" debug="${debug}"> + <exclude name="${generated.dir}/**"/> + <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/IceDiscovery/hello/config.client b/java/demo/IceDiscovery/hello/config.client new file mode 100644 index 00000000000..879b4c378c6 --- /dev/null +++ b/java/demo/IceDiscovery/hello/config.client @@ -0,0 +1,58 @@ +# +# Enable the Ice discovery plugin +# +Ice.Plugin.IceDiscovery=IceDiscovery:IceDiscovery.PluginFactory + +# +# Warn about connection exceptions +# +Ice.Warn.Connections=1 + +# +# Locator tracing +# +Ice.Trace.Locator=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 + +# +# Security Tracing +# +# 0 = no security tracing +# 1 = trace messages +# +#IceSSL.Trace.Security=1 + +# +# SSL Configuration +# +Ice.Plugin.IceSSL=IceSSL:IceSSL.PluginFactory +IceSSL.DefaultDir=../../../../certs +IceSSL.ImportCert.CurrentUser.Root=cacert.pem +IceSSL.CertFile=c_rsa1024.pfx +IceSSL.Password=password + +# +# IceMX configuration. +# +#Ice.Admin.Endpoints=tcp -h localhost -p 10003 +Ice.Admin.InstanceName=client +IceMX.Metrics.Debug.GroupBy=id +IceMX.Metrics.ByParent.GroupBy=parent + diff --git a/java/demo/IceDiscovery/hello/config.server b/java/demo/IceDiscovery/hello/config.server new file mode 100644 index 00000000000..72d439f5275 --- /dev/null +++ b/java/demo/IceDiscovery/hello/config.server @@ -0,0 +1,65 @@ +# +# The server creates one single object adapter with the name +# "Hello". The following sets the endpoints and the adapter ID for +# this adapter. +# +Hello.Endpoints=tcp -h localhost:udp -h localhost:ssl -h localhost +Hello.AdapterId=HelloAdapter + +# +# Warn about connection exceptions +# +Ice.Warn.Connections=1 + +# +# Locator tracing +# +Ice.Trace.Locator=0 + +# +# Enable the Ice discovery plugin +# +Ice.Plugin.IceDiscovery=IceDiscovery:IceDiscovery.PluginFactory + +# +# 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 + +# +# Security Tracing +# +# 0 = no security tracing +# 1 = trace messages +# +#IceSSL.Trace.Security=1 + +# +# SSL Configuration +# +Ice.Plugin.IceSSL=IceSSL:IceSSL.PluginFactory +IceSSL.DefaultDir=../../../../certs +IceSSL.ImportCert.CurrentUser.Root=cacert.pem +IceSSL.CertFile=c_rsa1024.pfx +IceSSL.Password=password + +# +# IceMX configuration. +# +#Ice.Admin.Endpoints=tcp -h localhost -p 10003 +Ice.Admin.InstanceName=client +IceMX.Metrics.Debug.GroupBy=id +IceMX.Metrics.ByParent.GroupBy=parent diff --git a/java/demo/IceDiscovery/hello/expect.py b/java/demo/IceDiscovery/hello/expect.py new file mode 100755 index 00000000000..f676194c10f --- /dev/null +++ b/java/demo/IceDiscovery/hello/expect.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# Copyright (c) 2003-2013 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 RuntimeError("can't find toplevel directory!") +sys.path.append(path[0]) + +from demoscript import Util +from demoscript.Ice import hello + +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('.*==>') + +hello.run(client, server) diff --git a/java/demo/IceDiscovery/replication/Client.java b/java/demo/IceDiscovery/replication/Client.java new file mode 100644 index 00000000000..94e489d4724 --- /dev/null +++ b/java/demo/IceDiscovery/replication/Client.java @@ -0,0 +1,131 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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(); + } + } + } + + 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()); + + // + // Get the hello proxy. We configure the proxy to not cache the + // server connection with the proxy and to disable the locator + // cache. With this configuration, the IceGrid locator will be + // queried for each invocation on the proxy and the invocation + // will be sent over the server connection matching the returned + // endpoints. + // + Ice.ObjectPrx obj = communicator().stringToProxy("hello"); + obj = obj.ice_connectionCached(false); + obj = obj.ice_locatorCacheTimeout(0); + + HelloPrx hello = HelloPrxHelper.checkedCast(obj); + if(hello == null) + { + System.err.println("invalid proxy"); + return 1; + } + + while(true) + { + System.out.print("enter the number of iterations: "); + System.out.flush(); + + try + { + java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); + String line = in.readLine(); + if(line == null || line.equals("x")) + { + break; + } + + int count = Integer.parseInt(line); + + System.out.print("enter the delay between each greetings (in ms): "); + System.out.flush(); + line = in.readLine(); + if(line == null || line.equals("x")) + { + break; + } + int delay = Integer.parseInt(line); + + if(delay < 0) + { + delay = 500; // 500 milli-seconds + } + + for(int i = 0; i < count; i++) + { + System.out.println(hello.getGreeting()); + try + { + Thread.currentThread().sleep(delay); + } + catch(InterruptedException ex1) + { + } + } + } + catch(Ice.LocalException ex) + { + ex.printStackTrace(); + } + catch(NumberFormatException ex) + { + System.out.println("please specify a valid integer value"); + } + catch(java.io.IOException 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/IceDiscovery/replication/Hello.ice b/java/demo/IceDiscovery/replication/Hello.ice new file mode 100644 index 00000000000..dd12535592d --- /dev/null +++ b/java/demo/IceDiscovery/replication/Hello.ice @@ -0,0 +1,21 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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. +// +// ********************************************************************** + +#pragma once + +module Demo +{ + +interface Hello +{ + idempotent string getGreeting(); + void shutdown(); +}; + +}; diff --git a/java/demo/IceDiscovery/replication/HelloI.java b/java/demo/IceDiscovery/replication/HelloI.java new file mode 100644 index 00000000000..83eff590b44 --- /dev/null +++ b/java/demo/IceDiscovery/replication/HelloI.java @@ -0,0 +1,33 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 HelloI(String name) + { + _name = name; + } + + public String + getGreeting(Ice.Current current) + { + return _name + " says Hello World!"; + } + + public void + shutdown(Ice.Current current) + { + System.out.println("Shutting down..."); + current.adapter.getCommunicator().shutdown(); + } + + final private String _name; +}; diff --git a/java/demo/IceDiscovery/replication/README b/java/demo/IceDiscovery/replication/README new file mode 100644 index 00000000000..6b2f1ecee25 --- /dev/null +++ b/java/demo/IceDiscovery/replication/README @@ -0,0 +1,18 @@ +To run the demo, start the 3 servers: + +$ server.exe --Ice.Config=config.server1 +$ server.exe --Ice.Config=config.server2 +$ server.exe --Ice.Config=config.server3 + +In a separate window: + +$ client.exe + +The client invokes the number of specified iterations with a given +delay on a well-known proxy configured to use per-request load +balancing. Each invocation on the proxy queries the Ice locator +implemented by the IceDiscovery plugin. + +While the client is running and invoking on the server, you can try to +stop some of the servers. As long as one server is still running, the +client will continue to work. diff --git a/java/demo/IceDiscovery/replication/Server.java b/java/demo/IceDiscovery/replication/Server.java new file mode 100644 index 00000000000..523921ba2b0 --- /dev/null +++ b/java/demo/IceDiscovery/replication/Server.java @@ -0,0 +1,38 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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"); + HelloI hello = new HelloI(communicator().getProperties().getProperty("Ice.ProgramName")); + adapter.add(hello, 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); + System.exit(status); + } +} diff --git a/java/demo/IceDiscovery/replication/build.xml b/java/demo/IceDiscovery/replication/build.xml new file mode 100644 index 00000000000..2bb8d11d282 --- /dev/null +++ b/java/demo/IceDiscovery/replication/build.xml @@ -0,0 +1,44 @@ +<!-- + ********************************************************************** + + Copyright (c) 2003-2013 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_IceDiscovery_replication" 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}"> + <fileset dir="." includes="Hello.ice"/> + </slice2java> + </target> + + <target name="compile" depends="generate"> + <mkdir dir="${class.dir}"/> + <javac srcdir=".:${generated.dir}" destdir="${class.dir}" debug="${debug}"> + <exclude name="${generated.dir}/**"/> + <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/IceDiscovery/replication/config.client b/java/demo/IceDiscovery/replication/config.client new file mode 100644 index 00000000000..92ba5e5ccdb --- /dev/null +++ b/java/demo/IceDiscovery/replication/config.client @@ -0,0 +1,9 @@ +# +# Enable the Ice discovery plugin +# +Ice.Plugin.IceDiscovery=IceDiscovery:IceDiscovery.PluginFactory + +# +# Ensure connection establishment doesn't take too long. +# +Ice.Override.ConnectTimeout=1000 diff --git a/java/demo/IceDiscovery/replication/config.server1 b/java/demo/IceDiscovery/replication/config.server1 new file mode 100755 index 00000000000..ec49e0b279b --- /dev/null +++ b/java/demo/IceDiscovery/replication/config.server1 @@ -0,0 +1,31 @@ +# +# The server creates one single object adapter with the name "Hello". +# +# The following configures the object adapter. We set endpoints with +# no fixed port and we assign it a unique adapter ID that will be +# embedded in indirect proxies. +# +# We also configure the replica group ID, object adapters sharing the +# same replica group ID are part of the same replica group. When the +# client resolves an indirect proxy refering to this replica group, +# the locator implementation will return the endpoints of all the +# object adapters part of the replica group. +# +Hello.Endpoints=tcp +Hello.AdapterId=HelloAdapter1 +Hello.ReplicaGroupId=ReplicatedHelloAdapter + +# +# Identify the server +# +Ice.ProgramName=Server1 + +# +# Enable the Ice discovery plugin +# +Ice.Plugin.IceDiscovery=IceDiscovery:IceDiscovery.PluginFactory + +# +# Ensure connection establishment doesn't take too long. +# +Ice.Override.ConnectTimeout=1000 diff --git a/java/demo/IceDiscovery/replication/config.server2 b/java/demo/IceDiscovery/replication/config.server2 new file mode 100755 index 00000000000..5eabd1f91cf --- /dev/null +++ b/java/demo/IceDiscovery/replication/config.server2 @@ -0,0 +1,31 @@ +# +# The server creates one single object adapter with the name "Hello". +# +# The following configures the object adapter. We set endpoints with +# no fixed port and we assign it a unique adapter ID that will be +# embedded in indirect proxies. +# +# We also configure the replica group ID, object adapters sharing the +# same replica group ID are part of the same replica group. When the +# client resolves an indirect proxy refering to this replica group, +# the locator implementation will return the endpoints of all the +# object adapters part of the replica group. +# +Hello.Endpoints=tcp +Hello.AdapterId=HelloAdapter2 +Hello.ReplicaGroupId=ReplicatedHelloAdapter + +# +# Identify the server +# +Ice.ProgramName=Server2 + +# +# Enable the Ice discovery plugin +# +Ice.Plugin.IceDiscovery=IceDiscovery:IceDiscovery.PluginFactory + +# +# Ensure connection establishment doesn't take too long. +# +Ice.Override.ConnectTimeout=1000 diff --git a/java/demo/IceDiscovery/replication/config.server3 b/java/demo/IceDiscovery/replication/config.server3 new file mode 100755 index 00000000000..12362e47722 --- /dev/null +++ b/java/demo/IceDiscovery/replication/config.server3 @@ -0,0 +1,31 @@ +# +# The server creates one single object adapter with the name "Hello". +# +# The following configures the object adapter. We set endpoints with +# no fixed port and we assign it a unique adapter ID that will be +# embedded in indirect proxies. +# +# We also configure the replica group ID, object adapters sharing the +# same replica group ID are part of the same replica group. When the +# client resolves an indirect proxy refering to this replica group, +# the locator implementation will return the endpoints of all the +# object adapters part of the replica group. +# +Hello.Endpoints=tcp +Hello.AdapterId=HelloAdapter3 +Hello.ReplicaGroupId=ReplicatedHelloAdapter + +# +# Identify the server +# +Ice.ProgramName=Server3 + +# +# Enable the Ice discovery plugin +# +Ice.Plugin.IceDiscovery=IceDiscovery:IceDiscovery.PluginFactory + +# +# Ensure connection establishment doesn't take too long. +# +Ice.Override.ConnectTimeout=1000 diff --git a/java/demo/IceDiscovery/replication/expect.py b/java/demo/IceDiscovery/replication/expect.py new file mode 100755 index 00000000000..af25a3ce17c --- /dev/null +++ b/java/demo/IceDiscovery/replication/expect.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# Copyright (c) 2003-2013 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 RuntimeError("can't find toplevel directory!") +sys.path.append(path[0]) + +from demoscript import Util +from demoscript.Ice import hello + +server = Util.spawn('server.exe --Ice.PrintAdapterReady --Ice.Warn.Connections=0') +server.expect('.* ready') +client = Util.spawn('client.exe --Ice.Warn.Connections=0') +client.expect('.*==>') + +hello.run(client, server) diff --git a/java/src/IceDiscovery/LocatorI.java b/java/src/IceDiscovery/LocatorI.java new file mode 100644 index 00000000000..be49f1f47ad --- /dev/null +++ b/java/src/IceDiscovery/LocatorI.java @@ -0,0 +1,40 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 IceDiscovery; + +class LocatorI extends Ice._LocatorDisp +{ + public LocatorI(LookupI lookup, Ice.LocatorRegistryPrx registry) + { + _lookup = lookup; + _registry = registry; + } + + public void + findObjectById_async(Ice.AMD_Locator_findObjectById cb, Ice.Identity id, Ice.Current current) + { + _lookup.findObject(cb, id); + } + + public void + findAdapterById_async(Ice.AMD_Locator_findAdapterById cb, String adapterId, Ice.Current current) + { + _lookup.findAdapter(cb, adapterId); + } + + public Ice.LocatorRegistryPrx + getRegistry(Ice.Current current) + { + return _registry; + } + + private final LookupI _lookup; + private final Ice.LocatorRegistryPrx _registry; +} diff --git a/java/src/IceDiscovery/LocatorRegistryI.java b/java/src/IceDiscovery/LocatorRegistryI.java new file mode 100644 index 00000000000..d1a03ecb713 --- /dev/null +++ b/java/src/IceDiscovery/LocatorRegistryI.java @@ -0,0 +1,176 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 IceDiscovery; + +import java.util.Map; +import java.util.HashMap; +import java.util.Set; +import java.util.HashSet; +import java.util.List; +import java.util.ArrayList; + +class LocatorRegistryI extends Ice._LocatorRegistryDisp +{ + public LocatorRegistryI(Ice.Communicator com) + { + _wellKnownProxy = com.stringToProxy("p").ice_locator(null).ice_router(null).ice_collocationOptimized(true); + } + + synchronized public void + setAdapterDirectProxy_async(Ice.AMD_LocatorRegistry_setAdapterDirectProxy cb, + String adapterId, + Ice.ObjectPrx proxy, + Ice.Current current) + { + if(proxy != null) + { + _adapters.put(adapterId, proxy); + } + else + { + _adapters.remove(adapterId); + } + cb.ice_response(); + } + + synchronized public void + setReplicatedAdapterDirectProxy_async(Ice.AMD_LocatorRegistry_setReplicatedAdapterDirectProxy cb, + String adapterId, + String replicaGroupId, + Ice.ObjectPrx proxy, + Ice.Current current) + { + if(proxy != null) + { + _adapters.put(adapterId, proxy); + Set<String> s = _replicaGroups.get(replicaGroupId); + if(s == null) + { + s = new HashSet<String>(); + _replicaGroups.put(replicaGroupId, s); + } + s.add(adapterId); + } + else + { + _adapters.remove(adapterId); + Set<String> s = _replicaGroups.get(replicaGroupId); + if(s != null) + { + s.remove(adapterId); + if(s.isEmpty()) + { + _replicaGroups.remove(adapterId); + } + } + } + cb.ice_response(); + } + + public void + setServerProcessProxy_async(Ice.AMD_LocatorRegistry_setServerProcessProxy cb, + String serverId, + Ice.ProcessPrx process, + Ice.Current current) + { + cb.ice_response(); + } + + synchronized Ice.ObjectPrx + findObject(Ice.Identity id) + { + if(id.name.length() == 0) + { + return null; + } + + Ice.ObjectPrx prx = _wellKnownProxy.ice_identity(id); + + List<String> adapterIds = new ArrayList<String>(); + for(String a : _replicaGroups.keySet()) + { + try + { + prx.ice_adapterId(a).ice_ping(); + adapterIds.add(a); + } + catch(Ice.LocalException ex) + { + } + } + if(adapterIds.isEmpty()) + { + for(String a : _adapters.keySet()) + { + try + { + prx.ice_adapterId(a).ice_ping(); + adapterIds.add(a); + } + catch(Ice.LocalException ex) + { + } + } + } + + if(adapterIds.isEmpty()) + { + return null; + } + java.util.Collections.shuffle(adapterIds); + return prx.ice_adapterId(adapterIds.get(0)); + } + + synchronized Ice.ObjectPrx + findAdapter(String adapterId, Ice.BooleanHolder isReplicaGroup) + { + Ice.ObjectPrx proxy = _adapters.get(adapterId); + if(proxy != null) + { + isReplicaGroup.value = false; + return proxy; + } + + Set<String> s = _replicaGroups.get(adapterId); + if(s != null) + { + List<Ice.Endpoint> endpoints = new ArrayList<Ice.Endpoint>(); + Ice.ObjectPrx prx = null; + for(String a : s) + { + proxy = _adapters.get(a); + if(proxy == null) + { + continue; // TODO: Inconsistency + } + + if(prx == null) + { + prx = proxy; + } + + endpoints.addAll(java.util.Arrays.asList(proxy.ice_getEndpoints())); + } + + if(prx != null) + { + isReplicaGroup.value = true; + return prx.ice_endpoints(endpoints.toArray(new Ice.Endpoint[endpoints.size()])); + } + } + isReplicaGroup.value = false; + return null; + } + + final Ice.ObjectPrx _wellKnownProxy; + final Map<String, Ice.ObjectPrx> _adapters = new HashMap<String, Ice.ObjectPrx>(); + final Map<String, Set<String>> _replicaGroups = new HashMap<String, Set<String>>(); +} + diff --git a/java/src/IceDiscovery/LookupI.java b/java/src/IceDiscovery/LookupI.java new file mode 100644 index 00000000000..7d696190614 --- /dev/null +++ b/java/src/IceDiscovery/LookupI.java @@ -0,0 +1,340 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 IceDiscovery; + +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +class LookupI extends _LookupDisp +{ + abstract private class Request<T, AmdCB> implements IceInternal.TimerTask + { + public Request(T id, int retryCount) + { + _id = id; + _nRetry = retryCount; + } + + public T + getId() + { + return _id; + } + + public boolean + addCallback(AmdCB cb) + { + _callbacks.add(cb); + return _callbacks.size() == 1; + } + + public boolean + retry() + { + return --_nRetry >= 0; + } + + protected int _nRetry; + protected List<AmdCB> _callbacks = new ArrayList<AmdCB>(); + private T _id; + }; + + private class AdapterRequest extends Request<String, Ice.AMD_Locator_findAdapterById> + { + public AdapterRequest(String id, int retryCount) + { + super(id, retryCount); + _start = System.nanoTime(); + _latency = 0; + } + + public boolean + retry() + { + return _proxies.size() == 0 && --_nRetry >= 0; + } + + public boolean + response(Ice.ObjectPrx proxy, boolean isReplicaGroup) + { + if(isReplicaGroup) + { + _proxies.add(proxy); + if(_latency == 0) + { + _latency = (long)((System.nanoTime() - _start) * _latencyMultiplier / 100000.0); + if(_latency == 0) + { + _latency = 1; // 1ms + } + _timer.cancel(this); + _timer.schedule(this, _latency); + } + return false; + } + finished(proxy); + return true; + } + + public void + finished(Ice.ObjectPrx proxy) + { + if(proxy != null || _proxies.isEmpty()) + { + sendResponse(proxy); + return; + } + else if(_proxies.size() == 1) + { + sendResponse(_proxies.get(0)); + return; + } + + List<Ice.Endpoint> endpoints = new ArrayList<Ice.Endpoint>(); + Ice.ObjectPrx result = null; + for(Ice.ObjectPrx prx : _proxies) + { + if(result == null) + { + result = prx; + } + endpoints.addAll(java.util.Arrays.asList(prx.ice_getEndpoints())); + } + sendResponse(result.ice_endpoints(endpoints.toArray(new Ice.Endpoint[endpoints.size()]))); + } + + public void + runTimerTask() + { + adapterRequestTimedOut(this); + } + + private void + sendResponse(Ice.ObjectPrx proxy) + { + for(Ice.AMD_Locator_findAdapterById cb : _callbacks) + { + cb.ice_response(proxy); + } + _callbacks.clear(); + } + + private List<Ice.ObjectPrx> _proxies = new ArrayList<Ice.ObjectPrx>(); + private long _start; + private long _latency; + }; + + private class ObjectRequest extends Request<Ice.Identity, Ice.AMD_Locator_findObjectById> + { + public + ObjectRequest(Ice.Identity id, int retryCount) + { + super(id, retryCount); + } + + public void + response(Ice.ObjectPrx proxy) + { + finished(proxy); + } + + public void + finished(Ice.ObjectPrx proxy) + { + for(Ice.AMD_Locator_findObjectById cb : _callbacks) + { + cb.ice_response(proxy); + } + _callbacks.clear(); + } + + public void runTimerTask() + { + objectRequestTimedOut(this); + } + }; + + public LookupI(LocatorRegistryI registry, LookupPrx lookup, Ice.Properties properties) + { + _registry = registry; + _lookup = lookup; + _timeout = properties.getPropertyAsIntWithDefault("IceDiscovery.Timeout", 300); + _retryCount = properties.getPropertyAsIntWithDefault("IceDiscovery.RetryCount", 3); + _latencyMultiplier = properties.getPropertyAsIntWithDefault("IceDiscovery.LatencyMultiplier", 1); + _domainId = properties.getProperty("IceDiscovery.DomainId"); + _timer = IceInternal.Util.getInstance(lookup.ice_getCommunicator()).timer(); + } + + void + setLookupReply(LookupReplyPrx lookupReply) + { + _lookupReply = lookupReply; + } + + public void + findObjectById(String domainId, Ice.Identity id, IceDiscovery.LookupReplyPrx reply, Ice.Current c) + { + if(!domainId.equals(_domainId)) + { + return; // Ignore. + } + + Ice.ObjectPrx proxy = _registry.findObject(id); + if(proxy != null) + { + // + // Reply to the mulicast request using the given proxy. + // + reply.begin_foundObjectById(id, proxy); + } + } + + public void + findAdapterById(String domainId, String adapterId, IceDiscovery.LookupReplyPrx reply, Ice.Current c) + { + if(!domainId.equals(_domainId)) + { + return; // Ignore. + } + + Ice.BooleanHolder isReplicaGroup = new Ice.BooleanHolder(); + Ice.ObjectPrx proxy = _registry.findAdapter(adapterId, isReplicaGroup); + if(proxy != null) + { + // + // Reply to the multicast request using the given proxy. + // + reply.begin_foundAdapterById(adapterId, proxy, isReplicaGroup.value); + } + } + + synchronized void + findObject(Ice.AMD_Locator_findObjectById cb, Ice.Identity id) + { + ObjectRequest request = _objectRequests.get(id); + if(request == null) + { + request = new ObjectRequest(id, _retryCount); + _objectRequests.put(id, request); + } + + if(request.addCallback(cb)) + { + _lookup.findObjectById(_domainId, id, _lookupReply); + _timer.schedule(request, _timeout); + } + } + + synchronized void + findAdapter(Ice.AMD_Locator_findAdapterById cb, String adapterId) + { + AdapterRequest request = _adapterRequests.get(adapterId); + if(request == null) + { + request = new AdapterRequest(adapterId, _retryCount); + _adapterRequests.put(adapterId, request); + } + + if(request.addCallback(cb)) + { + _lookup.findAdapterById(_domainId, adapterId, _lookupReply); + _timer.schedule(request, _timeout); + } + } + + synchronized void + foundObject(Ice.Identity id, Ice.ObjectPrx proxy) + { + ObjectRequest request = _objectRequests.get(id); + if(request == null) + { + return; + } + + request.response(proxy); + _timer.cancel(request); + _objectRequests.remove(id); + } + + synchronized void + foundAdapter(String adapterId, Ice.ObjectPrx proxy, boolean isReplicaGroup) + { + AdapterRequest request = _adapterRequests.get(adapterId); + if(request == null) + { + return; + } + + if(request.response(proxy, isReplicaGroup)) + { + _timer.cancel(request); + _adapterRequests.remove(adapterId); + } + } + + synchronized void + objectRequestTimedOut(ObjectRequest request) + { + ObjectRequest r = _objectRequests.get(request.getId()); + if(r == null || request != r) + { + return; + } + + if(request.retry()) + { + _lookup.findObjectById(_domainId, request.getId(), _lookupReply); + _timer.schedule(request, _timeout); + } + else + { + request.finished(null); + _objectRequests.remove(request.getId()); + } + } + + synchronized void + adapterRequestTimedOut(AdapterRequest request) + { + AdapterRequest r = _adapterRequests.get(request.getId()); + if(r == null || r != request) + { + return; + } + + if(request.retry()) + { + _lookup.findAdapterById(_domainId, request.getId(), _lookupReply); + _timer.schedule(request, _timeout); + } + else + { + request.finished(null); + _adapterRequests.remove(request.getId()); + } + } + + private LocatorRegistryI _registry; + private final LookupPrx _lookup; + private LookupReplyPrx _lookupReply; + private final int _timeout; + private final int _retryCount; + private final int _latencyMultiplier; + private final String _domainId; + + private final IceInternal.Timer _timer; + + private Map<Ice.Identity, ObjectRequest> _objectRequests = new HashMap<Ice.Identity, ObjectRequest>(); + private Map<String, AdapterRequest> _adapterRequests = new HashMap<String, AdapterRequest>(); + +} + diff --git a/java/src/IceDiscovery/LookupReplyI.java b/java/src/IceDiscovery/LookupReplyI.java new file mode 100644 index 00000000000..70397fc4c3e --- /dev/null +++ b/java/src/IceDiscovery/LookupReplyI.java @@ -0,0 +1,33 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 IceDiscovery; + +class LookupReplyI extends _LookupReplyDisp +{ + public LookupReplyI(LookupI lookup) + { + _lookup = lookup; + } + + public void + foundObjectById(Ice.Identity id, Ice.ObjectPrx proxy, Ice.Current current) + { + _lookup.foundObject(id, proxy); + } + + public void + foundAdapterById(String adapterId, Ice.ObjectPrx proxy, boolean isReplicaGroup, Ice.Current current) + { + _lookup.foundAdapter(adapterId, proxy, isReplicaGroup); + } + + private final LookupI _lookup; +} + diff --git a/java/src/IceDiscovery/PluginFactory.java b/java/src/IceDiscovery/PluginFactory.java new file mode 100644 index 00000000000..e400e885d7b --- /dev/null +++ b/java/src/IceDiscovery/PluginFactory.java @@ -0,0 +1,19 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 IceDiscovery; + +public class PluginFactory implements Ice.PluginFactory +{ + public Ice.Plugin + create(Ice.Communicator communicator, String name, String[] args) + { + return new PluginI(communicator); + } +} diff --git a/java/src/IceDiscovery/PluginI.java b/java/src/IceDiscovery/PluginI.java new file mode 100644 index 00000000000..f11f0f366cf --- /dev/null +++ b/java/src/IceDiscovery/PluginI.java @@ -0,0 +1,128 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 IceDiscovery; + +public class PluginI implements Ice.Plugin +{ + public + PluginI(Ice.Communicator communicator) + { + _communicator = communicator; + + Ice.InitializationData initData = new Ice.InitializationData(); + initData.properties = communicator.getProperties()._clone(); + initData.properties.setProperty("Ice.Default.CollocationOptimized", "0"); + java.util.Map<String, String> props = initData.properties.getPropertiesForPrefix("Ice.Plugin."); + for(String key : props.keySet()) + { + initData.properties.setProperty(key, ""); + } + _pluginCommunicator = Ice.Util.initialize(initData); + } + + public void + initialize() + { + Ice.Properties properties = _pluginCommunicator.getProperties(); + + boolean ipv4 = properties.getPropertyAsIntWithDefault("Ice.IPv4", 1) > 0; + String address; + if(ipv4) + { + address = properties.getPropertyWithDefault("IceDiscovery.Address", "239.255.0.1"); + } + else + { + address = properties.getPropertyWithDefault("IceDiscovery.Address", "ff15::1"); + } + int port = properties.getPropertyAsIntWithDefault("IceDiscovery.Port", 4061); + String intf = properties.getProperty("IceDiscovery.Interface"); + + if(properties.getProperty("IceDiscovery.Multicast.Endpoints").isEmpty()) + { + StringBuilder s = new StringBuilder(); + s.append("udp -h \"").append(address).append("\" -p ").append(port); + if(!intf.isEmpty()) + { + s.append(" --interface \"").append(intf).append("\""); + } + properties.setProperty("IceDiscovery.Multicast.Endpoints", s.toString()); + } + if(properties.getProperty("IceDiscovery.Reply.Endpoints").isEmpty()) + { + StringBuilder s = new StringBuilder(); + s.append("udp"); + if(!intf.isEmpty()) + { + s.append(" -h \"").append(intf).append("\""); + } + properties.setProperty("IceDiscovery.Reply.Endpoints", s.toString()); + } + if(properties.getProperty("IceDiscovery.Locator.Endpoints").isEmpty()) + { + properties.setProperty("IceDiscovery.Locator.Endpoints", "tcp -h 127.0.0.1"); + } + + Ice.ObjectAdapter multicastAdapter = _pluginCommunicator.createObjectAdapter("IceDiscovery.Multicast"); + Ice.ObjectAdapter replyAdapter = _pluginCommunicator.createObjectAdapter("IceDiscovery.Reply"); + Ice.ObjectAdapter locatorAdapter = _pluginCommunicator.createObjectAdapter("IceDiscovery.Locator"); + + // + // Setup locatory registry. + // + LocatorRegistryI locatorRegistry = new LocatorRegistryI(_communicator); + Ice.LocatorRegistryPrx locatorRegistryPrx = Ice.LocatorRegistryPrxHelper.uncheckedCast( + locatorAdapter.addWithUUID(locatorRegistry)); + + String lookupEndpoints = properties.getProperty("IceDiscovery.Lookup"); + if(lookupEndpoints.isEmpty()) + { + StringBuilder s = new StringBuilder(); + s.append("udp -h \"").append(address).append("\" -p ").append(port); + if(!intf.isEmpty()) + { + s.append(" --interface \"").append(intf).append("\""); + } + lookupEndpoints = s.toString(); + } + + Ice.ObjectPrx lookupPrx = _pluginCommunicator.stringToProxy("IceDiscovery/Lookup -d:" + lookupEndpoints); + lookupPrx = lookupPrx.ice_collocationOptimized(false); + + // + // Add lookup and lookup reply Ice objects + // + LookupI lookup = new LookupI(locatorRegistry, LookupPrxHelper.uncheckedCast(lookupPrx), properties); + multicastAdapter.add(lookup, _pluginCommunicator.stringToIdentity("IceDiscovery/Lookup")); + + Ice.ObjectPrx lookupReply = replyAdapter.addWithUUID(new LookupReplyI(lookup)).ice_datagram(); + lookup.setLookupReply(LookupReplyPrxHelper.uncheckedCast(lookupReply)); + + // + // Setup locator on the communicator. + // + Ice.ObjectPrx locator = locatorAdapter.addWithUUID(new LocatorI(lookup, locatorRegistryPrx)); + _communicator.setDefaultLocator( + Ice.LocatorPrxHelper.uncheckedCast(_communicator.stringToProxy(locator.toString()))); + + multicastAdapter.activate(); + replyAdapter.activate(); + locatorAdapter.activate(); + } + + public void + destroy() + { + _pluginCommunicator.destroy(); + } + + private Ice.Communicator _communicator; + private Ice.Communicator _pluginCommunicator; +} diff --git a/java/src/IceInternal/EndpointFactoryManager.java b/java/src/IceInternal/EndpointFactoryManager.java index a8bd6fcb836..94f61fbb873 100644 --- a/java/src/IceInternal/EndpointFactoryManager.java +++ b/java/src/IceInternal/EndpointFactoryManager.java @@ -126,7 +126,6 @@ public final class EndpointFactoryManager read(BasicStream s) { short type = s.readShort(); - for(int i = 0; i < _factories.size(); i++) { EndpointFactory f = _factories.get(i); diff --git a/java/src/IceInternal/LocatorInfo.java b/java/src/IceInternal/LocatorInfo.java index 5d2f0b93865..39091b78fad 100644 --- a/java/src/IceInternal/LocatorInfo.java +++ b/java/src/IceInternal/LocatorInfo.java @@ -112,7 +112,7 @@ public final class LocatorInfo if(!_sent) { _sent = true; - send(true); + send(); } } } @@ -129,7 +129,7 @@ public final class LocatorInfo if(!_sent) { _sent = true; - send(true); + send(); } while(!_response && _exception == null) @@ -201,12 +201,6 @@ public final class LocatorInfo protected void exception(Exception ex) { - if(ex instanceof Ice.CollocationOptimizationException) - { - send(false); // Use synchronous collocation optimized locator request instead. - return; - } - synchronized(this) { _locatorInfo.finishRequest(_ref, _wellKnownRefs, null, ex instanceof Ice.UserException); @@ -219,7 +213,7 @@ public final class LocatorInfo } } - protected abstract void send(boolean async); + protected abstract void send(); final protected LocatorInfo _locatorInfo; final protected Reference _ref; @@ -241,38 +235,32 @@ public final class LocatorInfo } protected void - send(boolean async) + send() { try { - if(async) - { - _locatorInfo.getLocator().begin_findObjectById(_ref.getIdentity(), - new Ice.Callback_Locator_findObjectById() + _locatorInfo.getLocator().begin_findObjectById( + _ref.getIdentity(), + new Ice.Callback_Locator_findObjectById() + { + public void + response(Ice.ObjectPrx proxy) { - public void - response(Ice.ObjectPrx proxy) - { - ObjectRequest.this.response(proxy); - } - - public void - exception(Ice.UserException ex) - { - ObjectRequest.this.exception(ex); - } - - public void - exception(Ice.LocalException ex) - { - ObjectRequest.this.exception(ex); - } - }); - } - else - { - response(_locatorInfo.getLocator().findObjectById(_ref.getIdentity())); - } + ObjectRequest.this.response(proxy); + } + + public void + exception(Ice.UserException ex) + { + ObjectRequest.this.exception(ex); + } + + public void + exception(Ice.LocalException ex) + { + ObjectRequest.this.exception(ex); + } + }); } catch(Exception ex) { @@ -290,38 +278,32 @@ public final class LocatorInfo } protected void - send(boolean async) + send() { try { - if(async) - { - _locatorInfo.getLocator().begin_findAdapterById(_ref.getAdapterId(), - new Ice.Callback_Locator_findAdapterById() + _locatorInfo.getLocator().begin_findAdapterById( + _ref.getAdapterId(), + new Ice.Callback_Locator_findAdapterById() + { + public void + response(Ice.ObjectPrx proxy) { - public void - response(Ice.ObjectPrx proxy) - { - AdapterRequest.this.response(proxy); - } - - public void - exception(Ice.UserException ex) - { - AdapterRequest.this.exception(ex); - } - - public void - exception(Ice.LocalException ex) - { - AdapterRequest.this.exception(ex); - } - }); - } - else - { - response(_locatorInfo.getLocator().findAdapterById(_ref.getAdapterId())); - } + AdapterRequest.this.response(proxy); + } + + public void + exception(Ice.UserException ex) + { + AdapterRequest.this.exception(ex); + } + + public void + exception(Ice.LocalException ex) + { + AdapterRequest.this.exception(ex); + } + }); } catch(Exception ex) { @@ -332,7 +314,7 @@ public final class LocatorInfo LocatorInfo(Ice.LocatorPrx locator, LocatorTable table, boolean background) { - _locator = locator; + _locator = (Ice.LocatorPrx)locator.ice_collocationOptimized(false); _table = table; _background = background; } @@ -389,7 +371,8 @@ public final class LocatorInfo // // Do not make locator calls from within sync. // - Ice.LocatorRegistryPrx locatorRegistry = _locator.getRegistry(); + Ice.LocatorRegistryPrx locatorRegistry = + (Ice.LocatorRegistryPrx)_locator.getRegistry().ice_collocationOptimized(false); synchronized(this) { diff --git a/java/src/IceInternal/PropertyNames.java b/java/src/IceInternal/PropertyNames.java index ea30926a1ff..eee2fcac14a 100644 --- a/java/src/IceInternal/PropertyNames.java +++ b/java/src/IceInternal/PropertyNames.java @@ -8,7 +8,7 @@ // ********************************************************************** ///* jshint -W044*/ -// Generated by makeprops.py from file ../config/PropertyNames.xml, Mon Apr 7 14:21:14 2014 +// Generated by makeprops.py from file ./config/PropertyNames.xml, Tue Apr 22 17:39:32 2014 // IMPORTANT: Do not edit this file -- any edits made here will be lost! @@ -173,6 +173,112 @@ public final class PropertyNames null }; + public static final Property IceDiscoveryProps[] = + { + new Property("IceDiscovery\\.Multicast\\.ACM", false, null), + new Property("IceDiscovery\\.Multicast\\.AdapterId", false, null), + new Property("IceDiscovery\\.Multicast\\.Endpoints", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.EndpointSelection", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.ConnectionCached", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.PreferSecure", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.LocatorCacheTimeout", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.Locator", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.Router", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.CollocationOptimized", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator\\.Context\\.[^\\s]+", false, null), + new Property("IceDiscovery\\.Multicast\\.Locator", false, null), + new Property("IceDiscovery\\.Multicast\\.PublishedEndpoints", false, null), + new Property("IceDiscovery\\.Multicast\\.ReplicaGroupId", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.EndpointSelection", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.ConnectionCached", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.PreferSecure", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.LocatorCacheTimeout", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.Locator", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.Router", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.CollocationOptimized", false, null), + new Property("IceDiscovery\\.Multicast\\.Router\\.Context\\.[^\\s]+", false, null), + new Property("IceDiscovery\\.Multicast\\.Router", false, null), + new Property("IceDiscovery\\.Multicast\\.ProxyOptions", false, null), + new Property("IceDiscovery\\.Multicast\\.ThreadPool\\.Size", false, null), + new Property("IceDiscovery\\.Multicast\\.ThreadPool\\.SizeMax", false, null), + new Property("IceDiscovery\\.Multicast\\.ThreadPool\\.SizeWarn", false, null), + new Property("IceDiscovery\\.Multicast\\.ThreadPool\\.StackSize", false, null), + new Property("IceDiscovery\\.Multicast\\.ThreadPool\\.Serialize", false, null), + new Property("IceDiscovery\\.Multicast\\.ThreadPool\\.ThreadIdleTime", false, null), + new Property("IceDiscovery\\.Multicast\\.ThreadPool\\.ThreadPriority", false, null), + new Property("IceDiscovery\\.Reply\\.ACM", false, null), + new Property("IceDiscovery\\.Reply\\.AdapterId", false, null), + new Property("IceDiscovery\\.Reply\\.Endpoints", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.EndpointSelection", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.ConnectionCached", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.PreferSecure", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.LocatorCacheTimeout", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.Locator", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.Router", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.CollocationOptimized", false, null), + new Property("IceDiscovery\\.Reply\\.Locator\\.Context\\.[^\\s]+", false, null), + new Property("IceDiscovery\\.Reply\\.Locator", false, null), + new Property("IceDiscovery\\.Reply\\.PublishedEndpoints", false, null), + new Property("IceDiscovery\\.Reply\\.ReplicaGroupId", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.EndpointSelection", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.ConnectionCached", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.PreferSecure", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.LocatorCacheTimeout", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.Locator", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.Router", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.CollocationOptimized", false, null), + new Property("IceDiscovery\\.Reply\\.Router\\.Context\\.[^\\s]+", false, null), + new Property("IceDiscovery\\.Reply\\.Router", false, null), + new Property("IceDiscovery\\.Reply\\.ProxyOptions", false, null), + new Property("IceDiscovery\\.Reply\\.ThreadPool\\.Size", false, null), + new Property("IceDiscovery\\.Reply\\.ThreadPool\\.SizeMax", false, null), + new Property("IceDiscovery\\.Reply\\.ThreadPool\\.SizeWarn", false, null), + new Property("IceDiscovery\\.Reply\\.ThreadPool\\.StackSize", false, null), + new Property("IceDiscovery\\.Reply\\.ThreadPool\\.Serialize", false, null), + new Property("IceDiscovery\\.Reply\\.ThreadPool\\.ThreadIdleTime", false, null), + new Property("IceDiscovery\\.Reply\\.ThreadPool\\.ThreadPriority", false, null), + new Property("IceDiscovery\\.Locator\\.ACM", false, null), + new Property("IceDiscovery\\.Locator\\.AdapterId", false, null), + new Property("IceDiscovery\\.Locator\\.Endpoints", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.EndpointSelection", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.ConnectionCached", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.PreferSecure", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.LocatorCacheTimeout", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.Locator", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.Router", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.CollocationOptimized", false, null), + new Property("IceDiscovery\\.Locator\\.Locator\\.Context\\.[^\\s]+", false, null), + new Property("IceDiscovery\\.Locator\\.Locator", false, null), + new Property("IceDiscovery\\.Locator\\.PublishedEndpoints", false, null), + new Property("IceDiscovery\\.Locator\\.ReplicaGroupId", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.EndpointSelection", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.ConnectionCached", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.PreferSecure", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.LocatorCacheTimeout", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.Locator", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.Router", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.CollocationOptimized", false, null), + new Property("IceDiscovery\\.Locator\\.Router\\.Context\\.[^\\s]+", false, null), + new Property("IceDiscovery\\.Locator\\.Router", false, null), + new Property("IceDiscovery\\.Locator\\.ProxyOptions", false, null), + new Property("IceDiscovery\\.Locator\\.ThreadPool\\.Size", false, null), + new Property("IceDiscovery\\.Locator\\.ThreadPool\\.SizeMax", false, null), + new Property("IceDiscovery\\.Locator\\.ThreadPool\\.SizeWarn", false, null), + new Property("IceDiscovery\\.Locator\\.ThreadPool\\.StackSize", false, null), + new Property("IceDiscovery\\.Locator\\.ThreadPool\\.Serialize", false, null), + new Property("IceDiscovery\\.Locator\\.ThreadPool\\.ThreadIdleTime", false, null), + new Property("IceDiscovery\\.Locator\\.ThreadPool\\.ThreadPriority", false, null), + new Property("IceDiscovery\\.Lookup", false, null), + new Property("IceDiscovery\\.Timeout", false, null), + new Property("IceDiscovery\\.RetryCount", false, null), + new Property("IceDiscovery\\.LatencyMultiplier", false, null), + new Property("IceDiscovery\\.Address", false, null), + new Property("IceDiscovery\\.Port", false, null), + new Property("IceDiscovery\\.Interface", false, null), + new Property("IceDiscovery\\.DomainId", false, null), + null + }; + public static final Property IceBoxProps[] = { new Property("IceBox\\.InheritProperties", false, null), @@ -826,6 +932,7 @@ public final class PropertyNames { IceProps, IceMXProps, + IceDiscoveryProps, IceBoxProps, IceBoxAdminProps, IceGridAdminProps, @@ -843,6 +950,7 @@ public final class PropertyNames { "Ice", "IceMX", + "IceDiscovery", "IceBox", "IceBoxAdmin", "IceGridAdmin", diff --git a/java/src/IceInternal/UdpTransceiver.java b/java/src/IceInternal/UdpTransceiver.java index 9eb01362d16..5b8e3105c1c 100644 --- a/java/src/IceInternal/UdpTransceiver.java +++ b/java/src/IceInternal/UdpTransceiver.java @@ -563,9 +563,14 @@ final class UdpTransceiver implements Transceiver intf = java.net.NetworkInterface.getByName(interfaceAddr); if(intf == null) { - java.net.InetSocketAddress addr = Network.getAddressForServer(interfaceAddr, 0, - Network.EnableIPv4, false); - intf = java.net.NetworkInterface.getByInetAddress(addr.getAddress()); + try + { + intf = java.net.NetworkInterface.getByInetAddress( + java.net.InetAddress.getByName(interfaceAddr)); + } + catch(Exception ex) + { + } } } diff --git a/java/test/Ice/proxy/AllTests.java b/java/test/Ice/proxy/AllTests.java index de50e7bf99d..34361a35d3f 100644 --- a/java/test/Ice/proxy/AllTests.java +++ b/java/test/Ice/proxy/AllTests.java @@ -412,7 +412,8 @@ public class AllTests test(proxyProps.get("Test.Locator").equals( "locator -t -e " + Ice.Util.encodingVersionToString(Ice.Util.currentEncoding()))); - test(proxyProps.get("Test.Locator.CollocationOptimized").equals("1")); + // Locator collocation optimization is always disabled. + //test(proxyProps.get("Test.Locator.CollocationOptimized").equals("1")); test(proxyProps.get("Test.Locator.ConnectionCached").equals("0")); test(proxyProps.get("Test.Locator.PreferSecure").equals("1")); test(proxyProps.get("Test.Locator.EndpointSelection").equals("Random")); diff --git a/java/test/IceDiscovery/simple/AllTests.java b/java/test/IceDiscovery/simple/AllTests.java new file mode 100644 index 00000000000..97546028b59 --- /dev/null +++ b/java/test/IceDiscovery/simple/AllTests.java @@ -0,0 +1,220 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 test.IceDiscovery.simple; + +import test.IceDiscovery.simple.Test.*; + +import java.util.List; +import java.util.ArrayList; + +import java.util.Set; +import java.util.HashSet; + +public class AllTests +{ + private static void + test(boolean b) + { + if(!b) + { + throw new RuntimeException(); + } + } + + public static void + allTests(Ice.Communicator communicator, int num) + { + List<ControllerPrx> proxies = new ArrayList<ControllerPrx>(); + List<ControllerPrx> indirectProxies = new ArrayList<ControllerPrx>(); + for(int i = 0; i < num; ++i) + { + String id = "controller" + i; + proxies.add(ControllerPrxHelper.uncheckedCast(communicator.stringToProxy(id))); + indirectProxies.add(ControllerPrxHelper.uncheckedCast(communicator.stringToProxy(id + "@control" + i))); + } + + System.out.print("testing indirect proxies... "); + System.out.flush(); + { + for(ControllerPrx prx : indirectProxies) + { + prx.ice_ping(); + } + } + System.out.println("ok"); + + System.out.print("testing well-known proxies... "); + System.out.flush(); + { + for(ControllerPrx prx : proxies) + { + prx.ice_ping(); + } + } + System.out.println("ok"); + + System.out.print("testing object adapter registration... "); + System.out.flush(); + { + try + { + communicator.stringToProxy("object @ oa1").ice_ping(); + } + catch(Ice.NoEndpointException ex) + { + } + + proxies.get(0).activateObjectAdapter("oa", "oa1", ""); + + try + { + communicator.stringToProxy("object @ oa1").ice_ping(); + } + catch(Ice.ObjectNotExistException ex) + { + } + + proxies.get(0).deactivateObjectAdapter("oa"); + + try + { + communicator.stringToProxy("object @ oa1").ice_ping(); + } + catch(Ice.NoEndpointException ex) + { + } + } + System.out.println("ok"); + + System.out.print("testing object adapter migration..."); + System.out.flush(); + { + proxies.get(0).activateObjectAdapter("oa", "oa1", ""); + proxies.get(0).addObject("oa", "object"); + communicator.stringToProxy("object @ oa1").ice_ping(); + proxies.get(0).removeObject("oa", "object"); + proxies.get(0).deactivateObjectAdapter("oa"); + + proxies.get(1).activateObjectAdapter("oa", "oa1", ""); + proxies.get(1).addObject("oa", "object"); + communicator.stringToProxy("object @ oa1").ice_ping(); + proxies.get(1).removeObject("oa", "object"); + proxies.get(1).deactivateObjectAdapter("oa"); + } + System.out.println("ok"); + + System.out.print("testing object migration..."); + System.out.flush(); + { + proxies.get(0).activateObjectAdapter("oa", "oa1", ""); + proxies.get(1).activateObjectAdapter("oa", "oa2", ""); + + proxies.get(0).addObject("oa", "object"); + communicator.stringToProxy("object @ oa1").ice_ping(); + communicator.stringToProxy("object").ice_ping(); + proxies.get(0).removeObject("oa", "object"); + + proxies.get(1).addObject("oa", "object"); + communicator.stringToProxy("object @ oa2").ice_ping(); + communicator.stringToProxy("object").ice_ping(); + proxies.get(1).removeObject("oa", "object"); + + try + { + communicator.stringToProxy("object @ oa1").ice_ping(); + } + catch(Ice.ObjectNotExistException ex) + { + } + try + { + communicator.stringToProxy("object @ oa2").ice_ping(); + } + catch(Ice.ObjectNotExistException ex) + { + } + + proxies.get(0).deactivateObjectAdapter("oa"); + proxies.get(1).deactivateObjectAdapter("oa"); + } + System.out.println("ok"); + + System.out.print("testing replica groups..."); + System.out.flush(); + { + proxies.get(0).activateObjectAdapter("oa", "oa1", "rg"); + proxies.get(1).activateObjectAdapter("oa", "oa2", "rg"); + proxies.get(2).activateObjectAdapter("oa", "oa3", "rg"); + + proxies.get(0).addObject("oa", "object"); + proxies.get(1).addObject("oa", "object"); + proxies.get(2).addObject("oa", "object"); + + communicator.stringToProxy("object @ oa1").ice_ping(); + communicator.stringToProxy("object @ oa2").ice_ping(); + communicator.stringToProxy("object @ oa3").ice_ping(); + + communicator.stringToProxy("object @ rg").ice_ping(); + + Set<String> adapterIds = new HashSet<String>(); + adapterIds.add("oa1"); + adapterIds.add("oa2"); + adapterIds.add("oa3"); + TestIntfPrx intf = TestIntfPrxHelper.uncheckedCast(communicator.stringToProxy("object")); + intf = (TestIntfPrx)intf.ice_connectionCached(false).ice_locatorCacheTimeout(0); + while(!adapterIds.isEmpty()) + { + adapterIds.remove(intf.getAdapterId()); + } + + while(true) + { + adapterIds.add("oa1"); + adapterIds.add("oa2"); + adapterIds.add("oa3"); + intf = TestIntfPrxHelper.uncheckedCast( + communicator.stringToProxy("object @ rg").ice_connectionCached(false)); + int nRetry = 100; + while(!adapterIds.isEmpty() && --nRetry > 0) + { + adapterIds.remove(intf.getAdapterId()); + } + if(nRetry > 0) + { + break; + } + + // The previous locator lookup probably didn't return all the replicas... try again. + communicator.stringToProxy("object @ rg").ice_locatorCacheTimeout(0).ice_ping(); + } + + proxies.get(0).deactivateObjectAdapter("oa"); + proxies.get(1).deactivateObjectAdapter("oa"); + test(TestIntfPrxHelper.uncheckedCast( + communicator.stringToProxy("object @ rg")).getAdapterId().equals("oa3")); + proxies.get(2).deactivateObjectAdapter("oa"); + + proxies.get(0).activateObjectAdapter("oa", "oa1", "rg"); + proxies.get(0).addObject("oa", "object"); + test(TestIntfPrxHelper.uncheckedCast( + communicator.stringToProxy("object @ rg")).getAdapterId().equals("oa1")); + proxies.get(0).deactivateObjectAdapter("oa"); + } + System.out.println("ok"); + + System.out.print("shutting down... "); + System.out.flush(); + for(ControllerPrx prx : proxies) + { + prx.shutdown(); + } + System.out.println("ok"); + } +} diff --git a/java/test/IceDiscovery/simple/Client.java b/java/test/IceDiscovery/simple/Client.java new file mode 100644 index 00000000000..fda8e46a37d --- /dev/null +++ b/java/test/IceDiscovery/simple/Client.java @@ -0,0 +1,37 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 test.IceDiscovery.simple; + +public class Client extends test.Util.Application +{ + public int run(String[] args) + { + int num; + try + { + num = args.length == 1 ? Integer.parseInt(args[0]) : 0; + } + catch(NumberFormatException ex) + { + num = 0; + } + AllTests.allTests(communicator(), num); + return 0; + } + + public static void main(String[] args) + { + Client c = new Client(); + int status = c.main("Client", args); + + System.gc(); + System.exit(status); + } +} diff --git a/java/test/IceDiscovery/simple/ControllerI.java b/java/test/IceDiscovery/simple/ControllerI.java new file mode 100644 index 00000000000..1c8c2d837c7 --- /dev/null +++ b/java/test/IceDiscovery/simple/ControllerI.java @@ -0,0 +1,61 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 test.IceDiscovery.simple; + +import test.IceDiscovery.simple.Test.*; + +public final class ControllerI extends _ControllerDisp +{ + public void + activateObjectAdapter(String name, String adapterId, String replicaGroupId, Ice.Current current) + { + Ice.Communicator communicator = current.adapter.getCommunicator(); + Ice.Properties properties = communicator.getProperties(); + properties.setProperty(name + ".AdapterId", adapterId); + properties.setProperty(name + ".ReplicaGroupId", replicaGroupId); + properties.setProperty(name + ".Endpoints", "default"); + Ice.ObjectAdapter oa = communicator.createObjectAdapter(name); + _adapters.put(name, oa); + oa.activate(); + } + + public void + deactivateObjectAdapter(String name, Ice.Current current) + { + _adapters.get(name).destroy(); + _adapters.remove(name); + } + + public void + addObject(String oaName, String id, Ice.Current current) + { + assert(_adapters.containsKey(oaName)); + Ice.Identity identity = new Ice.Identity(); + identity.name = id; + _adapters.get(oaName).add(new TestIntfI(), identity); + } + + public void + removeObject(String oaName, String id, Ice.Current current) + { + assert(_adapters.containsKey(oaName)); + Ice.Identity identity = new Ice.Identity(); + identity.name = id; + _adapters.get(oaName).remove(identity); + } + + public void shutdown(Ice.Current current) + { + current.adapter.getCommunicator().shutdown(); + } + + final private java.util.Map<String, Ice.ObjectAdapter> _adapters = + new java.util.HashMap<String, Ice.ObjectAdapter>(); +} diff --git a/java/test/IceDiscovery/simple/Server.java b/java/test/IceDiscovery/simple/Server.java new file mode 100644 index 00000000000..5a671e99003 --- /dev/null +++ b/java/test/IceDiscovery/simple/Server.java @@ -0,0 +1,45 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 test.IceDiscovery.simple; + +public class Server extends test.Util.Application +{ + public int run(String[] args) + { + Ice.Properties properties = communicator().getProperties(); + + int num = 0; + try + { + num = Integer.parseInt(args[0]); + } + catch(NumberFormatException ex) + { + } + + properties.setProperty("ControlAdapter.Endpoints", "default -p " + (12010 + num)); + properties.setProperty("ControlAdapter.AdapterId", "control" + num); + properties.setProperty("ControlAdapter.ThreadPool.Size", "1"); + + Ice.ObjectAdapter adapter = communicator().createObjectAdapter("ControlAdapter"); + adapter.add(new ControllerI(), communicator().stringToIdentity("controller" + num)); + adapter.activate(); + + return WAIT; + } + + public static void main(String[] args) + { + Server c = new Server(); + int status = c.main("Server", args); + System.gc(); + System.exit(status); + } +} diff --git a/java/test/IceDiscovery/simple/Test.ice b/java/test/IceDiscovery/simple/Test.ice new file mode 100644 index 00000000000..03d59e513a7 --- /dev/null +++ b/java/test/IceDiscovery/simple/Test.ice @@ -0,0 +1,33 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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. +// +// ********************************************************************** + +#pragma once + + +[["java:package:test.IceDiscovery.simple"]] +module Test +{ + +interface TestIntf +{ + string getAdapterId(); +}; + +interface Controller +{ + void activateObjectAdapter(string name, string adapterId, string replicaGroupId); + void deactivateObjectAdapter(string name); + + void addObject(string oaName, string id); + void removeObject(string oaName, string id); + + void shutdown(); +}; + +}; diff --git a/java/test/IceDiscovery/simple/TestIntfI.java b/java/test/IceDiscovery/simple/TestIntfI.java new file mode 100644 index 00000000000..fca6235ce6b --- /dev/null +++ b/java/test/IceDiscovery/simple/TestIntfI.java @@ -0,0 +1,21 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2013 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 test.IceDiscovery.simple; + +import test.IceDiscovery.simple.Test.*; + +public final class TestIntfI extends _TestIntfDisp +{ + public String + getAdapterId(Ice.Current current) + { + return current.adapter.getCommunicator().getProperties().getProperty(current.adapter.getName() + ".AdapterId"); + } +} diff --git a/java/test/IceDiscovery/simple/run.py b/java/test/IceDiscovery/simple/run.py new file mode 100755 index 00000000000..d23d4644fae --- /dev/null +++ b/java/test/IceDiscovery/simple/run.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# Copyright (c) 2003-2013 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 os, sys + +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, "scripts", "TestUtil.py")) ] +if len(path) == 0: + raise RuntimeError("can't find toplevel directory!") +sys.path.append(os.path.join(path[0], "scripts")) +import TestUtil + +server = "test.IceDiscovery.simple.Server" +client = "test.IceDiscovery.simple.Client" + +num = 3 + +args = " --Ice.Plugin.IceDiscovery=IceDiscovery:IceDiscovery.PluginFactory" +args += " --IceDiscovery.Timeout=30" +args += " --IceDiscovery.RetryCount=1" +if TestUtil.ipv6: + args += " --IceDiscovery.Interface=\"::1\"" +else: + args += " --IceDiscovery.Interface=127.0.0.1" + +serverProc = [] +for i in range(0, num): + sys.stdout.write("starting server #%d... " % (i + 1)) + sys.stdout.flush() + serverProc.append(TestUtil.startServer(server, "%d %s" % (i, args))) + print("ok") + +sys.stdout.write("starting client... ") +sys.stdout.flush() +clientProc = TestUtil.startClient(client, "%d %s" % (num, args), startReader = False) +print("ok") +clientProc.startReader() + +clientProc.waitTestSuccess() +for p in serverProc: + p.waitTestSuccess() |