ROOT  6.06/08
Reference Guide
TXSocket.h
Go to the documentation of this file.
1 // @(#)root/proofx:$Id$
2 // Author: G. Ganis Oct 2005
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TXSocket
13 #define ROOT_TXSocket
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TXSocket //
18 // //
19 // High level handler of connections to xproofd. //
20 // //
21 //////////////////////////////////////////////////////////////////////////
22 
23 #define DFLT_CONNECTMAXTRY 10
24 
25 #ifndef ROOT_TMutex
26 #include "TMutex.h"
27 #endif
28 #ifndef ROOT_TSemaphore
29 #include "TSemaphore.h"
30 #endif
31 #ifndef ROOT_TString
32 #include "TString.h"
33 #endif
34 #ifndef ROOT_TList
35 #include "TList.h"
36 #endif
37 #ifndef ROOT_TMessage
38 #include "TMessage.h"
39 #endif
40 #ifndef ROOT_TUrl
41 #include "TUrl.h"
42 #endif
43 #ifndef ROOT_TSocket
44 #include "TSocket.h"
45 #endif
46 #ifndef __XPTYPES_H
47 #include "XProtocol/XPtypes.hh"
48 #endif
49 #ifndef XRC_UNSOLMSG_H
50 #include "XrdClient/XrdClientUnsolMsg.hh"
51 #endif
52 
53 #include <list>
54 
55 class TObjString;
56 class TXSockBuf;
57 class TXSockPipe;
58 class TXHandler;
59 class TXSocketHandler;
60 class XrdClientMessage;
61 class XrdProofConn;
62 
63 // To transmit info to Handlers
64 typedef struct {
69 } XHandleIn_t;
70 typedef struct {
72  const char *fMsg;
73 } XHandleErr_t;
74 
75 class TXSocket : public TSocket, public XrdClientAbsUnsolMsgHandler {
76 
77 friend class TXProofMgr;
78 friend class TXProofServ;
79 friend class TXSlave;
80 friend class TXSocketHandler;
81 friend class TXSockPipe;
82 friend class TXUnixSocket;
83 
84 private:
85  char fMode; // 'e' (def) or 'i' (internal - proofsrv)
86  kXR_int32 fSendOpt; // Options for sending messages
87  Short_t fSessionID; // proofsrv: remote ID of connected session
88  TString fUser; // Username used for login
89  TString fHost; // Remote host
90  Int_t fPort; // Remote port
91 
92  Int_t fLogLevel; // Log level to be transmitted to servers
93 
94  TString fBuffer; // Container for exchanging information
95  TObject *fReference; // Generic object reference of this socket
96  TXHandler *fHandler; // Handler of asynchronous events (input, error)
97 
98  XrdProofConn *fConn; // instance of the underlying connection module
99 
100  // Asynchronous messages
101  TSemaphore fASem; // Control access to conn async msg queue
102  TMutex *fAMtx; // To protect async msg queue
103  Bool_t fAWait; // kTRUE if waiting at the async msg queue
104  std::list<TXSockBuf *> fAQue; // list of asynchronous messages
105  Int_t fByteLeft; // bytes left in the first buffer
106  Int_t fByteCur; // current position in the first buffer
107  TXSockBuf *fBufCur; // current read buffer
108 
109  TSemaphore fAsynProc; // Control actions while processing async messages
110 
111  // Interrupts
112  TMutex *fIMtx; // To protect interrupt queue
113  kXR_int32 fILev; // Highest received interrupt
114  Bool_t fIForward; // Whether the interrupt should be propagated
115 
116  // Process ID of the instatiating process (to signal interrupts)
118 
119  // Whether to timeout or not
120  Bool_t fDontTimeout; // If true wait forever for incoming messages
121  Bool_t fRDInterrupt; // To interrupt waiting for messages
122 
123  // Version of the remote XrdProofdProtocol
125 
126  // Static area for input handling
127  static TXSockPipe fgPipe; // Pipe for input monitoring
128  static TString fgLoc; // Location string
129  static Bool_t fgInitDone; // Avoid initializing more than once
130 
131  // List of spare buffers
132  static TMutex fgSMtx; // To protect spare list
133  static std::list<TXSockBuf *> fgSQue; // list of spare buffers
134 
135  // Manage asynchronous message
136  Int_t PickUpReady();
138  void PushBackSpare();
139 
140  // Post a message into the queue for asynchronous processing
141  void PostMsg(Int_t type, const char *msg = 0);
142 
143  // Auxilliary
144  Int_t GetLowSocket() const;
145 
146  static void SetLocation(const char *loc = ""); // Set location string
147 
148  static void InitEnvs(); // Initialize environment variables
149 
150 public:
151  // Should be the same as in proofd/src/XrdProofdProtocol::Urgent
152  enum EUrgentMsgType { kStopProcess = 2000 };
153 
154  TXSocket(const char *url, Char_t mode = 'M', Int_t psid = -1, Char_t ver = -1,
155  const char *logbuf = 0, Int_t loglevel = -1, TXHandler *handler = 0);
156 #if 0
157  TXSocket(const TXSocket &xs);
158  TXSocket& operator=(const TXSocket& xs);
159 #endif
160  virtual ~TXSocket();
161 
162  virtual void Close(Option_t *opt = "");
163  Bool_t Create(Bool_t attach = kFALSE);
164  void DisconnectSession(Int_t id, Option_t *opt = "");
165 
166  void DoError(int level,
167  const char *location, const char *fmt, va_list va) const;
168 
169  virtual UnsolRespProcResult ProcessUnsolicitedMsg(XrdClientUnsolMsgSender *s,
170  XrdClientMessage *msg);
171 
172  virtual Int_t GetClientID() const { return -1; }
173  virtual Int_t GetClientIDSize() const { return 1; }
174  Int_t GetLogConnID() const;
175  Int_t GetOpenError() const;
176  Int_t GetServType() const;
177  Int_t GetSessionID() const;
179 
180  Bool_t IsValid() const;
182  virtual void RemoveClientID() { }
183  virtual void SetClientID(Int_t) { }
184  void SetSendOpt(ESendRecvOptions o) { fSendOpt = o; }
185  void SetSessionID(Int_t id);
186 
187  // Send interfaces
188  Int_t Send(const TMessage &mess);
189  Int_t Send(Int_t kind) { return TSocket::Send(kind); }
190  Int_t Send(Int_t status, Int_t kind)
191  { return TSocket::Send(status, kind); }
192  Int_t Send(const char *mess, Int_t kind = kMESS_STRING)
193  { return TSocket::Send(mess, kind); }
194  Int_t SendRaw(const void *buf, Int_t len,
196 
197  TObjString *SendCoordinator(Int_t kind, const char *msg = 0, Int_t int2 = 0,
198  Long64_t l64 = 0, Int_t int3 = 0, const char *opt = 0);
199 
200  // Recv interfaces
201  Int_t Recv(TMessage *&mess);
202  Int_t Recv(Int_t &status, Int_t &kind)
203  { return TSocket::Recv(status, kind); }
204  Int_t Recv(char *mess, Int_t max)
205  { return TSocket::Recv(mess, max); }
206  Int_t Recv(char *mess, Int_t max, Int_t &kind)
207  { return TSocket::Recv(mess, max, kind); }
208  Int_t RecvRaw(void *buf, Int_t len,
209  ESendRecvOptions opt = kDefault);
210 
211  // Interrupts
213  Int_t GetInterrupt(Bool_t &forward);
214 
215  // Urgent message
216  void SendUrgent(Int_t type, Int_t int1, Int_t int2);
217 
218  // Interrupt the low level socket
219  void SetInterrupt(Bool_t i = kTRUE);
220  inline Bool_t IsInterrupt() { R__LOCKGUARD(fAMtx); return fRDInterrupt; }
221 
222  // Set / Check async msg queue waiting status
223  inline void SetAWait(Bool_t w = kTRUE) { R__LOCKGUARD(fAMtx); fAWait = w; }
224  inline Bool_t IsAWait() { R__LOCKGUARD(fAMtx); return fAWait; }
225 
226  // Flush the asynchronous queue
227  Int_t Flush();
228 
229  // Ping the counterpart
230  Bool_t Ping(const char *ord = 0);
231 
232  // Request remote touch of the admin file associated with this connection
233  void RemoteTouch();
234  // Propagate a Ctrl-C
235  void CtrlC();
236 
237  // Standard options cannot be set
239 
240  // Disable / Enable read timeout
241  void DisableTimeout() { fDontTimeout = kTRUE; }
242  void EnableTimeout() { fDontTimeout = kFALSE; }
243 
244  // Try reconnection after error
245  virtual Int_t Reconnect();
246 
247  ClassDef(TXSocket, 0) //A high level connection class for PROOF
248 };
249 
250 
251 //
252 // The following structure is used to store buffers received asynchronously
253 //
254 class TXSockBuf {
255 public:
261 
262  TXSockBuf(Char_t *bp=0, Int_t sz=0, Bool_t own=1);
263  ~TXSockBuf();
264 
265  void Resize(Int_t sz);
266 
267  static Long64_t BuffMem();
268  static Long64_t GetMemMax();
269  static void SetMemMax(Long64_t memmax);
270 
271 private:
273  static Long64_t fgBuffMem; // Total allocated memory
274  static Long64_t fgMemMax; // Max allocated memory allowed
275 };
276 
277 //
278 // The following class describes internal pipes
279 //
280 class TXSockPipe {
281 public:
282 
283  TXSockPipe(const char *loc = "");
284  virtual ~TXSockPipe();
285 
286  Bool_t IsValid() const { return ((fPipe[0] >= 0 && fPipe[1] >= 0) ? kTRUE : kFALSE); }
287 
288  TXSocket *GetLastReady();
289 
290  Int_t GetRead() const { return fPipe[0]; }
291  Int_t Post(TSocket *s); // Notify socket ready via global pipe
292  Int_t Clean(TSocket *s); // Clean previous pipe notification
293  Int_t Flush(TSocket *s); // Remove any instance of 's' from the pipe
294  void DumpReadySock();
295 
296  void SetLoc(const char *loc = "") { fLoc = loc; }
297 
298 private:
299  TMutex fMutex; // Protect access to the sockets-ready list
300  Int_t fPipe[2]; // Pipe for input monitoring
301  TString fLoc; // Location string
302  TList fReadySock; // List of sockets ready to be read
303 };
304 
305 //
306 // Guard for a semaphore
307 //
309 public:
310 
311  TXSemaphoreGuard(TSemaphore *sem) : fSem(sem), fValid(kTRUE) { if (!fSem || fSem->TryWait()) fValid = kFALSE; }
312  virtual ~TXSemaphoreGuard() { if (fValid && fSem) fSem->Post(); }
313 
314  Bool_t IsValid() const { return fValid; }
315 
316 private:
319 };
320 
321 #endif
void SetSendOpt(ESendRecvOptions o)
Definition: TXSocket.h:184
Definition: TMutex.h:37
Int_t fCid
Definition: TXSocket.h:260
static void SetLocation(const char *loc="")
Set location string.
Definition: TXSocket.cxx:254
virtual void RemoveClientID()
Definition: TXSocket.h:182
virtual void SetClientID(Int_t)
Definition: TXSocket.h:183
Int_t Send(const char *mess, Int_t kind=kMESS_STRING)
Send a character string buffer.
Definition: TXSocket.h:192
TSemaphore fAsynProc
Definition: TXSocket.h:109
Int_t Recv(Int_t &status, Int_t &kind)
Receives a status and a message type.
Definition: TXSocket.h:202
long long Long64_t
Definition: RtypesCore.h:69
Int_t SendRaw(const void *buf, Int_t len, ESendRecvOptions opt=kDontBlock)
Send a raw buffer of specified length.
Definition: TXSocket.cxx:1216
TSemaphore fASem
Definition: TXSocket.h:101
void SetLoc(const char *loc="")
Definition: TXSocket.h:296
Int_t Recv(char *mess, Int_t max)
Receive a character string message of maximum max length.
Definition: TXSocket.h:204
int GetServType() const
Definition: XrdProofConn.h:143
Collectable string class.
Definition: TObjString.h:32
int GetLogConnID() const
Definition: XrdProofConn.h:140
Bool_t IsAWait()
Definition: TXSocket.h:224
const char Option_t
Definition: RtypesCore.h:62
ESockOptions
Definition: TSocket.h:52
Int_t fLogLevel
Definition: TXSocket.h:92
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:520
XrdProofConn * fConn
Definition: TXSocket.h:98
char fMode
Definition: TXSocket.h:85
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
EUrgentMsgType
Definition: TXSocket.h:152
TMutex * fAMtx
Definition: TXSocket.h:102
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual ~TXSocket()
Destructor.
Definition: TXSocket.cxx:239
Int_t Recv(char *mess, Int_t max, Int_t &kind)
Receive a character string message of maximum max length.
Definition: TXSocket.h:206
const Bool_t kFALSE
Definition: Rtypes.h:92
TMutex * fIMtx
Definition: TXSocket.h:112
void SetSessionID(Int_t id)
Set session ID to &#39;id&#39;. If id < 0, disable also the asynchronous handler.
Definition: TXSocket.cxx:268
Int_t fInt3
Definition: TXSocket.h:67
void RemoteTouch()
Remote touch functionality: contact the server to proof our vitality.
Definition: TXSocket.cxx:1345
Int_t fOpt
Definition: TXSocket.h:71
ESendRecvOptions
Definition: TSocket.h:65
Int_t fInt2
Definition: TXSocket.h:66
static Long64_t fgBuffMem
Definition: TXSocket.h:273
virtual ~TXSemaphoreGuard()
Definition: TXSocket.h:312
int GetOpenError() const
Definition: XrdProofConn.h:142
void SetInterrupt()
Interrupt the underlying socket.
static Long64_t fgMemMax
Definition: TXSocket.h:274
Bool_t fAWait
Definition: TXSocket.h:103
Int_t fPort
Definition: TXSocket.h:90
Int_t RecvRaw(void *buf, Int_t len, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TXSocket.cxx:1581
#define ClassDef(name, id)
Definition: Rtypes.h:254
static void InitEnvs()
Init environment variables for XrdClient.
Definition: TXSocket.cxx:1984
static std::list< TXSockBuf * > fgSQue
Definition: TXSocket.h:133
Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TXSocket.cxx:1689
Int_t GetInterrupt(Bool_t &forward)
Get latest interrupt level and reset it; if the interrupt has to be propagated to lower stages forwar...
Definition: TXSocket.cxx:970
Bool_t fIForward
Definition: TXSocket.h:114
TXHandler * fHandler
Definition: TXSocket.h:96
TXSockBuf * PopUpSpare(Int_t sz)
Pop-up a buffer of at least size bytes from the spare list If none is found either one is reallocated...
Definition: TXSocket.cxx:1510
void DoError(int level, const char *location, const char *fmt, va_list va) const
Interface to ErrorHandler (protected).
Definition: TXSocket.cxx:71
Bool_t IsServProofd()
Return kTRUE if the remote server is a &#39;proofd&#39;.
Definition: TXSocket.cxx:957
TString fUser
Definition: TXSocket.h:88
Int_t fInt4
Definition: TXSocket.h:68
A doubly linked list.
Definition: TList.h:47
Bool_t fOwn
Definition: TXSocket.h:259
Bool_t Create(Bool_t attach=kFALSE)
This method sends a request for creation of (or attachment to) a remote server application.
Definition: TXSocket.cxx:1051
Int_t fByteCur
Definition: TXSocket.h:106
Int_t Flush()
Flush the asynchronous queue.
Definition: TXSocket.cxx:1002
Bool_t IsInterrupt()
Definition: TXSocket.h:220
TString fHost
Definition: TXSocket.h:89
virtual Int_t GetClientID() const
Definition: TXSocket.h:172
void SetAWait(Bool_t w=kTRUE)
Definition: TXSocket.h:223
Int_t fXrdProofdVersion
Definition: TXSocket.h:124
void PostMsg(Int_t type, const char *msg=0)
Post a message of type &#39;type&#39; into the read messages queue.
Definition: TXSocket.cxx:860
Bool_t Ping(const char *ord=0)
Ping functionality: contact the server to check its vitality.
Definition: TXSocket.cxx:1273
TString fBuffer
Definition: TXSocket.h:94
static TMutex fgSMtx
Definition: TXSocket.h:132
short Short_t
Definition: RtypesCore.h:35
std::list< TXSockBuf * > fAQue
Definition: TXSocket.h:104
static TString fgLoc
Definition: TXSocket.h:128
Char_t * fBuf
Definition: TXSocket.h:258
TString fLoc
Definition: TXSocket.h:301
TList fReadySock
Definition: TXSocket.h:302
Int_t fSiz
Definition: TXSocket.h:256
Bool_t IsValid() const
Definition: TXSocket.h:286
TXSemaphoreGuard(TSemaphore *sem)
Definition: TXSocket.h:311
Int_t fLen
Definition: TXSocket.h:257
Int_t Send(Int_t status, Int_t kind)
Send a status and a single message opcode.
Definition: TXSocket.h:190
void DisconnectSession(Int_t id, Option_t *opt="")
Disconnect a session.
Definition: TXSocket.cxx:280
TXSockBuf * fBufCur
Definition: TXSocket.h:107
int GetLowSocket()
Return the socket descriptor of the underlying connection.
Short_t fSessionID
Definition: TXSocket.h:87
Int_t Send(Int_t kind)
Send a single message opcode.
Definition: TXSocket.h:189
friend class TXSocket
Definition: XrdProofConn.h:60
Bool_t IsValid() const
Definition: TXSocket.h:314
int type
Definition: TGX11.cxx:120
virtual void Close(const char *opt="")
Close connection.
#define R__LOCKGUARD(mutex)
Bool_t fDontTimeout
Definition: TXSocket.h:120
void DisableTimeout()
Definition: TXSocket.h:241
Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TXSocket.cxx:1774
Char_t * fMem
Definition: TXSocket.h:272
Int_t fInt1
Definition: TXSocket.h:65
kXR_int32 fSendOpt
Definition: TXSocket.h:86
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
Bool_t fRDInterrupt
Definition: TXSocket.h:121
TObject * fReference
Definition: TXSocket.h:95
Binding & operator=(OUT(*fun)(void))
Mother of all ROOT objects.
Definition: TObject.h:58
Int_t SetOption(ESockOptions, Int_t)
Set socket options.
Definition: TXSocket.h:238
char Char_t
Definition: RtypesCore.h:29
void PushBackSpare()
Release read buffer giving back to the spare list.
Definition: TXSocket.cxx:1560
const char * fMsg
Definition: TXSocket.h:72
kXR_int32 fILev
Definition: TXSocket.h:113
Int_t PickUpReady()
Wait and pick-up next buffer from the asynchronous queue.
Definition: TXSocket.cxx:1419
static Bool_t fgInitDone
Definition: TXSocket.h:129
Int_t Reconnect()
Try reconnection after failure.
TObjString * SendCoordinator(Int_t kind, const char *msg=0, Int_t int2=0, Long64_t l64=0, Int_t int3=0, const char *opt=0)
Send message to intermediate coordinator.
Definition: TXSocket.cxx:1825
void SendUrgent(Int_t type, Int_t int1, Int_t int2)
Send urgent message to counterpart; &#39;type&#39; specifies the type of the message (see TXSocket::EUrgentMs...
Definition: TXSocket.cxx:1942
static TXSockPipe fgPipe
Definition: TXSocket.h:127
short GetSessionID() const
Definition: XrdProofConn.h:144
bool IsValid() const
Test validity of this connection.
virtual Int_t GetClientIDSize() const
Definition: TXSocket.h:173
Int_t SendInterrupt(Int_t type)
Send urgent message (interrupt) to remote server Returns 0 or -1 in case of error.
Definition: TXSocket.cxx:1638
Int_t fPid
Definition: TXSocket.h:117
TSemaphore * fSem
Definition: TXSocket.h:317
const Bool_t kTRUE
Definition: Rtypes.h:91
Int_t fByteLeft
Definition: TXSocket.h:105
Int_t GetRead() const
Definition: TXSocket.h:290
void EnableTimeout()
Definition: TXSocket.h:242
void CtrlC()
Interrupt the remote protocol instance.
Definition: TXSocket.cxx:1383
TMutex fMutex
Definition: TXSocket.h:299
Int_t GetXrdProofdVersion() const
Definition: TXSocket.h:178
virtual UnsolRespProcResult ProcessUnsolicitedMsg(XrdClientUnsolMsgSender *s, XrdClientMessage *m)
We are here if an unsolicited response comes from a logical conn The response comes in the form of an...