home *** CD-ROM | disk | FTP | other *** search
/ Datatid 1999 #6 / Datatid_1999-06.iso / internet / Tango352Promo / Tango / data.z / ConnectionManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-02-03  |  3.1 KB  |  112 lines

  1. package com.everyware.tango.jas;
  2.  
  3. import java.io.IOException;
  4. import java.io.InterruptedIOException;
  5. import java.net.InetAddress;
  6. import java.net.ServerSocket;
  7. import java.net.SocketException;
  8. import java.net.UnknownHostException;
  9.  
  10. public class ConnectionManager extends Thread {
  11.    private JAS jas;
  12.    private ThreadManager threadManager;
  13.    private ServerSocket serverSocket;
  14.  
  15.    public ConnectionManager(JAS var1, ThreadManager var2) {
  16.       this.jas = var1;
  17.       this.threadManager = var2;
  18.       if (var1.isTracing) {
  19.          var1.traceM.trace("CM: Attempting to create server socket");
  20.       }
  21.  
  22.       try {
  23.          this.createServerSocket();
  24.       } catch (Exception var3) {
  25.          if (var1.isTracing) {
  26.             var1.traceM.trace("CM: Could not create server socket");
  27.          }
  28.  
  29.          var1.finalize();
  30.       }
  31.    }
  32.  
  33.    public void run() {
  34.       while(true) {
  35.          if (!this.threadManager.hasFreeResources()) {
  36.             if (this.jas.isTracing) {
  37.                this.jas.traceM.trace("CM: TM has no free resources, releasing control...");
  38.             }
  39.  
  40.             try {
  41.                Thread.sleep(5L);
  42.             } catch (InterruptedException var2) {
  43.             }
  44.          } else {
  45.             try {
  46.                if (this.jas.isTracing) {
  47.                   this.jas.traceM.trace("CM: Waiting for new requests");
  48.                }
  49.  
  50.                this.threadManager.newRequest(this.serverSocket.accept());
  51.                if (this.jas.isTracing) {
  52.                   this.jas.traceM.trace("CM: Passed new request to ThreadManager");
  53.                }
  54.             } catch (InterruptedIOException var3) {
  55.                if (this.jas.isTracing) {
  56.                   this.jas.traceM.trace("CM: No messages received within required server socket timeout");
  57.                }
  58.  
  59.                this.jas.finalize();
  60.             } catch (IOException var4) {
  61.                if (this.jas.isTracing) {
  62.                   this.jas.traceM.trace("CM: IOException caught: " + var4);
  63.                }
  64.             }
  65.          }
  66.       }
  67.    }
  68.  
  69.    private void createServerSocket() throws Exception {
  70.       String var1 = this.jas.getHostName();
  71.  
  72.       try {
  73.          InetAddress var7 = null;
  74.          if (var1 != null) {
  75.             var7 = InetAddress.getByName(var1);
  76.          }
  77.  
  78.          this.serverSocket = new ServerSocket(this.jas.getPort(), this.jas.getMaxNumBacklog(), var7);
  79.       } catch (UnknownHostException var4) {
  80.          String var6 = "CM: Could not obtain address for host " + (var1 == null ? "(null)" : var1);
  81.          if (this.jas.isTracing) {
  82.             this.jas.traceM.trace(var6);
  83.          }
  84.  
  85.          throw new Exception(var6);
  86.       } catch (IOException var5) {
  87.          String var2 = "CM: Could not instantiate server socket";
  88.          if (this.jas.isTracing) {
  89.             this.jas.traceM.trace(var2);
  90.          }
  91.  
  92.          throw new Exception(var2);
  93.       }
  94.  
  95.       try {
  96.          this.serverSocket.setSoTimeout(this.jas.getServerSocketTimeout() * 1000);
  97.       } catch (SocketException var3) {
  98.          String var8 = "CM: Could not set keep server socket timeout duration";
  99.          if (this.jas.isTracing) {
  100.             this.jas.traceM.trace(var8);
  101.          }
  102.  
  103.          throw new Exception(var8);
  104.       }
  105.  
  106.       if (this.jas.isTracing) {
  107.          this.jas.traceM.trace("CM: Server socket ready on hostname " + (var1 == null ? "(null)" : var1) + ", port " + this.jas.getPort() + ", max backlog " + this.jas.getMaxNumBacklog());
  108.       }
  109.  
  110.    }
  111. }
  112.