001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.shell.impl.remoting;
021    
022    import org.crsh.shell.ShellProcess;
023    import org.crsh.shell.ShellProcessContext;
024    import org.crsh.shell.ShellResponse;
025    import org.crsh.text.Chunk;
026    
027    import java.io.IOException;
028    import java.util.ArrayList;
029    
030    class ClientProcessContext implements ShellProcessContext {
031    
032      /** . */
033      final ClientAutomaton client;
034    
035      /** . */
036      final ShellProcess process;
037    
038      /** . */
039      final ArrayList<Chunk> buffer;
040    
041      ClientProcessContext(ClientAutomaton client, ShellProcess process) {
042        this.client = client;
043        this.process = process;
044        this.buffer = new ArrayList<Chunk>(1000);
045      }
046    
047      /**
048       * Ensure we have a recent size, the size is considered as recent if it's younger than 2 second, otherwise
049       * send a get size message.
050       */
051      private void ensureSize() {
052        if (System.currentTimeMillis() - client.last > 2000) {
053          synchronized (this) {
054            try {
055              client.out.writeObject(ServerMessage.GET_SIZE);
056              client.out.flush();
057            }
058            catch (Exception e) {
059              //
060            }
061          }
062        }
063      }
064    
065      public int getWidth() {
066        ensureSize();
067        return client.getWidth();
068      }
069    
070      public int getHeight() {
071        ensureSize();
072        return client.getHeight();
073      }
074    
075      public String getProperty(String name) {
076        return null;
077      }
078    
079      public String readLine(String msg, boolean echo) {
080    //    try {
081    //      client.out.writeObject(ServerMessage.READLINE);
082    //      client.out.writeObject(msg);
083    //      client.out.writeObject(echo);
084    //      client.out.flush();
085    //      return (String)client.in.readObject();
086    //    }
087    //    catch (Exception e) {
088    //      return null;
089    //    }
090        return null;
091      }
092    
093      public void provide(Chunk element) throws IOException {
094        buffer.add(element);
095      }
096    
097      public synchronized void flush() {
098        if (buffer.size() > 0) {
099          try {
100            for (Chunk chunk : buffer) {
101              client.out.writeObject(ServerMessage.CHUNK);
102              client.out.writeObject(chunk);
103            }
104            client.out.writeObject(ServerMessage.FLUSH);
105            client.out.flush();
106          }
107          catch (IOException ignore) {
108            //
109          }
110          finally {
111            buffer.clear();
112          }
113        }
114      }
115    
116      public synchronized void end(ShellResponse response) {
117    
118        // Flush what we have in buffer first
119        flush();
120    
121        // Send end message
122        try {
123          client.current = null;
124          client.out.writeObject(ServerMessage.END);
125          client.out.writeObject(response);
126          client.out.flush();
127        }
128        catch (IOException ignore) {
129          //
130        }
131        finally {
132          if (response instanceof ShellResponse.Close) {
133            client.close();
134          }
135        }
136      }
137    }