2 * $Id: target_manager.c 44 2011-02-15 12:32:29Z 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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
39 #include "target_manager.h"
42 #include "fcgi_stdio.h"
43 #define logstream FCGI_stdout
45 #define FCGI_stdout stdout
46 #define FCGI_stderr stderr
47 #define logstream stderr
51 targetlist_param_t * gene_targetlist()
53 targetlist_param_t *targetlist;
55 targetlist = (targetlist_param_t *)malloc( sizeof(targetlist_param_t));
57 targetlist->first = NULL;
58 targetlist->last = NULL;
65 * open jp2 format image file
67 * @param[in] filename file name (.jp2)
68 * @return file descriptor
70 int open_jp2file( char filename[]);
72 target_param_t * gene_target( targetlist_param_t *targetlist, char *targetpath)
74 target_param_t *target;
76 index_param_t *jp2idx;
77 static int last_csn = 0;
79 if( targetpath[0]=='\0'){
80 fprintf( FCGI_stderr, "Error: exception, no targetpath in gene_target()\n");
84 if((fd = open_jp2file( targetpath)) == -1){
85 fprintf( FCGI_stdout, "Status: 404\r\n");
89 if( !(jp2idx = parse_jp2file( fd))){
90 fprintf( FCGI_stdout, "Status: 501\r\n");
94 target = (target_param_t *)malloc( sizeof(target_param_t));
95 snprintf( target->tid, MAX_LENOFTID, "%x-%x", (unsigned int)time(NULL), (unsigned int)rand());
96 target->filename = strdup( targetpath);
98 target->csn = last_csn++;
99 target->codeidx = jp2idx;
100 target->num_of_use = 0;
101 target->jppstream = true;
102 target->jptstream = isJPTfeasible( *jp2idx);
105 if( targetlist->first) // there are one or more entries
106 targetlist->last->next = target;
108 targetlist->first = target;
109 targetlist->last = target;
112 fprintf( logstream, "local log: target %s generated\n", targetpath);
118 void refer_target( target_param_t *reftarget, target_param_t **ptr)
121 reftarget->num_of_use++;
124 void unrefer_target( target_param_t *target)
126 target->num_of_use--;
129 void delete_target( target_param_t **target)
131 close( (*target)->fd);
133 if( (*target)->codeidx)
134 delete_index ( &(*target)->codeidx);
137 fprintf( logstream, "local log: target: %s deleted\n", (*target)->filename);
140 free( (*target)->filename);
145 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
149 if( *target == targetlist->first)
150 targetlist->first = (*target)->next;
152 ptr = targetlist->first;
153 while( ptr->next != *target){
157 ptr->next = (*target)->next;
159 if( *target == targetlist->last)
160 targetlist->last = ptr;
162 delete_target( target);
165 void delete_targetlist(targetlist_param_t **targetlist)
167 target_param_t *targetPtr, *targetNext;
169 targetPtr = (*targetlist)->first;
170 while( targetPtr != NULL){
171 targetNext=targetPtr->next;
172 delete_target( &targetPtr);
173 targetPtr=targetNext;
178 void print_target( target_param_t *target)
180 fprintf( logstream, "target:\n");
181 fprintf( logstream, "\t tid=%s\n", target->tid);
182 fprintf( logstream, "\t csn=%d\n", target->csn);
183 fprintf( logstream, "\t target=%s\n\n", target->filename);
186 void print_alltarget( targetlist_param_t *targetlist)
190 ptr = targetlist->first;
197 target_param_t * search_target( char targetname[], targetlist_param_t *targetlist)
199 target_param_t *foundtarget;
201 foundtarget = targetlist->first;
203 while( foundtarget != NULL){
205 if( strcmp( targetname, foundtarget->filename) == 0)
208 foundtarget = foundtarget->next;
213 target_param_t * search_targetBytid( char tid[], targetlist_param_t *targetlist)
215 target_param_t *foundtarget;
217 foundtarget = targetlist->first;
219 while( foundtarget != NULL){
221 if( strcmp( tid, foundtarget->tid) == 0)
224 foundtarget = foundtarget->next;
230 int open_jp2file( char filename[])
235 if( (fd = open( filename, O_RDONLY)) == -1){
236 fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filename);
239 // Check resource is a JP family file.
240 if( lseek( fd, 0, SEEK_SET)==-1){
242 fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filename);
246 data = (char *)malloc( 12); // size of header
247 if( read( fd, data, 12) != 12){
250 fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filename);
254 if( *data || *(data + 1) || *(data + 2) ||
255 *(data + 3) != 12 || strncmp (data + 4, "jP \r\n\x87\n", 8)){
258 fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filename);