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