Revision 7818ee58

View differences:

include/trade_msg_ha.h
1
/** @file trade_msg_ha.h
2
 *
3
 * Chunk Delivery API - Higher Abstraction
4
 *
5
 * The Chunk Delivery HA provides the primitives for effective chunks exchange with other peers. <br>
6
 * This is a part of the Data Exchange Protocol which provides high level abstraction for sending chunks to a target peers.
7
 *
8
 * @image html SOM_Internals.png "Data Exchange Protocol of the SOM module" width=50
9
 */
10
#include "chunk.h"
11
/**
12
  * Send a Chunk to a target Peer
13
  *
14
  * Send a single Chunk to a given Peer
15
  *
16
  * @param[in] to destination peer
17
  * @param[in] c Chunk to send
18
  * @return 0 on success, <0 on error
19
  */
20
int sendChunk(struct nodeID *to, struct chunk *c);
21

  
22
/**
23
  * Init the Chunk trading stuff...
24
  *
25
  *
26
  * @param myID address of this peer
27
  * @return >= 0 on success, <0 on error
28
  */
29
int chunkInit(struct nodeID *myID);
30

  
31

  
32
/** 
33
  * Notification function for a Chunk arrival
34
  */
35
typedef int (*ChunkNotification)(struct peer *from, struct chunk  *c);
36

  
37
/**
38
  * Register a notification for Chunk arrival
39
  * 
40
  * Register a notification function that should be called at every Chunk arrival
41
  *
42
  * @param[in] som Handle to the enclosing SOM instance
43
  * @param[in] p notify a send of chunk from Peer p, or from any peer if NULL
44
  * @param[in] fn pointer to the notification function.
45
  * @return a handle to the notification or NULL on error @see unregisterSendChunk
46
  */
47
void registerSendChunkNotifier(struct peer *p, ChunkNotification *fn);
som/ChunkTrading/chunk_delivery.c
1
/*
2
 *  Copyright (c) 2009 Alessandro Russo.
3
 *
4
 *  This is free software;
5
 *  see GPL.txt
6
 */
7
 //header to clean
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <stdint.h>
11

  
12
#include "chunk.h"
13
#include "net_helper.h"
14
#include "trade_msg_la.h"
15
#include "trade_msg_ha.h"
16

  
17
static struct nodeID *localID;
18

  
19
static void chunk_print(FILE *f, const struct chunk *c)
20
{
21
  const uint8_t *p;
22

  
23
  fprintf(f, "Chunk %d:\n", c->id);
24
  fprintf(f, "\tTS: %llu\n", c->timestamp);
25
  fprintf(f, "\tPayload size: %d\n", c->size);
26
  fprintf(f, "\tAttributes size: %d\n", c->attributes_size);
27
  p = c->data;
28
  fprintf(f, "\tPayload:\n");
29
  fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
30
  if (c->attributes_size > 0) {
31
    p = c->attributes;
32
    fprintf(f, "\tAttributes:\n");
33
    fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
34
  }
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

  
47
//TO CHECK AND CORRECT
48
//XXX Send data is in char while our buffer is in uint8
49
int sendChunk(struct nodeID *to, struct chunk *c){
50
    int buff_len;
51
    uint8_t *buff;
52
    int res;
53

  
54
    buff_len  = 20 + c->size + c->attributes_size;
55
    buff = malloc(buff_len + 1);
56
    if (buff == NULL) {
57
        return -1;
58
    }
59
    res = encodeChunk(c, buff + 1, buff_len);
60
    fprintf(stdout, "Encoding chunk %d in %d bytes\n",res, buff_len);
61
    chunk_print(stdout, c);
62
    buff[0] = 12;
63
    send_data(localID, to, buff, buff_len + 1);
64
    free(buff);
65

  
66
    return (EXIT_SUCCESS);
67
}
68

  
69
int chunkInit(struct nodeID *myID)
70
{
71
  localID = myID;
72
  return 1;
73
}
74

  

Also available in: Unified diff