// Path: Source/src/de/tum/in/www1/jReto/module/api/Advertiser.java
// public interface Advertiser {
// 	/**
// 	* The Advertiser.Handler interface allows the Advertiser to inform its delegate about various events.
// 	*/
// 	public static interface Handler {
// 	    /** Called when the advertiser started advertising. */
// 		void onAdvertisingStarted(Advertiser advertiser);
// 	    /** Called when the advertiser stopped advertising. */
// 		void onAdvertisingStopped(Advertiser advertiser);
// 	    /** Called when the advertiser received an incoming connection from a remote peer. */
// 		void onConnection(Advertiser advertiser, Connection connection);
// 	}
// 	
// 	/** Sets the advertiser's delegate. */
// 	void setAdvertiserHandler(Handler handler);
// 	/** Returns the advertiser's delegate. */
// 	Handler getAdvertiserHandler();
//     /** Whether the advertiser is currently active. */
// 	boolean isAdvertising();
//     /** 
//     * Starts advertising.
//     * @param identifier A UUID identifying the local peer.
//     */
// 	void startAdvertisingWithPeerIdentifier(UUID identifier);
//     /**
//     * Stops advertising.
//     */
// 	void stopAdvertising();
// }
// 
// Path: Source/src/de/tum/in/www1/jReto/module/api/Connection.java
// public interface Connection {
// 	/** 
// 	* The Connection.Handler interface allows the Connection to inform its delegate about various events.
// 	*/
// 	public static interface Handler {
// 	    /** Called when the connection connected successfully.*/
// 		void onConnect(Connection connection);
// 	    /** Called when the connection closes. Has an optional error parameter to indicate issues. (Used to report problems to the user). */
// 		void onClose(Connection connection);
// 	    /** Called when data was received. */
// 		void onDataReceived(Connection connection, ByteBuffer data);
// 	    /** Called for each writeData call, when it is complete. */
// 		void onDataSent(Connection connection);
// 	}
// 	
//     /** Sets the connection's delegate. */
// 	void setHandler(Handler handler);
//     /** The connection's delegate. */
// 	Handler getHandler();
// 	
//     /** Whether this connection is currently connected. */
// 	boolean isConnected();
//     /** Reto sends packets which may vary in size. This property may return an ideal packet size that should be used if possible. */
// 	int getRecommendedPacketSize();
// 	
//     /** Connects the connection. */
// 	void connect();
//     /** Closes the connection. */
// 	void close();
// 	
//     /** Sends data using the connection. */
// 	void writeData(ByteBuffer data);
// }
// 
// Path: Source/src/de/tum/in/www1/jReto/util/StartStopHelper.java
// public class StartStopHelper {
// 	/**
// 	 * Represents the desired states this class should help reach.
// 	 * */
// 	private static enum State {
// 		Started, 
// 		Stopped
// 	}
// 	
// 	/** A RetryableActionExecutor that attempts to exectute the start action */
// 	private final RetryableActionExecutor starter;
// 	/** A RetryableActionExecutor that attempts to exectute the stop action */
// 	private final RetryableActionExecutor stopper;
// 	
// 	/** The state that should be reached. 
// 	 * 
// 	 * E.g.: When the switching the desired state to the started state (by calling start), the StartStopHelper will call 
// 	 * the start action until it is notified about a successful start via onStart(). */
// 	private State desiredState = State.Stopped;
// 	
// 	/**
// 	 * Creates a new StartStopHelper.
// 	 * 
// 	 * @param startAction The start action
// 	 * @param stopAction The stop action
// 	 * @param timerSettings The timer settings used to retry the start and stop actions
// 	 * @param executor The executor to execute the start and stop action on.
// 	 * */
// 	public StartStopHelper(RetryableAction startAction, RetryableAction stopAction, Timer.BackoffTimerSettings timerSettings, Executor executor) {
// 		this.starter = new RetryableActionExecutor(startAction, timerSettings, executor);
// 		this.stopper = new RetryableActionExecutor(stopAction, timerSettings, executor);
// 	}
// 	
// 	/**
// 	 * Runs the startAction in delays until onStart is called.
// 	 * */
// 	public void start() {
// 		this.desiredState = State.Started;
// 		
// 		this.stopper.stop();
// 		this.starter.start();
// 	}
// 	/**
// 	 * Runs the startAction in delays until onStart is called.
// 	 * */
// 	public void stop() {
// 		this.desiredState = State.Stopped;
// 		
// 		this.starter.stop();
// 		this.stopper.start();
// 	}
// 
// 	/**
// 	 * Call this method when the startAction succeeds, or a start occurs for another reason. Stops calling the start action. Starts calling the stop action if the stop() was called last (as opposed to start()).
// 	 * */
// 	public void onStart() {
// 		this.starter.stop();
// 		if (this.desiredState == State.Stopped) this.stopper.start();
// 	}
// 	/**
// 	 * Call this method when the stopAction succeeds, or a start occurs for another reason. Stops calling the stop action. Starts calling the start action if the start() was called last (as opposed to stop()).
// 	 * */
// 	public void onStop() {
// 		this.stopper.stop();
// 		if (this.desiredState == State.Started) this.starter.start();
// 	}
// }


import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Executor;
import de.tum.in.www1.jReto.module.api.Advertiser;
import de.tum.in.www1.jReto.module.api.Connection;
import de.tum.in.www1.jReto.util.StartStopHelper;
package de.tum.in.www1.jReto.routing.managed_module;



/**
 * A ManagedAdvertiser automatically attempts to restart an Advertiser if starting the advertiser failed. The same concept applies to stopping the advertiser.
 * */
public class ManagedAdvertiser implements Advertiser, Advertiser.Handler {	
	private final Advertiser advertiser;
// Path: Source/src/de/tum/in/www1/jReto/module/api/Advertiser.java
// public interface Advertiser {
// 	/**
// 	* The Advertiser.Handler interface allows the Advertiser to inform its delegate about various events.
// 	*/
// 	public static interface Handler {
// 	    /** Called when the advertiser started advertising. */
// 		void onAdvertisingStarted(Advertiser advertiser);
// 	    /** Called when the advertiser stopped advertising. */
// 		void onAdvertisingStopped(Advertiser advertiser);
// 	    /** Called when the advertiser received an incoming connection from a remote peer. */
// 		void onConnection(Advertiser advertiser, Connection connection);
// 	}
// 	
// 	/** Sets the advertiser's delegate. */
// 	void setAdvertiserHandler(Handler handler);
// 	/** Returns the advertiser's delegate. */
// 	Handler getAdvertiserHandler();
//     /** Whether the advertiser is currently active. */
// 	boolean isAdvertising();
//     /** 
//     * Starts advertising.
//     * @param identifier A UUID identifying the local peer.
//     */
// 	void startAdvertisingWithPeerIdentifier(UUID identifier);
//     /**
//     * Stops advertising.
//     */
// 	void stopAdvertising();
// }
// 
// Path: Source/src/de/tum/in/www1/jReto/module/api/Connection.java
// public interface Connection {
// 	/** 
// 	* The Connection.Handler interface allows the Connection to inform its delegate about various events.
// 	*/
// 	public static interface Handler {
// 	    /** Called when the connection connected successfully.*/
// 		void onConnect(Connection connection);
// 	    /** Called when the connection closes. Has an optional error parameter to indicate issues. (Used to report problems to the user). */
// 		void onClose(Connection connection);
// 	    /** Called when data was received. */
// 		void onDataReceived(Connection connection, ByteBuffer data);
// 	    /** Called for each writeData call, when it is complete. */
// 		void onDataSent(Connection connection);
// 	}
// 	
//     /** Sets the connection's delegate. */
// 	void setHandler(Handler handler);
//     /** The connection's delegate. */
// 	Handler getHandler();
// 	
//     /** Whether this connection is currently connected. */
// 	boolean isConnected();
//     /** Reto sends packets which may vary in size. This property may return an ideal packet size that should be used if possible. */
// 	int getRecommendedPacketSize();
// 	
//     /** Connects the connection. */
// 	void connect();
//     /** Closes the connection. */
// 	void close();
// 	
//     /** Sends data using the connection. */
// 	void writeData(ByteBuffer data);
// }
// 
// Path: Source/src/de/tum/in/www1/jReto/util/StartStopHelper.java
// public class StartStopHelper {
// 	/**
// 	 * Represents the desired states this class should help reach.
// 	 * */
// 	private static enum State {
// 		Started, 
// 		Stopped
// 	}
// 	
// 	/** A RetryableActionExecutor that attempts to exectute the start action */
// 	private final RetryableActionExecutor starter;
// 	/** A RetryableActionExecutor that attempts to exectute the stop action */
// 	private final RetryableActionExecutor stopper;
// 	
// 	/** The state that should be reached. 
// 	 * 
// 	 * E.g.: When the switching the desired state to the started state (by calling start), the StartStopHelper will call 
// 	 * the start action until it is notified about a successful start via onStart(). */
// 	private State desiredState = State.Stopped;
// 	
// 	/**
// 	 * Creates a new StartStopHelper.
// 	 * 
// 	 * @param startAction The start action
// 	 * @param stopAction The stop action
// 	 * @param timerSettings The timer settings used to retry the start and stop actions
// 	 * @param executor The executor to execute the start and stop action on.
// 	 * */
// 	public StartStopHelper(RetryableAction startAction, RetryableAction stopAction, Timer.BackoffTimerSettings timerSettings, Executor executor) {
// 		this.starter = new RetryableActionExecutor(startAction, timerSettings, executor);
// 		this.stopper = new RetryableActionExecutor(stopAction, timerSettings, executor);
// 	}
// 	
// 	/**
// 	 * Runs the startAction in delays until onStart is called.
// 	 * */
// 	public void start() {
// 		this.desiredState = State.Started;
// 		
// 		this.stopper.stop();
// 		this.starter.start();
// 	}
// 	/**
// 	 * Runs the startAction in delays until onStart is called.
// 	 * */
// 	public void stop() {
// 		this.desiredState = State.Stopped;
// 		
// 		this.starter.stop();
// 		this.stopper.start();
// 	}
// 
// 	/**
// 	 * Call this method when the startAction succeeds, or a start occurs for another reason. Stops calling the start action. Starts calling the stop action if the stop() was called last (as opposed to start()).
// 	 * */
// 	public void onStart() {
// 		this.starter.stop();
// 		if (this.desiredState == State.Stopped) this.stopper.start();
// 	}
// 	/**
// 	 * Call this method when the stopAction succeeds, or a start occurs for another reason. Stops calling the stop action. Starts calling the start action if the start() was called last (as opposed to stop()).
// 	 * */
// 	public void onStop() {
// 		this.stopper.stop();
// 		if (this.desiredState == State.Started) this.starter.start();
// 	}
// }


// Path: Source/src/de/tum/in/www1/jReto/routing/managed_module/ManagedAdvertiser.java
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.Executor;
import de.tum.in.www1.jReto.module.api.Advertiser;
import de.tum.in.www1.jReto.module.api.Connection;
import de.tum.in.www1.jReto.util.StartStopHelper;

package de.tum.in.www1.jReto.routing.managed_module;



/**
 * A ManagedAdvertiser automatically attempts to restart an Advertiser if starting the advertiser failed. The same concept applies to stopping the advertiser.
 * */
public class ManagedAdvertiser implements Advertiser, Advertiser.Handler {	
	private final Advertiser advertiser;
	private final StartStopHelper startStopHelper;