Statistics
| Branch: | Revision:

grapes / src / Chunkiser / output-stream-dummy.c @ d72492e1

History | View | Annotate | Download (1.65 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 "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
};
26

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

    
32
  res = malloc(sizeof(struct dechunkiser_ctx));
33
  if (res == NULL) {
34
    return NULL;
35
  }
36
  res->type = chunk_id;
37
  res->last_id = -1;
38
  cfg_tags = config_parse(config);
39
  if (cfg_tags) {
40
    const char *pt;
41

    
42
    pt = config_value_str(cfg_tags, "type");
43
    if (pt) {
44
      if (!strcmp(pt, "stats")) {
45
        res->type = stats;
46
      }
47
    }
48
  }
49
  free(cfg_tags);
50

    
51
  return res;
52
}
53

    
54
static void dummy_write(struct dechunkiser_ctx *o, int id, uint8_t *data, int size)
55
{
56
  switch (o->type) {
57
    case chunk_id:
58
      printf("Chunk %d: size %d\n", id, size);
59
      break;
60
    case stats:
61
      if (o->last_id >= 0) {
62
        int i;
63

    
64
        for (i = 1; i < id - o->last_id; i++) {
65
          o->lost++;
66
          printf("Lost chunk %d\n", o->last_id + i);
67
        }
68
        printf("# Lost chunk ratio: %f\n", (double)o->lost / (double)(id - o->first_id));
69
      } else {
70
        o->first_id = id;
71
      }
72
      break;
73
    default:
74
      fprintf(stderr, "Internal error!\n");
75
      exit(-1);
76
  }
77
  o->last_id = id;
78
}
79

    
80
static void dummy_close(struct dechunkiser_ctx *s)
81
{
82
  free(s);
83
}
84

    
85
struct dechunkiser_iface out_dummy = {
86
  .open = dummy_open,
87
  .write = dummy_write,
88
  .close = dummy_close,
89
};