pq2ping.cxx

Go to the documentation of this file.
00001 // @(#)root/proof:$Id: pq2ping.cxx 33259 2010-04-28 06:54:00Z ganis $
00002 // Author: G. Ganis, Mar 2010
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 // ************************************************************************* //
00013 // *                                                                       * //
00014 // *                           p q 2 p i n g                               * //
00015 // *                                                                       * //
00016 // * This file implements the daemon checking functions used by pq2main    * //
00017 // *                                                                       * //
00018 // ************************************************************************* //
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 
00023 #include "Bytes.h"
00024 #include "TSocket.h"
00025 #include "TString.h"
00026 #include "TSystem.h"
00027 #include "TUrl.h"
00028 
00029 #include "redirguard.h"
00030 #include "pq2ping.h"
00031 
00032 // Auxilliary structures for Xrootd/Xproofd pinging ...
00033 // The client request
00034 typedef struct {
00035    int first;
00036    int second;
00037    int third;
00038    int fourth;
00039    int fifth;
00040 } clnt_HS_t;
00041 // The body received after the first handshake's header
00042 typedef struct {
00043    int msglen;
00044    int protover;
00045    int msgval;
00046 } srv_HS_t;
00047 
00048 // Global variables used by other PQ2 components
00049 TUrl    gUrl;
00050 Bool_t  gIsProof = kFALSE;
00051 
00052 // Global variables defined by other PQ2 components
00053 extern Int_t gverbose;
00054 
00055 //_______________________________________________________________________________________
00056 Int_t checkUrl(const char *url, const char *flog, bool def_proof)
00057 {
00058    // Check if something is running at gUrl
00059    // Return
00060    //        0 if OK and data server
00061    //        1 if OK and PROOF server
00062    //       -1 if nothing valid is available
00063 
00064    gIsProof = kFALSE;
00065    gUrl.SetUrl(url);
00066    TString protocol(gUrl.GetProtocol());
00067    if (protocol == "root" || protocol == "xroot") {
00068       // Check Xrootd
00069       if (pingXrootdAt() != 0) {
00070          Printf("checkUrl: specified URL does not identifies a running (x)rootd server: %s", url);
00071          return -1;
00072       }
00073    } else if (protocol == "proof") {
00074       // Check PROOF
00075       if (pingXproofdAt() != 0) {
00076          Printf("checkUrl: specified URL does not identifies a running PROOF master: %s", url);
00077          return -1;
00078       }
00079       gIsProof = kTRUE;
00080       // Always force a new session (do not attach)
00081       gUrl.SetOptions("N");
00082    } else {
00083       Int_t rc = -1;
00084       if (def_proof) {
00085          // Check first PROOF
00086          {  redirguard rog(flog, "a", 0);
00087             if ((rc = pingXproofdAt()) == 0)
00088                gIsProof = kTRUE;
00089          }
00090          if (rc != 0) {
00091             // Check also a generic data server
00092             if (pingServerAt() != 0) {
00093                Printf("checkUrl: specified URL does not identifies a valid PROOF or data server: %s", url);
00094                return -1;
00095             }
00096          }
00097       } else {
00098          // Check first generic data server
00099          {  redirguard rog(flog, "a", 0);
00100             rc = pingServerAt();
00101          }
00102          if (rc != 0) {
00103             // Check also PROOF
00104             if (pingXproofdAt() != 0) {
00105                Printf("checkUrl: specified URL does not identifies a valid data or PROOF server: %s", url);
00106                return -1;
00107             }
00108             gIsProof = kTRUE;
00109          }
00110       }
00111    }
00112    if (gverbose > 0) Printf("checkUrl: %s service", (gIsProof ? "PROOF" : "Data"));
00113 
00114    // Done (if we are here the test was successful)
00115    return ((gIsProof) ? 1 : 0);
00116 }
00117 
00118 //_______________________________________________________________________________________
00119 Int_t pingXrootdAt()
00120 {
00121    // Check if a XrdXrootd service is running on 'port' at 'host'
00122    // Return
00123    //        0 if OK
00124    //       -1 if nothing is listening on the port (connection cannot be open)
00125    //        1 if something is listening but not XROOTD
00126 
00127    Int_t port = gUrl.GetPort();
00128    const char *host = gUrl.GetHost();
00129 
00130    // Open the connection
00131    TSocket s(host, port);
00132    if (!(s.IsValid())) {
00133       if (gDebug > 0)
00134          Printf("pingXrootdAt: could not open connection to %s:%d", host, port);
00135       return -1;
00136    }
00137    // Send the first bytes
00138    clnt_HS_t initHS;
00139    memset(&initHS, 0, sizeof(initHS));
00140    initHS.fourth = host2net((int)4);
00141    initHS.fifth  = host2net((int)2012);
00142    int len = sizeof(initHS);
00143    s.SendRaw(&initHS, len);
00144    // Read first server response
00145    int type;
00146    len = sizeof(type);
00147    int readCount = s.RecvRaw(&type, len); // 4(2+2) bytes
00148    if (readCount != len) {
00149       if (gDebug > 0)
00150          Printf("pingXrootdAt: 1st: wrong number of bytes read: %d (expected: %d)",
00151                 readCount, len);
00152       return 1;
00153    }
00154    // to host byte order
00155    type = net2host(type);
00156    // Check if the server is the eXtended proofd
00157    if (type == 0) {
00158       srv_HS_t xbody;
00159       len = sizeof(xbody);
00160       readCount = s.RecvRaw(&xbody, len); // 12(4+4+4) bytes
00161       if (readCount != len) {
00162          if (gDebug > 0)
00163             Printf("pingXrootdAt: 2nd: wrong number of bytes read: %d (expected: %d)",
00164                    readCount, len);
00165          return 1;
00166       }
00167 
00168    } else if (type == 8) {
00169       // Standard proofd
00170       if (gDebug > 0)
00171          Printf("pingXrootdAt: server is ROOTD");
00172       return 1;
00173    } else {
00174       // We don't know the server type
00175       if (gDebug > 0)
00176          Printf("pingXrootdAt: unknown server type: %d", type);
00177       return 1;
00178    }
00179    // Done
00180    return 0;
00181 }
00182 
00183 //_______________________________________________________________________________________
00184 Int_t pingXproofdAt()
00185 {
00186    // Check if a XrdProofd service is running on 'port' at 'host'
00187    // Return
00188    //        0 if OK
00189    //       -1 if nothing is listening on the port (connection cannot be open)
00190    //        1 if something is listening but not XPROOFD
00191 
00192    Int_t port = gUrl.GetPort();
00193    const char *host = gUrl.GetHost();
00194 
00195    // Open the connection
00196    TSocket s(host, port);
00197    if (!(s.IsValid())) {
00198       if (gDebug > 0)
00199          Printf("pingXproofdAt: could not open connection to %s:%d", host, port);
00200       return -1;
00201    }
00202    // Send the first bytes
00203    clnt_HS_t initHS;
00204    memset(&initHS, 0, sizeof(initHS));
00205    initHS.third  = (int)host2net((int)1);
00206    int len = sizeof(initHS);
00207    s.SendRaw(&initHS, len);
00208    // These 8 bytes are need by 'proofd' and discarded by XPD
00209    int dum[2];
00210    dum[0] = (int)host2net((int)4);
00211    dum[1] = (int)host2net((int)2012);
00212    s.SendRaw(&dum[0], sizeof(dum));
00213    // Read first server response
00214    int type;
00215    len = sizeof(type);
00216    int readCount = s.RecvRaw(&type, len); // 4(2+2) bytes
00217    if (readCount != len) {
00218       if (gDebug > 0)
00219          Printf("pingXproofdAt: 1st: wrong number of bytes read: %d (expected: %d)",
00220                 readCount, len);
00221       return 1;
00222    }
00223    // to host byte order
00224    type = net2host(type);
00225    // Check if the server is the eXtended proofd
00226    if (type == 0) {
00227       srv_HS_t xbody;
00228       len = sizeof(xbody);
00229       readCount = s.RecvRaw(&xbody, len); // 12(4+4+4) bytes
00230       if (readCount != len) {
00231          if (gDebug > 0)
00232             Printf("pingXproofdAt: 2nd: wrong number of bytes read: %d (expected: %d)",
00233                    readCount, len);
00234          return 1;
00235       }
00236       xbody.protover = net2host(xbody.protover);
00237       xbody.msgval = net2host(xbody.msglen);
00238       xbody.msglen = net2host(xbody.msgval);
00239 
00240    } else if (type == 8) {
00241       // Standard proofd
00242       if (gDebug > 0)
00243          Printf("pingXproofdAt: server is PROOFD");
00244       return 1;
00245    } else {
00246       // We don't know the server type
00247       if (gDebug > 0)
00248          Printf("pingXproofdAt: unknown server type: %d", type);
00249       return 1;
00250    }
00251    // Done
00252    return 0;
00253 }
00254 
00255 //_______________________________________________________________________________________
00256 Int_t pingServerAt()
00257 {
00258    // Check if service is running at 'url'
00259    // Return
00260    //        0 if OK
00261    //       -1 if nothing is listening at the URL
00262    //        1 if not a directory
00263 
00264    Int_t rc = -1;
00265    FileStat_t st;
00266    if (gSystem->GetPathInfo(gUrl.GetUrl(), st) == 0) {
00267       rc = 1;
00268       if (R_ISDIR(st.fMode)) rc = 0;
00269    }
00270 
00271    // Done
00272    return rc;
00273 }

Generated on Tue Jul 5 14:50:54 2011 for ROOT_528-00b_version by  doxygen 1.5.1