gs_probdesc.c

Go to the documentation of this file.
00001 
00009 /* $Id: gs_probdesc.c,v 1.11 2007/04/11 16:47:35 seymour Exp $ */
00010 /* $UTK_Copyright: $ */
00011 
00012 #include "utility.h"
00013 
00014 #include <stdlib.h>
00015 #include <string.h>
00016 
00017 #include "comm_data.h"
00018 #include "comm_basics.h"
00019 #include "comm_encode.h"
00020 #include "general.h"
00021 
00031 int
00032 main(int argc, char **argv)
00033 {
00034   gs_problem_t *problem = NULL;
00035   int sock, agentport, tag;
00036   char *msg = NULL;
00037 
00038   if(argc < 3) {
00039     fprintf(stderr,"Usage: GS_probdesc <agent name> <problem name>\n");
00040     exit(EXIT_FAILURE);
00041   }
00042 
00043   initialize_sockets();
00044 
00045   agentport = getenv_int("GRIDSOLVE_AGENT_PORT", GRIDSOLVE_AGENT_PORT_DEFAULT);
00046   if((sock = gs_connect_direct(argv[1], agentport)) == INVALID_SOCKET) {
00047     fprintf(stderr,"%s cannot be contacted\n", argv[1]);
00048     exit(EXIT_FAILURE);
00049   }
00050 
00051   if((gs_send_tag(sock, GS_PROT_PROBLEM_DESC) < 0) ||
00052      (gs_send_string(sock, VERSION) < 0)) {
00053     fprintf(stderr,"Cannot communicate with %s\n", argv[1]);
00054     fprintf(stderr,"Error sending tag GS_PROT_PROBLEM_DESC\n");
00055     exit(EXIT_FAILURE);
00056   }
00057 
00058   if(gs_recv_tag(sock, &tag) < 0) {
00059     fprintf(stderr,"Error communicating with agent.\n");
00060     exit(EXIT_FAILURE);
00061   }
00062 
00063   if(tag != GS_PROT_OK) {
00064     if(tag == GS_PROT_VERSION_MISMATCH)
00065       fprintf(stderr, "Error: Agent is an incompatible version\n");
00066     else
00067       fprintf(stderr, "Error: Agent refused with code %d\n", tag);
00068     exit(EXIT_FAILURE);
00069   }
00070 
00071   if(gs_send_string(sock, argv[2]) < 0) {
00072     fprintf(stderr,"Cannot communicate with %s\n", argv[1]);
00073     exit(EXIT_FAILURE);
00074   }
00075 
00076   if(gs_recv_string(sock, &msg) < 0) {
00077     fprintf(stderr,"Error communicating with agent.\n");
00078     exit(EXIT_FAILURE);
00079   }
00080 
00081   if(!strcmp(msg, "not found")) {
00082     fprintf(stderr,"Problem '%s' was not found.\n", argv[2]);
00083     exit(EXIT_FAILURE);
00084   }
00085 
00086   problem = (gs_problem_t *) CALLOC(1, sizeof(gs_problem_t));
00087 
00088   if(!problem) {
00089     fprintf(stderr,"Failed to allocate problem struct\n");
00090     return -1;
00091   }
00092 
00093   if(gs_decode_problem(msg, problem) < 0) {
00094     fprintf(stderr,"Error encoding problem description.\n");
00095     exit(EXIT_FAILURE);
00096   }
00097 
00098   gs_problem_dump(problem);
00099 
00100   cleanup_sockets();
00101 
00102   return 0;
00103 }