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    
029    class ClientProcessContext implements ShellProcessContext {
030    
031      /** . */
032      final ClientAutomaton client;
033    
034      /** . */
035      final ShellProcess process;
036    
037      ClientProcessContext(ClientAutomaton client, ShellProcess process) {
038        this.client = client;
039        this.process = process;
040      }
041    
042      public int getWidth() {
043        return client.getWidth();
044      }
045    
046      public int getHeight() {
047        return client.getHeight();
048      }
049    
050      public String getProperty(String name) {
051        return null;
052      }
053    
054      public String readLine(String msg, boolean echo) {
055        try {
056          client.out.writeObject(ServerMessage.READLINE);
057          client.out.writeObject(msg);
058          client.out.writeObject(echo);
059          client.out.flush();
060          return (String)client.in.readObject();
061        }
062        catch (Exception e) {
063          return null;
064        }
065      }
066    
067      public void provide(Chunk element) throws IOException {
068        try {
069          client.out.writeObject(ServerMessage.CHUNK);
070          client.out.writeObject(element);
071          client.out.flush();
072        }
073        catch (IOException ignore) {
074          //
075        }
076      }
077    
078      public void flush() {
079        try {
080          client.out.writeObject(ServerMessage.FLUSH);
081          client.out.flush();
082        }
083        catch (IOException ignore) {
084          //
085        }
086      }
087    
088      public void end(ShellResponse response) {
089        try {
090          client.current = null;
091          client.out.writeObject(ServerMessage.END);
092          client.out.writeObject(response);
093          client.out.flush();
094        }
095        catch (IOException ignore) {
096          //
097        }
098        finally {
099          if (response instanceof ShellResponse.Close) {
100            client.close();
101          }
102        }
103      }
104    }