gs_info.c

Go to the documentation of this file.
00001 
00007 /* $Id: gs_info.c,v 1.4 2007/04/11 18:25:45 seymour Exp $ */
00008 /* $UTK_Copyright: $ */
00009 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 
00014 #include "idl_export.h"
00015 #include "grpc.h"
00016 #include "idl_request.h"
00017 #include "translate.h"
00018 
00019 #include "utility.h"
00020 #include "comm_data.h"
00021 #include "comm_basics.h"
00022 #include "comm_encode.h"
00023 #include "general.h"
00024 
00038 void
00039 gs_info(int argc, IDL_VPTR* argv_idl)
00040 {
00041   grpc_function_handle_t *handle;
00042   char *fname, *nickname;
00043   grpc_error_t retval;
00044 
00045   /* 
00046    * Obtain the problem name to be invoked, 
00047    * and peel off "()" of the name if necessary.
00048    */
00049   fname    = IDL_VarGetString(argv_idl[0]);
00050   nickname = strdup(fname);
00051 
00052   if(!nickname) {
00053     IDL_Message(IDL_M_GENERIC, IDL_MSG_INFO, "Error: strdup failed!");
00054     return;
00055   }
00056 
00057   if(trunc_fname(nickname) < 0) {
00058     IDL_Message(IDL_M_GENERIC, IDL_MSG_INFO, "Error: invalid problem name!");
00059     free(nickname);
00060     return;
00061   }
00062 
00063   /* Initialize GridRPC system */
00064   retval = grpc_initialize(NULL);
00065   if((retval != GRPC_NO_ERROR) && (retval != GRPC_ALREADY_INITIALIZED)) {
00066     IDL_Message(IDL_M_GENERIC, IDL_MSG_INFO, "Error: cannot initialize!");
00067     return;
00068   }
00069 
00070   /* Create its GridRPC handle */
00071   handle = (grpc_function_handle_t *)malloc(sizeof(grpc_function_handle_t));
00072 
00073   if(!handle) {
00074     IDL_Message(IDL_M_GENERIC, IDL_MSG_INFO, "Error: malloc failed!");
00075     free(nickname);
00076     return;
00077   }
00078 
00079   IDL_TimerBlock(IDL_TRUE);
00080   retval = grpc_function_handle_default(handle, nickname);
00081   IDL_TimerBlock(IDL_FALSE);
00082 
00083   free(nickname);
00084 
00085   if(retval != GRPC_NO_ERROR) {
00086     IDL_Message(IDL_M_GENERIC,IDL_MSG_INFO, "Unknown problem.");
00087     free(handle);
00088     return;
00089   }
00090 
00091   if(!handle->problem_desc) {
00092     IDL_Message(IDL_M_GENERIC, IDL_MSG_INFO, "Error: bad problem desc!");
00093     grpc_function_handle_destruct(handle);
00094     free(handle);
00095     return;
00096   }
00097 
00098   gs_problem_dump(handle->problem_desc);
00099 
00100   grpc_function_handle_destruct(handle);
00101   free(handle);
00102 
00103   return;
00104 }
00105 
00110 void
00111 gridsolve(int argc, IDL_VPTR* argv_idl)
00112 {
00113   char *agent, *msg, errmsg[512], dottedIP[20], proxy_dottedIP[20],
00114      cid_string[2 * CID_LEN + 1];
00115   int i, sock, num_servers, agentport, tag;
00116   gs_server_t **server_list;
00117   grpc_error_t retval;
00118 
00119   agent = getenv("GRIDSOLVE_AGENT");
00120 
00121   if(!agent)
00122     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, 
00123        "Error: GRIDSOLVE_AGENT not set!");
00124 
00125   /* Initialize GridRPC system */
00126   retval = grpc_initialize(NULL);
00127   if((retval != GRPC_NO_ERROR) && (retval != GRPC_ALREADY_INITIALIZED))
00128     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, 
00129        "Error: cannot initialize GridSolve!");
00130 
00131   agentport = getenv_int("GRIDSOLVE_AGENT_PORT", GRIDSOLVE_AGENT_PORT_DEFAULT);
00132   if((sock = gs_connect_direct(agent, agentport)) == INVALID_SOCKET) {
00133     sprintf(errmsg, "%s cannot be contacted\n", agent);
00134     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, errmsg);
00135   }
00136 
00137   if((gs_send_tag(sock, GS_PROT_SERVER_LIST) < 0) ||
00138      (gs_send_string(sock, VERSION) < 0)) {
00139     sprintf(errmsg, "Cannot communicate with %s\n", agent);
00140     IDL_Message(IDL_M_GENERIC, IDL_MSG_INFO, errmsg);
00141     sprintf(errmsg, "Error sending tag GS_PROT_SERVER_LIST\n");
00142     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, errmsg);
00143   }
00144 
00145   if(gs_recv_tag(sock, &tag) < 0)
00146     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, "Error communicating with agent.");
00147 
00148   if(tag != GS_PROT_OK) {
00149     if(tag == GS_PROT_VERSION_MISMATCH)
00150       sprintf(errmsg, "Error: Agent is an incompatible version\n");
00151     else
00152       sprintf(errmsg, "Error: Agent refused with code %d\n", tag);
00153     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, errmsg);
00154   }
00155 
00156   if(gs_recv_int(sock, &num_servers) < 0)
00157     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, "Error communicating with agent.");
00158 
00159   printf("AGENT: %s [%d servers]\n", agent, num_servers);
00160 
00161   if(num_servers <= 0)
00162     return;
00163 
00164   server_list = (gs_server_t **) CALLOC(num_servers, sizeof(gs_server_t *));
00165   if(!server_list)
00166     IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP,
00167       "Failed to allocate memory for the server list.");
00168 
00169   for(i=0;i<num_servers;i++) {
00170     server_list[i] = (gs_server_t *) CALLOC(1,sizeof(gs_server_t));
00171     if(gs_recv_string(sock, &msg) < 0)
00172       IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, "Error communicating with agent.");
00173     if(gs_decode_server(msg, server_list[i]) < 0)
00174       IDL_Message(IDL_M_GENERIC, IDL_MSG_LONGJMP, "Error decoding server information.");
00175     FREE(msg);
00176   }
00177 
00178   for(i=0;i<num_servers;i++) {
00179     proxy_cid_to_str(cid_string, server_list[i]->componentid);
00180     proxy_ip_to_str(server_list[i]->ipaddress, dottedIP);
00181     proxy_ip_to_str(server_list[i]->proxyip, proxy_dottedIP);
00182     if(server_list[i]->proxyip != 0)
00183       printf("SERVER: %s (%s:%d, proxy=%s:%d) [cid=%s]\n", server_list[i]->hostname, 
00184         dottedIP, server_list[i]->port, proxy_dottedIP, 
00185         server_list[i]->proxyport, cid_string);
00186     else
00187       printf("SERVER: %s (%s:%d) [cid=%s]\n", server_list[i]->hostname, 
00188         dottedIP, server_list[i]->port, cid_string);
00189   }
00190 
00191   return;
00192 }