Statistics
| Branch: | Revision:

grapes / som / ChunkTrading / chunk_delivery.c @ 7818ee58

History | View | Annotate | Download (1.63 KB)

1
/*
2
 *  Copyright (c) 2009 Alessandro Russo.
3
 *
4
 *  This is free software;
5
 *  see GPL.txt
6
 */
7
 //header to clean
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <stdint.h>
11

    
12
#include "chunk.h"
13
#include "net_helper.h"
14
#include "trade_msg_la.h"
15
#include "trade_msg_ha.h"
16

    
17
static struct nodeID *localID;
18

    
19
static void chunk_print(FILE *f, const struct chunk *c)
20
{
21
  const uint8_t *p;
22

    
23
  fprintf(f, "Chunk %d:\n", c->id);
24
  fprintf(f, "\tTS: %llu\n", c->timestamp);
25
  fprintf(f, "\tPayload size: %d\n", c->size);
26
  fprintf(f, "\tAttributes size: %d\n", c->attributes_size);
27
  p = c->data;
28
  fprintf(f, "\tPayload:\n");
29
  fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
30
  if (c->attributes_size > 0) {
31
    p = c->attributes;
32
    fprintf(f, "\tAttributes:\n");
33
    fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
34
  }
35
}
36

    
37
/**
38
 * Send a Chunk to a target Peer
39
 *
40
 * Send a single Chunk to a given Peer
41
 *
42
 * @param[in] to destination peer
43
 * @param[in] c Chunk to send
44
 * @return 0 on success, <0 on error
45
 */
46

    
47
//TO CHECK AND CORRECT
48
//XXX Send data is in char while our buffer is in uint8
49
int sendChunk(struct nodeID *to, struct chunk *c){
50
    int buff_len;
51
    uint8_t *buff;
52
    int res;
53

    
54
    buff_len  = 20 + c->size + c->attributes_size;
55
    buff = malloc(buff_len + 1);
56
    if (buff == NULL) {
57
        return -1;
58
    }
59
    res = encodeChunk(c, buff + 1, buff_len);
60
    fprintf(stdout, "Encoding chunk %d in %d bytes\n",res, buff_len);
61
    chunk_print(stdout, c);
62
    buff[0] = 12;
63
    send_data(localID, to, buff, buff_len + 1);
64
    free(buff);
65

    
66
    return (EXIT_SUCCESS);
67
}
68

    
69
int chunkInit(struct nodeID *myID)
70
{
71
  localID = myID;
72
  return 1;
73
}
74