Revision f6e93568 src/Tests/chunkiser_test.c

View differences:

src/Tests/chunkiser_test.c
10 10
#include <stdint.h>
11 11
#include <stdlib.h>
12 12
#include <sys/types.h>
13
#include <sys/time.h>
13 14

  
14 15
#include "chunk.h"
15 16
#include "chunkiser.h"
......
22 23
static int udp_port;
23 24
static int out_udp_port;
24 25

  
26
static int cycle;
27
static struct timeval tnext;
28

  
25 29
static void help(const char *name)
26 30
{
27 31
  fprintf(stderr, "Usage: %s [options] <input> <output>\n", name);
......
136 140
  return optind - 1;
137 141
}
138 142

  
139
static void in_wait(const int *fd)
143
void tout_init(struct timeval *tv)
140 144
{
141
  int my_fd[10];
145
  struct timeval tnow;
146

  
147
  gettimeofday(&tnow, NULL);
148
  if(timercmp(&tnow, &tnext, <)) {
149
    timersub(&tnext, &tnow, tv);
150
  } else {
151
    *tv = (struct timeval){0, 0};
152
  }
153
}
154

  
155
static void in_wait(const int *fd, uint64_t ts)
156
{
157
  int my_fd[10], *pfd;
142 158
  int i = 0;
159
  struct timeval tv, *ptv, tadd;
160
  static struct timeval tfirst;
161
  static uint64_t tsfirst;
143 162
  
144
  if (fd == NULL) {
145
    return;
163
  if (ts == 0) {
164
    ptv = NULL;
165
  } else {
166
    if (tfirst.tv_sec == 0) {
167
      gettimeofday(&tfirst, NULL);
168
      tsfirst = ts;
169
    }
170
printf("Sleep %llu\n", ts - tsfirst + cycle);
171
    tadd.tv_sec = (ts - tsfirst + cycle) / 1000000;
172
    tadd.tv_usec = (ts - tsfirst + cycle) % 1000000;
173
    timeradd(&tfirst, &tadd, &tnext);
174
    tout_init(&tv);
175
    ptv = &tv;
146 176
  }
147
  while(fd[i] != -1) {
148
    my_fd[i] = fd[i];
149
    i++;
177
  if (fd) {
178
    while(fd[i] != -1) {
179
      my_fd[i] = fd[i];
180
      i++;
181
    }
182
    pfd = my_fd;
183
  } else {
184
    pfd = NULL;
150 185
  }
151 186
  my_fd[i] = -1;
152 187

  
153
  wait4data(NULL, NULL, my_fd);
188
  wait4data(NULL, ptv, pfd);
154 189
}
155 190

  
156 191
int main(int argc, char *argv[])
......
159 194
  struct input_stream *input;
160 195
  struct output_stream *output;
161 196
  const int *in_fds;
197
  unsigned long long int ts;
162 198

  
163 199
  if (argc < 3) {
164 200
    help(argv[0]);
......
176 212
    in_fds = input_get_fds(input);
177 213
  } else {
178 214
    in_fds = NULL;
215
    cycle = period;
179 216
  }
180 217
  output = out_stream_init(argv[2], out_opts);
181 218
  if (output == NULL) {
......
184 221
    return -1;
185 222
  }
186 223

  
224
  ts = 1;
187 225
  done = 0; id = 0;
188 226
  while(!done) {
189 227
    int res;
190 228
    struct chunk c;
191 229

  
192
    in_wait(in_fds);
230
    in_wait(in_fds, ts);
193 231
    c.id = id;
194 232
    res = chunkise(input, &c);
195 233
    if (res > 0) {
......
198 236
    } else if (res < 0) {
199 237
      done = 1;
200 238
    }
239
    ts = c.timestamp;
201 240
    free(c.data);
202 241
  }
203 242
  input_stream_close(input);

Also available in: Unified diff