grapes / som / ChunkTrading / chunk_delivery.c @ cb201402
History | View | Annotate | Download (1.01 KB)
1 | 7818ee58 | luca | /*
|
---|---|---|---|
2 | * Copyright (c) 2009 Alessandro Russo.
|
||
3 | *
|
||
4 | * This is free software;
|
||
5 | * see GPL.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 | 026a7e5d | Luca Abeni | #include "msg_types.h" |
15 | 7818ee58 | luca | |
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 | c11a78e6 | Luca | int sendChunk(const struct nodeID *to, struct chunk *c){ |
31 | 7818ee58 | luca | 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 | 026a7e5d | Luca Abeni | buff[0] = MSG_TYPE_CHUNK;
|
42 | b576198c | Luca Abeni | send_to_peer(localID, to, buff, buff_len + 1);
|
43 | 7818ee58 | luca | free(buff); |
44 | |||
45 | return (EXIT_SUCCESS);
|
||
46 | } |
||
47 | |||
48 | int chunkInit(struct nodeID *myID) |
||
49 | { |
||
50 | localID = myID; |
||
51 | return 1; |
||
52 | } |