summaryrefslogtreecommitdiff
path: root/java/demo/Ice/session
diff options
context:
space:
mode:
Diffstat (limited to 'java/demo/Ice/session')
-rw-r--r--java/demo/Ice/session/Client.java230
-rw-r--r--java/demo/Ice/session/Server.java27
-rw-r--r--java/demo/Ice/session/SessionClient.java239
-rw-r--r--java/demo/Ice/session/SessionServer.java36
4 files changed, 253 insertions, 279 deletions
diff --git a/java/demo/Ice/session/Client.java b/java/demo/Ice/session/Client.java
index 44e8da1b2df..4a2c9cbc35f 100644
--- a/java/demo/Ice/session/Client.java
+++ b/java/demo/Ice/session/Client.java
@@ -9,12 +9,238 @@
import Demo.*;
-public class Client
+public class Client extends Ice.Application
{
+ static private class SessionRefreshThread extends Thread
+ {
+ SessionRefreshThread(Ice.Logger logger, long timeout, SessionPrx session)
+ {
+ _logger = logger;
+ _session = session;
+ _timeout = timeout;
+ }
+
+ synchronized public void
+ run()
+ {
+ while(!_terminated)
+ {
+ try
+ {
+ wait(_timeout);
+ }
+ catch(InterruptedException e)
+ {
+ }
+ if(!_terminated)
+ {
+ try
+ {
+ _session.refresh();
+ }
+ catch(Ice.LocalException ex)
+ {
+ _logger.warning("SessionRefreshThread: " + ex);
+ _terminated = true;
+ }
+ }
+ }
+ }
+
+ synchronized private void
+ terminate()
+ {
+ _terminated = true;
+ notify();
+ }
+
+ final private Ice.Logger _logger;
+ final private SessionPrx _session;
+ final private long _timeout;
+ private boolean _terminated = false;
+ }
+
+ private static void
+ menu()
+ {
+ System.out.println(
+ "usage:\n" +
+ "c: create a new per-client hello object\n" +
+ "0-9: send a greeting to a hello object\n" +
+ "s: shutdown the server and exit\n" +
+ "x: exit\n" +
+ "t: exit without destroying the session\n" +
+ "?: help\n");
+ }
+
+ public int
+ run(String[] args)
+ {
+ java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
+ String name;
+ try
+ {
+ do
+ {
+ System.out.print("Please enter your name ==> ");
+ System.out.flush();
+ name = in.readLine().trim();
+ }
+ while(name.length() == 0);
+ }
+ catch(java.io.IOException ex)
+ {
+ ex.printStackTrace();
+ return 0;
+ }
+
+ Ice.Properties properties = communicator().getProperties();
+ final String proxyProperty = "SessionFactory.Proxy";
+ String proxy = properties.getProperty(proxyProperty);
+ if(proxy.length() == 0)
+ {
+ System.err.println("property `" + proxyProperty + "' not set");
+ return 1;
+ }
+
+ Ice.ObjectPrx base = communicator().stringToProxy(proxy);
+ SessionFactoryPrx factory = SessionFactoryPrxHelper.checkedCast(base);
+ if(factory == null)
+ {
+ System.err.println("invalid proxy");
+ return 1;
+ }
+
+ SessionPrx session = factory.create(name);
+ SessionRefreshThread refresh = new SessionRefreshThread(communicator().getLogger(), 5000, session);
+ refresh.start();
+
+ java.util.ArrayList hellos = new java.util.ArrayList();
+
+ menu();
+
+
+ try
+ {
+ boolean destroy = true;
+ boolean shutdown = false;
+ while(true)
+ {
+ System.out.print("==> ");
+ System.out.flush();
+ String line = in.readLine();
+ if(line == null)
+ {
+ break;
+ }
+ if(line.length() > 0 && Character.isDigit(line.charAt(0)))
+ {
+ int index;
+ try
+ {
+ index = Integer.parseInt(line);
+ }
+ catch(NumberFormatException e)
+ {
+ menu();
+ continue;
+ }
+ if(index < hellos.size())
+ {
+ HelloPrx hello = (HelloPrx)hellos.get(index);
+ hello.sayHello();
+ }
+ else
+ {
+ System.out.println("index is too high. " + hellos.size() + " exist so far. " +
+ "Use 'c' to create a new hello object.");
+ }
+ }
+ else if(line.equals("c"))
+ {
+ hellos.add(session.createHello());
+ System.out.println("created hello object " + (hellos.size()-1));
+ }
+ else if(line.equals("s"))
+ {
+ destroy = false;
+ shutdown = true;
+ break;
+ }
+ else if(line.equals("x"))
+ {
+ break;
+ }
+ else if(line.equals("t"))
+ {
+ destroy = false;
+ break;
+ }
+ else if(line.equals("?"))
+ {
+ menu();
+ }
+ else
+ {
+ System.out.println("unknown command `" + line + "'");
+ menu();
+ }
+ }
+ //
+ // The refresher thread must be terminated before destroy is
+ // called, otherwise it might get ObjectNotExistException. refresh
+ // is set to 0 so that if session->destroy() raises an exception
+ // the thread will not be re-terminated and re-joined.
+ //
+ refresh.terminate();
+ try
+ {
+ refresh.join();
+ }
+ catch(InterruptedException e)
+ {
+ }
+ refresh = null;
+
+ if(destroy)
+ {
+ session.destroy();
+ }
+ if(shutdown)
+ {
+ factory.shutdown();
+ }
+ }
+ catch(java.io.IOException ex)
+ {
+ ex.printStackTrace();
+ }
+ catch(Ice.LocalException ex)
+ {
+ ex.printStackTrace();
+ }
+ finally
+ {
+ if(refresh != null)
+ {
+ refresh.terminate();
+ try
+ {
+ refresh.join();
+ }
+ catch(InterruptedException e)
+ {
+ }
+ }
+ }
+
+ return 0;
+ }
+
public static void
main(String[] args)
{
- SessionClient app = new SessionClient();
+ Client app = new Client();
int status = app.main("Client", args, "config");
System.exit(status);
}
diff --git a/java/demo/Ice/session/Server.java b/java/demo/Ice/session/Server.java
index dadc0a8915a..6bcbc329aa9 100644
--- a/java/demo/Ice/session/Server.java
+++ b/java/demo/Ice/session/Server.java
@@ -9,12 +9,35 @@
import Demo.*;
-public class Server
+public class Server extends Ice.Application
{
+ public int
+ run(String[] args)
+ {
+ Ice.ObjectAdapter adapter = communicator().createObjectAdapter("SessionFactory");
+ ReapThread reaper = new ReapThread();
+ reaper.start();
+
+ adapter.add(new SessionFactoryI(reaper), Ice.Util.stringToIdentity("SessionFactory"));
+ adapter.activate();
+ communicator().waitForShutdown();
+
+ reaper.terminate();
+ try
+ {
+ reaper.join();
+ }
+ catch(InterruptedException e)
+ {
+ }
+
+ return 0;
+ }
+
public static void
main(String[] args)
{
- SessionServer app = new SessionServer();
+ Server app = new Server();
int status = app.main("Server", args, "config");
System.exit(status);
}
diff --git a/java/demo/Ice/session/SessionClient.java b/java/demo/Ice/session/SessionClient.java
deleted file mode 100644
index 848d33f17b9..00000000000
--- a/java/demo/Ice/session/SessionClient.java
+++ /dev/null
@@ -1,239 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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.*;
-
-class SessionClient extends Ice.Application
-{
- static private class SessionRefreshThread extends Thread
- {
- SessionRefreshThread(Ice.Logger logger, long timeout, SessionPrx session)
- {
- _logger = logger;
- _session = session;
- _timeout = timeout;
- }
-
- synchronized public void
- run()
- {
- while(!_terminated)
- {
- try
- {
- wait(_timeout);
- }
- catch(InterruptedException e)
- {
- }
- if(!_terminated)
- {
- try
- {
- _session.refresh();
- }
- catch(Ice.LocalException ex)
- {
- _logger.warning("SessionRefreshThread: " + ex);
- _terminated = true;
- }
- }
- }
- }
-
- synchronized private void
- terminate()
- {
- _terminated = true;
- notify();
- }
-
- final private Ice.Logger _logger;
- final private SessionPrx _session;
- final private long _timeout;
- private boolean _terminated = false;
- }
-
- private static void
- menu()
- {
- System.out.println(
- "usage:\n" +
- "c: create a new per-client hello object\n" +
- "0-9: send a greeting to a hello object\n" +
- "s: shutdown the server and exit\n" +
- "x: exit\n" +
- "t: exit without destroying the session\n" +
- "?: help\n");
- }
-
- public int
- run(String[] args)
- {
- java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
- String name;
- try
- {
- do
- {
- System.out.print("Please enter your name ==> ");
- System.out.flush();
- name = in.readLine().trim();
- }
- while(name.length() == 0);
- }
- catch(java.io.IOException ex)
- {
- ex.printStackTrace();
- return 0;
- }
-
- Ice.Properties properties = communicator().getProperties();
- final String proxyProperty = "SessionFactory.Proxy";
- String proxy = properties.getProperty(proxyProperty);
- if(proxy.length() == 0)
- {
- System.err.println("property `" + proxyProperty + "' not set");
- return 1;
- }
-
- Ice.ObjectPrx base = communicator().stringToProxy(proxy);
- SessionFactoryPrx factory = SessionFactoryPrxHelper.checkedCast(base);
- if(factory == null)
- {
- System.err.println("invalid proxy");
- return 1;
- }
-
- SessionPrx session = factory.create(name);
- SessionRefreshThread refresh = new SessionRefreshThread(communicator().getLogger(), 5000, session);
- refresh.start();
-
- java.util.ArrayList hellos = new java.util.ArrayList();
-
- menu();
-
-
- try
- {
- boolean destroy = true;
- boolean shutdown = false;
- while(true)
- {
- System.out.print("==> ");
- System.out.flush();
- String line = in.readLine();
- if(line == null)
- {
- break;
- }
- if(line.length() > 0 && Character.isDigit(line.charAt(0)))
- {
- int index;
- try
- {
- index = Integer.parseInt(line);
- }
- catch(NumberFormatException e)
- {
- menu();
- continue;
- }
- if(index < hellos.size())
- {
- HelloPrx hello = (HelloPrx)hellos.get(index);
- hello.sayHello();
- }
- else
- {
- System.out.println("index is too high. " + hellos.size() + " exist so far. " +
- "Use 'c' to create a new hello object.");
- }
- }
- else if(line.equals("c"))
- {
- hellos.add(session.createHello());
- System.out.println("created hello object " + (hellos.size()-1));
- }
- else if(line.equals("s"))
- {
- destroy = false;
- shutdown = true;
- break;
- }
- else if(line.equals("x"))
- {
- break;
- }
- else if(line.equals("t"))
- {
- destroy = false;
- break;
- }
- else if(line.equals("?"))
- {
- menu();
- }
- else
- {
- System.out.println("unknown command `" + line + "'");
- menu();
- }
- }
- //
- // The refresher thread must be terminated before destroy is
- // called, otherwise it might get ObjectNotExistException. refresh
- // is set to 0 so that if session->destroy() raises an exception
- // the thread will not be re-terminated and re-joined.
- //
- refresh.terminate();
- try
- {
- refresh.join();
- }
- catch(InterruptedException e)
- {
- }
- refresh = null;
-
- if(destroy)
- {
- session.destroy();
- }
- if(shutdown)
- {
- factory.shutdown();
- }
- }
- catch(java.io.IOException ex)
- {
- ex.printStackTrace();
- }
- catch(Ice.LocalException ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- if(refresh != null)
- {
- refresh.terminate();
- try
- {
- refresh.join();
- }
- catch(InterruptedException e)
- {
- }
- }
- }
-
- return 0;
- }
-}
diff --git a/java/demo/Ice/session/SessionServer.java b/java/demo/Ice/session/SessionServer.java
deleted file mode 100644
index 757fd8a2ead..00000000000
--- a/java/demo/Ice/session/SessionServer.java
+++ /dev/null
@@ -1,36 +0,0 @@
-// **********************************************************************
-//
-// Copyright (c) 2003-2005 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.*;
-
-class SessionServer extends Ice.Application
-{
- public int
- run(String[] args)
- {
- Ice.ObjectAdapter adapter = communicator().createObjectAdapter("SessionFactory");
- ReapThread reaper = new ReapThread();
- reaper.start();
-
- adapter.add(new SessionFactoryI(reaper), Ice.Util.stringToIdentity("SessionFactory"));
- adapter.activate();
- communicator().waitForShutdown();
-
- reaper.terminate();
- try
- {
- reaper.join();
- }
- catch(InterruptedException e)
- {
- }
-
- return 0;
- }
-}