/* * [PersistReceiverState.java] * * Summary: Persist receiver state of what has already been downloaded in the registry. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2007-07-18 Created with IntelliJ IDEA. */ package com.mindprod.replicator; import com.mindprod.common18.EIO; import com.mindprod.replicatorcommon.Config; import com.mindprod.replicatorcommon.MiniFD; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import static java.lang.System.*; /** * Persist receiver state of what has already been downloaded in the registry. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2007-07-18 Created with IntelliJ IDEA. * @since 2007 */ final class PersistReceiverState { /** * Layout version number. */ private static final long serialVersionUID = 501L; /** * The most recent file we have processed so far. Since we process zips in order, we must have handled everything * prior to this. We need not download zips prior to this. */ static long clumpingProcessedSoFar; /** * restore state of Receiver from receiver.ser */ static void restoreStateFromReceiverSer() { String persistFilename = ConfigForReceiver.RECEIVER_ZIP_STAGING_DIR + File.separatorChar + ConfigForReceiver.PERSIST_RECEIVER_SER; try { ObjectInputStream ois = EIO.getObjectInputStream( new File( persistFilename ), 4 * 1024, false ); long fileSerialVersionUID = ( Long ) ois.readObject(); if ( fileSerialVersionUID != PersistReceiverState.serialVersionUID ) { throw new IOException( "wrong version of " + ConfigForReceiver .PERSIST_RECEIVER_SER + " got: " + fileSerialVersionUID + " expected: " + MiniFD .serialVersionUID ); } clumpingProcessedSoFar = ( Long ) ois.readObject(); Log.println( "So far files have been downloaded up to " + Config.TIMESTAMP_MILLISECOND_FORMAT .format( clumpingProcessedSoFar ) ); ois.close(); } catch ( Exception e ) { final String trouble = new StringBuilder( 100 ).append( "Unable to retrieve previous program state from " ) .append( persistFilename ) .append( "\n" ) .append( e.getMessage() ) .append( "\n" ) .append( "Incompatible version. Starting over." ) .toString(); err.println( trouble ); Log.println( trouble ); clumpingProcessedSoFar = 0; } } /** * Save state of client in receiver.ser */ static void saveStateToReceiverSer() { String persistFilename = ConfigForReceiver .RECEIVER_ZIP_STAGING_DIR + File .separatorChar + ConfigForReceiver .PERSIST_RECEIVER_SER; try { FileOutputStream fos = new FileOutputStream( persistFilename ); ObjectOutputStream oos = new ObjectOutputStream( fos ); oos.writeObject( PersistReceiverState.serialVersionUID ); oos.writeObject( clumpingProcessedSoFar ); oos.close(); } catch ( IOException e ) { Replicator.fatal( "Unable to save system state in receiver.ser." + "\n" + e.getMessage() ); } } }