ChannelSession.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 import java.util.*;
00033 
00040 class ChannelSession extends Channel{
00041   private static byte[] _session=Util.str2byte("session");
00042 
00043   protected boolean agent_forwarding=false;
00044   protected boolean xforwading=false;
00045   protected Hashtable env=null;
00046 
00047   protected boolean pty=false;
00048 
00049   protected String ttype="vt100";
00050   protected int tcol=80;
00051   protected int trow=24;
00052   protected int twp=640;
00053   protected int thp=480;
00054   protected byte[] terminal_mode=null;
00055 
00056   ChannelSession(){
00057     super();
00058     type=_session;
00059     io=new IO();
00060   }
00061 
00067   public void setAgentForwarding(boolean enable){ 
00068     agent_forwarding=enable;
00069   }
00070 
00077   public void setXForwarding(boolean enable){
00078     xforwading=enable; 
00079   }
00080 
00086   public void setEnv(Hashtable env){ 
00087     synchronized(this){
00088       this.env=env; 
00089     }
00090   }
00091 
00102   public void setEnv(String name, String value){
00103     setEnv(Util.str2byte(name), Util.str2byte(value));
00104   }
00105 
00114   public void setEnv(byte[] name, byte[] value){
00115     synchronized(this){
00116       getEnv().put(name, value);
00117     }
00118   }
00119 
00120   private Hashtable getEnv(){
00121     if(env==null)
00122       env=new Hashtable();
00123     return env;
00124   }
00125 
00132   public void setPty(boolean enable){ 
00133     pty=enable; 
00134   }
00135 
00141   public void setTerminalMode(byte[] terminal_mode){
00142     this.terminal_mode=terminal_mode;
00143   }
00144 
00154   public void setPtySize(int col, int row, int wp, int hp){
00155     setPtyType(this.ttype, col, row, wp, hp);
00156     if(!pty || !isConnected()){
00157       return;
00158     }
00159     try{
00160       RequestWindowChange request=new RequestWindowChange();
00161       request.setSize(col, row, wp, hp);
00162       request.request(getSession(), this);
00163     }
00164     catch(Exception e){
00165       //System.err.println("ChannelSessio.setPtySize: "+e);
00166     }
00167   }
00168 
00176   public void setPtyType(String ttype){
00177     setPtyType(ttype, 80, 24, 640, 480);
00178   }
00179 
00190   public void setPtyType(String ttype, int col, int row, int wp, int hp){
00191     this.ttype=ttype;
00192     this.tcol=col;
00193     this.trow=row;
00194     this.twp=wp;
00195     this.thp=hp;
00196   }
00197 
00205   protected void sendRequests() throws Exception{
00206     Session _session=getSession();
00207     Request request;
00208     if(agent_forwarding){
00209       request=new RequestAgentForwarding();
00210       request.request(_session, this);
00211     }
00212 
00213     if(xforwading){
00214       request=new RequestX11();
00215       request.request(_session, this);
00216     }
00217 
00218     if(pty){
00219       request=new RequestPtyReq();
00220       ((RequestPtyReq)request).setTType(ttype);
00221       ((RequestPtyReq)request).setTSize(tcol, trow, twp, thp);
00222       if(terminal_mode!=null){
00223         ((RequestPtyReq)request).setTerminalMode(terminal_mode);
00224       }
00225       request.request(_session, this);
00226     }
00227 
00228     if(env!=null){
00229       for(Enumeration _env=env.keys(); _env.hasMoreElements();){
00230         Object name=_env.nextElement();
00231         Object value=env.get(name);
00232         request=new RequestEnv();
00233         ((RequestEnv)request).setEnv(toByteArray(name), 
00234                                      toByteArray(value));
00235         request.request(_session, this);
00236       }
00237     }
00238   }
00239 
00240   private byte[] toByteArray(Object o){
00241     if(o instanceof String){
00242       return Util.str2byte((String)o);
00243     }
00244     return (byte[])o;
00245   }
00246 
00247 
00252   public void run(){
00253     //System.err.println(this+":run >");
00254 
00255     Buffer buf=new Buffer(rmpsize);
00256     Packet packet=new Packet(buf);
00257     int i=-1;
00258     try{
00259       while(isConnected() &&
00260         thread!=null && 
00261             io!=null && 
00262             io.in!=null){
00263         i=io.in.read(buf.buffer, 
00264                      14,    
00265                      buf.buffer.length-14
00266                      -Session.buffer_margin
00267              );
00268     if(i==0)continue;
00269     if(i==-1){
00270       eof();
00271       break;
00272     }
00273     if(close)break;
00274         //System.out.println("write: "+i);
00275         packet.reset();
00276         buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
00277         buf.putInt(recipient);
00278         buf.putInt(i);
00279         buf.skip(i);
00280     getSession().write(packet, this, i);
00281       }
00282     }
00283     catch(Exception e){
00284       //System.err.println("# ChannelExec.run");
00285       //e.printStackTrace();
00286     }
00287     Thread _thread=thread; 
00288     if(_thread!=null){
00289       synchronized(_thread){ _thread.notifyAll(); }
00290     }
00291     thread=null;
00292     //System.err.println(this+":run <");
00293   }
00294 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1