cMsg Messaging System  5.2
 All Classes Namespaces Files Functions Variables Friends
cMsg.hxx
Go to the documentation of this file.
1 // still to do:
2 
3 // why is myMsgPointer public?
4 // is friend class needed?
5 // should subscription config be public?
6 // add stream operators to cMsgMessage? e.g: msg << setName(aName) << aValue;
7 
8 
9 
10 /*----------------------------------------------------------------------------*
11 * Copyright (c) 2005 Southeastern Universities Research Association, *
12 * Thomas Jefferson National Accelerator Facility *
13 * *
14 * This software was developed under a United States Government license *
15 * described in the NOTICE file included as part of this distribution. *
16 * *
17 * E.Wolin, 25-Feb-2005, Jefferson Lab *
18 * *
19 * Authors: Elliott Wolin *
20 * wolin@jlab.org Jefferson Lab, MS-6B *
21 * Phone: (757) 269-7365 12000 Jefferson Ave. *
22 * Fax: (757) 269-5519 Newport News, VA 23606 *
23 *
24 *----------------------------------------------------------------------------*/
25 
26 
27 #ifndef _cMsg_hxx
28 #define _cMsg_hxx
29 
30 
31 #include <cMsg.h>
32 #include <string>
33 #include <exception>
34 #include <vector>
35 #include <map>
36 
37 
41 namespace cmsg {
42 
43 using namespace std;
44 
45 
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 
49 
53 class cMsgException : public std::exception {
54 
55 public:
56  cMsgException(void);
57  cMsgException(const string &descr);
58  cMsgException(const string &descr, int code);
59  cMsgException(const cMsgException &e);
60  virtual ~cMsgException(void) throw();
61 
62  virtual string toString(void) const throw();
63  virtual const char *what(void) const throw();
64 
65 
66 public:
67  string descr;
68  int returnCode;
69 };
70 
71 
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 
75 
79 class cMsgMessage {
80 
81  friend class cMsg;
84 public:
85  cMsgMessage(void) throw(cMsgException);
86  cMsgMessage(const cMsgMessage &m) throw(cMsgException);
87  cMsgMessage(void *msgPointer) throw(cMsgException);
88  virtual ~cMsgMessage(void);
89 
90  virtual string getSubject(void) const throw(cMsgException);
91  virtual void setSubject(const string &subject) throw(cMsgException);
92  virtual string getType(void) const throw(cMsgException);
93  virtual void setType(const string &type) throw(cMsgException);
94  virtual string getText(void) const throw(cMsgException);
95  virtual void setText(const string &text) throw(cMsgException);
96 
97  virtual void setByteArrayLength(int length) throw(cMsgException);
98  virtual void resetByteArrayLength();
99  virtual int getByteArrayLength(void);
100  virtual int getByteArrayLengthFull(void);
101  virtual void setByteArrayOffset(int offset) throw(cMsgException);
102  virtual int getByteArrayOffset(void);
103  virtual int getByteArrayEndian(void);
104  virtual void setByteArrayEndian(int endian) throw(cMsgException);
105  virtual bool needToSwap(void) const throw(cMsgException);
106  virtual char* getByteArray(void);
107  virtual void setByteArray(char *array, int length) throw(cMsgException);
108  virtual void setByteArrayNoCopy(char* array, int length) throw(cMsgException);
109 
110  virtual int getUserInt(void) const throw(cMsgException);
111  virtual void setUserInt(int i) throw(cMsgException);
112  virtual struct timespec getUserTime(void) const throw(cMsgException);
113  virtual void setUserTime(const struct timespec &userTime) throw(cMsgException);
114  virtual int getVersion(void) const throw(cMsgException);
115  virtual string getDomain(void) const throw(cMsgException);
116  virtual string getReceiver(void) const throw(cMsgException);
117  virtual string getReceiverHost(void) const throw(cMsgException);
118  virtual string getSender(void) const throw(cMsgException);
119  virtual string getSenderHost(void) const throw(cMsgException);
120  virtual struct timespec getReceiverTime(void) const throw(cMsgException);
121  virtual struct timespec getSenderTime(void) const throw(cMsgException);
122  virtual bool isGetRequest(void) const throw(cMsgException);
123  virtual bool isGetResponse(void) const throw(cMsgException);
124  virtual bool isNullGetResponse(void) const throw(cMsgException);
125 
126  virtual void makeNullResponse(const cMsgMessage &msg) throw(cMsgException);
127  virtual void makeNullResponse(const cMsgMessage *msg) throw(cMsgException);
128  virtual void makeResponse(const cMsgMessage &msg) throw(cMsgException);
129  virtual void makeResponse(const cMsgMessage *msg) throw(cMsgException);
130 
131  virtual void setGetResponse(bool b) throw(cMsgException);
132  virtual void setNullGetResponse(bool b) throw(cMsgException);
133  virtual string toString(void) const throw(cMsgException);
134  virtual cMsgMessage *copy(void) const throw(cMsgException);
135  virtual cMsgMessage *nullResponse(void) const throw(cMsgException);
136  virtual cMsgMessage *response(void) const throw(cMsgException);
137  virtual string getSubscriptionDomain() const throw(cMsgException);
138  virtual string getSubscriptionSubject() const throw(cMsgException);
139  virtual string getSubscriptionType() const throw(cMsgException);
140  virtual string getSubscriptionUDL() const throw(cMsgException);
141  virtual int getSubscriptionCueSize(void) const throw(cMsgException);
142  virtual bool getReliableSend(void) const throw(cMsgException);
143  virtual void setReliableSend(bool b) throw(cMsgException);
144 
145 
146 
147  //---------------
148  // PAYLOAD STUFF
149  //---------------
150 
151 
152 public:
153 
154  virtual bool hasPayload() const;
155 
156  virtual void payloadClear(void);
157  virtual void payloadReset(void);
158  virtual void payloadPrint(void) const;
159  virtual void payloadCopy(const cMsgMessage &msg) throw(cMsgException);
160 
161  virtual bool payloadRemoveField(const string &name);
162  virtual string payloadGetText() const;
163  virtual void payloadSetFromText(const string &txt) throw(cMsgException);
164  virtual string payloadGetFieldDescription(const string &name) const throw(cMsgException);
165 
166  virtual map<string,int> *payloadGet() const throw(cMsgException);
167  virtual int payloadGetCount() const;
168  virtual bool payloadContainsName (const string &name) const;
169  virtual int payloadGetType (const string &name) const throw(cMsgException);
170  virtual void setHistoryLengthMax (int len) const throw(cMsgException);
171 
172 
173  //
174  // Methods to get a payload item's value
175  //
176  virtual void getBinary(const string &name, const char **val, int &len, int &endian)
177  const throw(cMsgException);
178  virtual void getBinaryArray(const string &name, const char ***vals, int **lens, int **endians, int &count)
179  const throw(cMsgException);
180 
181  virtual cMsgMessage *getMessage(const string &name) const throw(cMsgException);
182  virtual vector<cMsgMessage> *getMessageVector(const string &name) const throw(cMsgException);
183  virtual vector<cMsgMessage*> *getMessagePVector(const string &name) const throw(cMsgException);
184  virtual cMsgMessage *getMessageArray (const string &name) const throw(cMsgException);
185  virtual cMsgMessage* *getMessagePArray (const string &name) const throw(cMsgException);
186 
187  virtual string getString(const string &name) const throw(cMsgException);
188  virtual vector<string> *getStringVector(const string &name) const throw(cMsgException);
189  virtual string *getStringArray(const string &name) const throw(cMsgException);
190 
191  virtual float getFloat(const string &name) const throw(cMsgException);
192  virtual vector<float> *getFloatVector(const string &name) const throw(cMsgException);
193  virtual float *getFloatArray(const string &name) const throw(cMsgException);
194 
195  virtual double getDouble(const string &name) const throw(cMsgException);
196  virtual vector<double> *getDoubleVector(const string &name) const throw(cMsgException);
197  virtual double *getDoubleArray(const string &name) const throw(cMsgException);
198 
199  virtual int8_t getInt8 (const string &name) const throw(cMsgException);
200  virtual int16_t getInt16 (const string &name) const throw(cMsgException);
201  virtual int32_t getInt32 (const string &name) const throw(cMsgException);
202  virtual int64_t getInt64 (const string &name) const throw(cMsgException);
203 
204  virtual vector<int8_t> *getInt8Vector (const string &name) const throw(cMsgException);
205  virtual vector<int16_t> *getInt16Vector(const string &name) const throw(cMsgException);
206  virtual vector<int32_t> *getInt32Vector(const string &name) const throw(cMsgException);
207  virtual vector<int64_t> *getInt64Vector(const string &name) const throw(cMsgException);
208 
209  virtual int8_t *getInt8Array (const string &name) const throw(cMsgException);
210  virtual int16_t *getInt16Array (const string &name) const throw(cMsgException);
211  virtual int32_t *getInt32Array (const string &name) const throw(cMsgException);
212  virtual int64_t *getInt64Array (const string &name) const throw(cMsgException);
213 
214 
215  virtual uint8_t getUint8 (const string &name) const throw(cMsgException);
216  virtual uint16_t getUint16(const string &name) const throw(cMsgException);
217  virtual uint32_t getUint32(const string &name) const throw(cMsgException);
218  virtual uint64_t getUint64(const string &name) const throw(cMsgException);
219 
220  virtual vector<uint8_t> *getUint8Vector (const string &name) const throw(cMsgException);
221  virtual vector<uint16_t> *getUint16Vector(const string &name) const throw(cMsgException);
222  virtual vector<uint32_t> *getUint32Vector(const string &name) const throw(cMsgException);
223  virtual vector<uint64_t> *getUint64Vector(const string &name) const throw(cMsgException);
224 
225  virtual uint8_t *getUint8Array (const string &name) const throw(cMsgException);
226  virtual uint16_t *getUint16Array (const string &name) const throw(cMsgException);
227  virtual uint32_t *getUint32Array (const string &name) const throw(cMsgException);
228  virtual uint64_t *getUint64Array (const string &name) const throw(cMsgException);
229 
230 
231  //
232  // Methods to add items to a payload
233  //
234 
235  virtual void add(const string &name, const char *src, int size, int endian);
236  virtual void add(const string &name, const char **srcs, int number,
237  const int sizes[], const int endians[]);
238 
239  virtual void add(const string &name, const string &s);
240  virtual void add(const string &name, const string *s);
241  virtual void add(const string &name, const char **strs, int len);
242  virtual void add(const string &name, const string *strs, int len);
243  virtual void add(const string &name, const vector<string> &strs);
244  virtual void add(const string &name, const vector<string> *strs);
245 
246  virtual void add(const string &name, const cMsgMessage &msg);
247  virtual void add(const string &name, const cMsgMessage *msg);
248  virtual void add(const string &name, const cMsgMessage *msg, int len);
249  virtual void add(const string &name, const cMsgMessage* *msg, int len);
250  virtual void add(const string &name, const vector<cMsgMessage> &msgVec);
251  virtual void add(const string &name, const vector<cMsgMessage> *msgVec);
252  virtual void add(const string &name, const vector<cMsgMessage*> &msgPVec);
253  virtual void add(const string &name, const vector<cMsgMessage*> *msgPVec);
254 
255  virtual void add(const string &name, float val);
256  virtual void add(const string &name, double val);
257  virtual void add(const string &name, const float *vals, int len);
258  virtual void add(const string &name, const double *vals, int len);
259  virtual void add(const string &name, const vector<float> &vals);
260  virtual void add(const string &name, const vector<float> *vals);
261  virtual void add(const string &name, const vector<double> &vals);
262  virtual void add(const string &name, const vector<double> *vals);
263 
264  virtual void add(const string &name, int8_t val);
265  virtual void add(const string &name, int16_t val);
266  virtual void add(const string &name, int32_t val);
267  virtual void add(const string &name, int64_t val);
268 
269  virtual void add(const string &name, uint8_t val);
270  virtual void add(const string &name, uint16_t val);
271  virtual void add(const string &name, uint32_t val);
272  virtual void add(const string &name, uint64_t val);
273 
274  virtual void add(const string &name, const int8_t *vals, int len);
275  virtual void add(const string &name, const int16_t *vals, int len);
276  virtual void add(const string &name, const int32_t *vals, int len);
277  virtual void add(const string &name, const int64_t *vals, int len);
278 
279  virtual void add(const string &name, const uint8_t *vals, int len);
280  virtual void add(const string &name, const uint16_t *vals, int len);
281  virtual void add(const string &name, const uint32_t *vals, int len);
282  virtual void add(const string &name, const uint64_t *vals, int len);
283 
284  virtual void add(const string &name, const vector<int8_t> &vals);
285  virtual void add(const string &name, const vector<int8_t> *vals);
286  virtual void add(const string &name, const vector<int16_t> &vals);
287  virtual void add(const string &name, const vector<int16_t> *vals);
288  virtual void add(const string &name, const vector<int32_t> &vals);
289  virtual void add(const string &name, const vector<int32_t> *vals);
290  virtual void add(const string &name, const vector<int64_t> &vals);
291  virtual void add(const string &name, const vector<int64_t> *vals);
292 
293  virtual void add(const string &name, const vector<uint8_t> &vals);
294  virtual void add(const string &name, const vector<uint8_t> *vals);
295  virtual void add(const string &name, const vector<uint16_t> &vals);
296  virtual void add(const string &name, const vector<uint16_t> *vals);
297  virtual void add(const string &name, const vector<uint32_t> &vals);
298  virtual void add(const string &name, const vector<uint32_t> *vals);
299  virtual void add(const string &name, const vector<uint64_t> &vals);
300  virtual void add(const string &name, const vector<uint64_t> *vals);
301 
302 
303 public:
304  void *myMsgPointer;
305 };
306 
307 
308 //-----------------------------------------------------------------------------
309 //-----------------------------------------------------------------------------
310 
311 
316 
317 public:
318  virtual void callback(cMsgMessage *msg, void *userObject) = 0;
319  virtual ~cMsgCallback() {};
320 };
321 
322 
323 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
325 
326 
331 
332 public:
334  virtual ~cMsgSubscriptionConfig(void);
335 
336  virtual int getMaxCueSize(void) const;
337  virtual void setMaxCueSize(int size);
338  virtual int getSkipSize(void) const;
339  virtual void setSkipSize(int size);
340  virtual bool getMaySkip(void) const;
341  virtual void setMaySkip(bool maySkip);
342  virtual bool getMustSerialize(void) const;
343  virtual void setMustSerialize(bool mustSerialize);
344  virtual int getMaxThreads(void) const;
345  virtual void setMaxThreads(int max);
346  virtual int getMessagesPerThread(void) const;
347  virtual void setMessagesPerThread(int mpt);
348  virtual size_t getStackSize(void) const;
349  virtual void setStackSize(size_t size);
350 
351 public: // cMsg class must pass this to underlying C routines
352  cMsgSubscribeConfig *config;
353 };
354 
355 
356 //-----------------------------------------------------------------------------
357 //-----------------------------------------------------------------------------
358 
359 
363 class cMsg {
364 
365 public:
366  cMsg(const string &UDL, const string &name, const string &descr);
367  virtual ~cMsg(void);
368  virtual void connect() throw(cMsgException);
369  virtual void disconnect(void) throw(cMsgException);
370  virtual void send(cMsgMessage &msg) throw(cMsgException);
371  virtual void send(cMsgMessage *msg) throw(cMsgException);
372  virtual int syncSend(cMsgMessage &msg, const struct timespec *timeout = NULL) throw(cMsgException);
373  virtual int syncSend(cMsgMessage *msg, const struct timespec *timeout = NULL) throw(cMsgException);
374  virtual void *subscribe(const string &subject, const string &type, cMsgCallback *cb, void *userArg,
375  const cMsgSubscriptionConfig *cfg = NULL) throw(cMsgException);
376  virtual void *subscribe(const string &subject, const string &type, cMsgCallback &cb, void *userArg,
377  const cMsgSubscriptionConfig *cfg = NULL) throw(cMsgException);
378  virtual void unsubscribe(void *handle) throw(cMsgException);
379  virtual void subscriptionPause(void *handle) throw(cMsgException);
380  virtual void subscriptionResume(void *handle) throw(cMsgException);
381  virtual void subscriptionQueueClear(void *handle) throw(cMsgException);
382  virtual int subscriptionQueueCount(void *handle) throw(cMsgException);
383  virtual bool subscriptionQueueIsFull(void *handle) throw(cMsgException);
384  virtual int subscriptionMessagesTotal(void *handle) throw(cMsgException);
385  virtual cMsgMessage *sendAndGet(cMsgMessage &sendMsg, const struct timespec *timeout = NULL)
386  throw(cMsgException);
387  virtual cMsgMessage *sendAndGet(cMsgMessage *sendMsg, const struct timespec *timeout = NULL)
388  throw(cMsgException);
389  virtual cMsgMessage *subscribeAndGet(const string &subject, const string &type, const struct timespec *timeout = NULL)
390  throw(cMsgException);
391  virtual void flush(const struct timespec *timeout = NULL) throw(cMsgException);
392  virtual void start(void) throw(cMsgException);
393  virtual void stop(void) throw(cMsgException);
394  virtual void setUDL(const string &udl) throw(cMsgException);
395  virtual string getUDL(void) const;
396  virtual string getCurrentUDL(void) const throw(cMsgException);
397  virtual string getName(void) const;
398  virtual string getDescription(void) const;
399  virtual bool isConnected(void) const throw(cMsgException);
400  virtual bool isReceiving(void) const throw(cMsgException);
401  virtual void setShutdownHandler(cMsgShutdownHandler *handler, void* userArg) throw(cMsgException);
402  virtual void shutdownClients(const string &client, int flag) throw(cMsgException);
403  virtual void shutdownServers(const string &server, int flag) throw(cMsgException);
404  virtual cMsgMessage *monitor(const string &monString) throw(cMsgException);
405  virtual void setMonitoringString(const string &monString) throw(cMsgException);
406 
407 
408 private:
409  void *myDomainId;
410  string myUDL;
411  string myName;
412  string myDescr;
413  bool initialized;
414 };
415 
416 
417 //-----------------------------------------------------------------------------
418 //-----------------------------------------------------------------------------
419 // private templates that should not be in doxygen doc
420 #include <cMsgPrivate.hxx>
421 
422 
423 } // namespace cMsg
424 
425 #endif /* _cMsg_hxx */
426 
Class for wrapping cMsg message.
Definition: cMsg.hxx:79
Wraps most cMsg C calls, provides main functionality.
Definition: cMsg.hxx:363
Interface defines callback method.
Definition: cMsg.hxx:315
virtual ~cMsgCallback()
Definition: cMsg.hxx:319
int returnCode
Return code.
Definition: cMsg.hxx:68
cMsgSubscribeConfig * config
Pointer to subscription config struct.
Definition: cMsg.hxx:352
Exception includes description and return code.
Definition: cMsg.hxx:53
string descr
Description.
Definition: cMsg.hxx:67
Manages subscriptions configurations.
Definition: cMsg.hxx:330