// Copyright, 2001, Astra Network Inc.  All Rights Reserved

// This source code has been published by Astra Network Inc. However, any
// use, reproduction, modification, distribution or transfer of this
// software, or any software which includes or is based upon any of this
// code, is only permitted if expressly authorized by a written license
// agreement from Astra. Contact your Astra representitive directly for
// more information.

//
// This example script keeps the programs server and client running.


// Prototype modules required by this script.

include "exec.include";
include "getpidbyname.include";
include "log.include";
include "logf1.include";

define POLL_INTERVAL 100

system: client
{	
  // Create global instances of the modules required
  exec launch();
  getpidbyname getpid();
  log logerr(0, "test");
  logf1 logserverpid(0, "test", "server pid is %s");
  logf1 logclientpid(0, "test", "client pid is %s");

  // check once per POLL_INTERVAL milliseconds to see if client is alive
  process  client POLL_INTERVAL: server
  {
    // run this once to start it
    start
    {
      // check to see if it was started by someone else in which case we
      // just record the pid.
      client = getpid( "client" );
	  if( client == 0)
	    {
	      launch("./client");
	      logerr(5, "Starting client");
	      client = getpid("client");
	    }
	  endif;
	  logclientpid(5, client );
	}

      restart
	{
	  client = getpid("client");
	  if( client == 0 )
	    {
	      launch("./client");
	      logerr(5, "Restarting client");
	      client = getpid("client");
	    }
	  else
	    {
	      logerr( 5, "No restart required");
	    }
	  endif;
	  logclientpid(5, client );
	}
      dependfail
	{
	  logerr(5, "client needs server running");
	}
    }
  
  process  server POLL_INTERVAL:
    {
      // run this once to start it
      start
	{
	  // check to see if it was started by someone else in which case we
	  // just record the pid.
	  server = getpid( "server" );
	  if( server == 0)
	    {
	      launch("./server");
	      logerr(5, "Starting server");
	      server = getpid("server");
	    }
	  endif;
	  logserverpid(5, server );
	}
      
      restart
	{
	  server = getpid("server");
	  if( server == 0 )
	    {
	      launch("./server");
	      logerr(5, "Restarting server");
	      server = getpid("server");
	    }
	  else
	    {
	      logerr( 5, "No restart required");
	    }
	  endif;
	  logserverpid(5, server );
	}
    }
}

