gs_stage_file.c

Go to the documentation of this file.
00001 
00008 /* $Id: gs_stage_file.c,v 1.4 2007/07/16 19:56:53 yarkhan Exp $ */
00009 /* $UTK_Copyright: $ */
00010 
00011 #include "utility.h"
00012 
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <errno.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   char *host, *filename, *remote_file_info;
00035   gs_server_t *gs_server = NULL;
00036   int server_sock, tag, port;
00037   struct hostent *hp;
00038   char cid[CID_LEN];
00039   ipaddr_t ipaddr;
00040 
00041   memset(cid, 0xFF, CID_LEN);
00042 
00043   if(argc < 3) {
00044     fprintf(stderr, "Usage: GS_stage_file <file name> <server name> [server port]\n");
00045     exit(EXIT_FAILURE);
00046   }
00047 
00048   filename = argv[1];
00049   host = argv[2];
00050 
00051   if(argc == 4)
00052     port = atoi(argv[3]);
00053   else
00054     port = GRIDSOLVE_SERVER_PORT_DEFAULT;
00055   
00056   DBGPRINTF("Connecting to host %s port %d\n", host, port);
00057 
00058   if ((hp = gethostbyname(host)) == NULL) {
00059     errno = errno_socket();
00060     ERRPRINTF("Could not gethostbyname for %s (errno %d) \n", host, errno);
00061     perror("gethostbyname()");
00062     return INVALID_SOCKET;
00063   }
00064 
00065   memcpy((void *) &ipaddr, hp->h_addr_list[0], sizeof(ipaddr));
00066   
00067   /* all ones will match any component ID */
00068   memset(cid, 0xFF, CID_LEN);
00069 
00070   server_sock = gs_connect_to_host(cid, ipaddr, port, 0, 0);
00071 
00072   if(server_sock < 0) {
00073     fprintf(stderr, "Unsuccessful (connecting server)\n");
00074     gs_server_free(gs_server);
00075     exit(EXIT_FAILURE);
00076   }
00077 
00078   if((gs_send_tag(server_sock, GS_PROT_STORE_FILE) < 0) ||
00079      (gs_send_string(server_sock, VERSION) < 0)) {
00080     fprintf(stderr, "Unsuccessful (sending tag to Server)\n");
00081     gs_server_free(gs_server);
00082     exit(EXIT_FAILURE);
00083   }
00084 
00085   if(gs_recv_tag(server_sock, &tag) < 0) {
00086     fprintf(stderr, "Error communicating with server.\n");
00087     exit(EXIT_FAILURE);
00088   }
00089 
00090   if(tag != GS_PROT_OK) {
00091     if(tag == GS_PROT_VERSION_MISMATCH)
00092       fprintf(stderr, "Error: Server is an incompatible version\n");
00093     else
00094       fprintf(stderr, "Error: Server refused with code %d\n", tag);
00095     exit(EXIT_FAILURE);
00096   }
00097 
00098   remote_file_info="foo";
00099 
00100   if(gs_send_string(server_sock, remote_file_info) < 0) {
00101     fprintf(stderr, "Unsuccessful (Sending remote_file_info)\n");
00102     gs_server_free(gs_server);
00103     exit(EXIT_FAILURE);
00104   }
00105 
00106   if(gs_send_arg_from_file(server_sock, filename) < 0) {
00107     fprintf(stderr, "Error sending from file '%s'\n", filename);
00108     gs_server_free(gs_server);
00109     exit(EXIT_FAILURE);
00110   }
00111   
00112   gs_server_free(gs_server);
00113 
00114   exit(EXIT_SUCCESS);
00115 }