/*
 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.
 *
 * EXPORT CONTROLLED
 * ECCN#   EAR99
 * Warning:  This document contains technical data which is controlled under the
 * Bureau of Industry & Security's Export Administration Regulations and may not be
 * exported or re-exported without authorization by the Bureau of Industry & Security.
 * Diversion contrary to US law is prohibited.
 *
 * (c) 2011 United Technologies Corp.
 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,
 * including any software or models in source or binary form, as well as any drawings, 
 * specifications, and documentation (collectively "the Data"), to deal in the Data without 
 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Data.
 *
 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE
 * USE OR OTHER DEALINGS IN THE DATA.
 */
package meta.ampl.runner;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;

/**
 * Wait for an AMPL request to complete
 */
public class AMPLwaitSave extends Thread   {
    private BlockingQueue<Integer> q;
    private File file;
    private File fileRenamed;
    private UUID uid; 
    private static final int MAX_TIMES = 60;
	/**
	 * Create an exception with a single error message
	 * @param msg  The message
	 */
	public AMPLwaitSave(File file, BlockingQueue<Integer> q )    {
		this.file = file;
		this.q = q; 
		uid = UUID.randomUUID();
		 
			String fp = file.getPath();
			fileRenamed = new File(String.format("%s_%s",fp,uid.toString()));
	}
	/**
	 * Try to rename the file. if successful, the AMPL process 
	 * finished writing to it and closed it. If not, wait and
	 * try again. Once renamed, rename it back.
	 */
	@Override
	public void run() {
	   int rc = rename(file,fileRenamed);	
	   if ( rc > 0 ) {
		   q.add(new Integer(rc));
		   return;
	   }
	   rc = rename(fileRenamed,file);	
	   long length = file.length();
	   System.out.println("File length:" + length);
	   q.add(new Integer(rc));
	}
	private int rename(File f1, File f2) {
	   int count = MAX_TIMES;	
	   while ( count > 0 ) {
		  boolean rc = f1.renameTo(f2);
		  if ( rc ) return 0;
		  try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		 count--;
	   }
	   return 1;
	}
}
