CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SlaveQueue.h
Go to the documentation of this file.
1 #ifndef EVENTFILTER_UTILITIES_SLAVEQUEUE_H
2 #define EVENTFILTER_UTILITIES_SLAVEQUEUE_H
3 
4 #include <stdio.h> /* standard I/O functions. */
5 #include <stdlib.h> /* malloc(), free() etc. */
6 #include <sys/types.h> /* various type definitions. */
7 #include <sys/ipc.h> /* general SysV IPC structures */
8 #include <sys/msg.h> /* message queue functions and structs. */
9 #include <errno.h>
10 #include <string.h>
11 
13 
14 //@EM ToDo move implementation to .cc file
15 
16 namespace evf{
17 
18  class SlaveQueue{
19 
20  public:
21 
22  SlaveQueue(unsigned int ind) : queue_id_(0)
23  {
24 
25  /* get an (existing) public message queue */
26  queue_id_ = msgget(QUEUE_ID+ind, 0);
27  if (queue_id_ == -1) {
28  XCEPT_RAISE(evf::Exception, "failed to get message queue");
29  }
30  }
32  {
33  }
34 
35  int post(MsgBuf &ptr)
36  {
37  int rc; /* error code retuend by system calls. */
38  rc = msgsnd(queue_id_,ptr.ptr_, ptr.msize(),0);
39  // delete ptr;
40  if(rc==-1)
41  std::cout << "snd::Slave failed to post message - error:"
42  << strerror(errno) << std::endl;
43  return rc;
44  }
45  unsigned long rcv(MsgBuf &ptr)
46  {
47  unsigned long msg_type = MSQS_MESSAGE_TYPE_SLA;
48  int rc = msgrcv(queue_id_, ptr.ptr_, ptr.msize(), - msg_type, 0);
49  if (rc == -1 && errno != ENOMSG)
50  {
51  std::string serr = "rcv::Slave failed to get message from queue - error:";
52  serr += strerror(errno);
53  XCEPT_RAISE(evf::Exception, serr);
54  }
55  else if(rc == -1 && errno == ENOMSG) return MSGQ_MESSAGE_TYPE_RANGE;
56  return msg_type;
57  }
58  unsigned long rcvNonBlocking(MsgBuf &ptr)
59  {
60  unsigned long msg_type = MSQS_MESSAGE_TYPE_SLA;
61  int rc = msgrcv(queue_id_, ptr.ptr_, ptr.msize(), - msg_type, IPC_NOWAIT);
62  if (rc == -1 && errno != ENOMSG)
63  {
64  std::string serr = "rcvnb::Slave failed to get message from queue - error:";
65  serr += strerror(errno);
66  XCEPT_RAISE(evf::Exception, serr);
67  }
68  else if(rc == -1 && errno == ENOMSG) return MSGQ_MESSAGE_TYPE_RANGE;
69  return msg_type;
70  }
71  unsigned long rcvNonBlockingAny(MsgBuf &ptr)
72  {
73  unsigned long msg_type = 0;
74  int rc = msgrcv(queue_id_, ptr.ptr_, ptr.msize(), msg_type, IPC_NOWAIT);
75  if (rc == -1 && errno != ENOMSG)
76  {
77  std::string serr = "rcvnb::Slave failed to get message from queue - error:";
78  serr += strerror(errno);
79  XCEPT_RAISE(evf::Exception, serr);
80  }
81  else if(rc == -1 && errno == ENOMSG) return MSGQ_MESSAGE_TYPE_RANGE;
82  return msg_type;
83  }
84  int id() const {return queue_id_;}
85  private:
86 
87  int queue_id_; /* ID of the created queue. */
88 
89  };
90 }
91 #endif
int id() const
Definition: SlaveQueue.h:84
size_t msize()
Definition: MsgBuf.cc:34
#define MSQS_MESSAGE_TYPE_SLA
Definition: queue_defs.h:27
#define MSGQ_MESSAGE_TYPE_RANGE
Definition: queue_defs.h:13
struct msgbuf * ptr_
Definition: MsgBuf.h:22
unsigned long rcvNonBlocking(MsgBuf &ptr)
Definition: SlaveQueue.h:58
#define QUEUE_ID
Definition: queue_defs.h:9
int post(MsgBuf &ptr)
Definition: SlaveQueue.h:35
unsigned long rcv(MsgBuf &ptr)
Definition: SlaveQueue.h:45
unsigned long rcvNonBlockingAny(MsgBuf &ptr)
Definition: SlaveQueue.h:71
tuple cout
Definition: gather_cfg.py:41
SlaveQueue(unsigned int ind)
Definition: SlaveQueue.h:22