XrdCmsManList.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                      X r d C m s M a n L i s t . c c                       */
00004 /*                                                                            */
00005 /* (c) 2007 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 //         $Id: XrdCmsManList.cc 24468 2008-06-22 16:47:03Z ganis $
00012 
00013 // Original Version: 1.4 2007/01/22 03:40:24 abh
00014 
00015 const char *XrdCmsManListCVSID = "$Id: XrdCmsManList.cc 24468 2008-06-22 16:47:03Z ganis $";
00016 
00017 #include <ctype.h>
00018 #include <unistd.h>
00019 
00020 #include "XrdNet/XrdNetDNS.hh"
00021 #include "XrdSys/XrdSysPlatform.hh"
00022 #include "XrdCms/XrdCmsManList.hh"
00023 
00024 using namespace XrdCms;
00025 
00026 /******************************************************************************/
00027 /*                        G l o b a l   O b j e c t s                         */
00028 /******************************************************************************/
00029 
00030        XrdCmsManList  XrdCms::myMans;
00031 
00032 /******************************************************************************/
00033 /*                         L o c a l   C l a s s e s                          */
00034 /******************************************************************************/
00035   
00036 class XrdCmsManRef
00037 {
00038 public:
00039 
00040 XrdCmsManRef *Next;
00041 char         *Manager;
00042 unsigned int  ManRef;
00043 int           ManPort;
00044 int           ManLvl;
00045 
00046               XrdCmsManRef(unsigned int ref, char *name, int port, int lvl)
00047                           : Next(0), Manager(name), ManRef(ref),
00048                             ManPort(port), ManLvl(lvl) {};
00049 
00050              ~XrdCmsManRef() {if (Manager) free(Manager);}
00051 };
00052 
00053 /******************************************************************************/
00054 /*                            D e s t r u c t o r                             */
00055 /******************************************************************************/
00056   
00057 XrdCmsManList::~XrdCmsManList()
00058 {
00059    XrdCmsManRef *prp, *mrp = allMans;
00060 
00061    while(mrp) {prp = mrp; mrp = mrp->Next; delete prp;}
00062 }
00063 
00064 
00065 /******************************************************************************/
00066 /*                                   A d d                                    */
00067 /******************************************************************************/
00068   
00069 void XrdCmsManList::Add(unsigned int ref, char *manp, int manport, int lvl)
00070 {
00071    XrdCmsManRef *prp = 0, *mrp;
00072    struct sockaddr InetAddr;
00073    char *cp, *ipname;
00074    int port;
00075 
00076 // Find the colon in the host name
00077 //
00078    if (!(cp = index(manp, int(':')))) port = manport;
00079       else {if (!(port=atoi(cp+1)) || port > 0xffff) port=manport;
00080             *cp = '\0';
00081            }
00082 
00083 // Check if we need to translate ip address to name
00084 //
00085    if (!isdigit((int)*manp) || !XrdNetDNS::getHostAddr(manp, InetAddr)
00086    || !(ipname =  XrdNetDNS::getHostName(InetAddr))) ipname = strdup(manp);
00087    if (cp) *cp = ':';
00088 
00089 // Start up
00090 //
00091    mlMutex.Lock();
00092    mrp = allMans;
00093 
00094 // Chck if this is a duplicate
00095 //
00096    while(mrp)
00097         {if (!strcmp(mrp->Manager, ipname)) 
00098             {mlMutex.UnLock(); free(ipname); return;}
00099          if (mrp->Next)
00100             {if (mrp->Next->ManLvl > lvl) prp = mrp;}
00101             else if (!prp) prp = mrp;
00102          mrp = mrp->Next;
00103         }
00104 
00105 // Create new entry
00106 //
00107    mrp = new XrdCmsManRef(ref, ipname, port, lvl);
00108    if (!prp) nextMan = allMans = mrp;
00109       else {mrp->Next = prp->Next; prp->Next = mrp;
00110             if (nextMan->ManLvl > lvl) nextMan = mrp;
00111            }
00112    mlMutex.UnLock();
00113 }
00114 
00115 /******************************************************************************/
00116 /*                                   D e l                                    */
00117 /******************************************************************************/
00118   
00119 void XrdCmsManList::Del(unsigned int ref)
00120 {
00121    XrdCmsManRef *nrp, *prp = 0, *mrp;
00122 
00123 // Start up
00124 //
00125    mlMutex.Lock();
00126    mrp = allMans;
00127 
00128 // Delete all ref entries
00129 //
00130    while(mrp)
00131         {if (mrp->ManRef == ref)
00132             {nrp = mrp->Next;
00133              if (!prp) allMans  = nrp;
00134                 else {prp->Next = nrp;
00135                       if (mrp == allMans) allMans = nrp;
00136                      }
00137              if (mrp == nextMan) nextMan = nrp;
00138              delete mrp;
00139              mrp = nrp;
00140             } else {prp = mrp; mrp = mrp->Next;}
00141         }
00142 
00143 // All done
00144 //
00145    mlMutex.UnLock();
00146 }
00147 
00148 /******************************************************************************/
00149 /*                                  N e x t                                   */
00150 /******************************************************************************/
00151   
00152 int XrdCmsManList::Next(int &port, char *buff, int bsz)
00153 {
00154    XrdCmsManRef *np;
00155    int lvl;
00156 
00157    mlMutex.Lock();
00158    if (!(np = nextMan)) nextMan = allMans;
00159       else {strlcpy(buff, np->Manager, bsz);
00160             port = np->ManPort;
00161             nextMan = np->Next;
00162            }
00163    lvl = (np ? np->ManLvl : 0);
00164    mlMutex.UnLock();
00165    return lvl;
00166 }

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