AI2 Component  (Version nb184)
TelnetRepl.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 package com.google.appinventor.components.runtime.util;
7 
8 import android.util.Log;
9 
10 import gnu.expr.Language;
11 import gnu.mapping.Environment;
12 import gnu.mapping.OutPort;
13 import gnu.mapping.Procedure0;
14 import gnu.mapping.TtyInPort;
15 import gnu.mapping.Values;
16 import gnu.text.FilePath;
17 
18 import kawa.Shell;
19 import kawa.Telnet;
20 
25 public class TelnetRepl extends Procedure0 {
26  // close when finished.
27  java.net.Socket socket;
28 
29  Language language;
30  private static final int REPL_STACK_SIZE = 256*1024;
31 
32  public TelnetRepl(Language language, java.net.Socket socket) {
33  this.language = language;
34  this.socket = socket;
35  }
36 
37  public Object apply0 () {
38  Thread thread = Thread.currentThread();
39  ClassLoader contextClassLoader = thread.getContextClassLoader();
40  if (contextClassLoader == null) {
41  // TODO(markf): this is a hack to deal with calls to getContextClassLoader() within the
42  // Shell.run() call below which return null on some older Android phones.
43  thread.setContextClassLoader(Telnet.class.getClassLoader());
44  }
45 
46  try {
47  Shell.run(language, Environment.getCurrent());
48  return Values.empty;
49  } catch (RuntimeException e) {
50  Log.d("TelnetRepl", "Repl is exiting with error " + e.getMessage());
51  e.printStackTrace();
52  throw e;
53  } finally {
54  try {
55  socket.close();
56  } catch (java.io.IOException ex) {
57  // This ignoring of the exception was in the original version of the code - markf
58  }
59  }
60  }
61 
62 
68  public static Thread serve (Language language, java.net.Socket client)
69  throws java.io.IOException {
70  Telnet conn = new Telnet(client, true);
71  java.io.OutputStream sout = conn.getOutputStream();
72  java.io.InputStream sin = conn.getInputStream();
73  OutPort out = new OutPort(sout, FilePath.valueOf("/dev/stdout"));
74  TtyInPort in = new TtyInPort(sin, FilePath.valueOf("/dev/stdin"), out);
75  // The following was commented out in the original code - markf
76  /*
77  conn.request(Telnet.DO, Telnet.EOF);
78  conn.request(Telnet.DO, Telnet.NAWS);
79  conn.request(Telnet.DO, Telnet.TTYPE);
80  conn.request(Telnet.DO, Telnet.LINEMODE);
81  */
82 
83  Thread thread =
84  new BiggerFuture(new TelnetRepl(language, client),
85  in, out, out, "Telnet Repl Thread", REPL_STACK_SIZE);
86  thread.start();
87  return thread;
88  }
89 }
com.google.appinventor.components.runtime.util.TelnetRepl
Definition: TelnetRepl.java:25
com.google.appinventor.components.runtime.util.BiggerFuture
Definition: BiggerFuture.java:17
com.google.appinventor.components.runtime.util.TelnetRepl.serve
static Thread serve(Language language, java.net.Socket client)
Definition: TelnetRepl.java:68
com.google.appinventor.components.runtime.util.TelnetRepl.apply0
Object apply0()
Definition: TelnetRepl.java:37
com.google.appinventor.components.runtime.util.TelnetRepl.TelnetRepl
TelnetRepl(Language language, java.net.Socket socket)
Definition: TelnetRepl.java:32