XRootD
XrdSsiAlert.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S s i A l e r t . c c */
4 /* */
5 /* (c) 2017 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <cstddef>
32 #include <cstring>
33 #include <sys/uio.h>
34 
35 #include "XrdOuc/XrdOucErrInfo.hh"
36 #include "XrdSsi/XrdSsiAlert.hh"
37 #include "XrdSsi/XrdSsiRRInfo.hh"
38 
39 /******************************************************************************/
40 /* S t a t i c s */
41 /******************************************************************************/
42 
43 XrdSysMutex XrdSsiAlert::aMutex;
44 XrdSsiAlert *XrdSsiAlert::free = 0;
45 int XrdSsiAlert::fNum = 0;
46 int XrdSsiAlert::fMax = XrdSsiAlert::fmaxDflt;
47 
48 /******************************************************************************/
49 /* A l l o c */
50 /******************************************************************************/
51 
53 {
54  XrdSsiAlert *aP;
55 
56 // Obtain a lock
57 //
58  aMutex.Lock();
59 
60 // Allocate via stack or a new call
61 //
62  if (!(aP = free)) aP = new XrdSsiAlert();
63  else {free = aP->next; fNum--;}
64 
65 // Unlock mutex
66 //
67  aMutex.UnLock();
68 
69 // Fill out object and return it
70 //
71  aP->next = 0;
72  aP->theMsg = &aMsg;
73  return aP;
74 }
75 
76 /******************************************************************************/
77 /* D o n e */
78 /******************************************************************************/
79 
80 // Gets invoked only after query() on wtresp signal was sent
81 
82 void XrdSsiAlert::Done(int &retc, XrdOucErrInfo *eiP, const char *name)
83 {
84 
85 // This is an async callback so we need to delete our errinfo object.
86 //
87  delete eiP;
88 
89 // Simply recycle this object.
90 //
91  Recycle();
92 }
93 
94 /******************************************************************************/
95 /* R e c y c l e */
96 /******************************************************************************/
97 
99 {
100 
101 // Issue callback to release the message if we have one
102 //
103  if (theMsg) theMsg->RecycleMsg();
104 
105 // Place object on the queue unless we have too many
106 //
107  aMutex.Lock();
108  if (fNum >= fMax) delete this;
109  else {next = free; free = this; fNum++;}
110  aMutex.UnLock();
111 }
112 
113 /******************************************************************************/
114 /* S e t I n f o */
115 /******************************************************************************/
116 
117 int XrdSsiAlert::SetInfo(XrdOucErrInfo &eInfo, char *aMsg, int aLen)
118 {
119  static const int aIovSz = 3;
120  struct AlrtResp {struct iovec ioV[aIovSz]; XrdSsiRRInfoAttn aHdr;};
121 
122  AlrtResp *alrtResp;
123  char *mBuff, *aData;
124  int n;
125 
126 // We will be constructing the response in the message buffer. This is
127 // gauranteed to be big enough for our purposes so no need to check the size.
128 //
129  mBuff = eInfo.getMsgBuff(n);
130 
131 // Initialize the response
132 //
133  alrtResp = (AlrtResp *)mBuff;
134  memset(alrtResp, 0, sizeof(AlrtResp));
135  alrtResp->aHdr.pfxLen = htons(sizeof(XrdSsiRRInfoAttn));
136 
137 // Fill out iovec to point to our header
138 //
139 // alrtResp->ioV[0].iov_len = sizeof(XrdSsiRRInfoAttn) + msgBlen;
140  alrtResp->ioV[1].iov_base = mBuff+offsetof(struct AlrtResp, aHdr);
141  alrtResp->ioV[1].iov_len = sizeof(XrdSsiRRInfoAttn);
142 
143 // Fill out the iovec for the alert data
144 //
145  aData = theMsg->GetMsg(n);
146  alrtResp->ioV[2].iov_base = aData;
147  alrtResp->ioV[2].iov_len = n;
148  alrtResp->aHdr.mdLen = htonl(n);
149  alrtResp->aHdr.tag = XrdSsiRRInfoAttn::alrtResp;
150 
151 // Return up to 8 bytes of alert data for debugging purposes
152 //
153  if (aMsg) memcpy(aMsg, aData, (n < (int)sizeof(aMsg) ? n : 8));
154 
155 // Setup to have metadata actually sent to the requestor
156 //
157  eInfo.setErrCode(aIovSz);
158  return n;
159 }
char * getMsgBuff(int &mblen)
int setErrCode(int code)
void Recycle()
Definition: XrdSsiAlert.cc:98
void Done(int &Result, XrdOucErrInfo *cbInfo, const char *path=0)
Definition: XrdSsiAlert.cc:82
static XrdSsiAlert * Alloc(XrdSsiRespInfoMsg &aMsg)
Definition: XrdSsiAlert.cc:52
int SetInfo(XrdOucErrInfo &eInfo, char *aMsg, int aLen)
Definition: XrdSsiAlert.cc:117
XrdSsiAlert * next
Definition: XrdSsiAlert.hh:41
char * GetMsg(int &mlen)
virtual void RecycleMsg(bool sent=true)=0
static const int alrtResp
Definition: XrdSsiRRInfo.hh:91