001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.commandline;
006    
007    import bsh.ConsoleInterface;
008    import bsh.Interpreter;
009    import graphlab.platform.core.BlackBoard;
010    
011    import java.io.IOException;
012    import java.io.InputStreamReader;
013    import java.io.PrintStream;
014    import java.io.Reader;
015    import java.net.ServerSocket;
016    import java.net.Socket;
017    
018    
019    /**
020     * @author Mohamad Ali Rostami
021     * @email mamaliam@gmail.com
022     */
023    
024    public class ShellServer {
025        public static Thread thread;
026        Shell shell;
027    
028        public ShellServer(BlackBoard bb, Shell shell) {
029            this.shell = shell;
030        }
031    
032        public void performJob(String eventKey, Object val) {
033            thread = new Thread() {
034                public void run() {
035                    try {
036                        ServerSocket ss = new ServerSocket(1234);
037                        final Socket s = ss.accept();
038                        ConsoleInterface ci = new ConsoleInterface() {
039                            Reader r = new InputStreamReader(s.getInputStream());
040    
041                            public Reader getIn() {
042                                try {
043                                    return new InputStreamReader(s.getInputStream());
044                                } catch (IOException e) {
045                                    e.printStackTrace();
046                                }
047                                return null;
048    
049                            }
050    
051                            public PrintStream getOut() {
052                                try {
053                                    return new PrintStream(s.getOutputStream());
054                                } catch (IOException e) {
055                                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
056                                }
057                                return null;
058                            }
059    
060                            public PrintStream getErr() {
061                                try {
062                                    return new PrintStream(s.getOutputStream());
063                                } catch (IOException e) {
064                                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
065                                }
066                                return null;
067                            }
068    
069                            public void println(Object object) {
070    
071                                this.getOut().println(object);
072                            }
073    
074                            public void print(Object object) {
075                                this.getOut().print(object);
076                            }
077    
078                            public void error(Object object) {
079                                this.getOut().println(object);
080                            }
081                        };
082    
083                        Interpreter interpreter = new Interpreter(ci);
084                        interpreter.eval(shell.getEvaluations());
085                        interpreter.set("me", shell.get("me"));
086                        interpreter.set("current_interpreter", interpreter);
087                        interpreter.run();
088                    } catch (Exception e) {
089                        e.printStackTrace();
090                    }
091                }
092            };
093            thread.start();
094        }
095    }