Merge pull request #1518 from dg0yt/static-windows
[openjpeg.git] / src / lib / openjpip / session_manager.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, 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 *)opj_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 *)opj_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     }
75     sessionlist->last = session;
76
77     return session;
78 }
79
80 OPJ_BOOL search_session_and_channel(char cid[],
81                                     sessionlist_param_t *sessionlist,
82                                     session_param_t **foundsession,
83                                     channel_param_t **foundchannel)
84 {
85     *foundsession = sessionlist->first;
86
87     while (*foundsession != NULL) {
88
89         *foundchannel = (*foundsession)->channellist->first;
90
91         while (*foundchannel != NULL) {
92
93             if (strcmp(cid, (*foundchannel)->cid) == 0) {
94                 return OPJ_TRUE;
95             }
96
97             *foundchannel = (*foundchannel)->next;
98         }
99         *foundsession = (*foundsession)->next;
100     }
101
102     fprintf(FCGI_stdout, "Status: 503\r\n");
103     fprintf(FCGI_stdout, "Reason: Channel %s not found\r\n", cid);
104
105     return OPJ_FALSE;
106 }
107
108 void insert_cachemodel_into_session(session_param_t *session,
109                                     cachemodel_param_t *cachemodel)
110 {
111     if (!cachemodel) {
112         return;
113     }
114
115 #ifndef SERVER
116     fprintf(logstream, "local log: insert cachemodel into session\n");
117 #endif
118     if (session->cachemodellist->first != NULL) {
119         session->cachemodellist->last->next = cachemodel;
120     } else {
121         session->cachemodellist->first = cachemodel;
122     }
123     session->cachemodellist->last = cachemodel;
124 }
125
126 OPJ_BOOL delete_session(session_param_t **session,
127                         sessionlist_param_t *sessionlist)
128 {
129     session_param_t *ptr;
130
131     if (*session == NULL) {
132         return OPJ_FALSE;
133     }
134
135
136     if (*session == sessionlist->first) {
137         sessionlist->first = (*session)->next;
138     } else {
139         ptr = sessionlist->first;
140         while (ptr->next != *session) {
141             ptr = ptr->next;
142         }
143         ptr->next = (*session)->next;
144
145         if (*session == sessionlist->last) {
146             sessionlist->last = ptr;
147         }
148     }
149
150     delete_channellist(&((*session)->channellist));
151     delete_cachemodellist(&((*session)->cachemodellist));
152
153 #ifndef SERVER
154     fprintf(logstream, "local log: session: %p deleted!\n", (void *)(*session));
155 #endif
156     opj_free(*session);
157
158     return OPJ_TRUE;
159 }
160
161 void delete_sessionlist(sessionlist_param_t **sessionlist)
162 {
163     session_param_t *sessionPtr, *sessionNext;
164
165     sessionPtr = (*sessionlist)->first;
166     while (sessionPtr != NULL) {
167         sessionNext = sessionPtr->next;
168
169         delete_channellist(&(sessionPtr->channellist));
170         delete_cachemodellist(&(sessionPtr->cachemodellist));
171
172 #ifndef SERVER
173         fprintf(logstream, "local log: session: %p deleted!\n", (void *)sessionPtr);
174 #endif
175         opj_free(sessionPtr);
176
177         sessionPtr = sessionNext;
178     }
179
180     (*sessionlist)->first = NULL;
181     (*sessionlist)->last  = NULL;
182
183     opj_free(*sessionlist);
184 }
185
186 void print_allsession(sessionlist_param_t *sessionlist)
187 {
188     session_param_t *ptr;
189     cachemodel_param_t *cachemodel;
190     int i = 0;
191
192     fprintf(logstream, "SESSIONS info:\n");
193
194     ptr = sessionlist->first;
195     while (ptr != NULL) {
196         fprintf(logstream, "session No.%d\n", i++);
197         print_allchannel(ptr->channellist);
198         cachemodel = ptr->cachemodellist->first;
199         while (cachemodel) {
200             print_target(cachemodel->target);
201             cachemodel = cachemodel->next;
202         }
203         ptr = ptr->next;
204     }
205 }