Go to the documentation of this file.00001
00008
00009
00010
00011 #include "utility.h"
00012
00013 #include <stdlib.h>
00014 #include <string.h>
00015
00016 #include "comm_data.h"
00017 #include "comm_basics.h"
00018 #include "comm_encode.h"
00019 #include "general.h"
00020
00030 int
00031 main(int argc, char **argv)
00032 {
00033 int i, sock, num_problems, agentport, tag;
00034 gs_problem_t **problem_list;
00035 char *msg;
00036
00037 if(argc < 2) {
00038 fprintf(stderr,"Usage: GS_problems <agent name>\n");
00039 exit(EXIT_FAILURE);
00040 }
00041
00042 initialize_sockets();
00043
00044 agentport = getenv_int("GRIDSOLVE_AGENT_PORT", GRIDSOLVE_AGENT_PORT_DEFAULT);
00045 if((sock = gs_connect_direct(argv[1], agentport)) == INVALID_SOCKET) {
00046 fprintf(stderr,"%s cannot be contacted\n", argv[1]);
00047 exit(EXIT_FAILURE);
00048 }
00049
00050 if((gs_send_tag(sock, GS_PROT_PROBLEM_LIST) < 0) ||
00051 (gs_send_string(sock, VERSION) < 0)) {
00052 fprintf(stderr,"Cannot communicate with %s\n", argv[1]);
00053 fprintf(stderr,"Error sending tag GS_PROT_PROBLEM_LIST\n");
00054 exit(EXIT_FAILURE);
00055 }
00056
00057 if(gs_recv_tag(sock, &tag) < 0) {
00058 fprintf(stderr,"Error communicating with agent.\n");
00059 exit(EXIT_FAILURE);
00060 }
00061
00062 if(tag != GS_PROT_OK) {
00063 if(tag == GS_PROT_VERSION_MISMATCH)
00064 fprintf(stderr, "Error: Agent is an incompatible version\n");
00065 else
00066 fprintf(stderr, "Error: Agent refused with code %d\n", tag);
00067 exit(EXIT_FAILURE);
00068 }
00069
00070 if(gs_recv_int(sock, &num_problems) < 0) {
00071 fprintf(stderr,"Error communicating with agent.\n");
00072 exit(EXIT_FAILURE);
00073 }
00074
00075 printf("AGENT: %s [%d problems]\n", argv[1], num_problems);
00076
00077 if(num_problems <= 0)
00078 exit(EXIT_SUCCESS);
00079
00080 problem_list = (gs_problem_t **) CALLOC(num_problems, sizeof(gs_problem_t *));
00081 if(!problem_list) {
00082 fprintf(stderr,"Failed to allocate memory for the problem list.\n");
00083 exit(EXIT_FAILURE);
00084 }
00085
00086 for(i=0;i<num_problems;i++) {
00087 problem_list[i] = (gs_problem_t *) CALLOC(1,sizeof(gs_problem_t));
00088 if(gs_recv_string(sock, &msg) < 0) {
00089 fprintf(stderr,"Error communicating with agent.\n");
00090 exit(EXIT_FAILURE);
00091 }
00092 if(gs_decode_problem(msg, problem_list[i]) < 0) {
00093 fprintf(stderr,"Error decoding problem information.\n");
00094 exit(EXIT_FAILURE);
00095 }
00096 FREE(msg);
00097 }
00098
00099 for(i=0;i<num_problems;i++) {
00100 printf("%s\n", problem_list[i]->name);
00101 }
00102
00103 cleanup_sockets();
00104
00105 return 0;
00106 }