[trunk] Import rev 1103 into trunk.
[openjpeg.git] / applications / jpip / libopenjpip / channel_manager.c
1 /*
2  * $Id: channel_manager.c 44 2011-02-15 12:32:29Z kaori $
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 <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include "channel_manager.h"
35
36 #ifdef SERVER
37 #include "fcgi_stdio.h"
38 #define logstream FCGI_stdout
39 #else
40 #define FCGI_stdout stdout
41 #define FCGI_stderr stderr
42 #define logstream stderr
43 #endif //SERVER
44
45 channellist_param_t * gene_channellist()
46 {
47   channellist_param_t *channellist;
48
49   channellist = (channellist_param_t *)malloc( sizeof(channellist_param_t));
50   
51   channellist->first = NULL;
52   channellist->last  = NULL;
53
54   return channellist;
55 }
56
57 channel_param_t * gene_channel( query_param_t query_param, auxtrans_param_t auxtrans, cachemodel_param_t *cachemodel, channellist_param_t *channellist)
58 {
59   channel_param_t *channel;
60   char transport[4][10] = { "non", "http", "http-tcp", "http-udp"};
61   
62   if( !cachemodel){
63     fprintf( FCGI_stdout, "Status: 404\r\n"); 
64     fprintf( FCGI_stdout, "Reason: cnew cancelled\r\n"); 
65     return NULL;
66   }
67
68   channel = (channel_param_t *)malloc( sizeof(channel_param_t));
69   channel->cachemodel = cachemodel;
70
71   // set channel ID and get present time
72   snprintf( channel->cid, MAX_LENOFCID, "%x%x", (unsigned int)time( &channel->start_tm), (unsigned int)rand());
73   
74   channel->aux = query_param.cnew;
75   
76   // only tcp implemented for now
77   if( channel->aux == udp)
78     channel->aux = tcp;
79   
80   channel->next=NULL;
81
82   set_channel_variable_param( query_param, channel);
83
84   if( channellist->first != NULL)
85     channellist->last->next = channel;
86   else
87     channellist->first = channel;
88   channellist->last = channel;
89   
90   fprintf( FCGI_stdout, "JPIP-cnew: cid=%s", channel->cid);
91   fprintf( FCGI_stdout, ",transport=%s", transport[channel->aux]);
92   
93   if( channel->aux == tcp || channel->aux == udp)
94     fprintf( FCGI_stdout, ",auxport=%d", channel->aux==tcp ? auxtrans.tcpauxport : auxtrans.udpauxport);
95
96   fprintf( FCGI_stdout, "\r\n");
97
98   return channel;
99 }
100
101
102 void set_channel_variable_param( query_param_t query_param, channel_param_t *channel)
103 {
104   // set roi information
105 }
106
107
108 void delete_channel( channel_param_t **channel, channellist_param_t *channellist)
109 {
110   channel_param_t *ptr;
111
112   if( *channel == channellist->first)
113     channellist->first = (*channel)->next;
114   else{
115     ptr = channellist->first;
116     while( ptr->next != *channel){
117       ptr=ptr->next;
118     }
119     
120     ptr->next = (*channel)->next;
121     
122     if( *channel == channellist->last)
123       channellist->last = ptr;
124   }
125 #ifndef SERVER
126   fprintf( logstream, "local log: channel: %s deleted\n", (*channel)->cid);
127 #endif
128   free(*channel);
129 }
130
131 void delete_channellist( channellist_param_t **channellist)
132 {
133   channel_param_t *channelPtr, *channelNext;
134     
135   channelPtr = (*channellist)->first;
136   while( channelPtr != NULL){
137     channelNext=channelPtr->next;
138 #ifndef SERVER
139       fprintf( logstream, "local log: channel %s deleted!\n", channelPtr->cid);
140 #endif
141       free(channelPtr);
142       channelPtr=channelNext;
143   }
144   free( *channellist);
145 }
146
147 void print_allchannel( channellist_param_t *channellist)
148 {
149   channel_param_t *ptr;
150
151   ptr = channellist->first;
152   while( ptr != NULL){
153     fprintf( logstream,"channel-ID=%s \t target=%s\n", ptr->cid, ptr->cachemodel->target->filename);
154     ptr=ptr->next;
155   }
156 }
157
158 channel_param_t * search_channel( char cid[], channellist_param_t *channellist)
159 {
160   channel_param_t *foundchannel;
161
162   foundchannel = channellist->first;
163   
164   while( foundchannel != NULL){
165     
166     if( strcmp( cid, foundchannel->cid) == 0)
167       return foundchannel;
168       
169     foundchannel = foundchannel->next;
170   }
171   fprintf( FCGI_stdout, "Status: 503\r\n");
172   fprintf( FCGI_stdout, "Reason: Channel %s not found in this session\r\n", cid); 
173
174   return NULL;
175 }