001    package graphlab.plugins.commandline.commands;
002    
003    import graphlab.platform.core.BlackBoard;
004    import graphlab.platform.lang.CommandAttitude;
005    import graphlab.plugins.main.GraphData;
006    import graphlab.plugins.commandline.Shell;
007    
008    import java.net.ServerSocket;
009    import java.net.Socket;
010    import java.io.Reader;
011    import java.io.InputStreamReader;
012    import java.io.IOException;
013    import java.io.PrintStream;
014    
015    import bsh.ConsoleInterface;
016    import bsh.Interpreter;
017    
018    /**
019     * @author Mohamad Ali Rostami
020     * @email rostamiev@gmail.com
021     */
022    
023    public class ShellServerCommands {
024    
025        BlackBoard bb;
026        GraphData datas;
027        Thread thread;
028    
029        public ShellServerCommands(BlackBoard bb) {
030            this.bb = bb;
031            datas = new GraphData(bb);
032        }
033    
034        @CommandAttitude(name = "run_server", abbreviation = "_rs"
035                    , description = "")
036    
037        public void run() {
038            final Shell shell = Shell.getCurrentShell(bb);
039                thread = new Thread() {
040                public void run() {
041                    try {
042                        ServerSocket ss = new ServerSocket(1234);
043                        final Socket s = ss.accept();
044                        ConsoleInterface ci = new ConsoleInterface() {
045                            Reader r = new InputStreamReader(s.getInputStream());
046    
047                            public Reader getIn() {
048                                try {
049                                    return new InputStreamReader(s.getInputStream());
050                                } catch (IOException e) {
051                                    e.printStackTrace();
052                                }
053                                return null;
054    
055                            }
056    
057                            public PrintStream getOut() {
058                                try {
059                                    return new PrintStream(s.getOutputStream());
060                                } catch (IOException e) {
061                                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
062                                }
063                                return null;
064                            }
065    
066                            public PrintStream getErr() {
067                                try {
068                                    return new PrintStream(s.getOutputStream());
069                                } catch (IOException e) {
070                                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
071                                }
072                                return null;
073                            }
074    
075                            public void println(Object object) {
076    
077                                this.getOut().println(object);
078                            }
079    
080                            public void print(Object object) {
081                                this.getOut().print(object);
082                            }
083    
084                            public void error(Object object) {
085                                this.getOut().println(object);
086                            }
087                        };
088    
089                        Interpreter interpreter = new Interpreter(ci);
090                        interpreter.eval(shell.getEvaluations());
091                        interpreter.set("me", shell.get("me"));
092                        interpreter.set("current_interpreter", interpreter);
093                        interpreter.run();
094                    } catch (Exception e) {
095                        e.printStackTrace();
096                    }
097                }
098            };
099            thread.start();
100        }
101    
102        @CommandAttitude(name = "exit_server", abbreviation = "_rs"
103                    , description = "")
104    
105        public void exit() {
106            thread.stop();
107        }
108    }