/*
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2011 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation. 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details. You should have received
 * a copy of the GNU Lesser General Public License along with this library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.ccnx.ccn.profiles.ccnd;

import java.io.IOException;

import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.CCNInterestListener;
import org.ccnx.ccn.config.SystemConfiguration;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.ContentObject;
import org.ccnx.ccn.protocol.Interest;

public class CCNDCacheManager implements CCNInterestListener {
	
	public void clearCache(ContentName prefix, CCNHandle handle, long timeout) throws IOException {
		Interest interest = new Interest(prefix);
		interest.answerOriginKind(Interest.DEFAULT_ANSWER_ORIGIN_KIND + Interest.MARK_STALE);
		interest.scope(0);
		long currentTime = System.currentTimeMillis();
		long endTime = currentTime + timeout;
		boolean noTimeout = timeout == SystemConfiguration.NO_TIMEOUT;
		while (noTimeout || currentTime < endTime) {
			handle.expressInterest(interest, this);
			synchronized (this) {
				try {
					this.wait(SystemConfiguration.SHORT_TIMEOUT);
				} catch (InterruptedException e) {}
			}
			long prevTime = currentTime;
			currentTime = System.currentTimeMillis();
			if (currentTime - prevTime > SystemConfiguration.SHORT_TIMEOUT)
				return;
		}
		throw new IOException("Clear Cache timed out before completion");
	}

	public Interest handleContent(ContentObject data, Interest interest) {
		synchronized (this) {
			this.notifyAll();
		}
		return interest;
	}
}

@@ -828,16 +828,48 @@
 ception;
+%0Aimport java.util.logging.Level;
 %0A%0Aimport
@@ -978,24 +978,62 @@
 figuration;%0A
+import org.ccnx.ccn.impl.support.Log;%0A
 import org.c
@@ -1767,37 +1767,38 @@
 emConfiguration.
-SHORT
+MEDIUM
 _TIMEOUT);%0A%09%09%09%09%7D
@@ -1970,22 +1970,178 @@
 ion.
-SHORT_TIMEOUT)
+MEDIUM_TIMEOUT) %7B%0A%09%09%09%09if (Log.isLoggable(Log.FAC_NETMANAGER, Level.FINER))%0A%09%09%09%09%09Log.finer(Log.FAC_NETMANAGER, %22ClearCache finished after %7B0%7D ms%22, currentTime - prevTime);
 %0A%09%09%09
@@ -2151,18 +2151,80 @@
 turn;%0A%09%09
+%09
 %7D%0A
+%09%09%7D%0A%09%09Log.warning(%22ClearCache timed out before completion%22);%0A
 %09%09throw 
@@ -2245,17 +2245,16 @@
 n(%22Clear
- 
 Cache ti
@@ -2403,16 +2403,137 @@
 yAll();%0A
+%09%09%09if (Log.isLoggable(Log.FAC_NETMANAGER, Level.FINER))%0A%09%09%09%09Log.finer(Log.FAC_NETMANAGER, %22Set %7B0%7D stale%22, data.name());%0A
 %09%09%7D%0A%09%09re

Increase timeout in cache clear, add log messages refs #100367