libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / examples / gnu / classpath / examples / CORBA / swing / x5 / X5Server.java
blob7717834604394a1941f80170fd60b6957ad79a5d
1 /* GameManagerAddressServer.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.io.BufferedWriter;
42 import java.io.File;
43 import java.io.FileWriter;
44 import java.io.IOException;
45 import java.io.OutputStream;
46 import java.net.InetAddress;
47 import java.net.ServerSocket;
48 import java.net.Socket;
50 /**
51 * The main executable class of the game manager server.
53 * The manager address server returns the IOR address string of the game
54 * manager. Hence the user does not need to enter the rather long IOR address
55 * string and only needs to specify the host and port of the machine where the
56 * game manager is running.
58 * The manager address server starts the main game manager as well.
60 * This server acts as a HTTP server that always returns the same response. This
61 * primitive functionality is sufficient for its task.
63 * The more complex CORBA applications should use the name service instead. We
64 * do not use the name service as this would require to start additional
65 * external application, specific for the different java platforms.
67 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
69 public class X5Server
71 /**
72 * Start the game manager.
74 public static void main(String[] args)
76 // Start the game manager, write the IOR to the agreed location.
77 OrbStarter.startManager(args);
79 if (!GameManagerImpl.ok)
81 System.out.println("Unable to start the game manager:");
82 System.exit(1);
85 // Print the IOR.
86 System.out.println(GameManagerImpl.ior);
88 String manager_address = null;
90 // Start the game manager server.
91 ServerSocket nameServer = null;
92 try
94 nameServer = new ServerSocket(OrbStarter.MANAGER_NAMER_PORT);
96 System.out.println("The game manager is listening at:");
97 manager_address = "http://"
98 + InetAddress.getLocalHost().getHostAddress() + ":"
99 + nameServer.getLocalPort();
101 System.out.println(manager_address);
103 System.out.println("Enter this address to the "
104 + "input field of the game client.");
106 System.out.println("Use ^C to stop the manager.");
108 catch (Exception ex)
110 System.out.println("The port " + OrbStarter.MANAGER_NAMER_PORT
111 + " is not available. The game manager namer will not start.");
112 System.exit(1);
115 // Write the IOR to the local file system.
116 if (OrbStarter.WRITE_URL_TO_FILE != null)
120 File gmf = new File(OrbStarter.WRITE_URL_TO_FILE);
121 FileWriter f = new FileWriter(gmf);
122 BufferedWriter b = new BufferedWriter(f);
124 b.write(manager_address);
125 b.close();
127 catch (IOException e)
129 System.out.println("Local filesystem not accessible."
130 + "Read IOR from console.");
134 // Do forever.
135 while (true)
139 Socket socket = nameServer.accept();
141 System.out.println("Connected.");
143 // Set the two minutes timeout.
144 socket.setSoTimeout(1000 * 120);
146 OutputStream out = socket.getOutputStream();
148 int length = GameManagerImpl.ior.length();
150 StringBuilder b = new StringBuilder();
151 b.append("HTTP/1.0 200 OK\r\n");
152 b.append("Content-Length: " + length + "\r\n");
153 b.append("Connection: close\r\n");
154 b.append("Content-Type: text/plain; charset=UTF-8\r\n");
155 b.append("\r\n");
157 b.append(GameManagerImpl.ior);
159 out.write(b.toString().getBytes("UTF-8"));
161 socket.shutdownOutput();
163 if (!socket.isClosed())
164 socket.close();
166 System.out.println("Completed.");
168 catch (Exception exc)
170 exc.printStackTrace();
171 System.out.println("Network problem.");