Revision 8e0aa9cb

View differences:

external_chunk_transcoding.c
1
#include <string.h>
2
#include <stdint.h>
3

  
4
#include "external_chunk_transcoding.h"
5

  
6
static inline int bit32_encoded_pull(uint8_t *p)
7
{
8
	int tmp;
9
  
10
	memcpy(&tmp, p, CHUNK_TRANSCODING_INT_SIZE);
11
	tmp = ntohl(tmp);
12

  
13
	return tmp;
14
}
15

  
16
static inline void bit32_encoded_push(uint32_t v, uint8_t *p)
17
{
18
	uint32_t tmp;
19
  
20
	tmp = htonl(v);
21
	memcpy(p, &tmp, CHUNK_TRANSCODING_INT_SIZE);
22
}
23

  
24
void print_block(const uint8_t *b, int size) {
25
int i=0;
26
printf("BEGIN OF %d BYTES---\n", size);
27
for(i=0; i<size; i++) {
28
printf("%d ", *(b+i));
29
}
30
printf("END OF %d BYTES---\n", size);
31
}
32

  
33
void chunker_logger(const char *s) {
34
	printf("%s\n", s);
35
}
36

  
37

  
38
void *packExternalChunkToAttributes(ExternalChunk *echunk, size_t attr_size) {
39
	void *attr_block = NULL;
40
	int64_t half_prio;
41
	int64_t prio = 0.0;
42
	static size_t needed_space = 0;
43
	
44
	needed_space = 5*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 2*CHUNK_TRANSCODING_INT_SIZE + 1*CHUNK_TRANSCODING_INT_SIZE*2;
45
	
46
	if(attr_size != needed_space) {
47
		chunker_logger("space mismatch in expected allocation of attrib block size!");
48
		return NULL;
49
	}
50
	
51
	if( (attr_block = malloc(needed_space)) == NULL ) {
52
		chunker_logger("attrib block malloc failed!");
53
		return NULL;
54
	}
55
	
56
	/* copy the content of the external_chunk structure into a proper attributes block */
57
	/* also network-encoding the 4bytes pieces */
58
	bit32_encoded_push(echunk->seq, attr_block);
59
	bit32_encoded_push(echunk->frames_num, attr_block + CHUNK_TRANSCODING_INT_SIZE);
60
	
61
	/* unfold the timeval structure fields */
62
	bit32_encoded_push(echunk->start_time.tv_sec, attr_block + CHUNK_TRANSCODING_INT_SIZE*2);
63
	bit32_encoded_push(echunk->start_time.tv_usec, attr_block + CHUNK_TRANSCODING_INT_SIZE*3);
64
	bit32_encoded_push(echunk->end_time.tv_sec, attr_block + CHUNK_TRANSCODING_INT_SIZE*4);
65
	bit32_encoded_push(echunk->end_time.tv_usec, attr_block + CHUNK_TRANSCODING_INT_SIZE*5);
66
	
67
	bit32_encoded_push(echunk->payload_len, attr_block + CHUNK_TRANSCODING_INT_SIZE*6);
68
	bit32_encoded_push(echunk->len, attr_block + CHUNK_TRANSCODING_INT_SIZE*7);
69
	bit32_encoded_push(echunk->category, attr_block + CHUNK_TRANSCODING_INT_SIZE*8);
70
	/* this is a double, should be 64bits, split it */
71
	prio = (uint64_t)echunk->priority;
72
	half_prio = prio >> 32;
73
	bit32_encoded_push(half_prio, attr_block + CHUNK_TRANSCODING_INT_SIZE*9);
74
	half_prio = prio;
75
	bit32_encoded_push(half_prio, attr_block + CHUNK_TRANSCODING_INT_SIZE*10);
76
	/* ref count is not needed over the wire */
77
	
78
	return attr_block;
79
}
80

  
81
ExternalChunk *grapesChunkToExternalChunk(Chunk *gchunk) {
82
	uint64_t tmp_prio;
83
	ExternalChunk *echunk = (ExternalChunk *)malloc(sizeof(ExternalChunk));
84
	if(!echunk) {
85
		printf("Memory error in chunkToExternalchunk!\n");
86
		return NULL;
87
	}
88
	/* pull out info from the attributes block from the grapes chunk */
89
		echunk->seq = bit32_encoded_pull(gchunk->attributes);
90
		echunk->frames_num = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE);
91
		echunk->start_time.tv_sec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*2);
92
		echunk->start_time.tv_usec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*3);
93
		echunk->end_time.tv_sec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*4);
94
		echunk->end_time.tv_usec = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*5);
95
		echunk->payload_len = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*6);
96
		echunk->len = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*7);
97
		echunk->category = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*8);
98
		tmp_prio = bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*9);
99
		tmp_prio = tmp_prio << 32;
100
		tmp_prio |= bit32_encoded_pull(gchunk->attributes + CHUNK_TRANSCODING_INT_SIZE*10);
101
		echunk->priority = (double)tmp_prio;
102

  
103
		/* pass the payload along */
104
		echunk->data = gchunk->data;
105

  
106
	return echunk;
107
}

Also available in: Unified diff