Functions

gs_get_example.c File Reference

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

Go to the source code of this file.

Functions

int main (int argc, char **argv)

Detailed Description

This program will print example GridRPC code for the specified problem. The usage is: GS_get_example <problem name>=""> [server name] if a server name is not specified, the agent will be contacted and the first server will be used.

Definition in file gs_get_example.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Entry point for GS_get_example.

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

NOTE: at this point we are using gridsolve-specific features of GridRPC handle data structure, so this is not meant to be a portable GridRPC program.

Definition at line 35 of file gs_get_example.c.

{
  char *msg, *server_name, *problem_name, *example;
  int tag, server_sock, filelen;
  gs_server_t *gs_server = NULL;
  grpc_function_handle_t handle;
  grpc_error_t status;

  msg = server_name = problem_name = NULL;

  if(argc == 2) {
    problem_name = argv[1];
    server_name = NULL;
  }
  else if(argc == 3) {
    problem_name = argv[1];
    server_name = argv[2];
  }
  else {
    fprintf(stderr,"Usage: GS_get_example <problem name> [server name]\n");
    exit(EXIT_FAILURE);
  }

  if(grpc_initialize(NULL) < 0) {
    fprintf(stderr, "Error initializing\n");
    exit(EXIT_FAILURE);
  }

  if(server_name)
    status = grpc_function_handle_init(&handle, server_name, problem_name);
  else
    status = grpc_function_handle_default(&handle, problem_name);

  if(status != GRPC_NO_ERROR) {
    fprintf(stderr, "Error getting problem information: %s.\n",
      grpc_error_string(status));
    exit(EXIT_FAILURE);
  }

  gs_server = handle.server_list[0];
  server_sock = gs_connect_to_host(gs_server->componentid, gs_server->ipaddress,
     gs_server->port, gs_server->proxyip, gs_server->proxyport);

  if(server_sock < 0) {
    fprintf(stderr,"Unsuccessful (connecting server)\n");
    exit(EXIT_FAILURE);
  }

  if((gs_send_tag(server_sock, GS_PROT_PROBLEM_EXAMPLE) < 0) ||
     (gs_send_string(server_sock, VERSION) < 0)) {
    fprintf(stderr,"Unsuccessful (sending tag to Server)\n");
    exit(EXIT_FAILURE);
  }

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

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

  if(gs_send_string(server_sock, problem_name) < 0) {
    fprintf(stderr,"Unsuccessful sending problem name.\n");
    exit(EXIT_FAILURE);
  }

  if(gs_recv_int(server_sock, &filelen) < 0) {
    fprintf(stderr,"Error receiving length of response.\n");
    exit(EXIT_FAILURE);
  }

  example = (char *)malloc(filelen + 1);

  if(gs_tread(server_sock, example, filelen) == -1) {
    fprintf(stderr,"Unsuccessful receiving response.\n");
    exit(EXIT_FAILURE);
  }
  example[filelen] = 0;

  printf("%s", example);

  gs_close_socket(server_sock);

  exit(EXIT_SUCCESS);
}

Here is the call graph for this function: