libdas2
das2 core C utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
io.h
Go to the documentation of this file.
1 /* Copyright (C) 2004-2017 Jeremy Faden <jeremy-faden@uiowa.edu>
2  * Chris Piker <chris-piker@uiowa.edu>
3  *
4  * This file is part of libdas2, the Core Das2 C Library.
5  *
6  * Libdas2 is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 2.1 as published
8  * by the Free Software Foundation.
9  *
10  * Libdas2 is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 2.1 along with libdas2; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
22 #ifndef _das_io_h_
23 #define _das_io_h_
24 
25 #include <stdio.h>
26 #include <zlib.h>
27 #include <das2/stream.h>
28 #include <das2/oob.h>
29 #include <das2/processor.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /* max number of stream processor objects */
36 #define DAS2_MAX_PROCESSORS 10
37 
38 #define DASIO_NAME_SZ 128
39 
48 typedef struct das_io_struct {
49  char rw; /* w' for write, 'r' for read */
50  bool compressed; /* 1 if stream is compressed or should be compressed */
51 
52  int mode; /* STREAM_MODE_STRING, STREAM_MODE_FILE,
53  * STREAM_MODE_SOCKET, STREAM_MODE_SSL */
54  char sName[DASIO_NAME_SZ]; /* A human readable name for data source or sink */
55 
56  long int offset; /* current offset for file reads */
57 
58  /* Socket I/O */
59  int nSockFd; /* Socket file descriptor */
60 
61  /* SSL I/O */
62  void* pSsl; /* OpenSSL connection */
63 
64  /* File I/O */
65  FILE *file; /* input/output file (File I/O) */
66 
67  /* Buffer IO */
68  char *sBuffer; /* buffer for string input/output */
69  int nLength; /* length of buffer pointed to by sbuffer */
70 
71  /* Compressed I/O */
72  z_stream *zstrm; /* z_stream for inflate/deflate operations */
73  Byte *inbuf; /* input buffer */
74  Byte *outbuf; /* output buffer */
75  int zerr; /* error code for last stream operation */
76  int eof; /* set if end of input file */
77 
78  /* data object processor's with callbacks (Input / Output) */
79  StreamHandler* pProcs[DAS2_MAX_PROCESSORS+1];
80  bool bSentHeader;
81 
82  /* Sub Object Writing (output) */
83  DasBuf* pDb; /* Sub-Object serializing buffer */
84 
85  int logLevel; /* to-stream logging level. (output) */
86 
87  int taskSize; /* progress indicator max value (output) */
88  long tmLastProgMsg; /* Time the last progress message was emitted (output)*/
89 
90  OobComment cmt; /* Hold buffers for comments and logs */
91 } DasIO;
92 
116 DasIO* new_DasIO_cfile(const char* sProg, FILE* file, const char* mode);
117 
135 DasIO* new_DasIO_cmd(const char* sProg, const char* sCmd);
136 
137 
138 
154 DasIO* new_DasIO_file(const char* sProg, const char* sFile, const char* mode);
155 
171 DasIO* new_DasIO_socket(const char* sProg, int nSockFd, const char* mode);
172 
173 DasIO* new_DasIO_str(const char* sProg, char* sbuf, size_t len, const char* mode);
174 
205 DasIO* new_DasIO_ssl(const char* sProg, void* pSsl, const char* mode);
206 
216 void del_DasIO(DasIO* pThis);
217 
218 
236 int DasIO_addProcessor(DasIO* pThis, StreamHandler* pProc);
237 
238 
252 int DasIO_readAll(DasIO* pThis);
253 
254 
264 
273 
285 
291 
299 
300 #define LOGLVL_FINEST 0
301 #define LOGLVL_FINER 300
302 #define LOGLVL_FINE 400
303 #define LOGLVL_CONFIG 500
304 #define LOGLVL_INFO 600
305 #define LOGLVL_WARNING 700
306 #define LOGLVL_ERROR 800
307 
314 DasErrCode DasIO_sendLog(DasIO* pThis, int level, char * msg, ... );
315 
335 void DasIO_setLogLvl(DasIO* pThis, int minLevel);
336 
342 int DasIO_getLogLvl(const DasIO* pThis);
343 
350 const char* LogLvl_string(int logLevel);
351 
352 
353 /* Identifies the size of task for producing a stream, in arbitrary units.
354  *
355  * This number is used to generate a relative progress position, and also as
356  * a weight if several Das2 Streams (implicitly of similar type) are combined.
357  * See setTaskProgress().
358  *
359  * @memberof DasIO
360  */
361 DasErrCode DasIO_setTaskSize(DasIO* pThis, int size);
362 
375 DasErrCode DasIO_setTaskProgress( DasIO* pThis, int progress );
376 
404  DasIO* pThis, StreamDesc* pSd, const char* type, char* msg
405 );
406 
414 void DasIO_close(DasIO* pThis);
415 
428 int DasIO_serverExcept(DasIO* pThis, const char* fmt, ...);
429 
446 int DasIO_queryExcept(DasIO* pThis, const char* fmt, ...);
447 
448 
465 int DasIO_closeNoData(DasIO* pThis, const char* fmt, ...);
466 
476 int DasIO_printf(DasIO* pThis, const char* format, ...);
477 
481 size_t DasIO_write(DasIO* pThis, const char* data, int length);
482 
486 int DasIO_read(DasIO* pThis, DasBuf* pBuf, size_t nBytes);
487 
492 int DasIO_getc(DasIO* pThis);
493 
494 #ifdef __cplusplus
495 }
496 #endif
497 
498 #endif /* _das_io_h_ */
DasErrCode DasIO_writeStreamDesc(DasIO *pThis, StreamDesc *pSd)
Writes the data describing the stream to the output channel (e.g.
const char * LogLvl_string(int logLevel)
Returns a string identifying the log level.
DasIO * new_DasIO_socket(const char *sProg, int nSockFd, const char *mode)
Create a new DasIO object from a socket.
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: util.h:117
void del_DasIO(DasIO *pThis)
Free resources associated with a DasIO structure Typically you don&#39;t need to do this as heap memory i...
Defines the &quot;Out of Band&quot; objects in a stream.
Holds information for a single packet type in a Das2 stream.
Definition: packet.h:138
size_t DasIO_write(DasIO *pThis, const char *data, int length)
Anolog of fwrite (Low-level API)
DasIO * new_DasIO_file(const char *sProg, const char *sFile, const char *mode)
Create a new DasIO object from a disk file.
int DasIO_getLogLvl(const DasIO *pThis)
Get logging verbosity level.
DasErrCode DasIO_writeException(DasIO *pThis, OobExcept *pSe)
Output an exception structure.
int DasIO_read(DasIO *pThis, DasBuf *pBuf, size_t nBytes)
Analog of fread (Low-level API)
Little buffer class to handle accumulating string data.
Definition: buffer.h:49
void DasIO_throwException(DasIO *pThis, StreamDesc *pSd, const char *type, char *msg)
Set the logging level.
DasIO * new_DasIO_cmd(const char *sProg, const char *sCmd)
Create a new DasIO object from a shell command.
Objects representing a Das2 Stream as a whole.
int DasIO_readAll(DasIO *pThis)
Starts the processing of the stream read from FILE* infile.
int DasIO_addProcessor(DasIO *pThis, StreamHandler *pProc)
Add a packet processor to be invoked during I/O operations.
void DasIO_setLogLvl(DasIO *pThis, int minLevel)
Set the minimum log level that will be transmitted on the stream.
DasErrCode DasIO_setTaskProgress(DasIO *pThis, int progress)
Place rate-limited progress comments on an output stream.
int DasIO_queryExcept(DasIO *pThis, const char *fmt,...)
Throw a bad query exception and close the stream.
Tracks input and output operations for das2 stream headers and data.
Definition: io.h:48
Describes the stream itself, in particular the compression used, current packetDescriptors, etc.
Definition: stream.h:46
DasIO * new_DasIO_ssl(const char *sProg, void *pSsl, const char *mode)
Create a new DasIO object using an encripted connection.
DasErrCode DasIO_writePktData(DasIO *pThis, PktDesc *pPd)
Sends the data packet on to the stream after checking validity.
DasIO * new_DasIO_cfile(const char *sProg, FILE *file, const char *mode)
Create a new DasIO object from a standard C FILE.
int DasIO_closeNoData(DasIO *pThis, const char *fmt,...)
Send a &quot;no data in interval&quot; message and close the stream.
DasErrCode DasIO_writePktDesc(DasIO *pThis, PktDesc *pd)
Writes the data describing a packet type to the output channel (e.g.
Callback processing for das2 stream reads and writes.
describes an exception that can live in a stream.
Definition: oob.h:80
int DasIO_printf(DasIO *pThis, const char *format,...)
Print a string with a format specifier (Low-level API) This works similar to the C printf function...
describes human-consumable messages that exist on the stream.
Definition: oob.h:134
A set of callbacks used for input and output stream processing.
Definition: processor.h:96
int DasIO_serverExcept(DasIO *pThis, const char *fmt,...)
Throw a server exception and close the stream.
void DasIO_close(DasIO *pThis)
Normal stream close with no unusual condiditons Closes the output file descriptor, flushes a gzip buffer, etc.
int DasIO_getc(DasIO *pThis)
Analog of getc (Low-level API)
DasErrCode DasIO_writeComment(DasIO *pThis, OobComment *pSc)
Output a StreamComment Stream comments are generally messages interpreted only by humans and may chan...
DasErrCode DasIO_sendLog(DasIO *pThis, int level, char *msg,...)
Send a log message onto the stream at the given log level.