OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / CORBA / swing / x5 / OrbStarter.java
1 /* OrbStarter.java --
2  Copyright (C) 2005 Free Software Foundation, Inc.
3
4  This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package gnu.classpath.examples.CORBA.swing.x5;
40
41 import java.rmi.RemoteException;
42 import java.util.Properties;
43
44 import javax.rmi.PortableRemoteObject;
45 import javax.rmi.CORBA.Tie;
46
47 import org.omg.CORBA.ORB;
48 import org.omg.PortableServer.POA;
49 import org.omg.PortableServer.POAHelper;
50 import org.omg.PortableServer.Servant;
51
52 /**
53  * Starts the ORBs, involved into this application.
54  * 
55  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
56  */
57 public class OrbStarter
58 {
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.
62    */
63   static int MANAGER_NAMER_PORT = 1500;
64           
65   /**
66    * The used port range (understood and used by GNU Classpath only).
67    */
68   static String USED_PORT_RANGE = "1501-1503";
69           
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.
76    */
77   static String WRITE_URL_TO_FILE = "game_manager_ior.txt";     
78         
79   /**
80    * Start the manager ORB.
81    * @return the manager URL if it starts.
82    */
83   public static String startManager(final String[] args)
84   {
85     GameManagerImpl.ior = null;
86     GameManagerImpl.ok = false;
87
88     final Properties p = new Properties();
89     p.put("gnu.CORBA.ListenerPort", USED_PORT_RANGE);
90
91     try
92       {
93         new Thread()
94         {
95           public void run()
96           {
97             try
98               {
99                 GameManagerImpl.orb = ORB.init(args, p);
100
101                 // Obtain the root poa:
102                 POA rootPOA = POAHelper.narrow(GameManagerImpl.orb.resolve_initial_references("RootPOA"));
103
104                 GameManagerImpl impl = new GameManagerImpl();
105
106                 PortableRemoteObject.exportObject(impl);
107
108                 // Construct the tie that is also the servant.
109                 Tie tie = new _GameManagerImpl_Tie();
110
111                 // Set the invocation target for this tie.
112                 tie.setTarget(impl);
113
114                 // Obtain the reference to the corresponding CORBA object:
115                 org.omg.CORBA.Object object = rootPOA.servant_to_reference((Servant) tie);
116
117                 GameManagerImpl.ok = true;
118
119                 // Activate the root POA.
120                 rootPOA.the_POAManager().activate();
121
122                 // Get the IOR URL that must be passed to clients.
123                 GameManagerImpl.ior = GameManagerImpl.orb.object_to_string(object);
124
125                 GameManagerImpl.orb.run();
126               }
127             catch (Exception exc)
128               {
129                 exc.printStackTrace();
130                 GameManagerImpl.ior = "Unable to start the ORB: " + exc;
131               }
132           }
133         }.start();
134
135         // Wait the thread to enter orb.run.
136         long t = System.currentTimeMillis();
137         while (GameManagerImpl.ior == null
138           && System.currentTimeMillis() - t < 20000)
139           {
140             Thread.sleep(100);
141           }
142
143         return GameManagerImpl.ior;
144       }
145     catch (Exception e)
146       {
147         e.printStackTrace();
148         return "Exception: " + e;
149       }
150   }
151   
152   /**
153    * Start the client ORB.
154    */
155   public static String startPlayer(final Player player, final PlayingDesk desk)
156   {
157     desk.ior = null;
158     desk.ok = false;
159     
160     final Properties p = new Properties();
161     p.put("gnu.CORBA.ListenerPort", USED_PORT_RANGE);
162     
163     try
164       {
165         new Thread()
166         {
167           public void run()
168           {
169             try
170               {
171                 desk.orb = ORB.init(new String[0], p);
172
173                 POA rootPOA = POAHelper.narrow(desk.orb.resolve_initial_references("RootPOA"));
174                 rootPOA.the_POAManager().activate();
175
176                 // Construct the tie.
177                 Tie tie = new _PlayerImpl_Tie();
178
179                 // Set the implementing class (invocation target).
180                 tie.setTarget(new PlayerImpl());
181
182                 // Connect the tie as POA servant.
183                 org.omg.CORBA.Object object = rootPOA.servant_to_reference((Servant) tie);
184
185                 // Get the stringified reference.
186                 desk.ior = desk.orb.object_to_string(object);
187
188                 // Mark that the object was created OK.
189                 desk.ok = true;
190                 desk.orb.run();
191               }
192             catch (Exception exc)
193               {
194                 exc.printStackTrace();
195                 desk.ior = "Unable to start the ORB: " + exc;
196               }
197           }
198         }.start();
199
200         long t = System.currentTimeMillis();
201         while (desk.ior == null && System.currentTimeMillis() - t < 20000)
202           {
203             Thread.sleep(100);
204           }
205       }
206     catch (Exception e)
207       {
208         e.printStackTrace();
209         return "Exception: " + e;
210       }
211
212     // Add shutdown hook to unregister from the manager.
213     Runtime.getRuntime().addShutdownHook(new Thread()
214     {
215       public void run()
216       {
217         if (desk.manager != null && player != null)
218           {
219             try
220               {
221                 desk.manager.unregister(player);
222               }
223             catch (RemoteException ex)
224               {
225                 // We will print the exception because this is a demo
226                 // application that
227                 // may be modified for learning purposes.
228                 ex.printStackTrace();
229               }
230             desk.manager = null;
231           }
232       }
233     });
234     return desk.ior;
235   }  
236 }