Merge commit '7a455971fed716123933d0f685a0d6eebcf3282b'
[nikiroo-utils.git] / src / jexer / demos / Demo8.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.demos;
30
31 import java.net.ServerSocket;
32 import java.net.Socket;
33 import java.text.MessageFormat;
34 import java.util.ResourceBundle;
35
36 import jexer.TApplication;
37 import jexer.backend.*;
38 import jexer.demos.DemoApplication;
39 import jexer.net.TelnetServerSocket;
40
41
42 /**
43 * This class shows off the use of MultiBackend and MultiScreen.
44 */
45 public class Demo8 {
46
47 /**
48 * Translated strings.
49 */
50 private static final ResourceBundle i18n = ResourceBundle.getBundle(Demo8.class.getName());
51
52 // ------------------------------------------------------------------------
53 // Demo8 ------------------------------------------------------------------
54 // ------------------------------------------------------------------------
55
56 /**
57 * Main entry point.
58 *
59 * @param args Command line arguments
60 */
61 public static void main(final String [] args) {
62 ServerSocket server = null;
63 try {
64
65 /*
66 * In this demo we will create a headless application that anyone
67 * can telnet to.
68 */
69
70 /*
71 * Check the arguments for the port to listen on.
72 */
73 if (args.length == 0) {
74 System.err.println(i18n.getString("usageString"));
75 return;
76 }
77 int port = Integer.parseInt(args[0]);
78
79 /*
80 * We create a headless screen and use it to establish a
81 * MultiBackend.
82 */
83 HeadlessBackend headlessBackend = new HeadlessBackend();
84 MultiBackend multiBackend = new MultiBackend(headlessBackend);
85
86 /*
87 * Now we create the shared application (a standard demo) and
88 * spin it up.
89 */
90 DemoApplication demoApp = new DemoApplication(multiBackend);
91 (new Thread(demoApp)).start();
92 multiBackend.setListener(demoApp);
93
94 /*
95 * Fire up the telnet server.
96 */
97 server = new TelnetServerSocket(port);
98 while (demoApp.isRunning()) {
99 Socket socket = server.accept();
100 System.out.println(MessageFormat.
101 format(i18n.getString("newConnection"), socket));
102
103 ECMA48Backend ecmaBackend = new ECMA48Backend(demoApp,
104 socket.getInputStream(),
105 socket.getOutputStream());
106
107 /*
108 * Add this screen to the MultiBackend, and at this point we
109 * have the telnet client able to use the shared demo
110 * application.
111 */
112 multiBackend.addBackend(ecmaBackend);
113
114 /*
115 * Emit the connection information from telnet.
116 */
117 Thread.sleep(500);
118 System.out.println(MessageFormat.
119 format(i18n.getString("terminal"),
120 ((jexer.net.TelnetInputStream) socket.getInputStream()).
121 getTerminalType()));
122 System.out.println(MessageFormat.
123 format(i18n.getString("username"),
124 ((jexer.net.TelnetInputStream) socket.getInputStream()).
125 getUsername()));
126 System.out.println(MessageFormat.
127 format(i18n.getString("language"),
128 ((jexer.net.TelnetInputStream) socket.getInputStream()).
129 getLanguage()));
130
131 } // while (demoApp.isRunning())
132
133 /*
134 * When the application exits, kill all of the connections too.
135 */
136 multiBackend.shutdown();
137 server.close();
138
139 System.out.println(i18n.getString("exitMain"));
140
141 } catch (Exception e) {
142 e.printStackTrace();
143 } finally {
144 if (server != null) {
145 try {
146 server.close();
147 } catch (Exception e) {
148 // SQUASH
149 }
150 }
151 }
152 }
153
154 }