Functions

gs_problems.c File Reference

#include "utility.h"
#include <stdlib.h>
#include <string.h>
#include "comm_data.h"
#include "comm_basics.h"
#include "comm_encode.h"
#include "general.h"
Include dependency graph for gs_problems.c:

Go to the source code of this file.

Functions

int main (int argc, char **argv)

Detailed Description

This program will print a list of problems. The usage is: GS_problems <agent name>="">

Definition in file gs_problems.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Entry point for GS_problems.

Parameters:
argc -- arg count
argv -- array of command line args
Returns:
EXIT_SUCCESS on success, EXIT_FAILURE on error.

Definition at line 31 of file gs_problems.c.

{
  int i, sock, num_problems, agentport, tag;
  gs_problem_t **problem_list;
  char *msg;

  if(argc < 2) {
    fprintf(stderr,"Usage: GS_problems <agent name>\n");
    exit(EXIT_FAILURE);
  }

  initialize_sockets();

  agentport = getenv_int("GRIDSOLVE_AGENT_PORT", GRIDSOLVE_AGENT_PORT_DEFAULT);
  if((sock = gs_connect_direct(argv[1], agentport)) == INVALID_SOCKET) {
    fprintf(stderr,"%s cannot be contacted\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  if((gs_send_tag(sock, GS_PROT_PROBLEM_LIST) < 0) ||
     (gs_send_string(sock, VERSION) < 0)) {
    fprintf(stderr,"Cannot communicate with %s\n", argv[1]);
    fprintf(stderr,"Error sending tag GS_PROT_PROBLEM_LIST\n");
    exit(EXIT_FAILURE);
  }

  if(gs_recv_tag(sock, &tag) < 0) {
    fprintf(stderr,"Error communicating with agent.\n");
    exit(EXIT_FAILURE);
  }

  if(tag != GS_PROT_OK) {
    if(tag == GS_PROT_VERSION_MISMATCH)
      fprintf(stderr, "Error: Agent is an incompatible version\n");
    else
      fprintf(stderr, "Error: Agent refused with code %d\n", tag);
    exit(EXIT_FAILURE);
  }

  if(gs_recv_int(sock, &num_problems) < 0) {
    fprintf(stderr,"Error communicating with agent.\n");
    exit(EXIT_FAILURE);
  }

  printf("AGENT: %s [%d problems]\n", argv[1], num_problems);

  if(num_problems <= 0)
    exit(EXIT_SUCCESS);

  problem_list = (gs_problem_t **) CALLOC(num_problems, sizeof(gs_problem_t *));
  if(!problem_list) {
    fprintf(stderr,"Failed to allocate memory for the problem list.\n");
    exit(EXIT_FAILURE);
  }

  for(i=0;i<num_problems;i++) {
    problem_list[i] = (gs_problem_t *) CALLOC(1,sizeof(gs_problem_t));
    if(gs_recv_string(sock, &msg) < 0) {
      fprintf(stderr,"Error communicating with agent.\n");
      exit(EXIT_FAILURE);
    }
    if(gs_decode_problem(msg, problem_list[i]) < 0) {
      fprintf(stderr,"Error decoding problem information.\n");
      exit(EXIT_FAILURE);
    }
    FREE(msg);
  }

  for(i=0;i<num_problems;i++) {
    printf("%s\n", problem_list[i]->name);
  }

  cleanup_sockets();

  return 0;
}

Here is the call graph for this function: