ChannelSession.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
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
00285
00286 }
00287 Thread _thread=thread;
00288 if(_thread!=null){
00289 synchronized(_thread){ _thread.notifyAll(); }
00290 }
00291 thread=null;
00292
00293 }
00294 }