Statistics
| Branch: | Revision:

grapes / src / ChunkTrading / chunk_delivery.c @ f217ce38

History | View | Annotate | Download (1.46 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
int parseChunkMsg(const uint8_t *buff, int buff_len, struct chunk *c, uint16_t *transid)
18
{
19
  int res;
20

    
21
  if (c == NULL) {
22
    return -1;
23
  }
24

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

    
30
  *transid = int16_rcpy(buff);
31

    
32
  return 1;
33
}
34

    
35
/**
36
 * Send a Chunk to a target Peer
37
 *
38
 * Send a single Chunk to a given Peer
39
 *
40
 * @param[in] to destination peer
41
 * @param[in] c Chunk to send
42
 * @return 0 on success, <0 on error
43
 */
44
//TO CHECK AND CORRECT
45
//XXX Send data is in char while our buffer is in uint8
46
int sendChunk(const struct nodeID * localID, const struct nodeID *to, const struct chunk *c, uint16_t transid)
47
{
48
  int buff_len;
49
  uint8_t *buff;
50
  int res;
51

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

    
63
    return -2;
64
  }
65
  send_to_peer(localID, to, buff, buff_len + 1);
66
  free(buff);
67

    
68
  return EXIT_SUCCESS;
69
}
70

    
71
int chunkDeliveryInit(struct nodeID *myID)
72
{
73
  return 1;
74
}
75