Statistics
| Branch: | Revision:

grapes / src / Tests / peer.c @ 0c6efcd5

History | View | Annotate | Download (1.02 KB)

1
/*
2
 * peer.c
3
 *
4
 *  Created on: Mar 29, 2010
5
 *      Author: Marco Biazzini
6
 */
7

    
8
#include <stdio.h>
9

    
10
#include <sys/time.h>
11
#include <stdlib.h>
12
#include <stdint.h>
13
#include <string.h>
14
#include "net_helper.h"
15
#include "chunkidset.h"
16
#include "peer.h"
17

    
18
struct peer *peerCreate (struct nodeID *id, int *size)
19
{
20
        struct peer *res;
21

    
22
        *size = sizeof(struct peer);
23
        res = (struct peer *)malloc(*size);
24
        res->id = nodeid_dup(id);
25
        gettimeofday(&(res->creation_timestamp),NULL);
26
        return res;
27
}
28

    
29
void peerDelete (struct peer *p) {
30
        nodeid_free(p->id);
31
        free(p);
32
}
33

    
34
// serialize nodeID and cb_size only
35
int peerDump(const struct peer *p, uint8_t **buf) {
36

    
37
        uint8_t id[256];
38
        int id_size = nodeid_dump(id, p->id, 256);
39
        int res = id_size+sizeof(int);
40
        *buf = realloc(*buf, res);
41
        memcpy(*buf,id,id_size);
42

    
43
        return res;
44

    
45
}
46

    
47

    
48
struct peer* peerUndump(const uint8_t *buf, int *size) {
49

    
50
        struct peer *p;
51
        p = malloc(sizeof(struct peer));
52
        p->id = nodeid_undump(buf,size);
53
        *size = *size + sizeof(int);
54

    
55
        gettimeofday(&(p->creation_timestamp),NULL);
56

    
57
        return p;
58

    
59
}
60