/* HitMan.java
   Copyright 2012 Tommy Skodje (http://www.antares.no)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package no.antares.clutil.hitman;

import java.util.Timer;


/** HitMan starts an external process, then listens for "HIT ME" messages on port, 
 * terminates process after deadline (that may be extended).
 * @author tommy skodje
 */
public class HitMan {
	private static final int ticksPerSecond	= 1000;

	/** Set up deadLine checker and start external process */
	public static void runHitMan( int port, String command ) {
		MessageChannel channel	= MessageChannel.openInbound( port );
		try {
			HitMan hitMan	= new HitMan( command );
			hitMan.messageLoop( channel );
		} finally {
			channel.close();
		}
	}

	private final ProcessControl process;
	private final Timer restarter;
	boolean terminated = false;

	private final DeadLine restartAtExpiry	= new DeadLine() {
		void expired() {
			process.restart();
		}
	};
	private final DeadLine shutdownAtExpiry	= new DeadLine() {
		void expired() {
			process.kill();
			terminated	= true;
		}
	};

	/** Start external process and deadLine checker */
	private HitMan( String command ) {
		process	= new ProcessControl( command );
		Runtime.getRuntime().addShutdownHook( process.killer );

		process.start();

		restarter	= DeadLineChecker.periodical( restartAtExpiry, 5 * ticksPerSecond ).startInMillis( 5 * ticksPerSecond );
	}

	/** Process messages on channel  */
	private void messageLoop( MessageChannel channel ) {
		while ( ! terminated ) {
			Message message	= channel.waitForNextMessage();
			if ( message.isExtension() )
				restartAtExpiry.extend( message );
			if ( message.isTermination() ) {
				DeadLineChecker.oneOff( shutdownAtExpiry ).startInMillis( message.deadLine() - System.currentTimeMillis() );
				restarter.cancel();
			}
		}
	}
	public static void main(String[] args) throws Exception {
		runHitMan( 5555, "/Applications/TextWrangler.app/Contents/MacOS/TextWrangler" );
	}

}

@@ -1346,38 +1346,8 @@
 er;%0D
-%0A%09boolean terminated = false;%0D
 %0A%0D%0A%09
@@ -1566,25 +1566,24 @@
 %0A%09%09%09
-terminated%09= true
+System.exit( 0 )
 ;%0D%0A%09
@@ -2027,16 +2027,47 @@
 el ) %7B%0D%0A
+%09%09boolean terminated%09= false;%0D%0A
 %09%09while 
@@ -2248,16 +2248,57 @@
 () ) %7B%0D%0A
+%09%09%09%09shutdownAtExpiry.extend( message );%0D%0A
 %09%09%09%09Dead
@@ -2428,16 +2428,29 @@
 cel();%0D%0A
+%09%09%09%09return;%0D%0A
 %09%09%09%7D%0D%0A%09%09
@@ -2516,16 +2516,16 @@
 tion %7B%0D%0A
-
 %09%09runHit
@@ -2596,21 +2596,71 @@
 angler%22 );%0D%0A
+%09%09// MessageChannel.send( 5555, %22HIT ME IN 2%22 );%0D%0A
 %09%7D%0D%0A%0D%0A%7D%0D%0A

Terminate now seems to work