Connection.java

/*
* The contents of this file are subject to the BT "ZEUS" Open Source
* Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
* except in compliance with the Licence. You may obtain a copy of the Licence
* from $ZEUS_INSTALL/licence.html or alternatively from
* http://www.labs.bt.com/projects/agents/zeus/licence.htm
*
* Except as stated in Clause 7 of the Licence, software distributed under the
* Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the Licence for the specific language governing rights and
* limitations under the Licence.
*
* The Original Code is within the package zeus.*.
* The Initial Developer of the Original Code is British Telecommunications
* public limited company, whose registered office is at 81 Newgate Street,
* London, EC1A 7AJ, England. Portions created by British Telecommunications
* public limited company are Copyright 1996-9. All Rights Reserved.
*
* THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
*/



package zeus.actors;

import java.net.*;
import java.io.*;
import java.util.*;
import zeus.util.*;
import zeus.concepts.ZeusParser;
import zeus.concepts.Performative;

//added for secure socket support 5/10/2001
import javax.net.ssl.*;
    
class Connection extends Thread {
  protected static int BUF_SIZ = 8192;

  protected SSLSocket	client;
  protected Server    	server;
  protected InputStream ins;

  // Initialize the streams and start the thread
  public Connection(SSLSocket client, Server server ) {
        this.client = client;
    this.server = server;

    // Create the streams
    try {

      ins = client.getInputStream();
    }
    catch (IOException e) {
      System.err.println("Exception while getting socket streams: " + e);
      e.printStackTrace();
      try {
	client.close();
	client = null;
      }
      catch (IOException e2) {
	System.err.println("Exception while closing client: " + e2);
	e2.printStackTrace();
      }
      return;
    }
    // Start the thread up
    // this.setPriority(this.getPriority()-2);
    this.start();
  }

  public void run() {
    String text = "";
    byte buf[] = new byte[BUF_SIZ];
    int x = 0;
    boolean done = false;

    while ( !done ) {
      try {
	x = ins.read(buf);
      }
      catch (IOException e) {
	Core.DEBUG(1,"IOException: " + e);
	e.printStackTrace();
	server.updateCount(-1);
	try {
	  ins.close();
	  ins = null;
	  client.close();
	  client = null;
	}
	catch (IOException e1) {
	  System.err.println(e1);
	  e1.printStackTrace();
	}
	return;
      }

      if ( x == -1 ) {
	done = true;
      }
      else {
	text += new String(buf,0,x);
      }
    }
    buf = null;

    if ( text.equals("") ) {
      System.err.println("No data read from stream");
      server.updateCount(-1);
      return;
    }

    Performative msg = ZeusParser.performative(text);
    if ( !msg.isValid() ) {
      server.updateCount(-1);
      return;
    }

    server.newMsg(msg);
    server.updateCount(-1);
  }

  protected void finalize() {
    try {
      if ( ins != null    ) ins.close();
      if ( client != null ) client.close();
    }
    catch (IOException e) {
    }
  }

  public String toString() {
    return this.getName() + " connected to: " + client.getInetAddress().getHostName() + ":" + client.getPort();
  }
}

Last modified: Sun Oct 31 21:07:50 PST 2004