package primer12;

import java.net.*;
import java.io.*;
import java.util.*;

public class LookupThread implements Runnable {

	private List<String> entries;
	private PooledWeblog log;

	public LookupThread(List<String> entries, PooledWeblog log) {
		this.entries = entries;
		this.log = log;
	}

	public void run() {

		String entry;

		while (true) {

			synchronized (entries) {
				while (entries.size() == 0) {
					if (log.isFinished()) {
						System.out.println("Kraj niti");
						return;
					}
					try {
						entries.wait();
					} catch (InterruptedException ex) {
					}
				}
				entry = entries.remove(entries.size() - 1);
			}

			int index = entry.indexOf(' ', 0);
			String remoteHost = entry.substring(0, index);
			String theRest = entry.substring(index, entry.length());

			try {
				remoteHost = InetAddress.getByName(remoteHost).getHostName();
			} catch (Exception ex) {

			}

			try {
				log.log(remoteHost + theRest);
			} catch (IOException ex) {
			}

			Thread.yield();
		}
	}
}