[trunk] Start FolderReorgProposal task
[openjpeg.git] / src / lib / openjpip / session_manager.c
1 /*
2  * $Id: session_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 "session_manager.h"
35 #include "target_manager.h"
36
37 #ifdef SERVER
38 #include "fcgi_stdio.h"
39 #define logstream FCGI_stdout
40 #else
41 #define FCGI_stdout stdout
42 #define FCGI_stderr stderr
43 #define logstream stderr
44 #endif /*SERVER */
45
46
47 sessionlist_param_t * gene_sessionlist(void)
48 {
49   sessionlist_param_t *sessionlist;
50
51   sessionlist = (sessionlist_param_t *)malloc( sizeof(sessionlist_param_t));
52   
53   sessionlist->first = NULL;
54   sessionlist->last  = NULL;
55
56   return sessionlist;
57 }
58
59 session_param_t * gene_session( sessionlist_param_t *sessionlist)
60 {
61   session_param_t *session;
62   
63   session = (session_param_t *)malloc( sizeof(session_param_t));
64
65   session->channellist = gene_channellist();
66   session->cachemodellist = gene_cachemodellist();
67
68   session->next = NULL;
69   
70   if( sessionlist->first) /* there are one or more entries */
71     sessionlist->last->next = session;
72   else                   /* first entry */
73     sessionlist->first = session;
74   sessionlist->last = session;
75   
76   return session;
77 }
78
79 bool search_session_and_channel( char cid[], 
80                                  sessionlist_param_t *sessionlist, 
81                                  session_param_t **foundsession, 
82                                  channel_param_t **foundchannel)
83 {
84   *foundsession = sessionlist->first;
85   
86   while( *foundsession != NULL){
87
88     *foundchannel = (*foundsession)->channellist->first;
89     
90     while( *foundchannel != NULL){
91       
92       if( strcmp( cid, (*foundchannel)->cid) == 0)
93         return true;
94       
95       *foundchannel = (*foundchannel)->next;
96     }
97     *foundsession = (*foundsession)->next;
98   }
99   
100   fprintf( FCGI_stdout, "Status: 503\r\n");
101   fprintf( FCGI_stdout, "Reason: Channel %s not found\r\n", cid); 
102
103   return false;
104 }
105
106 void insert_cachemodel_into_session( session_param_t *session, cachemodel_param_t *cachemodel)
107 {
108   if(!cachemodel)
109     return;
110
111 #ifndef SERVER
112   fprintf( logstream, "local log: insert cachemodel into session\n");
113 #endif
114   if( session->cachemodellist->first != NULL)
115     session->cachemodellist->last->next = cachemodel;
116   else
117     session->cachemodellist->first = cachemodel;
118   session->cachemodellist->last = cachemodel;
119 }
120
121 bool delete_session( session_param_t **session, sessionlist_param_t *sessionlist)
122 {
123   session_param_t *ptr;
124
125   if( *session == NULL)
126     return false;
127
128
129   if( *session == sessionlist->first)
130     sessionlist->first = (*session)->next;
131   else{
132     ptr = sessionlist->first;
133     while( ptr->next != *session)
134       ptr = ptr->next;
135     ptr->next = (*session)->next;
136
137     if( *session == sessionlist->last)
138       sessionlist->last = ptr;
139   }
140   
141   delete_channellist( &((*session)->channellist));
142   delete_cachemodellist( &((*session)->cachemodellist));
143
144 #ifndef SERVER
145   fprintf( logstream, "local log: session: %p deleted!\n", (void *)(*session));
146 #endif
147   free( *session);
148
149   return true;
150 }
151
152 void delete_sessionlist( sessionlist_param_t **sessionlist)
153 {  
154   session_param_t *sessionPtr, *sessionNext;
155
156   sessionPtr = (*sessionlist)->first;
157   while( sessionPtr != NULL){
158     sessionNext=sessionPtr->next;
159
160     delete_channellist( &(sessionPtr->channellist));
161     delete_cachemodellist( &(sessionPtr->cachemodellist));
162
163 #ifndef SERVER
164     fprintf( logstream, "local log: session: %p deleted!\n", (void *)sessionPtr);
165 #endif
166     free( sessionPtr);
167
168     sessionPtr=sessionNext;
169   }
170
171   (*sessionlist)->first = NULL;
172   (*sessionlist)->last  = NULL;
173
174   free(*sessionlist);
175 }
176
177 void print_allsession( sessionlist_param_t *sessionlist)
178 {
179   session_param_t *ptr;
180   cachemodel_param_t *cachemodel;
181   int i=0;
182
183   fprintf( logstream, "SESSIONS info:\n");
184
185   ptr = sessionlist->first;
186   while( ptr != NULL){
187     fprintf( logstream, "session No.%d\n", i++);
188     print_allchannel( ptr->channellist);
189     cachemodel = ptr->cachemodellist->first;
190     while( cachemodel){
191       print_target( cachemodel->target);
192       cachemodel = cachemodel->next;
193     }
194     ptr=ptr->next;
195   }
196 }