TelnetRepl.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package kawa;
  2. import gnu.expr.*;
  3. import gnu.mapping.*;
  4. import gnu.kawa.io.OutPort;
  5. import gnu.kawa.io.FilePath;
  6. import gnu.kawa.io.TtyInPort;
  7. public class TelnetRepl extends Procedure0
  8. {
  9. // close when finished.
  10. java.net.Socket socket;
  11. Language language;
  12. public TelnetRepl(Language language, java.net.Socket socket)
  13. {
  14. this.language = language;
  15. this.socket = socket;
  16. }
  17. public Object apply0 ()
  18. {
  19. try
  20. {
  21. Shell.run(language, Environment.getCurrent());
  22. return Values.empty;
  23. }
  24. finally
  25. {
  26. try
  27. {
  28. socket.close();
  29. }
  30. catch (java.io.IOException ex)
  31. {
  32. }
  33. }
  34. }
  35. /** Run a Kawa repl as a telnet server.
  36. @param client A client that has connected to us,
  37. and that wants to use the telnet protocol to talk to a
  38. Scheme read-eval-print-loop. */
  39. public static Thread serve (Language language, java.net.Socket client)
  40. throws java.io.IOException
  41. {
  42. Telnet conn = new Telnet(client, true);
  43. java.io.OutputStream sout = conn.getOutputStream();
  44. java.io.InputStream sin = conn.getInputStream();
  45. OutPort out = new OutPort(sout, FilePath.valueOf("/dev/stdout"));
  46. TtyInPort in = new TtyInPort(sin, FilePath.valueOf("/dev/stdin"), out);
  47. /*
  48. conn.request(Telnet.DO, Telnet.EOF);
  49. conn.request(Telnet.DO, Telnet.NAWS);
  50. conn.request(Telnet.DO, Telnet.TTYPE);
  51. conn.request(Telnet.DO, Telnet.LINEMODE);
  52. */
  53. Runnable r = new RunnableClosure(new TelnetRepl(language, client),
  54. in, out, out);
  55. Thread thread = new Thread(r);
  56. thread.start();
  57. return thread;
  58. }
  59. }