server_ping.c

Go to the documentation of this file.
00001 
00008 /* $Id: server_ping.c,v 1.6 2008/08/28 21:37:35 seymour Exp $ */
00009 /* $UTK_Copyright: $ */
00010 
00011 #include <string.h>
00012 #include <sys/types.h>
00013 #include <sys/socket.h>
00014 #include <sys/stat.h>
00015 #include <sys/time.h>
00016 #include <netinet/in.h>
00017 #include <netdb.h>
00018 #include <stdio.h>
00019 #include <unistd.h>
00020 #include <glob.h>
00021 #include <dirent.h>
00022 #include <errno.h>
00023 #include <utime.h>
00024 
00025 #include "server.h"
00026 #include "utility.h"
00027 #include "comm_basics.h"
00028 #include "comm_data.h"
00029 #include "comm_encode.h"
00030 #include "general.h"
00031 #include "gs_pm_model.h"
00032 
00033 int
00034   gs_send_ping_updates(gs_server_t *, gs_server_t **, double *, int),
00035   gs_get_servers_to_be_pinged(gs_server_t *, gs_server_t ***, int *);
00036 
00037 #ifdef GS_SMART_GRIDSOLVE
00038 
00051 void 
00052 gs_server_ping(void **args)
00053 {
00054   gs_server_t *gs_server, **server_list;
00055   double *ping_times;
00056   int count, i;
00057   char *buf;
00058 
00059   if(!args || !args[0])
00060     return;
00061 
00062   gs_server = args[0];
00063  
00064 
00065   buf = (char *)malloc(GS_SERVER_PING_SIZE);
00066 
00067   if(!buf) {
00068     ERRPRINTF("malloc failed\n");
00069     return;
00070   }
00071 
00072   if(gs_get_servers_to_be_pinged(gs_server, &server_list, &count) < 0) {
00073     ERRPRINTF("Failed to get server list\n");
00074     return;
00075   }
00076 
00077   ping_times = (double *)malloc(count * sizeof(double));
00078 
00079   if(!ping_times) {
00080     ERRPRINTF("malloc ping_times array failed\n");
00081     return;
00082   }
00083 
00084 fprintf(stderr,"got %d servers to ping..\n", count);
00085   for(i=0;i<count;i++) {
00086 fprintf(stderr, "pinging server '%s'\n", server_list[i]->hostname);
00087     if(gs_do_ping(server_list[i], buf, GS_SERVER_PING_SIZE, &ping_times[i]) < 0) {
00088       ERRPRINTF("Warning: ping to '%s' failed.\n", server_list[i]->hostname);
00089       ping_times[i] = 99999.0;
00090     }
00091 fprintf(stderr, "....time = %g\n", ping_times[i]);
00092   }
00093 
00094 fprintf(stderr, "sending ping updates to the agent\n");
00095   if(gs_send_ping_updates(gs_server, server_list, ping_times, count) < 0) {
00096     ERRPRINTF("sending ping updates to agent failed\n");
00097     return;
00098   }
00099 
00100   return;
00101 }
00102 
00103 int
00104 gs_send_ping_updates(gs_server_t *server, gs_server_t **server_list, double *times, int count)
00105 {
00106   char *srv_ping_encoding = NULL, my_cid[2*CID_LEN+1];
00107   int sock, tag;
00108 
00109   proxy_cid_to_str(my_cid, server->componentid);
00110 
00111   if((sock = gs_connect_direct(server->agenthost, server->agentport)) == INVALID_SOCKET)
00112     return -1;
00113 
00114   if((gs_send_tag(sock, GS_PROT_SERVER_PING_UPDATE) < 0) ||
00115      (gs_send_string(sock, VERSION) < 0) ||
00116      (gs_recv_tag(sock, &tag) < 0))
00117     goto error_communication_failed;
00118   if(tag != GS_PROT_OK) {
00119     if(tag == GS_PROT_VERSION_MISMATCH) {
00120       ERRPRINTF("Version mismatch communicating with agent\n");
00121       return -1;
00122     } else {
00123       ERRPRINTF("Failed communicating with agent\n");
00124     }
00125   }
00126 
00127   if(gs_send_string(sock, my_cid) < 0)
00128     goto error_communication_failed;
00129 /*
00130   if(gs_encode_smart_info(&srv_ping_encoding, server, server_list, times, count) < 0) {
00131     ERRPRINTF("encoding of server ping times failed\n");
00132     return -1;
00133   }
00134 */
00135   
00136   if(gs_smart_encode_ping_info(&srv_ping_encoding, server, server_list, times, count) < 0) {
00137     ERRPRINTF("encoding of server ping times failed\n");
00138     return -1;
00139   }
00140 
00141   if(!srv_ping_encoding) {
00142     ERRPRINTF("encoding of server ping times failed (NULL string)\n");
00143     return -1;
00144   }
00145 
00146   if(gs_send_string(sock, srv_ping_encoding) < 0)
00147     goto error_communication_failed;
00148 
00149   FREE(srv_ping_encoding);
00150 
00151   if(sock != INVALID_SOCKET)  
00152     proxy_close(sock);
00153 
00154   return 0;
00155 
00156  error_communication_failed:
00157   FREE(srv_ping_encoding);
00158   DBGPRINTF("Error: Communication failed\n");
00159   if(sock != INVALID_SOCKET)
00160     proxy_close(sock);
00161   return -1;
00162 }
00163 
00176 int
00177 gs_get_servers_to_be_pinged(gs_server_t *server, gs_server_t ***slist, int *count)
00178 {
00179   char *msg = NULL, my_cid[2*CID_LEN+1];
00180   gs_server_t **server_list;
00181   int i, num_servers, tag;
00182   SOCKET sock;
00183 
00184   *slist = NULL;
00185   *count = 0;
00186 
00187   proxy_cid_to_str(my_cid, server->componentid);
00188 
00189   if((sock = gs_connect_direct(server->agenthost, server->agentport)) == INVALID_SOCKET)
00190     return -1;
00191 
00192   if((gs_send_tag(sock, GS_PROT_SERVER_PING_LIST) < 0) ||
00193      (gs_send_string(sock, VERSION) < 0) ||
00194      (gs_recv_tag(sock, &tag) < 0))
00195     goto error_communication_failed;
00196   if(tag != GS_PROT_OK) {
00197     if(tag == GS_PROT_VERSION_MISMATCH) {
00198       ERRPRINTF("Version mismatch communicating with agent\n");
00199       return -1;
00200     } else {
00201       ERRPRINTF("Failed communicating with agent\n");
00202     }
00203   }
00204 
00205   if(gs_send_string(sock, my_cid) < 0)
00206     goto error_communication_failed;
00207 
00208   if(gs_recv_int(sock, &num_servers) < 0)
00209     goto error_communication_failed;
00210 
00211   if(num_servers <= 0)
00212     return 0;
00213 
00214   server_list = (gs_server_t **) CALLOC(num_servers, sizeof(gs_server_t *));
00215   if(!server_list)
00216     return -1;
00217 
00218   for(i=0;i<num_servers;i++) {
00219     server_list[i] = (gs_server_t *) CALLOC(1,sizeof(gs_server_t));
00220     if(gs_recv_string(sock, &msg) < 0) {
00221       goto error_communication_failed;
00222     }
00223     if(gs_decode_server(msg, server_list[i]) < 0) {
00224       FREE(msg);
00225       return -1;
00226     }
00227     FREE(msg)
00228   }
00229 
00230   if(sock != INVALID_SOCKET)  
00231     proxy_close(sock);
00232 
00233   *slist = server_list;
00234   *count = num_servers;
00235 
00236   return 0;
00237 
00238  error_communication_failed:
00239   DBGPRINTF("Error: Communication failed\n");
00240   FREE(msg);
00241   if(sock != INVALID_SOCKET)
00242     proxy_close(sock);
00243   return -1;
00244 }
00245 
00246 #endif