home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / rmi / transport / tcp / TCPChannel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  9.2 KB  |  412 lines

  1. package sun.rmi.transport.tcp;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.io.PrintStream;
  8. import java.net.ConnectException;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import java.rmi.ConnectIOException;
  12. import java.rmi.MarshalException;
  13. import java.rmi.RemoteException;
  14. import java.rmi.server.LogStream;
  15. import java.rmi.server.RMISocketFactory;
  16. import java.util.Enumeration;
  17. import java.util.Vector;
  18. import sun.rmi.transport.Channel;
  19. import sun.rmi.transport.Connection;
  20. import sun.rmi.transport.Endpoint;
  21. import sun.rmi.transport.Notifiable;
  22. import sun.rmi.transport.RMIThread;
  23. import sun.rmi.transport.Utils;
  24.  
  25. public class TCPChannel implements Channel, Runnable {
  26.    // $FF: renamed from: ep sun.rmi.transport.tcp.TCPEndpoint
  27.    private TCPEndpoint field_0;
  28.    // $FF: renamed from: tr sun.rmi.transport.tcp.TCPTransport
  29.    private TCPTransport field_1;
  30.    private Vector notifyList = new Vector();
  31.    private Vector freeList = new Vector();
  32.    private boolean usingMultiplexer = false;
  33.    private ConnectionMultiplexer multiplexer;
  34.    private ConnectionAcceptor acceptor;
  35.    private static long timeout = Utils.getLong("sun.rmi.transport.connectionTimeout", 300000L);
  36.  
  37.    TCPChannel(TCPTransport var1, TCPEndpoint var2) {
  38.       this.field_1 = var1;
  39.       this.field_0 = var2;
  40.    }
  41.  
  42.    public Endpoint getEndpoint() {
  43.       return this.field_0;
  44.    }
  45.  
  46.    public Connection newConnection() throws RemoteException {
  47.       Vector var3 = this.freeList;
  48.       synchronized(var3){}
  49.  
  50.       try {
  51.          if (this.freeList.size() > 0) {
  52.             TCPConnection var19 = (TCPConnection)this.freeList.lastElement();
  53.             this.freeList.removeElementAt(this.freeList.size() - 1);
  54.             if (TCPTransport.logLevel >= 10) {
  55.                LogStream.log("tcp").println("TCPChannel.newConnection: reuse connection");
  56.             }
  57.  
  58.             TCPConnection var20 = var19;
  59.             return var20;
  60.          }
  61.       } catch (Throwable var18) {
  62.          throw var18;
  63.       }
  64.  
  65.       if (TCPTransport.logLevel >= 10) {
  66.          LogStream.log("tcp").println("TCPChannel.newConnection: create connection");
  67.       }
  68.  
  69.       TCPConnection var1;
  70.       if (!this.usingMultiplexer) {
  71.          Socket var2 = this.openSocket();
  72.          var1 = new TCPConnection(this, var2);
  73.  
  74.          try {
  75.             DataOutputStream var21 = new DataOutputStream(var1.getOutputStream());
  76.             if (!var1.isReusable()) {
  77.                var21.writeByte(76);
  78.             } else {
  79.                var21.writeByte(75);
  80.                var21.flush();
  81.                DataInputStream var4 = new DataInputStream(var1.getInputStream());
  82.                byte var5 = var4.readByte();
  83.                if (var5 != 78) {
  84.                   throw new MarshalException("Transport protocol not supported by server");
  85.                }
  86.  
  87.                TCPEndpoint var6 = TCPEndpoint.read(var4);
  88.                if (TCPTransport.logLevel >= 20) {
  89.                   LogStream.log("tcp").println("TCPChannel.newConnection: server suggested endpoint " + var6);
  90.                }
  91.  
  92.                TCPEndpoint.setLocalHost(var6.getHost());
  93.                var6 = TCPEndpoint.getLocalEndpoint(0);
  94.                var6.write(var21);
  95.                if (TCPTransport.logLevel >= 20) {
  96.                   LogStream.log("tcp").println("TCPChannel.newConnection: using endpoint " + var6);
  97.                }
  98.  
  99.                var21.flush();
  100.             }
  101.          } catch (IOException var17) {
  102.             if (var17 instanceof RemoteException) {
  103.                throw (RemoteException)var17;
  104.             }
  105.  
  106.             throw new MarshalException("Error marshaling transport header", var17);
  107.          }
  108.       } else {
  109.          try {
  110.             var1 = this.multiplexer.openConnection();
  111.          } catch (IOException var16) {
  112.             synchronized(this){}
  113.  
  114.             try {
  115.                this.usingMultiplexer = false;
  116.                this.multiplexer = null;
  117.             } catch (Throwable var15) {
  118.                throw var15;
  119.             }
  120.  
  121.             throw new ConnectIOException("Error creating multiplexed connection", var16);
  122.          }
  123.       }
  124.  
  125.       return var1;
  126.    }
  127.  
  128.    public void free(Connection var1, boolean var2) {
  129.       if (var1 != null) {
  130.          if (var2 && var1.isReusable()) {
  131.             if (TCPTransport.logLevel >= 10) {
  132.                LogStream.log("tcp").println("TCPChannel.free: reuse connection");
  133.             }
  134.  
  135.             this.freeList.addElement(var1);
  136.             ((TCPConnection)var1).setExpiration(System.currentTimeMillis() + timeout);
  137.          } else {
  138.             if (TCPTransport.logLevel >= 10) {
  139.                LogStream.log("tcp").println("TCPChannel.free: close connection");
  140.             }
  141.  
  142.             try {
  143.                var1.close();
  144.             } catch (IOException var3) {
  145.             }
  146.          }
  147.       }
  148.    }
  149.  
  150.    private Socket openSocket() throws RemoteException {
  151.       if (TCPTransport.logLevel >= 20) {
  152.          LogStream.log("tcp").println("TCPChannel.openSocket: opening socket to " + this.field_0.getHost() + ":" + this.field_0.getPort());
  153.       }
  154.  
  155.       RMISocketFactory var2 = RMISocketFactory.getSocketFactory();
  156.       if (var2 == null) {
  157.          var2 = TCPTransport.defaultSocketFactory;
  158.       }
  159.  
  160.       Socket var1;
  161.       try {
  162.          var1 = var2.createSocket(this.field_0.getHost(), this.field_0.getPort());
  163.       } catch (UnknownHostException var7) {
  164.          throw new java.rmi.UnknownHostException("Unknown host: " + this.field_0, var7);
  165.       } catch (ConnectException var8) {
  166.          throw new java.rmi.ConnectException("Connection refused to host: " + this.field_0, var8);
  167.       } catch (IOException var9) {
  168.          throw new ConnectIOException("Error creating connection to: " + this.field_0, var9);
  169.       }
  170.  
  171.       try {
  172.          var1.setTcpNoDelay(true);
  173.       } catch (Exception var6) {
  174.       }
  175.  
  176.       try {
  177.          var1.getInputStream();
  178.          OutputStream var3 = var1.getOutputStream();
  179.          DataOutputStream var4 = new DataOutputStream(var3);
  180.          var4.writeInt(1246907721);
  181.          var4.writeShort(2);
  182.          return var1;
  183.       } catch (IOException var5) {
  184.          throw new MarshalException("Error marshaling transport header", var5);
  185.       }
  186.    }
  187.  
  188.    private ConnectionMultiplexer openMultiplexer() throws RemoteException {
  189.       Socket var1 = this.openSocket();
  190.       TCPConnection var2 = new TCPConnection(this, var1);
  191.  
  192.       try {
  193.          DataOutputStream var4 = new DataOutputStream(var1.getOutputStream());
  194.          if (!var2.isReusable()) {
  195.             throw new ConnectIOException("Cannot open multiplexed connection to " + this.field_0);
  196.          } else {
  197.             var4.writeByte(77);
  198.             var4.flush();
  199.             DataInputStream var5 = new DataInputStream(var1.getInputStream());
  200.             byte var6 = var5.readByte();
  201.             if (var6 != 78) {
  202.                throw new MarshalException("Transport protocol not supported by receiver");
  203.             } else {
  204.                TCPEndpoint var7 = TCPEndpoint.read(var5);
  205.                if (TCPTransport.logLevel >= 20) {
  206.                   LogStream.log("tcp").println("TCPTransport(" + var7.getPort() + ").run: " + "server suggested endpoint " + var7);
  207.                }
  208.  
  209.                TCPEndpoint.setLocalHost(var7.getHost());
  210.                TCPEndpoint.setDefaultPort(var7.getPort());
  211.                var7 = TCPEndpoint.getLocalEndpoint(0);
  212.                var7.write(var4);
  213.                if (TCPTransport.logLevel >= 20) {
  214.                   LogStream.log("tcp").println("TCPTransport(" + var7.getPort() + ").run: " + "using endpoint " + var7);
  215.                }
  216.  
  217.                var4.flush();
  218.                ConnectionMultiplexer var3 = new ConnectionMultiplexer(this, var1.getInputStream(), var1.getOutputStream(), true);
  219.                return var3;
  220.             }
  221.          }
  222.       } catch (IOException var8) {
  223.          if (var8 instanceof RemoteException) {
  224.             throw (RemoteException)var8;
  225.          } else {
  226.             throw new MarshalException("Error marshaling transport header", var8);
  227.          }
  228.       }
  229.    }
  230.  
  231.    public void run() {
  232.       try {
  233.          this.multiplexer.run();
  234.       } catch (IOException var9) {
  235.          IOException var1 = var9;
  236.          if (TCPTransport.logLevel >= 20) {
  237.             LogStream var2 = LogStream.log("tcp");
  238.             synchronized(var2){}
  239.  
  240.             try {
  241.                ((PrintStream)var2).print("exception occurred in multiplexer: ");
  242.                ((Throwable)var1).printStackTrace(var2);
  243.             } catch (Throwable var8) {
  244.                throw var8;
  245.             }
  246.          }
  247.  
  248.          this.multiplexer = null;
  249.  
  250.          try {
  251.             this.haveMultiplexer();
  252.          } catch (RemoteException var7) {
  253.          }
  254.       }
  255.    }
  256.  
  257.    void haveMultiplexer() throws RemoteException {
  258.       if (this.multiplexer == null) {
  259.          if (TCPTransport.logLevel >= 20) {
  260.             LogStream.log("tcp").println("TCPChannel.haveMultiplexer(): attempting to open multiplex connection");
  261.          }
  262.  
  263.          this.multiplexer = this.openMultiplexer();
  264.          Thread var1 = RMIThread.newThread(this, "Multiplexer-" + this.field_0.getHost() + ":" + this.field_0.getPort(), true);
  265.          var1.start();
  266.       }
  267.  
  268.    }
  269.  
  270.    synchronized void useMultiplexer(ConnectionMultiplexer var1) {
  271.       this.multiplexer = var1;
  272.       this.usingMultiplexer = true;
  273.    }
  274.  
  275.    void acceptMultiplexConnection(Connection var1) {
  276.       if (this.acceptor == null) {
  277.          this.acceptor = new ConnectionAcceptor(this.field_1);
  278.          this.acceptor.startNewAcceptor();
  279.       }
  280.  
  281.       this.acceptor.accept(var1);
  282.    }
  283.  
  284.    int ping() {
  285.       TCPConnection var1 = null;
  286.       byte var2 = 0;
  287.  
  288.       try {
  289.          var1 = (TCPConnection)this.newConnection();
  290.       } catch (RemoteException var14) {
  291.          var2 = 1;
  292.       }
  293.  
  294.       try {
  295.          if (var1 != null) {
  296.             DataOutputStream var3 = new DataOutputStream(var1.getOutputStream());
  297.             var3.writeByte(82);
  298.             var1.releaseOutputStream();
  299.             DataInputStream var4 = new DataInputStream(var1.getInputStream());
  300.             byte var5 = var4.readByte();
  301.             if (var5 == 83) {
  302.                var2 = 0;
  303.             } else {
  304.                var2 = 2;
  305.             }
  306.          }
  307.       } catch (IOException var13) {
  308.          var2 = 2;
  309.       }
  310.  
  311.       if (var2 == 0) {
  312.          this.free(var1, true);
  313.       } else {
  314.          if (var1 != null) {
  315.             this.free(var1, false);
  316.          }
  317.  
  318.          this.dead(var2);
  319.       }
  320.  
  321.       long var17 = System.currentTimeMillis();
  322.       Vector var18 = this.freeList;
  323.       synchronized(var18){}
  324.  
  325.       try {
  326.          Enumeration var7 = this.freeList.elements();
  327.  
  328.          while(var7.hasMoreElements()) {
  329.             var1 = (TCPConnection)var7.nextElement();
  330.             if (var1.expired(var17)) {
  331.                if (TCPTransport.logLevel >= 10) {
  332.                   LogStream.log("tcp").println("TCPChannel.ping: connection time out expired");
  333.                }
  334.  
  335.                try {
  336.                   var1.close();
  337.                } catch (IOException var12) {
  338.                }
  339.  
  340.                this.freeList.removeElement(var1);
  341.             }
  342.          }
  343.       } catch (Throwable var15) {
  344.          throw var15;
  345.       }
  346.  
  347.       return var2;
  348.    }
  349.  
  350.    public void dead(int var1) {
  351.       if (var1 == 1) {
  352.          Vector var2 = this.freeList;
  353.          synchronized(var2){}
  354.  
  355.          try {
  356.             Enumeration var4 = this.freeList.elements();
  357.  
  358.             while(var4.hasMoreElements()) {
  359.                Connection var5 = (Connection)var4.nextElement();
  360.  
  361.                try {
  362.                   var5.close();
  363.                } catch (IOException var13) {
  364.                }
  365.             }
  366.  
  367.             this.freeList.removeAllElements();
  368.          } catch (Throwable var14) {
  369.             throw var14;
  370.          }
  371.       }
  372.  
  373.       Vector var3 = this.notifyList;
  374.       synchronized(var3){}
  375.  
  376.       Vector var15;
  377.       try {
  378.          var15 = this.notifyList;
  379.          this.notifyList = new Vector();
  380.       } catch (Throwable var12) {
  381.          throw var12;
  382.       }
  383.  
  384.       Enumeration var17 = var15.elements();
  385.  
  386.       while(var17.hasMoreElements()) {
  387.          Notifiable var18 = (Notifiable)var17.nextElement();
  388.          var18.notify(this.field_0, var1);
  389.       }
  390.  
  391.       Object var16 = null;
  392.    }
  393.  
  394.    public void addNotifiable(Endpoint var1, Notifiable var2) {
  395.       Vector var3 = this.notifyList;
  396.       synchronized(var3){}
  397.  
  398.       try {
  399.          if (!this.notifyList.contains(var2)) {
  400.             this.notifyList.addElement(var2);
  401.          }
  402.       } catch (Throwable var5) {
  403.          throw var5;
  404.       }
  405.  
  406.    }
  407.  
  408.    public void removeNotifiable(Endpoint var1, Notifiable var2) {
  409.       this.notifyList.removeElement(var2);
  410.    }
  411. }
  412.