[1.5][JPIP] backport r1037 to branch 1.5
[openjpeg.git] / applications / jpip / libopenjpip / dec_clientmsg_handler.c
1 /*
2  * $Id$
3  *
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  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include "dec_clientmsg_handler.h"
36 #include "ihdrbox_manager.h"
37 #include "jpipstream_manager.h"
38 #include "jp2k_encoder.h"
39
40 void handle_JPIPstreamMSG( SOCKET connected_socket, cachelist_param_t *cachelist,
41                            Byte_t **jpipstream, int *streamlen, msgqueue_param_t *msgqueue)
42 {
43   Byte_t *newjpipstream;
44   int newstreamlen = 0;
45   cache_param_t *cache;
46   char *target, *tid, *cid;
47   metadatalist_param_t *metadatalist;
48   
49   newjpipstream = receive_JPIPstream( connected_socket, &target, &tid, &cid, &newstreamlen);
50
51   parse_JPIPstream( newjpipstream, newstreamlen, *streamlen, msgqueue);
52
53   *jpipstream = update_JPIPstream( newjpipstream, newstreamlen, *jpipstream, streamlen);
54   free( newjpipstream);
55
56   metadatalist = gene_metadatalist();
57   parse_metamsg( msgqueue, *jpipstream, *streamlen, metadatalist);
58
59   // cid registration
60   if( target != NULL){
61     if((cache = search_cache( target, cachelist))){
62       if( tid != NULL)
63         update_cachetid( tid, cache);
64       if( cid != NULL)
65         add_cachecid( cid, cache);
66     }
67     else{
68       cache = gene_cache( target, msgqueue->last->csn, tid, cid);
69       insert_cache_into_list( cache, cachelist);
70     }
71   }
72   else
73     cache = search_cacheBycsn( msgqueue->last->csn, cachelist);
74
75   if( cache->metadatalist)
76     delete_metadatalist( &cache->metadatalist);
77   cache->metadatalist = metadatalist;
78
79   if( target)    free( target);
80   if( tid)    free( tid);
81   if( cid)    free( cid);
82
83   response_signal( connected_socket, true);
84 }
85
86 void handle_PNMreqMSG( SOCKET connected_socket, Byte_t *jpipstream, msgqueue_param_t *msgqueue, cachelist_param_t *cachelist)
87 {
88   Byte_t *pnmstream;
89   ihdrbox_param_t *ihdrbox;
90   char *CIDorTID, tmp[10];
91   cache_param_t *cache;
92   int fw, fh;
93
94   CIDorTID = receive_string( connected_socket);
95   
96   if(!(cache = search_cacheBycid( CIDorTID, cachelist)))
97     if(!(cache = search_cacheBytid( CIDorTID, cachelist))){
98       free( CIDorTID);
99       return;
100     }
101   
102   free( CIDorTID);
103
104   receive_line( connected_socket, tmp);
105   fw = atoi( tmp);
106
107   receive_line( connected_socket, tmp);
108   fh = atoi( tmp);
109
110   pnmstream = jpipstream_to_pnm( jpipstream, msgqueue, cache->csn, fw, fh, &cache->ihdrbox);
111   ihdrbox = cache->ihdrbox;
112
113   send_PNMstream( connected_socket, pnmstream, ihdrbox->width, ihdrbox->height, ihdrbox->nc, ihdrbox->bpc > 8 ? 255 : (1 << ihdrbox->bpc) - 1);
114
115   free( pnmstream);
116 }
117
118 void handle_XMLreqMSG( SOCKET connected_socket, Byte_t *jpipstream, cachelist_param_t *cachelist)
119 {
120   char *cid;
121   cache_param_t *cache;
122
123   cid = receive_string( connected_socket);
124
125   if(!(cache = search_cacheBycid( cid, cachelist))){
126     free( cid);
127     return;
128   }
129
130   free( cid);
131   
132   boxcontents_param_t *boxcontents = cache->metadatalist->last->boxcontents;
133   Byte_t *xmlstream = (Byte_t *)malloc( boxcontents->length);
134   memcpy( xmlstream, jpipstream+boxcontents->offset, boxcontents->length);
135   send_XMLstream( connected_socket, xmlstream, boxcontents->length);
136   free( xmlstream);
137 }
138
139 void handle_TIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
140 {
141   char *target, *tid = NULL;
142   cache_param_t *cache;
143   int tidlen = 0;
144
145   target = receive_string( connected_socket);
146   cache = search_cache( target, cachelist);
147
148   free( target);
149   
150   if( cache){
151     tid = cache->tid;
152     tidlen = strlen(tid);
153   }
154   send_TIDstream( connected_socket, tid, tidlen);
155 }
156
157 void handle_CIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
158 {
159   char *target, *cid = NULL;
160   cache_param_t *cache;
161   int cidlen = 0;
162
163   target = receive_string( connected_socket);
164   cache = search_cache( target, cachelist);
165   
166   free( target);
167
168   if( cache){
169     if( cache->numOfcid > 0){
170       cid = cache->cid[ cache->numOfcid-1];
171       cidlen = strlen(cid);
172     }
173   }
174   send_CIDstream( connected_socket, cid, cidlen);
175 }
176
177 void handle_dstCIDreqMSG( SOCKET connected_socket, cachelist_param_t *cachelist)
178 {
179   char *cid;
180
181   cid = receive_string( connected_socket);
182   remove_cachecid( cid, cachelist);
183   response_signal( connected_socket, true);
184   
185   free( cid);
186 }
187
188 void handle_JP2saveMSG( SOCKET connected_socket, cachelist_param_t *cachelist, msgqueue_param_t *msgqueue, Byte_t *jpipstream)
189 {
190   char *cid;
191   cache_param_t *cache;
192   Byte_t *jp2stream;
193   Byte8_t jp2len;
194
195   cid = receive_string( connected_socket);
196   if(!(cache = search_cacheBycid( cid, cachelist))){
197     free( cid);
198     return;
199   }
200   
201   free( cid);
202   
203   jp2stream = recons_jp2( msgqueue, jpipstream, cache->csn, &jp2len);
204
205   if( jp2stream){
206     save_codestream( jp2stream, jp2len, "jp2");
207     free( jp2stream);
208   }
209 }