Skip to content

Instantly share code, notes, and snippets.

@magno32
Last active August 29, 2015 14:13
Show Gist options
  • Save magno32/6810a33efd0d54359c9d to your computer and use it in GitHub Desktop.
Save magno32/6810a33efd0d54359c9d to your computer and use it in GitHub Desktop.
Example of threading in Lanterna 3.
package lanternaexample;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.gui2.BasicWindow;
import com.googlecode.lanterna.gui2.DefaultWindowTextGUI;
import com.googlecode.lanterna.gui2.EmptySpace;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.StackedModalWindowManager;
import com.googlecode.lanterna.gui2.TextGUI;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.dialogs.WaitingDialog;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.ansi.TelnetTerminal;
import com.googlecode.lanterna.terminal.ansi.TelnetTerminalServer;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
//Port to listen on
public static final int LISTEN_PORT = 1099;
//Socket connect timeout
public static final int SO_TIMEOUT = 200;
//The Terminal Server Thread
private static final ExecutorService TERMINAL_SERVER_THREAD = Executors.newSingleThreadExecutor();
//The program thread pool... Its unbounded, be careful.
private static final ExecutorService PROGRAM_THREADS = Executors.newCachedThreadPool();
public static final void main(String[] args) throws IOException {
TelnetTerminalServer server = new TelnetTerminalServer(LISTEN_PORT);
server.setSocketTimeout(SO_TIMEOUT);
System.out.println(String.format("Listening on port %s", LISTEN_PORT));
TERMINAL_SERVER_THREAD.submit(new TerminalServerThread(server));
System.out.println("Press any key to quit...");
System.in.read();
System.out.println("Stopping threads...");
TERMINAL_SERVER_THREAD.shutdownNow();
PROGRAM_THREADS.shutdownNow();
System.out.println("Exiting...");
}
private static class TerminalServerThread implements Runnable {
TelnetTerminalServer server;
public TerminalServerThread(TelnetTerminalServer server) {
this.server = server;
}
@Override
public void run() {
int connectionsMade = 0;
while (!Thread.currentThread().isInterrupted()) {
try {
TelnetTerminal tt = server.acceptConnection();
System.out.println(String.format("Accepted connection #%s from %s", ++connectionsMade, tt.getRemoteSocketAddress()));
PROGRAM_THREADS.submit(new TerminalProgramThread(tt));
} catch (SocketTimeoutException ex) {
//We expect this to happen... So do nothing
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Interrupt cought, ending...");
Thread.currentThread().interrupt();
}
}
private static class TerminalProgramThread implements Runnable {
TelnetTerminal tt;
private static int currentColor = 0;
private static final TextColor.ANSI[] COLORS = {
TextColor.ANSI.BLUE,
TextColor.ANSI.CYAN,
TextColor.ANSI.GREEN,
TextColor.ANSI.MAGENTA,
TextColor.ANSI.RED,
TextColor.ANSI.WHITE,
TextColor.ANSI.YELLOW
};
public TerminalProgramThread(TelnetTerminal tt) {
this.tt = tt;
}
@Override
public void run() {
try {
tt.setTerminalSize(32, 24);
Screen screen = new TerminalScreen(tt);
DefaultWindowTextGUI gui = new DefaultWindowTextGUI(screen, new StackedModalWindowManager(), new EmptySpace(COLORS[currentColor]));
currentColor++;
if (currentColor >= COLORS.length) {
currentColor = 0;
}
screen.startScreen();
Window w = new BasicWindow("Waiting");
Label timeLabel = new Label("Welcome!");
w.setComponent(timeLabel);
gui.getWindowManager().addWindow(w);
gui.updateScreen();
for (int i = 10; i > 0; i--) {
timeLabel.setText(String.format("Exiting in %s seconds.", i));
gui.updateScreen();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
timeLabel.setText("Server shutting down...");
gui.updateScreen();
try {
Thread.sleep(200);
} catch (InterruptedException ex1) {
} finally {
Thread.currentThread().interrupt();
tt.close();
return;
}
}
}
timeLabel.setText(String.format("Time is up!"));
gui.updateScreen();
tt.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment