Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ c6575994

History | View | Annotate | Download (4.44 KB)

1
/*
2
 *  Copyright (c) 2009 Alessandro Russo.
3
 *
4
 *  This is free software;
5
 *  see GPL.txt
6
 *
7
 * Chunk Signaling API - Higher Abstraction
8
 *
9
 * The Chunk Signaling HA provides a set of primitives for chunks signaling negotiation with other peers, in order to collect information for the effective chunk exchange with other peers. <br>
10
 * This is a part of the Data Exchange Protocol which provides high level abstraction for chunks' negotiations, like requesting and proposing chunks.
11
 *
12
 */
13

    
14
#include <stdint.h>
15
#include <stdlib.h>
16
#include <stdio.h>
17
#include <sys/time.h>
18
#include <errno.h>
19
#include <assert.h>
20
#include "peer.h"
21
#include "peerset.h"
22
#include "chunkidset.h"
23
#include "trade_sig_la.h"
24
#include "chunk_signaling.h"
25
#include "msg_types.h"
26
#include "net_helper.h"
27

    
28
#include "dbg.h"
29

    
30
static struct nodeID *localID;
31
static struct peerset *pset;
32

    
33

    
34
 /**
35
 * Dispatcher for signaling messages.
36
 *
37
 * This method decodes the signaling messages, retrieving the set of chunk and the signaling
38
 * message, invoking the corresponding method.
39
 *
40
 * @param[in] buff buffer which contains the signaling message
41
 * @param[in] buff_len length of the buffer
42
 * @param[in] msgtype type of message in the buffer
43
 * @param[in] max_deliver deliver at most this number of Chunks
44
 * @param[in] arg parameters associated to the signaling message
45
 * @return 0 on success, <0 on error
46
 */
47

    
48
int sigParseData(uint8_t *buff, int buff_len) {
49
    struct chunkID_set *c_set;
50
    void *meta;
51
    int meta_len;
52
    struct sig_nal *signal;
53
    int sig;
54
    dprintf("\tDecoding signaling message...");
55
    c_set = decodeChunkSignaling(&meta, &meta_len, buff+1, buff_len-1);
56
    dprintf("SIG_HEADER: len: %d, of which meta: %d\n", buff_len, meta_len);
57
    if (!c_set) {
58
      fprintf(stdout, "ERROR decoding signaling message\n");
59
      return -1;
60
    }
61
    dprintf("done\n");    
62
    signal = (struct sig_nal *) meta;       
63
    sig = (int) (signal->type);    
64
    dprintf("\tSignaling Type %d\n", sig);
65
    //MaxDelivery  and Trans_Id to be defined
66
    switch (sig) {
67
        case MSG_SIG_BMOFF:
68
        {//BufferMap Received
69
          int dummy;
70
          struct nodeID *ownerid = nodeid_undump(&(signal->third_peer),&dummy);
71
          struct peer *owner = peerset_get_peer(pset, ownerid);
72
          if (!owner) {
73
            printf("warning: received bmap of unknown peer: %s! Adding it to neighbourhood!\n", node_addr(ownerid));
74
            peerset_add_peer(pset,ownerid);
75
            owner = peerset_get_peer(pset,ownerid);
76
          }
77
          if (owner) {        //now we have it almost sure
78
            chunkID_set_union(owner->bmap,c_set);        //don't send it back
79
            gettimeofday(&owner->bmap_timestamp, NULL);
80
          }
81
        }
82
        default:
83
        {
84
            return -1;
85
        }
86
    }
87
    return 1;
88
}
89

    
90
/**
91
 * Send a BufferMap to a Peer.
92
 *
93
 * Send (our own or some other peer's) BufferMap to a third Peer.
94
 *
95
 * @param[in] to PeerID.
96
 * @param[in] owner Owner of the BufferMap to send.
97
 * @param[in] bmap the BufferMap to send.
98
 * @param[in] bmap_len length of the buffer map
99
 * @param[in] trans_id transaction number associated with this send
100
 * @return 0 on success, <0 on error
101
 */
102
int sendBufferMap(const struct nodeID *to_id, const struct nodeID *owner_id, ChunkIDSet *bmap, int bmap_len, int trans_id) {    
103
    int buff_len, id_len, msg_len, ret;
104
    uint8_t *buff;
105
    //msgtype = MSG_TYPE_SIGNALLING;
106
    struct sig_nal sigmex;
107
    sigmex.type = MSG_SIG_BMOFF;
108
    sigmex.trans_id = trans_id;
109
    id_len = nodeid_dump(&sigmex.third_peer, owner_id);
110
    buff_len = 1 + bmap_len * 4 + 12 + sizeof(sigmex)-1 + id_len; // this should be enough
111
    dprintf("SIG_HEADER: Type %d Tx %d PP %s\n",sigmex.type,sigmex.trans_id,node_addr(owner_id));
112
    buff = malloc(buff_len);
113
    if (!buff) {
114
      return -1;
115
    }
116
    buff[0] = MSG_TYPE_SIGNALLING;
117
    msg_len = 1 + encodeChunkSignaling(bmap, &sigmex, sizeof(sigmex)-1 + id_len, buff+1, buff_len-1);
118
    dprintf("SIG_HEADER: len: %d, of which meta: %d\n", msg_len, sizeof(sigmex)-1 + id_len);
119
    if (msg_len < 0) {
120
      fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
121
      ret = -1;
122
    } else {
123
      send_to_peer(localID, to_id, buff, msg_len);
124
    }
125
    ret = 1;
126
    free(buff);
127
    return ret;
128
}
129

    
130
int sendMyBufferMap(const struct nodeID *to_id, ChunkIDSet *bmap, int bmap_len, int trans_id)
131
{
132
  return sendBufferMap(to_id, localID, bmap, bmap_len, trans_id);
133
}
134

    
135
int sigInit(struct nodeID *myID, struct peerset *ps)
136
{
137
  localID = myID;
138
  pset = ps;
139
  return 1;
140
}