00001 /******************************************************************************/ 00002 /* */ 00003 /* X r d C m s R T a b l e . 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: XrdCmsRTable.cc 24468 2008-06-22 16:47:03Z ganis $ 00012 00013 // Original Version: 1.2 2006/04/05 02:28:05 abh 00014 00015 const char *XrdCmsRTableCVSID = "$Id: XrdCmsRTable.cc 24468 2008-06-22 16:47:03Z ganis $"; 00016 00017 #include "XrdCms/XrdCmsRTable.hh" 00018 #include "XrdCms/XrdCmsTrace.hh" 00019 00020 using namespace XrdCms; 00021 00022 /******************************************************************************/ 00023 /* G l o b a l s */ 00024 /******************************************************************************/ 00025 00026 XrdCmsRTable XrdCms::RTable; 00027 00028 /******************************************************************************/ 00029 /* A d d */ 00030 /******************************************************************************/ 00031 00032 short XrdCmsRTable::Add(XrdCmsNode *nP) 00033 { 00034 int i; 00035 00036 // Find a free slot for this node. 00037 // 00038 myMutex.Lock(); 00039 for (i = 1; i < maxRD; i++) if (!Rtable[i]) break; 00040 00041 // Insert the node if found 00042 // 00043 if (i >= maxRD) i = 0; 00044 else {Rtable[i] = nP; 00045 if (i > Hwm) Hwm = i; 00046 } 00047 00048 // All done 00049 // 00050 myMutex.UnLock(); 00051 return static_cast<short>(i); 00052 } 00053 00054 /******************************************************************************/ 00055 /* D e l */ 00056 /******************************************************************************/ 00057 00058 void XrdCmsRTable::Del(XrdCmsNode *nP) 00059 { 00060 int i; 00061 00062 // Find the slot for this node. 00063 // 00064 myMutex.Lock(); 00065 for (i = 1; i <= Hwm; i++) if (Rtable[i] == nP) break; 00066 00067 // Remove the node if found 00068 // 00069 if (i <= Hwm) 00070 {Rtable[i] = 0; 00071 if (i == Hwm) {while(--i) if (Rtable[i]) break; Hwm = i;} 00072 } 00073 00074 // All done 00075 // 00076 myMutex.UnLock(); 00077 } 00078 00079 /******************************************************************************/ 00080 /* F i n d */ 00081 /******************************************************************************/ 00082 00083 // Note that the caller *must* call Lock() prior to calling find. We do this 00084 // because this is the only way we can interlock the use of the node object 00085 // with deletion of that object as it must be removed prior to deletion. 00086 00087 XrdCmsNode *XrdCmsRTable::Find(short Num, int Inst) 00088 { 00089 00090 // Find the instance of the node in the indicated slot 00091 // 00092 if (Num <= Hwm && Rtable[Num] && Rtable[Num]->Inst() == Inst) 00093 return Rtable[Num]; 00094 return (XrdCmsNode *)0; 00095 } 00096 00097 /******************************************************************************/ 00098 /* S e n d */ 00099 /******************************************************************************/ 00100 00101 void XrdCmsRTable::Send(const char *What, const char *data, int dlen) 00102 { 00103 EPNAME("Send"); 00104 int i; 00105 00106 // Send the data to all nodes in this table 00107 // 00108 myMutex.Lock(); 00109 for (i = 1; i <= Hwm; i++) 00110 if (Rtable[i]) 00111 {DEBUG(What <<" to " <<Rtable[i]->Ident); 00112 Rtable[i]->Send(data, dlen); 00113 } 00114 myMutex.UnLock(); 00115 }