Reset branch to trunk.
[official-gcc.git] / trunk / libjava / classpath / examples / gnu / classpath / examples / CORBA / swing / x5 / OrbStarter.java
blob3852945c6b96cf9c7668427543520c216a66e6ab
1 /* OrbStarter.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.examples.CORBA.swing.x5;
41 import java.rmi.RemoteException;
42 import java.util.Properties;
44 import javax.rmi.PortableRemoteObject;
45 import javax.rmi.CORBA.Tie;
47 import org.omg.CORBA.ORB;
48 import org.omg.PortableServer.POA;
49 import org.omg.PortableServer.POAHelper;
50 import org.omg.PortableServer.Servant;
52 /**
53 * Starts the ORBs, involved into this application.
55 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
57 public class OrbStarter
59 /**
60 * The game manager name server port. This server allows to access the game
61 * manager by host (IP) and port rather than by the rather long IOR string.
63 static int MANAGER_NAMER_PORT = 1500;
65 /**
66 * The used port range (understood and used by GNU Classpath only).
68 static String USED_PORT_RANGE = "1501-1503";
70 /**
71 * Specify the file where under start the game manager writes its IOR.
72 * You may specify the path if the game manager and player clients have
73 * access to some share file system or if you prefer to write IOR to
74 * floppy and then read from the floppy on the client side. Both clients
75 * and server will use this constant. Set to null not to write the IOR.
77 static String WRITE_URL_TO_FILE = "game_manager_ior.txt";
79 /**
80 * Start the manager ORB.
81 * @return the manager URL if it starts.
83 public static String startManager(final String[] args)
85 GameManagerImpl.ior = null;
86 GameManagerImpl.ok = false;
88 final Properties p = new Properties();
89 p.put("gnu.CORBA.ListenerPort", USED_PORT_RANGE);
91 try
93 new Thread()
95 public void run()
97 try
99 GameManagerImpl.orb = ORB.init(args, p);
101 // Obtain the root poa:
102 POA rootPOA = POAHelper.narrow(GameManagerImpl.orb.resolve_initial_references("RootPOA"));
104 GameManagerImpl impl = new GameManagerImpl();
106 PortableRemoteObject.exportObject(impl);
108 // Construct the tie that is also the servant.
109 Tie tie = new _GameManagerImpl_Tie();
111 // Set the invocation target for this tie.
112 tie.setTarget(impl);
114 // Obtain the reference to the corresponding CORBA object:
115 org.omg.CORBA.Object object = rootPOA.servant_to_reference((Servant) tie);
117 GameManagerImpl.ok = true;
119 // Activate the root POA.
120 rootPOA.the_POAManager().activate();
122 // Get the IOR URL that must be passed to clients.
123 GameManagerImpl.ior = GameManagerImpl.orb.object_to_string(object);
125 GameManagerImpl.orb.run();
127 catch (Exception exc)
129 exc.printStackTrace();
130 GameManagerImpl.ior = "Unable to start the ORB: " + exc;
133 }.start();
135 // Wait the thread to enter orb.run.
136 long t = System.currentTimeMillis();
137 while (GameManagerImpl.ior == null
138 && System.currentTimeMillis() - t < 20000)
140 Thread.sleep(100);
143 return GameManagerImpl.ior;
145 catch (Exception e)
147 e.printStackTrace();
148 return "Exception: " + e;
153 * Start the client ORB.
155 public static String startPlayer(final Player player, final PlayingDesk desk)
157 desk.ior = null;
158 desk.ok = false;
160 final Properties p = new Properties();
161 p.put("gnu.CORBA.ListenerPort", USED_PORT_RANGE);
165 new Thread()
167 public void run()
171 desk.orb = ORB.init(new String[0], p);
173 POA rootPOA = POAHelper.narrow(desk.orb.resolve_initial_references("RootPOA"));
174 rootPOA.the_POAManager().activate();
176 // Construct the tie.
177 Tie tie = new _PlayerImpl_Tie();
179 // Set the implementing class (invocation target).
180 tie.setTarget(new PlayerImpl());
182 // Connect the tie as POA servant.
183 org.omg.CORBA.Object object = rootPOA.servant_to_reference((Servant) tie);
185 // Get the stringified reference.
186 desk.ior = desk.orb.object_to_string(object);
188 // Mark that the object was created OK.
189 desk.ok = true;
190 desk.orb.run();
192 catch (Exception exc)
194 exc.printStackTrace();
195 desk.ior = "Unable to start the ORB: " + exc;
198 }.start();
200 long t = System.currentTimeMillis();
201 while (desk.ior == null && System.currentTimeMillis() - t < 20000)
203 Thread.sleep(100);
206 catch (Exception e)
208 e.printStackTrace();
209 return "Exception: " + e;
212 // Add shutdown hook to unregister from the manager.
213 Runtime.getRuntime().addShutdownHook(new Thread()
215 public void run()
217 if (desk.manager != null && player != null)
221 desk.manager.unregister(player);
223 catch (RemoteException ex)
225 // We will print the exception because this is a demo
226 // application that
227 // may be modified for learning purposes.
228 ex.printStackTrace();
230 desk.manager = null;
234 return desk.ior;