Statistics
| Branch: | Revision:

grapes / som / ChunkTrading / chunk_delivery.c @ 8ab58ec7

History | View | Annotate | Download (1.02 KB)

1
/*
2
 *  Copyright (c) 2009 Alessandro Russo.
3
 *
4
 *  This is free software;
5
 *  see lgpl-2.1.txt
6
 */
7
#include <stdlib.h>
8
#include <stdint.h>
9

    
10
#include "chunk.h"
11
#include "net_helper.h"
12
#include "trade_msg_la.h"
13
#include "trade_msg_ha.h"
14
#include "msg_types.h"
15

    
16
static struct nodeID *localID;
17

    
18
/**
19
 * Send a Chunk to a target Peer
20
 *
21
 * Send a single Chunk to a given Peer
22
 *
23
 * @param[in] to destination peer
24
 * @param[in] c Chunk to send
25
 * @return 0 on success, <0 on error
26
 */
27

    
28
//TO CHECK AND CORRECT
29
//XXX Send data is in char while our buffer is in uint8
30
int sendChunk(const struct nodeID *to, struct chunk *c){
31
    int buff_len;
32
    uint8_t *buff;
33
    int res;
34

    
35
    buff_len  = 20 + c->size + c->attributes_size;
36
    buff = malloc(buff_len + 1);
37
    if (buff == NULL) {
38
        return -1;
39
    }
40
    res = encodeChunk(c, buff + 1, buff_len);
41
    buff[0] = MSG_TYPE_CHUNK;
42
    send_to_peer(localID, to, buff, buff_len + 1);
43
    free(buff);
44

    
45
    return (EXIT_SUCCESS);
46
}
47

    
48
int chunkInit(struct nodeID *myID)
49
{
50
  localID = myID;
51
  return 1;
52
}
53