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 *targetname)
74 target_param_t *target;
76 index_param_t *jp2idx;
77 static int last_csn = 0;
79 if( targetname[0]=='\0'){
80 fprintf( FCGI_stderr, "Error: exception, no targetname in gene_target()\n");
84 if((fd = open_jp2file( targetname)) == -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 strcpy( target->filename, targetname);
98 target->csn = last_csn++;
99 target->codeidx = jp2idx;
100 target->num_of_use = 0;
104 if( targetlist->first) // there are one or more entries
105 targetlist->last->next = target;
107 targetlist->first = target;
108 targetlist->last = target;
111 fprintf( logstream, "local log: target %s generated\n", targetname);
117 void refer_target( target_param_t *reftarget, target_param_t **ptr)
120 reftarget->num_of_use++;
123 void unrefer_target( target_param_t *target)
125 target->num_of_use--;
128 void delete_target( target_param_t **target)
130 close( (*target)->fd);
131 delete_index ( &(*target)->codeidx);
134 fprintf( logstream, "local log: target: %s deleted\n", (*target)->filename);
139 void delete_target_in_list( target_param_t **target, targetlist_param_t *targetlist)
143 if( *target == targetlist->first)
144 targetlist->first = (*target)->next;
146 ptr = targetlist->first;
147 while( ptr->next != *target){
151 ptr->next = (*target)->next;
153 if( *target == targetlist->last)
154 targetlist->last = ptr;
156 delete_target( target);
159 void delete_targetlist(targetlist_param_t **targetlist)
161 target_param_t *targetPtr, *targetNext;
163 targetPtr = (*targetlist)->first;
164 while( targetPtr != NULL){
165 targetNext=targetPtr->next;
166 delete_target( &targetPtr);
167 targetPtr=targetNext;
172 void print_target( target_param_t *target)
174 fprintf( logstream, "target:\n");
175 fprintf( logstream, "\t tid=%s\n", target->tid);
176 fprintf( logstream, "\t csn=%d\n", target->csn);
177 fprintf( logstream, "\t target=%s\n\n", target->filename);
180 void print_alltarget( targetlist_param_t *targetlist)
184 ptr = targetlist->first;
191 target_param_t * search_target( char targetname[], targetlist_param_t *targetlist)
193 target_param_t *foundtarget;
195 foundtarget = targetlist->first;
197 while( foundtarget != NULL){
199 if( strcmp( targetname, foundtarget->filename) == 0)
202 foundtarget = foundtarget->next;
207 target_param_t * search_targetBytid( char tid[], targetlist_param_t *targetlist)
209 target_param_t *foundtarget;
211 foundtarget = targetlist->first;
213 while( foundtarget != NULL){
215 if( strcmp( tid, foundtarget->tid) == 0)
218 foundtarget = foundtarget->next;
224 int open_jp2file( char filename[])
229 if( (fd = open( filename, O_RDONLY)) == -1){
230 fprintf( FCGI_stdout, "Reason: Target %s not found\r\n", filename);
233 // Check resource is a JP family file.
234 if( lseek( fd, 0, SEEK_SET)==-1){
236 fprintf( FCGI_stdout, "Reason: Target %s broken (lseek error)\r\n", filename);
240 data = (char *)malloc( 12); // size of header
241 if( read( fd, data, 12) != 12){
244 fprintf( FCGI_stdout, "Reason: Target %s broken (read error)\r\n", filename);
248 if( *data || *(data + 1) || *(data + 2) ||
249 *(data + 3) != 12 || strncmp (data + 4, "jP \r\n\x87\n", 8)){
252 fprintf( FCGI_stdout, "Reason: No JPEG 2000 Signature box in target %s\r\n", filename);