2 * $Id: msgqueue_manager.c 53 2011-05-09 16:55:39Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
7 * Copyright (c) 2011, Lucian Corlaciu, GSoC
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
39 #include "msgqueue_manager.h"
40 #include "metadata_manager.h"
41 #include "index_manager.h"
44 #include "fcgi_stdio.h"
45 #define logstream FCGI_stdout
47 #define FCGI_stdout stdout
48 #define FCGI_stderr stderr
49 #define logstream stderr
52 msgqueue_param_t * gene_msgqueue( bool stateless, cachemodel_param_t *cachemodel)
54 msgqueue_param_t *msgqueue;
56 msgqueue = (msgqueue_param_t *)malloc( sizeof(msgqueue_param_t));
58 msgqueue->first = NULL;
59 msgqueue->last = NULL;
61 msgqueue->stateless = stateless;
62 msgqueue->cachemodel = cachemodel;
67 void delete_msgqueue( msgqueue_param_t **msgqueue)
69 message_param_t *ptr, *next;
74 ptr = (*msgqueue)->first;
81 if( (*msgqueue)->stateless && (*msgqueue)->cachemodel)
82 delete_cachemodel( &((*msgqueue)->cachemodel));
87 void print_msgqueue( msgqueue_param_t *msgqueue)
90 char *message_class[] = { "Precinct", "Ext-Prec", "TileHead", "non", "Tile", "Ext-Tile", "Main", "non", "Meta"};
95 fprintf( logstream, "message queue:\n");
96 ptr = msgqueue->first;
99 fprintf( logstream, "\t class_id: %lld %s\n", ptr->class_id, message_class[ptr->class_id]);
100 fprintf( logstream, "\t in_class_id: %lld\n", ptr->in_class_id );
101 fprintf( logstream, "\t csn: %lld\n", ptr->csn );
102 fprintf( logstream, "\t bin_offset: %#llx\n", ptr->bin_offset );
103 fprintf( logstream, "\t length: %#llx\n", ptr->length );
104 fprintf( logstream, "\t aux: %lld\n", ptr->aux );
105 fprintf( logstream, "\t last_byte: %d\n", ptr->last_byte );
107 print_placeholder( ptr->phld);
109 fprintf( logstream, "\t res_offset: %#llx\n", ptr->res_offset );
110 fprintf( logstream, "\n");
116 void enqueue_message( message_param_t *msg, msgqueue_param_t *msgqueue);
118 void enqueue_mainheader( msgqueue_param_t *msgqueue)
120 cachemodel_param_t *cachemodel;
121 target_param_t *target;
122 index_param_t *codeidx;
123 message_param_t *msg;
125 cachemodel = msgqueue->cachemodel;
126 target = cachemodel->target;
127 codeidx = target->codeidx;
129 msg = (message_param_t *)malloc( sizeof(message_param_t));
131 msg->last_byte = true;
132 msg->in_class_id = 0;
133 msg->class_id = MAINHEADER_MSG;
134 msg->csn = target->csn;
136 msg->length = codeidx->mhead_length;
137 msg->aux = 0; // non exist
138 msg->res_offset = codeidx->offset;
142 enqueue_message( msg, msgqueue);
144 cachemodel->mhead_model = true;
147 void enqueue_tileheader( int tile_id, msgqueue_param_t *msgqueue)
149 cachemodel_param_t *cachemodel;
150 target_param_t *target;
151 index_param_t *codeidx;
152 message_param_t *msg;
154 cachemodel = msgqueue->cachemodel;
155 target = cachemodel->target;
156 codeidx = target->codeidx;
158 if( !cachemodel->th_model[ tile_id]){
159 msg = (message_param_t *)malloc( sizeof(message_param_t));
160 msg->last_byte = true;
161 msg->in_class_id = tile_id;
162 msg->class_id = TILE_HEADER_MSG;
163 msg->csn = target->csn;
165 msg->length = codeidx->tileheader[tile_id]->tlen;
166 msg->aux = 0; // non exist
167 msg->res_offset = codeidx->offset + get_elemOff( codeidx->tilepart, 0, tile_id); // Changed from Lucian's
171 enqueue_message( msg, msgqueue);
172 cachemodel->th_model[ tile_id] = true;
176 void enqueue_tile( int tile_id, int level, msgqueue_param_t *msgqueue)
178 cachemodel_param_t *cachemodel;
179 target_param_t *target;
181 Byte8_t numOftparts; // num of tile parts par tile
183 index_param_t *codeidx;
184 faixbox_param_t *tilepart;
185 message_param_t *msg;
186 Byte8_t binOffset, binLength;
189 cachemodel = msgqueue->cachemodel;
190 target = cachemodel->target;
191 codeidx = target->codeidx;
192 tilepart = codeidx->tilepart;
194 numOftparts = get_nmax( tilepart);
195 numOftiles = get_m( tilepart);
197 if( tile_id < 0 || numOftiles <= tile_id){
198 fprintf( FCGI_stderr, "Error, Invalid tile-id %d\n", tile_id);
202 tp_model = &cachemodel->tp_model[ tile_id*numOftparts];
205 for( i=0; i<numOftparts-level; i++){
206 binLength = get_elemLen( tilepart, i, tile_id);
209 msg = (message_param_t *)malloc( sizeof(message_param_t));
211 msg->last_byte = i==numOftparts-1? true : false;
212 msg->in_class_id = tile_id;
214 msg->class_id = TILE_MSG;
216 msg->class_id = EXT_TILE_MSG;
218 msg->csn = target->csn;
219 msg->bin_offset = binOffset;
220 msg->length = binLength;
221 msg->aux = numOftparts-i;
222 msg->res_offset = codeidx->offset+get_elemOff( tilepart, i, tile_id)/*-1*/;
226 enqueue_message( msg, msgqueue);
230 binOffset += binLength;
234 void enqueue_precinct( int seq_id, int tile_id, int comp_id, msgqueue_param_t *msgqueue)
236 cachemodel_param_t *cachemodel;
237 index_param_t *codeidx;
238 faixbox_param_t *precpacket;
239 message_param_t *msg;
242 cachemodel = msgqueue->cachemodel;
243 codeidx = cachemodel->target->codeidx;
244 precpacket = codeidx->precpacket[ comp_id];
246 nmax = get_nmax(precpacket);
248 if( !cachemodel->pp_model[comp_id][ tile_id*nmax+seq_id]){
249 msg = (message_param_t *)malloc( sizeof(message_param_t));
250 msg->last_byte = true;
251 msg->in_class_id = comp_precinct_id( tile_id, comp_id, seq_id, codeidx->SIZ.Csiz, codeidx->SIZ.XTnum * codeidx->SIZ.YTnum);
252 msg->class_id = PRECINCT_MSG;
253 msg->csn = cachemodel->target->csn;
255 msg->length = get_elemLen( precpacket, seq_id, tile_id);
257 msg->res_offset = codeidx->offset+get_elemOff( precpacket, seq_id, tile_id);
261 enqueue_message( msg, msgqueue);
262 cachemodel->pp_model[comp_id][ tile_id*nmax+seq_id] = true;
266 Byte8_t comp_precinct_id( int t, int c, int s, int num_components, int num_tiles)
268 return t + (c + s * num_components ) * num_tiles;
271 void enqueue_box( int meta_id, boxlist_param_t *boxlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset);
272 void enqueue_phld( int meta_id, placeholderlist_param_t *phldlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset);
273 void enqueue_boxcontents( int meta_id, boxcontents_param_t *boxcontents, msgqueue_param_t *msgqueue, Byte8_t *binOffset);
275 void enqueue_metadata( int meta_id, msgqueue_param_t *msgqueue)
277 metadatalist_param_t *metadatalist;
278 metadata_param_t *metadata;
281 metadatalist = msgqueue->cachemodel->target->codeidx->metadatalist;
282 metadata = search_metadata( meta_id, metadatalist);
285 fprintf( FCGI_stderr, "Error: metadata-bin %d not found\n", meta_id);
290 if( metadata->boxlist)
291 enqueue_box( meta_id, metadata->boxlist, msgqueue, &binOffset);
293 if( metadata->placeholderlist)
294 enqueue_phld( meta_id, metadata->placeholderlist, msgqueue, &binOffset);
296 if( metadata->boxcontents)
297 enqueue_boxcontents( meta_id, metadata->boxcontents, msgqueue, &binOffset);
299 msgqueue->last->last_byte = true;
302 message_param_t * gene_metamsg( int meta_id, Byte8_t binoffset, Byte8_t length, Byte8_t res_offset, placeholder_param_t *phld, Byte8_t csn);
304 void enqueue_box( int meta_id, boxlist_param_t *boxlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset)
307 message_param_t *msg;
309 box = boxlist->first;
311 msg = gene_metamsg( meta_id, *binOffset, box->length, box->offset, NULL, msgqueue->cachemodel->target->csn);
312 enqueue_message( msg, msgqueue);
314 *binOffset += box->length;
319 void enqueue_phld( int meta_id, placeholderlist_param_t *phldlist, msgqueue_param_t *msgqueue, Byte8_t *binOffset)
321 placeholder_param_t *phld;
322 message_param_t *msg;
324 phld = phldlist->first;
326 msg = gene_metamsg( meta_id, *binOffset, phld->LBox, 0, phld, msgqueue->cachemodel->target->csn);
327 enqueue_message( msg, msgqueue);
329 *binOffset += phld->LBox;
334 void enqueue_boxcontents( int meta_id, boxcontents_param_t *boxcontents, msgqueue_param_t *msgqueue, Byte8_t *binOffset)
336 message_param_t *msg;
338 msg = gene_metamsg( meta_id, *binOffset, boxcontents->length, boxcontents->offset, NULL, msgqueue->cachemodel->target->csn);
339 enqueue_message( msg, msgqueue);
341 *binOffset += boxcontents->length;
344 message_param_t * gene_metamsg( int meta_id, Byte8_t binOffset, Byte8_t length, Byte8_t res_offset, placeholder_param_t *phld, Byte8_t csn)
346 message_param_t *msg;
348 msg = (message_param_t *)malloc( sizeof(message_param_t));
350 msg->last_byte = false;
351 msg->in_class_id = meta_id;
352 msg->class_id = METADATA_MSG;
354 msg->bin_offset = binOffset;
355 msg->length = length;
356 msg->aux = 0; // non exist
357 msg->res_offset = res_offset;
364 void enqueue_message( message_param_t *msg, msgqueue_param_t *msgqueue)
367 msgqueue->last->next = msg;
369 msgqueue->first = msg;
371 msgqueue->last = msg;
375 void emit_bin_id_vbas( Byte_t bb, Byte_t c, Byte8_t in_class_id);
376 void emit_vbas( Byte8_t code);
377 void emit_body( message_param_t *msg, int fd);
378 void emit_placeholder( placeholder_param_t *phld);
380 void emit_stream_from_msgqueue( msgqueue_param_t *msgqueue)
382 message_param_t *msg;
383 Byte8_t class_id, csn;
389 msg = msgqueue->first;
393 if( msg->csn == csn){
394 if( msg->class_id == class_id)
398 class_id = msg->class_id;
403 class_id = msg->class_id;
407 c = msg->last_byte ? 1 : 0;
409 emit_bin_id_vbas( bb, c, msg->in_class_id);
412 emit_vbas( class_id);
416 emit_vbas( msg->bin_offset);
417 emit_vbas (msg->length);
419 if( msg->class_id%2) // Aux is present only if the id is odd
420 emit_vbas( msg->aux);
423 emit_placeholder( msg->phld);
425 emit_body( msg, msgqueue->cachemodel->target->fd);
431 void emit_vbas_with_bytelen( Byte8_t code, int bytelength);
432 void print_binarycode( Byte8_t n, int segmentlen);
434 void emit_bin_id_vbas( Byte_t bb, Byte_t c, Byte8_t in_class_id)
439 // A.2.3 In-class identifiers
440 // 7k-3bits, where k is the number of bytes in the VBAS
442 tmp = in_class_id >> 4;
448 in_class_id |= (((bb & 3) << 5) | (c & 1) << 4) << ((bytelength-1)*7);
450 emit_vbas_with_bytelen( in_class_id, bytelength);
453 void emit_vbas( Byte8_t code)
463 emit_vbas_with_bytelen( code, bytelength);
466 void emit_vbas_with_bytelen( Byte8_t code, int bytelength)
473 seg = ( code >> (n*7)) & 0x7f;
476 fputc(( Byte4_t)seg, FCGI_stdout);
481 void emit_body( message_param_t *msg, int fd)
485 if( lseek( fd, msg->res_offset, SEEK_SET)==-1){
486 fprintf( FCGI_stderr, "Error: fseek in emit_body()\n");
490 data = (Byte_t *)malloc( msg->length);
491 if( read( fd, data, msg->length) != msg->length){
493 fprintf( FCGI_stderr, "Error: fread in emit_body()\n");
497 if( fwrite( data, msg->length, 1, FCGI_stdout) < 1){
499 fprintf( FCGI_stderr, "Error: fwrite in emit_body()\n");
505 void emit_bigendian_bytes( Byte8_t code, int bytelength);
507 void emit_placeholder( placeholder_param_t *phld)
509 emit_bigendian_bytes( phld->LBox, 4);
510 if( fwrite( phld->TBox, 4, 1, FCGI_stdout) < 1){
511 fprintf( FCGI_stderr, "Error: fwrite in emit_placeholder()\n");
514 emit_bigendian_bytes( phld->Flags, 4);
515 emit_bigendian_bytes( phld->OrigID, 8);
517 if( fwrite( phld->OrigBH, phld->OrigBHlen, 1, FCGI_stdout) < 1){
518 fprintf( FCGI_stderr, "Error: fwrite in emit_placeholder()\n");
523 void emit_bigendian_bytes( Byte8_t code, int bytelength)
530 seg = ( code >> (n*8)) & 0xff;
531 fputc(( Byte4_t)seg, FCGI_stdout);
536 void print_binarycode( Byte8_t n, int segmentlen)
542 buf[i++] = n%2 ? '1' : '0';
545 for( j=segmentlen-1; j>=i; j--)
548 for( j=i-1, k=0; j>=0; j--, k++){
550 if( !((k+1)%segmentlen))
556 Byte_t * parse_bin_id_vbas( Byte_t *streamptr, Byte_t *bb, Byte_t *c, Byte8_t *in_class_id);
557 Byte_t * parse_vbas( Byte_t *streamptr, Byte8_t *elem);
559 void parse_JPIPstream( Byte_t *JPIPstream, Byte8_t streamlen, Byte8_t offset, msgqueue_param_t *msgqueue)
561 Byte_t *ptr; // stream pointer
562 message_param_t *msg;
564 Byte8_t class_id, csn;
566 class_id = -1; // dummy
569 while( ptr-JPIPstream < streamlen){
570 msg = (message_param_t *)malloc( sizeof(message_param_t));
572 ptr = parse_bin_id_vbas( ptr, &bb, &c, &msg->in_class_id);
574 msg->last_byte = c == 1 ? true : false;
577 ptr = parse_vbas( ptr, &class_id);
579 msg->class_id = class_id;
582 ptr = parse_vbas( ptr, &csn);
585 ptr = parse_vbas( ptr, &msg->bin_offset);
586 ptr = parse_vbas( ptr, &msg->length);
588 if( msg->class_id%2) // Aux is present only if the id is odd
589 ptr = parse_vbas( ptr, &msg->aux);
593 msg->res_offset = ptr-JPIPstream+offset;
598 msgqueue->last->next = msg;
600 msgqueue->first = msg;
601 msgqueue->last = msg;
607 void parse_metadata( metadata_param_t *metadata, message_param_t *msg, Byte_t *stream);
609 void parse_metamsg( msgqueue_param_t *msgqueue, Byte_t *stream, Byte8_t streamlen, metadatalist_param_t *metadatalist)
611 message_param_t *msg;
613 if( metadatalist == NULL)
616 msg = msgqueue->first;
618 if( msg->class_id == METADATA_MSG){
619 metadata_param_t *metadata = gene_metadata( msg->in_class_id, NULL, NULL, NULL);
620 insert_metadata_into_list( metadata, metadatalist);
621 parse_metadata( metadata, msg, stream+msg->res_offset);
627 placeholder_param_t * parse_phld( Byte_t *datastream, Byte8_t metalength);
629 void parse_metadata( metadata_param_t *metadata, message_param_t *msg, Byte_t *datastream)
631 char *boxtype = (char *)(datastream+4);
635 if( strncmp( boxtype, "phld", 4) == 0){
636 if( !metadata->placeholderlist)
637 metadata->placeholderlist = gene_placeholderlist();
639 placeholder_param_t *phld = parse_phld( datastream, msg->length);
641 insert_placeholder_into_list( phld, metadata->placeholderlist);
643 else if( isalpha(boxtype[0]) && isalpha(boxtype[1]) &&
644 (isalnum(boxtype[2])||isblank(boxtype[2])) &&
645 (isalpha(boxtype[3])||isblank(boxtype[3]))){
646 if( !metadata->boxlist)
647 metadata->boxlist = gene_boxlist();
649 box_param_t *box = gene_boxbyOffinStream( datastream, msg->res_offset);
650 insert_box_into_list( box, metadata->boxlist);
653 metadata->boxcontents = gene_boxcontents( msg->res_offset, msg->length);
656 placeholder_param_t * parse_phld( Byte_t *datastream, Byte8_t metalength)
658 placeholder_param_t *phld;
660 phld = (placeholder_param_t *)malloc( sizeof(placeholder_param_t));
662 phld->LBox = big4( datastream);
663 strcpy( phld->TBox, "phld");
664 phld->Flags = big4( datastream+8);
665 phld->OrigID = big8( datastream+12);
666 phld->OrigBHlen = metalength - 20;
667 phld->OrigBH = (Byte_t *)malloc(phld->OrigBHlen);
668 memcpy( phld->OrigBH, datastream+20, phld->OrigBHlen);
674 Byte_t * parse_bin_id_vbas( Byte_t *streamptr, Byte_t *bb, Byte_t *c, Byte8_t *in_class_id)
682 *bb = (code >> 5) & 3;
683 *c = (code >> 4) & 1;
685 *in_class_id = code & 15;
689 *in_class_id = (*in_class_id << 7) | (code & 0x7f);
694 Byte_t * parse_vbas( Byte_t *streamptr, Byte8_t *elem)
703 *elem = (*elem << 7) | (code & 0x7f);
709 void delete_message_in_msgqueue( message_param_t **msg, msgqueue_param_t *msgqueue)
711 message_param_t *ptr;
716 if( *msg == msgqueue->first)
717 msgqueue->first = (*msg)->next;
719 ptr = msgqueue->first;
720 while( ptr->next != *msg){
724 ptr->next = (*msg)->next;
726 if( *msg == msgqueue->last)
727 msgqueue->last = ptr;