gs_get_example.c

Go to the documentation of this file.
00001 
00011 /* $Id: gs_get_example.c,v 1.8 2007/04/11 16:47:34 seymour Exp $ */
00012 /* $UTK_Copyright: $ */
00013 
00014 #include "utility.h"
00015 
00016 #include <stdlib.h>
00017 #include <string.h>
00018 
00019 #include "grpc.h"
00020 #include "comm_data.h"
00021 #include "comm_basics.h"
00022 #include "comm_encode.h"
00023 #include "general.h"
00024 
00034 int
00035 main(int argc, char **argv)
00036 {
00037   char *msg, *server_name, *problem_name, *example;
00038   int tag, server_sock, filelen;
00039   gs_server_t *gs_server = NULL;
00040   grpc_function_handle_t handle;
00041   grpc_error_t status;
00042 
00043   msg = server_name = problem_name = NULL;
00044 
00045   if(argc == 2) {
00046     problem_name = argv[1];
00047     server_name = NULL;
00048   }
00049   else if(argc == 3) {
00050     problem_name = argv[1];
00051     server_name = argv[2];
00052   }
00053   else {
00054     fprintf(stderr,"Usage: GS_get_example <problem name> [server name]\n");
00055     exit(EXIT_FAILURE);
00056   }
00057 
00058   if(grpc_initialize(NULL) < 0) {
00059     fprintf(stderr, "Error initializing\n");
00060     exit(EXIT_FAILURE);
00061   }
00062 
00063   if(server_name)
00064     status = grpc_function_handle_init(&handle, server_name, problem_name);
00065   else
00066     status = grpc_function_handle_default(&handle, problem_name);
00067 
00068   if(status != GRPC_NO_ERROR) {
00069     fprintf(stderr, "Error getting problem information: %s.\n",
00070       grpc_error_string(status));
00071     exit(EXIT_FAILURE);
00072   }
00073 
00080   gs_server = handle.server_list[0];
00081   server_sock = gs_connect_to_host(gs_server->componentid, gs_server->ipaddress,
00082      gs_server->port, gs_server->proxyip, gs_server->proxyport);
00083 
00084   if(server_sock < 0) {
00085     fprintf(stderr,"Unsuccessful (connecting server)\n");
00086     exit(EXIT_FAILURE);
00087   }
00088 
00089   if((gs_send_tag(server_sock, GS_PROT_PROBLEM_EXAMPLE) < 0) ||
00090      (gs_send_string(server_sock, VERSION) < 0)) {
00091     fprintf(stderr,"Unsuccessful (sending tag to Server)\n");
00092     exit(EXIT_FAILURE);
00093   }
00094 
00095   if(gs_recv_tag(server_sock, &tag) < 0) {
00096     fprintf(stderr, "Error communicating with server.\n");
00097     exit(EXIT_FAILURE);
00098   }
00099 
00100   if(tag != GS_PROT_OK) {
00101     if(tag == GS_PROT_VERSION_MISMATCH)
00102       fprintf(stderr, "Error: Server is an incompatible version\n");
00103     else
00104       fprintf(stderr, "Error: Server refused with code %d\n", tag);
00105     exit(EXIT_FAILURE);
00106   }
00107 
00108   if(gs_send_string(server_sock, problem_name) < 0) {
00109     fprintf(stderr,"Unsuccessful sending problem name.\n");
00110     exit(EXIT_FAILURE);
00111   }
00112 
00113   if(gs_recv_int(server_sock, &filelen) < 0) {
00114     fprintf(stderr,"Error receiving length of response.\n");
00115     exit(EXIT_FAILURE);
00116   }
00117 
00118   example = (char *)malloc(filelen + 1);
00119 
00120   if(gs_tread(server_sock, example, filelen) == -1) {
00121     fprintf(stderr,"Unsuccessful receiving response.\n");
00122     exit(EXIT_FAILURE);
00123   }
00124   example[filelen] = 0;
00125 
00126   printf("%s", example);
00127 
00128   gs_close_socket(server_sock);
00129 
00130   exit(EXIT_SUCCESS);
00131 }