# include "pantasks.h"

/* pantasks-server is not a readline program.
   it runs three (or four) threads:
   - one thread listens on the socket for incomming connections 
     and performs client validation
   - one thread parses commands from the connected clients
   - one thread runs the scheduler loop
   - one thread may run the controller loop independently
   
   * note: the client shell traps certain commands and performs them
   locally. at a minimum: help.  perhaps: input, other I/O commands?
*/

/* program-dependent initialization */
int main (int argc, char **argv) {
  
  pthread_t jobsThread;
  pthread_t tasksThread;
  pthread_t inputsThread;
  pthread_t clientsThread;
  pthread_t controllerThread;
  int InitSocket, BindSocket;
  SockAddress Address;

  general_init (&argc, argv);
  DefineValidIP ();

  /* XXX not sure what is a good setting for this in the server */
  auto_break = TRUE;

  /* load the commands used by this implementation */
  InitBasic ();
  InitData ();
  InitPantasksServer ();
  
  InitInputs ();

  signal (SIGPIPE, gotsignal);
  // signal (SIGTSTP, gotsignal);
  // signal (SIGTTIN, gotsignal);
  signal (SIGINT, SIG_DFL);

  startup (&argc, argv);

  /* start up the background threads here */
  pthread_create (&clientsThread,    NULL, &ListenClients,    	   NULL);
  pthread_create (&tasksThread,      NULL, &CheckTasksThread, 	   NULL);
  pthread_create (&jobsThread,       NULL, &CheckJobsThread,  	   NULL);
  pthread_create (&controllerThread, NULL, &CheckControllerThread, NULL);
  pthread_create (&inputsThread,     NULL, &CheckInputsThread,     NULL);

  /* in this loop, we listen for incoming connections, validate, and
     pass them to the ListenClient thread */

  /* create the listening socket */
  InitSocket = InitServerSocket (&Address);
  
  InitPassword ();

  while (1) {

    /* wait for clients to make connection */
    BindSocket = WaitServerSocket (InitSocket, &Address);
    if (BindSocket == -1) continue;

    /* validate : wait for password */
    if (!CheckPassword (BindSocket)) continue;
    
    AddNewClient (BindSocket);
  }
}

/* add program-dependent exit functions here */
void cleanup () {
  // QuitImage ();
  // QuitGraph ();
  // QuitController ();
  return;
}

void gotsignal (int signum) {
  gprint (GP_ERR, "got signal : %d\n", signum);
  return;
}
