cis_config
PsiInterface.hpp
1 extern "C" {
2 #include <sys/stat.h> /* For mode constants */
3 #include <sys/msg.h>
4 #include <sys/sem.h>
5 #include <sys/shm.h>
6 #include <stdlib.h>
7 #include "PsiInterface.h"
8 };
9 #include <string>
10 #include <regex>
11 
12 
21 class PsiInput {
22  psiInput_t _pi;
23 public:
24 
30  PsiInput(const char *name) : _pi(psiInput(name)) {}
31 
41  int recv(char *data, int len) {
42  return psi_recv(_pi, data, len);
43  }
44 
54  int recv_nolimit(char **data, int len) {
55  return psi_recv_nolimit(_pi, data, len);
56  }
57 };
58 
59 
68 class PsiOutput {
69  psiOutput_t _pi;
70 public:
71 
77  PsiOutput(const char *name) : _pi(psiOutput(name)) {}
78 
87  int send(char *data, int len) {
88  return psi_send(_pi, data, len);
89  }
90 
98  int send_nolimit(char *data, int len) {
99  return psi_send_nolimit(_pi, data, len);
100  }
101 };
102 
103 
112 class PsiRpc {
113  psiRpc_t _pi;
114 public:
115 
125  PsiRpc(const char *outName, char *outFormat,
126  const char *inName, char *inFormat) :
127  _pi(psiRpc(outName, outFormat, inName, inFormat)) {}
128 
134  return _pi;
135  };
136 
145  int send(int nargs, ...) {
146  if (nargs != _pi._output._nfmt) {
147  error("PsiRpc(%s).send: %d args provided, but format expects %d.\n",
148  _pi._output._name, nargs, _pi._output._nfmt);
149  return -1;
150  }
151  va_list va;
152  va_start(va, nargs);
153  int ret = vrpcSend(_pi, va);
154  va_end(va);
155  return ret;
156  }
157 
168  int recv(int nargs, ...) {
169  if (nargs != _pi._input._nfmt) {
170  error("PsiRpc(%s).recv: %d args provided, but format expects %d.\n",
171  _pi._input._name, nargs, _pi._input._nfmt);
172  return -1;
173  }
174  va_list va;
175  va_start(va, nargs);
176  int ret = vrpcRecv(_pi, va);
177  va_end(va);
178  return ret;
179  }
180 };
181 
182 
191 class PsiRpcServer : public PsiRpc {
192 public:
193 
203  PsiRpcServer(const char *name, char *inFormat, char *outFormat) :
204  PsiRpc(name, outFormat, name, inFormat) {}
205 
206 };
207 
208 
217 class PsiRpcClient : public PsiRpc {
218 public:
219 
229  PsiRpcClient(const char *name, char *outFormat, char *inFormat) :
230  PsiRpc(name, outFormat, name, inFormat) {
231  }
232 
245  int call(int nargs, ...) {
246  psiRpc_t _cpi = pi();
247  int nfmt_tot = _cpi._output._nfmt + _cpi._input._nfmt;
248  if (nargs != nfmt_tot) {
249  error("PsiRpcClient(%s).call: %d args provided, but format expects %d.\n",
250  _cpi._output._name, nargs, nfmt_tot);
251  return -1;
252  }
253  va_list va;
254  va_start(va, nargs);
255  int ret = vrpcCall(_cpi, va);
256  va_end(va);
257  return ret;
258  }
259 
260 };
261 
262 
273 public:
274 
282  PsiAsciiFileOutput(const char *name, int dst_type = 1) :
283  _pi(psiAsciiFileOutput(name, dst_type)) {}
288  ~PsiAsciiFileOutput() { cleanup_pafo(&_pi); }
289 
295  int send_eof() { return af_send_eof(_pi); }
302  int send_line(char *line) { return af_send_line(_pi, line); }
303 
304 };
305 
306 
317 public:
318 
326  PsiAsciiFileInput(const char *name, int src_type = 1) :
327  _pi(psiAsciiFileInput(name, src_type)) {}
332  ~PsiAsciiFileInput() { cleanup_pafi(&_pi); }
333 
343  int recv_line(char *line, size_t n) { return af_recv_line(_pi, line, n); }
344 
345 };
346 
347 
358 public:
359 
369  PsiAsciiTableOutput(const char *name, char *format_str, int dst_type = 1) :
370  _pi(psiAsciiTableOutput(name, format_str, dst_type)) {}
375  ~PsiAsciiTableOutput() { cleanup_pato(&_pi); }
376 
384  int send(char *data, int len) { return at_psi_send(_pi, data, len); }
385 
391  int send_eof() { return at_send_eof(_pi); }
392 
400  int send_row(int nargs, ...) {
401  int nfmt;
402  if (_pi._type == 0)
403  nfmt = count_formats(_pi._table.format_str);
404  else
405  nfmt = _pi._psi._nfmt;
406  if (nargs != nfmt) {
407  error("PsiAsciiTableOutput(%s).send_row: %d args provided, but format expects %d.\n",
408  _pi._name, nargs, nfmt);
409  return -1;
410  }
411  int ret;
412  va_list ap;
413  va_start(ap, nargs);
414  ret = vsend_row(_pi, ap);
415  va_end(ap);
416  return ret;
417  }
418 
428  int send_array(int nargs, int nrows, ...) {
429  int nfmt;
430  if (_pi._type == 0)
431  nfmt = count_formats(_pi._table.format_str);
432  else
433  nfmt = _pi._psi._nfmt;
434  if (nargs != nfmt) {
435  error("PsiAsciiTableOutput(%s).send_array: %d args provided, but format expects %d.\n",
436  _pi._name, nargs, nfmt);
437  return -1;
438  }
439  int ret;
440  va_list ap;
441  va_start(ap, nrows);
442  ret = vsend_array(_pi, nrows, ap);
443  va_end(ap);
444  return ret;
445  }
446 
447 };
448 
449 
460 public:
461 
472  PsiAsciiTableInput(const char *name, int src_type = 1) :
473  _pi(psiAsciiTableInput(name, src_type)) {
474  // For input, remove precision from floats to avoid confusing vsscanf
475  std::regex e("%(?:\\d+\\$)?[+-]?(?:[ 0]|'.{1})?-?\\d*(?:\\.\\d+)?(?:[lhjztL])*([eEfFgG])");
476  std::string s(_pi._psi._fmt, strlen(_pi._psi._fmt));
477  std::string result;
478  result = std::regex_replace(s, e, "%$1");
479  strcpy(_pi._psi._fmt, result.c_str());
480  }
485  ~PsiAsciiTableInput() { cleanup_pati(&_pi); }
486 
496  int recv(char **data, int len) { return at_psi_recv(_pi, data, len); }
497 
507  int recv_row(int nargs, ...) {
508  int nfmt;
509  if (_pi._type == 0)
510  nfmt = count_formats(_pi._table.format_str);
511  else
512  nfmt = _pi._psi._nfmt;
513  if (nargs != nfmt) {
514  error("PsiAsciiTableInput(%s).recv_row: %d args provided, but format expects %d.\n",
515  _pi._name, nargs, nfmt);
516  return -1;
517  }
518  int ret;
519  va_list ap;
520  va_start(ap, nargs);
521  ret = vrecv_row(_pi, ap);
522  va_end(ap);
523  return ret;
524  }
525 
534  int recv_array(int nargs, ...) {
535  int nfmt;
536  if (_pi._type == 0)
537  nfmt = count_formats(_pi._table.format_str);
538  else
539  nfmt = _pi._psi._nfmt;
540  if (nargs != nfmt) {
541  error("PsiAsciiTableInput(%s).recv_array: %d args provided, but format expects %d.\n",
542  _pi._name, nargs, nfmt);
543  return -1;
544  }
545  int ret;
546  va_list ap;
547  va_start(ap, nargs);
548  ret = vrecv_array(_pi, ap);
549  va_end(ap);
550  return ret;
551  }
552 
553 };
int _nfmt
Number of fields expected from format string.
Definition: PsiInterface.h:236
C++ interface to psiRpc_t client-side functionality.
Definition: PsiInterface.hpp:217
int _type
0 if _name is a local table, 1 if it is a message queue.
Definition: PsiInterface.h:1319
int recv(char **data, int len)
Recv a nolimit message from a table input queue. See at_psi_recv in PsiInterface.h for details...
Definition: PsiInterface.hpp:496
psiOutput_t _psi
Associated output handler for queues.
Definition: PsiInterface.h:1310
const char * _name
Path to local table or name of message queue.
Definition: PsiInterface.h:1318
C++ interface to psiAsciiTableOutput_t functionality.
Definition: PsiInterface.hpp:356
int send_eof()
Send a nolimit EOF message to a table output queue. See at_send_eof in PsiInterface for details...
Definition: PsiInterface.hpp:391
C++ interface to psiAsciiFileInput_t functionality.
Definition: PsiInterface.hpp:315
char format_str[LINE_SIZE_MAX]
Format string for rows.
Definition: AsciiTable.h:89
int recv_line(char *line, size_t n)
Receive a single line from an associated file or queue. See af_recv_line in PsiInterface.h for details.
Definition: PsiInterface.hpp:343
int send_eof()
Send EOF message to output file, closing it. See af_send_eof in PsiInterface.h for details...
Definition: PsiInterface.hpp:295
psiInput_t _psi
Associated input handler for queues.
Definition: PsiInterface.h:1321
C++ interface to psiOutput_t functionality.
Definition: PsiInterface.hpp:68
int send_nolimit(char *data, int len)
Send a message larger than PSI_MSG_MAX to the output queue. See psi_send_nolimit in PsiInterface...
Definition: PsiInterface.hpp:98
int send(char *data, int len)
Send a message smaller than PSI_MSG_MAX to the output queue. If the message is larger than PSI_MSG_MA...
Definition: PsiInterface.hpp:87
int call(int nargs,...)
Send request to an RPC server from the client and wait for a response. See rpcCall in PsiInterface...
Definition: PsiInterface.hpp:245
C++ interface to psiInput_t functionality.
Definition: PsiInterface.hpp:21
asciiTable_t _table
Associated output handler for local tables.
Definition: PsiInterface.h:1309
const char * _name
Queue name.
Definition: PsiInterface.h:223
PsiRpcClient(const char *name, char *outFormat, char *inFormat)
Constructor for PsiRpcClient.
Definition: PsiInterface.hpp:229
int send_array(int nargs, int nrows,...)
Format and send table columns to the table file/queue. See at_send_array in PsiInterface.h for details.
Definition: PsiInterface.hpp:428
PsiAsciiTableOutput(const char *name, char *format_str, int dst_type=1)
Constructor for PsiAsciiTableOutput.
Definition: PsiInterface.hpp:369
int _nfmt
Number of fields expected from format string.
Definition: PsiInterface.h:225
PsiRpcServer(const char *name, char *inFormat, char *outFormat)
Constructor for PsiRpcServer.
Definition: PsiInterface.hpp:203
Structure for handling output to an ASCII table.
Definition: PsiInterface.h:1305
~PsiAsciiFileInput()
Destructor for PsiAsciiFileInput. See cleanup_pafi in PsiInterface.h for details. ...
Definition: PsiInterface.hpp:332
int recv(char *data, int len)
Receive a message shorter than PSI_MSG_MAX from the input queue. See psi_recv in PsiInterface.h for additional details.
Definition: PsiInterface.hpp:41
Remote Procedure Call (RPC) structure. Contains information required to coordinate sending/receiving ...
Definition: PsiInterface.h:826
~PsiAsciiTableInput()
Destructor for PsiAsciiTableInput. See cleanup_pati in PsiInterface.h for details.
Definition: PsiInterface.hpp:485
int recv_row(int nargs,...)
Recv and parse a row from the table file/queue. See at_recv_row in PsiInterface.h for details...
Definition: PsiInterface.hpp:507
PsiAsciiFileOutput(const char *name, int dst_type=1)
Constructor for PsiAsciiFileOutput.
Definition: PsiInterface.hpp:282
C++ interface to psiAsciiFileOutput_t functionality.
Definition: PsiInterface.hpp:271
const char * _name
Path to local table or name of message queue.
Definition: PsiInterface.h:1307
~PsiAsciiTableOutput()
Destructor for PsiAsciiTableOutput. See cleanup_pato in PsiInterface.h for details.
Definition: PsiInterface.hpp:375
int send_row(int nargs,...)
Format and send a row to the table file/queue. See at_send_row in PsiInterface.h for details...
Definition: PsiInterface.hpp:400
~PsiAsciiFileOutput()
Destructor for PsiAsciiFileOutput. See cleanup_pafo in PsiInterface.h for details.
Definition: PsiInterface.hpp:288
Output queue structure. Contains information on an output queue.
Definition: PsiInterface.h:232
psiInput_t _input
Input queue structure.
Definition: PsiInterface.h:827
C++ interface to psiRpc_t functionality.
Definition: PsiInterface.hpp:112
int recv_nolimit(char **data, int len)
Receive a message larger than PSI_MSG_MAX from the input queue. See psi_recv_nolimit in PsiInterface...
Definition: PsiInterface.hpp:54
PsiOutput(const char *name)
Constructor for PsiOutput.
Definition: PsiInterface.hpp:77
char * _fmt
Format for interpreting queue messages.
Definition: PsiInterface.h:224
int send_line(char *line)
Send a single line to a file or queue. See af_send_line in PsiInterface.h for details.
Definition: PsiInterface.hpp:302
PsiInput(const char *name)
Constructor for PsiInput.
Definition: PsiInterface.hpp:30
PsiAsciiTableInput(const char *name, int src_type=1)
Constructor for PsiAsciiTableInput. Due to issues with the C++ version of vsscanf, flags and precision indicators for floating point format specifiers (e.g. e, f), must be removed so that table input can be properly parsed.
Definition: PsiInterface.hpp:472
int recv(int nargs,...)
Receive and parse a message from an RPC input queue. See rpcRecv from PsiInterface.h for details.
Definition: PsiInterface.hpp:168
Input queue structure. Contains information on an input queue.
Definition: PsiInterface.h:221
Structure for handling input from an ASCII table.
Definition: PsiInterface.h:1316
psiRpc_t pi()
Return the psiRpc_t structure.
Definition: PsiInterface.hpp:133
int _type
0 if _name is a local table, 1 if it is a message queue.
Definition: PsiInterface.h:1308
const char * _name
Queue name.
Definition: PsiInterface.h:234
int send(int nargs,...)
Format and send a message to an RPC output queue. See rpcSend from PsiInterface.h for details...
Definition: PsiInterface.hpp:145
Structure of information for output to a file line by line.
Definition: PsiInterface.h:1064
C++ interface to psiRpc_t server-side functionality.
Definition: PsiInterface.hpp:191
PsiAsciiFileInput(const char *name, int src_type=1)
Constructor for PsiAsciiFileInput.
Definition: PsiInterface.hpp:326
Structure of information for input from a file line by line.
Definition: PsiInterface.h:1075
asciiTable_t _table
Associated input handler for local tables.
Definition: PsiInterface.h:1320
int send(char *data, int len)
Send a nolimit message to a table output queue. See at_psi_send in PsiInterface.h for details...
Definition: PsiInterface.hpp:384
C++ interface to psiAsciiTableInput_t functionality.
Definition: PsiInterface.hpp:458
int recv_array(int nargs,...)
Recv and parse columns from a table file/queue. See at_recv_array in PsiInterface.h for details.
Definition: PsiInterface.hpp:534
PsiRpc(const char *outName, char *outFormat, const char *inName, char *inFormat)
Constructor for PsiRpc.
Definition: PsiInterface.hpp:125
psiOutput_t _output
Output queue structure.
Definition: PsiInterface.h:828