- public static void main(final String [] args) throws Exception {
- // This demo will build everything "from the outside".
-
- // Swing is the default backend on Windows unless explicitly
- // overridden by jexer.Swing.
- TApplication.BackendType backendType = TApplication.BackendType.XTERM;
- if (System.getProperty("os.name").startsWith("Windows")) {
- backendType = TApplication.BackendType.SWING;
- }
- if (System.getProperty("os.name").startsWith("Mac")) {
- backendType = TApplication.BackendType.SWING;
- }
- if (System.getProperty("jexer.Swing") != null) {
- if (System.getProperty("jexer.Swing", "false").equals("true")) {
- backendType = TApplication.BackendType.SWING;
- } else {
- backendType = TApplication.BackendType.XTERM;
+ public static void main(final String [] args) {
+ ServerSocket server = null;
+ try {
+
+ /*
+ * In this demo we will create a headless application that anyone
+ * can telnet to.
+ */
+
+ /*
+ * Check the arguments for the port to listen on.
+ */
+ if (args.length == 0) {
+ System.err.println(i18n.getString("usageString"));
+ return;
+ }
+ int port = Integer.parseInt(args[0]);
+
+ /*
+ * We create a headless screen and use it to establish a
+ * MultiBackend.
+ */
+ HeadlessBackend headlessBackend = new HeadlessBackend();
+ MultiBackend multiBackend = new MultiBackend(headlessBackend);
+
+ /*
+ * Now we create the shared application (a standard demo) and
+ * spin it up.
+ */
+ DemoApplication demoApp = new DemoApplication(multiBackend);
+ (new Thread(demoApp)).start();
+ multiBackend.setListener(demoApp);
+
+ /*
+ * Fire up the telnet server.
+ */
+ server = new TelnetServerSocket(port);
+ while (demoApp.isRunning()) {
+ Socket socket = server.accept();
+ System.out.println(MessageFormat.
+ format(i18n.getString("newConnection"), socket));
+
+ ECMA48Backend ecmaBackend = new ECMA48Backend(demoApp,
+ socket.getInputStream(),
+ socket.getOutputStream());
+
+ /*
+ * Add this screen to the MultiBackend, and at this point we
+ * have the telnet client able to use the shared demo
+ * application.
+ */
+ multiBackend.addBackend(ecmaBackend);
+
+ /*
+ * Emit the connection information from telnet.
+ */
+ Thread.sleep(500);
+ System.out.println(MessageFormat.
+ format(i18n.getString("terminal"),
+ ((jexer.net.TelnetInputStream) socket.getInputStream()).
+ getTerminalType()));
+ System.out.println(MessageFormat.
+ format(i18n.getString("username"),
+ ((jexer.net.TelnetInputStream) socket.getInputStream()).
+ getUsername()));
+ System.out.println(MessageFormat.
+ format(i18n.getString("language"),
+ ((jexer.net.TelnetInputStream) socket.getInputStream()).
+ getLanguage()));
+
+ } // while (demoApp.isRunning())
+
+ /*
+ * When the application exits, kill all of the connections too.
+ */
+ multiBackend.shutdown();
+ server.close();
+
+ System.out.println(i18n.getString("exitMain"));
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ if (server != null) {
+ try {
+ server.close();
+ } catch (Exception e) {
+ // SQUASH
+ }