-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
195 lines (173 loc) · 5.89 KB
/
reader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2011, University Corporation for Atmospheric Research.
* See file COPYRIGHT for copying and redistribution conditions.
*/
#define _XOPEN_SOURCE 500
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "noaaportLog.h"
#include "fifo.h"
#include "reader.h" /* Eat own dog food */
struct reader {
Fifo* fifo; /**< Pointer to FIFO into which to put data
*/
unsigned char* buf; /**< Internal read buffer */
pthread_mutex_t mutex; /**< Object access lock */
unsigned long byteCount; /**< Number of bytes received */
size_t maxSize; /**< Maximum amount to read in a single
* call in bytes */
size_t nbytes; /**< Amount of data in buffer in bytes */
int fd; /**< File-descriptor to read from */
volatile int status; /**< Termination status */
};
/**
* Returns a new reader. The client should call \link \c readerFree() \endlink
* when the reader is no longer needed.
*
* This function is thread-safe.
*
* @retval 0 Success.
* @retval 1 Precondition failure. \c nplStart() called.
* @retval 2 O/S failure. \c nplStart() called.
*/
int readerNew(
const int fd, /**< [in] File-descriptor to read from */
Fifo* const fifo, /**< [in] Pointer to FIFO into which to put
* data */
const size_t maxSize, /**< [in] Maximum amount to read in a single
* call in bytes */
Reader** const reader) /**< [out] Pointer to pointer to address of
* reader */
{
int status = 2; /* default failure */
Reader* r = (Reader*)malloc(sizeof(Reader));
if (NULL == r) {
NPL_SERROR0("Couldn't allocate new reader");
}
else {
unsigned char* buf = (unsigned char*)malloc(maxSize);
if (NULL == buf) {
NPL_SERROR1("Couldn't allocate %lu bytes for buffer",
(unsigned long)maxSize);
}
else {
if ((status = pthread_mutex_init(&r->mutex, NULL)) != 0) {
NPL_ERRNUM0(status, "Couldn't initialize product-maker mutex");
status = 2;
}
else {
r->byteCount = 0;
r->fifo = fifo;
r->fd = fd;
r->maxSize = maxSize;
r->buf = buf;
r->status = 0;
*reader = r;
}
}
}
return status;
}
/**
* Frees a reader.
*/
void readerFree(
Reader* const reader) /**< Pointer to the reader to be freed */
{
if (NULL != reader) {
free(reader->buf);
free(reader);
}
}
/**
* Executes a reader. Returns when end-of-input is encountered or an error
* occurs.
*
* This function is thread-compatible but not thread-safe.
*
* @return NULL
* @see \link readerStatus() \endlink
*/
void* readerStart(
void* const arg) /**< Pointer to the reader to be executed */
{
Reader* const reader = (Reader*)arg;
int status = 0; /* default success */
for (;;) {
unsigned char* buf;
size_t size;
if (fifoWriteReserve(reader->fifo, reader->maxSize, &buf, &size) != 0) {
NPL_ADD1("Couldn't reserve %lu bytes in FIFO", reader->maxSize);
nplLog(LOG_ERR);
status = 2;
break;
}
else {
ssize_t nbytes;
if (size < reader->maxSize)
buf = reader->buf; /* read into internal buffer */
nbytes = read(reader->fd, buf, reader->maxSize);
if (0 == nbytes) {
break; /* end of input */
}
if (-1 == nbytes) {
NPL_SERROR0("read() failure");
nplLog(LOG_ERR);
status = 2;
break;
}
if (buf == reader->buf) {
if (fifoCopy(reader->fifo, buf, nbytes) != 0) {
NPL_ADD1("Couldn't copy %l bytes of data into FIFO",
(long)nbytes);
nplLog(LOG_ERR);
status = 2;
break;
}
}
else {
if (fifoWriteUpdate(reader->fifo, nbytes) != 0) {
NPL_ADD1("Couldn't update FIFO with %l bytes of data",
(long)nbytes);
nplLog(LOG_ERR);
status = 2;
break;
}
else {
(void)pthread_mutex_lock(&reader->mutex);
reader->byteCount += nbytes;
(void)pthread_mutex_unlock(&reader->mutex);
}
}
} /* FIFO space reserved */
} /* I/O loop */
reader->status = status;
return NULL;
}
/**
* Returns statistics since the last time this function was called or \link
* readerStart() \endlink was called.
*/
void readerGetStatistics(
Reader* const reader, /**< [in] Pointer to the reader */
unsigned long* const nbytes) /**< [out] Number of bytes received */
{
(void)pthread_mutex_lock(&reader->mutex);
*nbytes = reader->byteCount;
reader->byteCount = 0;
(void)pthread_mutex_unlock(&reader->mutex);
}
/**
* Returns the termination status of a data-reader.
*
* @retval 0 Success. End-of-file encountered.
* @retval 1 Precondition failure. \c nplStart() called.
* @retval 2 O/S failure. \c nplStart() called.
*/
int readerStatus(
Reader* const reader) /**< [in] Pointer to the reader */
{
return reader->status;
}