Statistics
| Branch: | Revision:

grapes / src / Chunkiser / output-stream-dummy.c @ 176b8de8

History | View | Annotate | Download (1.86 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6

    
7
#include <stdint.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <string.h>
11

    
12
#include "grapes_config.h"
13
#include "dechunkiser_iface.h"
14

    
15
enum output_type {
16
  chunk_id,
17
  stats,
18
};
19

    
20
struct dechunkiser_ctx {
21
  enum output_type type;
22
  int last_id;
23
  int lost;
24
  int first_id;
25
  FILE *f;
26
};
27

    
28
static struct dechunkiser_ctx *dummy_open(const char *fname, const char *config)
29
{
30
  struct dechunkiser_ctx *res;
31
  struct tag *cfg_tags;
32

    
33
  res = malloc(sizeof(struct dechunkiser_ctx));
34
  if (res == NULL) {
35
    return NULL;
36
  }
37
  res->f = stdout;
38
  res->type = chunk_id;
39
  if (fname) {
40
    res->f = fopen(fname, "w");
41
    if (res->f == NULL) {
42
      res->f = stdout;
43
    }
44
  }
45
  res->last_id = -1;
46
  res->lost = 0;
47
  cfg_tags = grapes_config_parse(config);
48
  if (cfg_tags) {
49
    const char *pt;
50

    
51
    pt = grapes_config_value_str(cfg_tags, "type");
52
    if (pt) {
53
      if (!strcmp(pt, "stats")) {
54
        res->type = stats;
55
      }
56
    }
57
  }
58
  free(cfg_tags);
59

    
60
  return res;
61
}
62

    
63
static void dummy_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
64
{
65
  switch (o->type) {
66
    case chunk_id:
67
      fprintf(o->f, "Chunk %d: size %d\n", id, size);
68
      break;
69
    case stats:
70
      if (o->last_id >= 0) {
71
        int i;
72

    
73
        for (i = 1; i < id - o->last_id; i++) {
74
          o->lost++;
75
          fprintf(o->f, "Lost chunk %d\n", o->last_id + i);
76
        }
77
        fprintf(o->f, "# Lost chunk ratio: %f\n", (double)o->lost / (double)(id - o->first_id));
78
      } else {
79
        o->first_id = id;
80
      }
81
      break;
82
    default:
83
      fprintf(stderr, "Internal error!\n");
84
      exit(-1);
85
  }
86
  fflush(o->f);
87
  o->last_id = id;
88
}
89

    
90
static void dummy_close(struct dechunkiser_ctx *s)
91
{
92
  free(s);
93
}
94

    
95
struct dechunkiser_iface out_dummy = {
96
  .open = dummy_open,
97
  .write = dummy_write,
98
  .close = dummy_close,
99
};