Statistics
| Branch: | Revision:

grapes / src / ChunkTrading / chunk_delivery.c @ 729826c9

History | View | Annotate | Download (1.43 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 "int_coding.h"
11
#include "chunk.h"
12
#include "net_helper.h"
13
#include "trade_msg_la.h"
14
#include "trade_msg_ha.h"
15
#include "grapes_msg_types.h"
16

    
17
static struct nodeID *localID;
18

    
19
int parseChunkMsg(const uint8_t *buff, int buff_len, struct chunk *c, uint16_t *transid)
20
{
21
  int res;
22

    
23
  if (c == NULL) {
24
    return -1;
25
  }
26

    
27
  res = decodeChunk(c, buff + sizeof(*transid), buff_len - sizeof(*transid));
28
  if (res < 0) {
29
    return -1;
30
  }
31

    
32
  *transid = int16_rcpy(buff);
33

    
34
  return 1;
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
//TO CHECK AND CORRECT
47
//XXX Send data is in char while our buffer is in uint8
48
int sendChunk(struct nodeID *to, const struct chunk *c, uint16_t transid)
49
{
50
  int buff_len;
51
  uint8_t *buff;
52
  int res;
53

    
54
  buff_len  = CHUNK_HEADER_SIZE + sizeof(transid) + c->size + c->attributes_size;
55
  buff = malloc(buff_len + 1);
56
  if (buff == NULL) {
57
      return -1;
58
  }
59
  buff[0] = MSG_TYPE_CHUNK;
60
  int16_cpy(buff + 1, transid);
61
  res = encodeChunk(c, buff + 1 + sizeof(transid), buff_len);
62
  send_to_peer(localID, to, buff, buff_len + 1);
63
  free(buff);
64

    
65
  return EXIT_SUCCESS;
66
}
67

    
68
int chunkDeliveryInit(struct nodeID *myID)
69
{
70
  localID = myID;
71

    
72
  return 1;
73
}
74