Functions

batch_template.c File Reference

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/param.h>
#include "utility.h"
#include "problem.h"
#include "comm_protocol.h"
#include "comm_basics.h"
#include "comm_data.h"
#include "comm_encode.h"
Include dependency graph for batch_template.c:

Go to the source code of this file.

Functions

int gs_problem_service (gs_problem_t *)
int service_template (int argc, char *argv[])

Detailed Description

This file contains a generic service template for the end service.

Definition in file batch_template.c.


Function Documentation

int gs_problem_service ( gs_problem_t *   ) 

Here is the caller graph for this function:

int service_template ( int  argc,
char *  argv[] 
)

This is a generic "batch service template" which forms the basis for the executable service that is forked/execed by the server and intended to be run in some sort of batch mode.

Definition at line 38 of file batch_template.c.

{
  gs_problem_t *problem;
  char *reqdir;
  int i;

#ifdef __CYGWIN__
  reqdir = argv[1];
#else
  reqdir = strdup(argv[0]);
  if(!reqdir)
    exit(GS_SVC_ERR_MALLOC);
  for(i=strlen(reqdir);i>=0;i--) {
    if(reqdir[i] == '/') {
      reqdir[i] = 0;
      break;
    }
  }
#endif
  fprintf(stderr, "CWD (reqdir) = '%s'\n", reqdir);

  if(chdir(reqdir) < 0) {
    fprintf(stderr, "Failed to chdir to reqdir: '%s'\n", reqdir);
    exit(GS_SVC_ERR_CHDIR);
  }

  problem = (gs_problem_t *) malloc(sizeof(gs_problem_t));

  if(!problem)
    exit(GS_SVC_ERR_MALLOC);

  /* Look for the service corresponding to the requested problem */
  if(gs_read_problem_from_file(GS_BATCH_XML, problem) < 0) {
    ERRPRINTF("Error loading service: '%s'.\n", GS_BATCH_XML);
    exit(GS_SVC_ERR_MISSING_XML);
  }
  else {
    char *problemstr = NULL;
    int my_dsig, fd = 0;
    double service_et;
    FILE *xmlfile;

    my_dsig = pvmgetdsig();

    if(gs_restore_input_args_from_file("input", problem, my_dsig, my_dsig) < 0) {
      ERRPRINTF("Error receiving input args.\n");
      exit(GS_SVC_ERR_RESTORE_ARGS);
    }

    service_et = gs_problem_service(problem);

    if(chdir(reqdir) < 0) {
      ERRPRINTF("Could not cd back to request directory '%s'.\n", reqdir);
      exit(GS_SVC_ERR_CHDIR);
    }

    xmlfile = fopen("problem.xml", "w");

    if(!xmlfile) {
      ERRPRINTF("Could not create xml file.\n");
      exit(GS_SVC_ERR_CREATE_XML);
    }

    if(gs_encode_problem(&problemstr, problem) < 0) {
      ERRPRINTF("Could not encode problem.\n");
      exit(GS_SVC_ERR_PROBLEM_ENC);
    }

    fprintf(xmlfile, "%s\n", problemstr);

    fclose(xmlfile);

    fd = open("data", O_WRONLY | O_CREAT, 0600);

    if(fd < 0) {
      ERRPRINTF("Could not create data file.\n");
      exit(GS_SVC_ERR_CREAT_DATA_FILE);
    }

    if(gs_save_output_args_to_file(fd, problem, my_dsig) < 0) {
      ERRPRINTF("Error sending output args.\n");
      exit(GS_SVC_ERR_IO);
    }

    close(fd);

    if(gs_create_timestamp_file(".", "done", service_et) < 0) {
      ERRPRINTF("Could not create completion file.\n");
      exit(GS_SVC_ERR_COMPLETION_FILE);
    }

    exit(0);
  }

  ERRPRINTF("Service terminating abnormally\n");
  exit(GS_SVC_ERR_UNSPECIFIED);
}

Here is the call graph for this function: