/************************************************
* Columbia University CS Department
* Systems and Security Group
*
* Open and Survivable Embedded Systems (OASES)
*
* Michael E. Locasto
*
* Protocol For Code Exchange
*  in Survivable Embedded Systems (PCXSES)
*
* This software is provided as is without any
* warranty or guarantee of merchantibility
* or fitness for a particular purpose. This
* software is released under the terms of the
* GNU General Public License (GPL).
*
* Last updated: November 19, 2002
*
***********************************************/

package locasto.pcxses.ac;

import locasto.logging.Logger;

import java.io.*;
import java.util.Vector;

/**
 * A thread that will push out to the client so
 * that the AC and RepositoryManager do not have
 * to spend time doing so.
 *
 *
 *
 *
 *
 *
 *
 * @author locasto@cs.columbia.edu
*/
//public class RepositoryWorker implements Runnable{
public class RepositoryWorker{


	private String [] classnames = new String[0];
	//private DataOutputStream dout=null;
	private RepositoryManager parent;

	//public RepositoryWorker( RepositoryManager parent, String [] classnames, DataOutputStream dout ){
	public RepositoryWorker( RepositoryManager parent, String [] classnames ){
		this.parent = parent;
		this.classnames = classnames;
		//this.dout = dout;
	}


	//public void run(){
	public Vector retrieve(){

		File f=null;
		if( classnames == null ){
			parent.log( "RepositoryWorker has a null classnames target. Cannot do lookups." );
			return null;
		}

		Vector v = new Vector( classnames.length );


		for( int i=0;i<classnames.length;i++ ){

			try{

				f = parent.lookup( classnames[ i ] );

				if( f!=null ){
					//push( f, dout );
					byte [] b = pull( f );
					if( b==null || b.length==0 )
						throw new Exception( "RepositoryWorker cannot add zero length class." );

					v.addElement( b );

				}else{
					parent.log( "RepositoryWorker.lookup() returned a null file for ["+classnames[ i ]+"]." );
				}

			}catch( Exception ex ){

				parent.log( "RepositoryWorker had a problem pulling the class file ["+classnames[i]+"] "+ex.getMessage() );

			}

		}

		return v;

	}

	private byte [] pull( File f ){


		String name=f.getName();
		int totalFileBytes = (int)f.length();
		byte [] b = new byte[ totalFileBytes ];

		try{

			parent.log( "Bytes in file ["+name+"]="+totalFileBytes );

			FileInputStream fin = new FileInputStream( f );

			int i=fin.read( b );
			//if( i==-1 )
			parent.log( "RepositoryWorker finished reading file ["+name+"] in, b contains "+i+" bytes." );
			//while( i != -1 ){
			//	b[c] = i;
			//	i=fin.read();
			//}

			fin.close();

			return b;

		}catch( IOException ioex ){
			parent.log( "RepositoryWorker cannot process file ["+name+"]."+ioex.getMessage() );
			//ioex.printStackTrace();
		}

		return (new byte[0]);
	}

	/** precondition: f is not null
	 * @deprecated
	*/
	private void push( File f, DataOutputStream client ){

		String name=f.getName();
		int totalFileBytes = (int)f.length();

		try{

			parent.log( "Bytes in file ["+name+"]="+totalFileBytes );

			FileInputStream fin = new FileInputStream( f );

			int i=fin.read();
			while( i != -1 ){
				client.writeByte( i );
				i=fin.read();
			}
			fin.close();
			client.flush();
			//client.close();
		}catch( IOException ioex ){
			parent.log( "RepositoryWorker cannot process file ["+name+"]."+ioex.getMessage() );
			//ioex.printStackTrace();
		}

	}

}
