package de.tudarmstadt.informatik.hostage.protocol.smbutils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import de.tudarmstadt.informatik.hostage.nio.Reader;
import de.tudarmstadt.informatik.hostage.nio.Writer;
import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
import de.tudarmstadt.informatik.hostage.protocol.SMB;
import de.tudarmstadt.informatik.hostage.wrapper.Packet;

/**
 * NetBIOS Session Service.
 * @author Wulf Pfeiffer
 */
public class NBSS extends Thread {
	
	private int nbssPort;
	private ServerSocket nbssServer;
	private Socket nbssSocket;
	private Reader reader;
	private Writer writer;
	private SMB smb;
	
	@Override
	public void run() {
		try {
			nbssPort = 139;
			nbssServer = new ServerSocket(nbssPort);
			nbssSocket = nbssServer.accept();
			smb = new SMB();
			talkToClient(nbssSocket.getInputStream(), nbssSocket.getOutputStream());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Used for communicating with a client.
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	private void talkToClient(InputStream in, OutputStream out) throws IOException {
		reader = new Reader(in, smb.toString());
		writer = new Writer(out);
		Packet inputLine;
		List<Packet> outputLine;
		if (smb.whoTalksFirst() == TALK_FIRST.SERVER) {
			outputLine = smb.processMessage(null);
			writer.write(outputLine);
		}
		while (!isInterrupted() && (inputLine = reader.read()) != null) {
			outputLine = smb.processMessage(inputLine);
			if (outputLine != null) {
				writer.write(outputLine);
			}
			if (smb.isClosed()) {
				break;
			}
		}
	}
	
}
