This is Unofficial EPICS BASE Doxygen Site
cac.h
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 /*
11  * L O S A L A M O S
12  * Los Alamos National Laboratory
13  * Los Alamos, New Mexico 87545
14  *
15  * Copyright, 1986, The Regents of the University of California.
16  *
17  *
18  * Author: Jeff Hill
19  *
20  */
21 
22 #ifndef INC_cac_H
23 #define INC_cac_H
24 
25 #include "compilerDependencies.h"
28 #include "epicsTimer.h"
29 #include "epicsEvent.h"
30 #include "freeList.h"
31 #include "localHostName.h"
32 
33 #include "libCaAPI.h"
34 #include "nciu.h"
35 #include "comBuf.h"
36 #include "bhe.h"
37 #include "cacIO.h"
38 #include "netIO.h"
39 #include "localHostName.h"
40 #include "virtualCircuit.h"
41 
42 class netWriteNotifyIO;
43 class netReadNotifyIO;
44 class netSubscription;
45 class tcpiiu;
46 
47 // used to control access to cac's recycle routines which
48 // should only be indirectly invoked by CAC when its lock
49 // is applied
50 class cacRecycle {
51 public:
52  virtual void recycleReadNotifyIO (
54  virtual void recycleWriteNotifyIO (
56  virtual void recycleSubscription (
58 protected:
59  virtual ~cacRecycle() {}
60 };
61 
62 struct CASG;
63 class inetAddrID;
64 class caServerID;
65 struct caHdrLargeArray;
66 
68 {
69 public:
71  void * allocate ( size_t );
72  void release ( void * );
73 private:
76  cacComBufMemoryManager & operator = ( const cacComBufMemoryManager & );
77 };
78 
79 class notifyGuard {
80 public:
82  ~notifyGuard ();
83 private:
84  cacContextNotify & notify;
85  notifyGuard ( const notifyGuard & );
86  notifyGuard & operator = ( const notifyGuard & );
87 };
88 
89 class callbackManager : public notifyGuard {
90 public:
93  epicsMutex & callbackControl );
95 };
96 
97 class cac :
98  public cacContext,
99  private cacRecycle,
101 {
102 public:
103  cac (
104  epicsMutex & mutualExclusion,
105  epicsMutex & callbackControl,
106  cacContextNotify & );
107  virtual ~cac ();
108 
109  // beacon management
110  void beaconNotify ( const inetAddrID & addr, const epicsTime & currentTime,
111  ca_uint32_t beaconNumber, unsigned protocolRevision );
112  unsigned beaconAnomaliesSinceProgramStart (
113  epicsGuard < epicsMutex > & ) const;
114 
115  // IO management
116  void flush ( epicsGuard < epicsMutex > & guard );
117  bool executeResponse ( callbackManager &, tcpiiu &,
118  const epicsTime & currentTime, caHdrLargeArray &, char *pMsgBody );
119 
120  // channel routines
121  void transferChanToVirtCircuit (
122  unsigned cid, unsigned sid,
123  ca_uint16_t typeCode, arrayElementCount count,
124  unsigned minorVersionNumber, const osiSockAddr &,
125  const epicsTime & currentTime );
126  cacChannel & createChannel (
127  epicsGuard < epicsMutex > & guard, const char * pChannelName,
129  void destroyChannel (
131  void initiateConnect (
133  nciu * lookupChannel (
135 
136  // IO requests
137  netWriteNotifyIO & writeNotifyRequest (
139  unsigned type, arrayElementCount nElem, const void * pValue,
140  cacWriteNotify & );
141  netReadNotifyIO & readNotifyRequest (
143  unsigned type, arrayElementCount nElem,
144  cacReadNotify & );
145  netSubscription & subscriptionRequest (
147  unsigned type, arrayElementCount nElem, unsigned mask,
148  cacStateNotify &, bool channelIsInstalled );
149  bool destroyIO (
150  CallbackGuard & callbackGuard,
151  epicsGuard < epicsMutex > & mutualExclusionGuard,
152  const cacChannel::ioid & idIn,
153  nciu & chan );
154  void disconnectAllIO (
155  epicsGuard < epicsMutex > & cbGuard,
157  nciu &, tsDLList < baseNMIU > & ioList );
158 
159  void ioShow (
161  const cacChannel::ioid &id, unsigned level ) const;
162 
163  // exception generation
164  void exception (
165  epicsGuard < epicsMutex > & cbGuard,
167  int status, const char * pContext,
168  const char * pFileName, unsigned lineNo );
169 
170  // search destination management
171  void registerSearchDest (
173  bool findOrCreateVirtCircuit (
175  unsigned, tcpiiu *&, unsigned, SearchDestTCP * pSearchDest = NULL );
176 
177  // diagnostics
178  unsigned circuitCount ( epicsGuard < epicsMutex > & ) const;
179  void show ( epicsGuard < epicsMutex > &, unsigned level ) const;
180  int printFormated (
181  epicsGuard < epicsMutex > & callbackControl,
182  const char *pformat, ... ) const;
183  int varArgsPrintFormated (
184  epicsGuard < epicsMutex > & callbackControl,
185  const char *pformat, va_list args ) const;
186  double connectionTimeout ( epicsGuard < epicsMutex > & );
187 
188  unsigned maxContiguousFrames ( epicsGuard < epicsMutex > & ) const;
189 
190  // misc
191  const char * userNamePointer () const;
192  unsigned getInitializingThreadsPriority () const;
193  epicsMutex & mutexRef ();
194  void attachToClientCtx ();
195  void selfTest (
196  epicsGuard < epicsMutex > & ) const;
197  double beaconPeriod (
199  const nciu & chan ) const;
200  static unsigned lowestPriorityLevelAbove ( unsigned priority );
201  static unsigned highestPriorityLevelBelow ( unsigned priority );
202  void destroyIIU ( tcpiiu & iiu );
203 
204  const char * pLocalHostName ();
205 
206 private:
207  epicsSingleton < localHostName > :: reference _refLocalHostName;
208  chronIntIdResTable < nciu > chanTable;
209  //
210  // !!!! There is at this point no good reason
211  // !!!! to maintain one IO table for all types of
212  // !!!! IO. It would probably be better to maintain
213  // !!!! an independent table for each IO type. The
214  // !!!! new adaptive sized hash table will not
215  // !!!! waste memory. Making this change will
216  // !!!! avoid virtual function overhead when
217  // !!!! accessing the different types of IO. This
218  // !!!! approach would also probably be safer in
219  // !!!! terms of detecting damaged protocol.
220  //
222  resTable < bhe, inetAddrID > beaconTable;
224  tsDLList < tcpiiu > circuitList;
225  tsDLList < SearchDest > searchDestList;
227  tsFreeList
228  < class tcpiiu, 32, epicsMutexNOOP >
229  freeListVirtualCircuit;
230  tsFreeList
231  < class netReadNotifyIO, 1024, epicsMutexNOOP >
232  freeListReadNotifyIO;
233  tsFreeList
234  < class netWriteNotifyIO, 1024, epicsMutexNOOP >
235  freeListWriteNotifyIO;
236  tsFreeList
237  < class netSubscription, 1024, epicsMutexNOOP >
238  freeListSubscription;
239  tsFreeList
240  < class nciu, 1024, epicsMutexNOOP >
241  channelFreeList;
242  tsFreeList
243  < class msgForMultiplyDefinedPV, 16 >
244  mdpvFreeList;
245  cacComBufMemoryManager comBufMemMgr;
246  bheFreeStore bheFreeList;
247  epicsTime programBeginTime;
248  double connTMO;
249  // **** lock hierarchy ****
250  // 1) callback lock must always be acquired before
251  // the primary mutex if both locks are needed
252  epicsMutex & mutex;
253  epicsMutex & cbMutex;
254  epicsEvent iiuUninstall;
255  ipAddrToAsciiEngine & ipToAEngine;
256  epicsTimerQueueActive & timerQueue;
257  char * pUserName;
258  class udpiiu * pudpiiu;
259  void * tcpSmallRecvBufFreeList;
260  void * tcpLargeRecvBufFreeList;
261  cacContextNotify & notify;
262  epicsThreadId initializingThreadsId;
263  unsigned initializingThreadsPriority;
264  unsigned maxRecvBytesTCP;
265  unsigned maxContigFrames;
266  unsigned beaconAnomalyCount;
267  unsigned short _serverPort;
268  unsigned iiuExistenceCount;
269  bool cacShutdownInProgress;
270 
271  void recycleReadNotifyIO (
273  void recycleWriteNotifyIO (
275  void recycleSubscription (
277 
278  void disconnectChannel (
279  epicsGuard < epicsMutex > & cbGuard,
280  epicsGuard < epicsMutex > & guard, nciu & chan );
281 
282  void ioExceptionNotify ( unsigned id, int status,
283  const char * pContext, unsigned type, arrayElementCount count );
284  void ioExceptionNotifyAndUninstall ( unsigned id, int status,
285  const char * pContext, unsigned type, arrayElementCount count );
286 
287  void pvMultiplyDefinedNotify ( msgForMultiplyDefinedPV & mfmdpv,
288  const char * pChannelName, const char * pAcc, const char * pRej );
289 
290  // recv protocol stubs
291  bool versionAction ( callbackManager &, tcpiiu &,
292  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
293  bool echoRespAction ( callbackManager &, tcpiiu &,
294  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
295  bool writeNotifyRespAction ( callbackManager &, tcpiiu &,
296  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
297  bool searchRespAction ( callbackManager &, tcpiiu &,
298  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
299  bool readNotifyRespAction ( callbackManager &, tcpiiu &,
300  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
301  bool eventRespAction ( callbackManager &, tcpiiu &,
302  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
303  bool readRespAction ( callbackManager &, tcpiiu &,
304  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
305  bool clearChannelRespAction ( callbackManager &, tcpiiu &,
306  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
307  bool exceptionRespAction ( callbackManager &, tcpiiu &,
308  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
309  bool accessRightsRespAction ( callbackManager &, tcpiiu &,
310  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
311  bool createChannelRespAction ( callbackManager &, tcpiiu &,
312  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
313  bool verifyAndDisconnectChan ( callbackManager &, tcpiiu &,
314  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
315  bool badTCPRespAction ( callbackManager &, tcpiiu &,
316  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
317 
318  typedef bool ( cac::*pProtoStubTCP ) (
319  callbackManager &, tcpiiu &,
320  const epicsTime & currentTime, const caHdrLargeArray &, void *pMsgBdy );
321  static const pProtoStubTCP tcpJumpTableCAC [];
322 
323  bool defaultExcep ( callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
324  const char *pCtx, unsigned status );
325  bool eventAddExcep ( callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
326  const char *pCtx, unsigned status );
327  bool readExcep ( callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
328  const char *pCtx, unsigned status );
329  bool writeExcep ( callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
330  const char *pCtx, unsigned status );
331  bool clearChanExcep ( callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
332  const char *pCtx, unsigned status );
333  bool readNotifyExcep ( callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
334  const char *pCtx, unsigned status );
335  bool writeNotifyExcep ( callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
336  const char *pCtx, unsigned status );
337  typedef bool ( cac::*pExcepProtoStubTCP ) (
338  callbackManager &, tcpiiu &iiu, const caHdrLargeArray &hdr,
339  const char *pCtx, unsigned status );
340  static const pExcepProtoStubTCP tcpExcepJumpTableCAC [];
341 
342  cac ( const cac & );
343  cac & operator = ( const cac & );
344 
345  friend class tcpiiu;
346 };
347 
348 inline const char * cac::userNamePointer () const
349 {
350  return this->pUserName;
351 }
352 
353 inline unsigned cac::getInitializingThreadsPriority () const
354 {
355  return this->initializingThreadsPriority;
356 }
357 
359 {
360  return this->mutex;
361 }
362 
364  epicsGuard < epicsMutex > & callbackControl,
365  const char *pformat, va_list args ) const
366 {
367  callbackControl.assertIdenticalMutex ( this->cbMutex );
368  return this->notify.varArgsPrintFormated ( pformat, args );
369 }
370 
372 {
373  this->notify.attachToClientCtx ();
374 }
375 
377  epicsGuard < epicsMutex > & guard ) const
378 {
379  guard.assertIdenticalMutex ( this->mutex );
380  return this->beaconAnomalyCount;
381 }
382 
384  notify ( notifyIn )
385 {
386  this->notify.callbackProcessingInitiateNotify ();
387 }
388 
390 {
391  this->notify.callbackProcessingCompleteNotify ();
392 }
393 
395  cacContextNotify & notify, epicsMutex & callbackControl ) :
396  notifyGuard ( notify ), cbGuard ( callbackControl )
397 {
398 }
399 
402  const cacChannel::ioid & idIn )
403 {
404  guard.assertIdenticalMutex ( this->mutex );
405  return this->chanTable.lookup ( idIn );
406 }
407 
408 inline const char * cac :: pLocalHostName ()
409 {
410  return _refLocalHostName->pointer ();
411 }
412 
413 inline unsigned cac ::
415 {
416  return maxContigFrames;
417 }
418 
419 inline double cac ::
421 {
422  guard.assertIdenticalMutex ( this->mutex );
423  return this->connTMO;
424 }
425 
426 #endif // ifndef INC_cac_H
const char * pLocalHostName()
Definition: cac.h:408
virtual ~cacRecycle()
Definition: cac.h:59
Definition: cac.h:97
unsigned getInitializingThreadsPriority() const
Definition: cac.h:353
virtual void recycleSubscription(epicsGuard< epicsMutex > &, netSubscription &io)=0
pvd::Status status
epicsMutex & cbMutex
pvd::StructureConstPtr type
unsigned int ca_uint32_t
Definition: caProto.h:76
void assertIdenticalMutex(const T &) const
Definition: epicsGuard.h:80
void attachToClientCtx()
Definition: cac.h:371
#define NULL
Definition: catime.c:38
double connectionTimeout(epicsGuard< epicsMutex > &)
Definition: cac.h:420
virtual void recycleWriteNotifyIO(epicsGuard< epicsMutex > &, netWriteNotifyIO &io)=0
unsigned short ca_uint16_t
Definition: caProto.h:75
unsigned priLev
Definition: cacIO.h:165
virtual void recycleReadNotifyIO(epicsGuard< epicsMutex > &, netReadNotifyIO &io)=0
Definition: cac.h:50
nciu * lookupChannel(epicsGuard< epicsMutex > &, const cacChannel::ioid &)
Definition: cac.h:400
unsigned ioid
Definition: cacIO.h:173
int varArgsPrintFormated(epicsGuard< epicsMutex > &callbackControl, const char *pformat, va_list args) const
Definition: cac.h:363
epicsMutex mutex
Definition: pvAccess.cpp:71
unsigned maxContiguousFrames(epicsGuard< epicsMutex > &) const
Definition: cac.h:414
char * allocate(unsigned int n)
Definition: antelope.c:230
epicsMutex & mutex
APIs for the epicsEvent binary semaphore.
epicsGuard< epicsMutex > cbGuard
Definition: cac.h:94
unsigned long arrayElementCount
Definition: cacIO.h:57
callbackManager(cacContextNotify &, epicsMutex &callbackControl)
Definition: cac.h:394
epicsEventId flush
Definition: errlog.c:70
int(* pProtoStubTCP)(caHdrLargeArray *mp, void *pPayload, struct client *client)
Definition: camessage.c:2315
Definition: udpiiu.h:79
virtual void callbackProcessingInitiateNotify()=0
Compiler specific declarations.
notifyGuard(cacContextNotify &)
Definition: cac.h:383
~notifyGuard()
Definition: cac.h:389
cacComBufMemoryManager()
Definition: cac.h:70
Definition: netiiu.h:37
unsigned beaconAnomaliesSinceProgramStart(epicsGuard< epicsMutex > &) const
Definition: cac.h:376
const char * userNamePointer() const
Definition: cac.h:348
epicsMutex & mutexRef()
Definition: cac.h:358
Definition: nciu.h:127
virtual void callbackProcessingCompleteNotify()=0