/*
 * CS255: Programming Project #1
 *
 * AuthorityServerThread.java 
 */

//package eLecture;

import java.io.*;
import java.net.*;
import java.util.*;

import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;

import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;

public class AuthorityServerThread extends Thread {
    
    // Master password
    char[] masterpwd = null; 

    // Encrypted password file
    String encpasswdfile = null;
    
    // Sockets for network connection
    private Socket clientSocket = null; 
    private DataOutputStream toClient = null;
    private DataInputStream fromClient = null;
        
    //
    // Your variable declarations (if any) can go here:
    //    

    public AuthorityServerThread(Socket csock, char[] pwd, String encf) {
	clientSocket = csock;
	masterpwd = pwd;
        encpasswdfile = encf;
    }

    public void run() {    
	
        // Set up communication streams
        try {	    
	    toClient = new DataOutputStream(clientSocket.getOutputStream());
	    fromClient = new DataInputStream(clientSocket.getInputStream());
	}
	catch( IOException iox ) {	    
	    System.out.println(iox.getMessage());	    
	    iox.printStackTrace();
	}

        //
        // Most of your code can go here, right now we just receive a message
        // from the client and echo if back. Your code should authenticate the 
        // client and provide him with the key for the protected content.        
        // 
        	
	// Receive a message from the client.
        String message = null;
        try {
	    byte[] authToken = fromClient.read();
	}	
	catch(IOException iox) {
	    System.out.println(iox.getMessage());
	    iox.printStackTrace();
	}

        // Send a message to the client.
	toClient.println("Client says: " + message);

        // Close the connection 
	try { 
	    toClient.close();
	    fromClient.close();	    
	    clientSocket.close();
	}
	catch(IOException iox) {
	    System.out.println(iox.getMessage());
	    iox.printStackTrace();
	}	
    }
}
