/** Spela svenskmud i java. @author Linus Tolke Ide, starta tv} tr}dar * en som l{ser fr}n ett java-f|nster och skickar till muddet. * en som l{ser fr}n muddet och stoppar in i www-dokumentet. Redirekta browsern till den applikationen. http://localhost:8754/ (funkar detta) */ import java.net.*; import java.awt.*; import java.applet.*; import java.io.*; public class Mud extends Applet { public static int PORT = 8754; MudControls controls; MudSession mudsession; WWWSession wwwsession; public void init() { setLayout(new BorderLayout()); add("Center", controls = new MudControls(this)); wwwsession = new WWWSession(this); mudsession = new MudSession(this); } public void start() { controls.enable(); } public void stop() { controls.disable(); mudsession.stop(); wwwsession.stop(); } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { System.exit(0); } return false; } synchronized public void send(String what) { /* H{r skall vi skicka sakerna till muddet. */ mudsession.receive(what); } synchronized public void receive(String what) { /* H{r {r det muddet som skickat n}got. */ wwwsession.receive(what); } public static void main(String args[]) { Frame f = new Frame("Mud"); Mud mud = new Mud(); mud.init(); mud.start(); f.add("Center", mud); f.resize(500, 100); f.show(); } } class MudControls extends Panel { TextField s; Mud mud; static String SEND = "Send"; static String SLUTA = "Sluta"; static String LE = "Le"; static String DHARRY = "Döda Harry"; public MudControls(Mud m) { this.mud = m; add(s = new TextField("", 50)); add(new Button(this.SEND)); add(new Button(this.SLUTA)); add(new Button(this.LE)); add(new Button(this.DHARRY)); } public boolean action(Event ev, Object arg) { if (ev.target instanceof Button) { String label = (String)arg; if (label.trim() == this.SEND) { mud.send(s.getText().trim()); } else if (label.trim() == this.SLUTA) { mud.send("sluta"); } else if (label.trim() == this.LE) { mud.send("le"); } else if (label.trim() == this.DHARRY) { mud.send("döda harry"); } return true; } return false; } } class MudSession extends Thread { Mud mud; PrintStream out; public MudSession(Mud m) { mud = m; this.start(); } public void receive(String what) { out.println(what); out.flush(); mud.receive("> " + what); } public void run() { try { /* Wait for Connect */ Socket socket = new Socket("svmud", 2046); System.out.println("Connectad."); String line; DataInputStream in = new DataInputStream(socket.getInputStream()); out = new PrintStream(socket.getOutputStream()); System.out.println("Skapat str|mmarna."); while (true) { line = in.readLine(); if (line == null) { System.out.println("Muddet st{ngde f|rbindelsen."); break; } else if (line.length() > 2 && line.charAt(0) == '>' && line.charAt(1) == ' ') { line = line.substring(2); } mud.receive(line); } } catch (UnknownHostException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } mud.stop(); } } class WWWSession extends Thread { PrintStream out; Mud mud; public WWWSession(Mud m) { mud = m; try { /* Wait for Connect */ ServerSocket server = new ServerSocket(Mud.PORT); Socket socket = server.accept(); out = new PrintStream(socket.getOutputStream()); } catch (IOException e) { System.out.println(e); } } public void receive(String what) { out.println(what + "
"); } synchronized public void run() { try { wait(); } catch (InterruptedException e) { System.out.println(e); } } }