Reformat whole codebase with astyle.options (#128)
[openjpeg.git] / src / bin / jpip / opj_server.c
1 /*
2  * $Id: opj_server.c 53 2011-05-09 16:55:39Z kaori $
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  * Copyright (c) 2011,      Lucian Corlaciu, GSoC
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*! \file
33  *  \brief opj_server is a JPIP server program, which supports HTTP connection, JPT-stream, session, channels, and cache model managements.
34  *
35  *  \section req Requirements
36  *    FastCGI development kit (http://www.fastcgi.com).
37  *
38  *  \section impinst Implementing instructions
39  *  Launch opj_server from the server terminal:\n
40  *   % spawn-fcgi -f ./opj_server -p 3000 -n
41  *
42  *  Note: JP2 files are stored in the working directory of opj_server\n
43  *  Check README for the JP2 Encoding\n
44  *
45  *  We tested this software with a virtual server running on the same Linux machine as the clients.
46  */
47
48 #include <stdio.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include "fcgi_stdio.h"
52 #include "openjpip.h"
53
54 #ifndef QUIT_SIGNAL
55 #define QUIT_SIGNAL "quitJPIP"
56 #endif
57
58 #ifdef _WIN32
59 WSADATA initialisation_win32;
60 #endif /*_WIN32*/
61
62 int main(void)
63 {
64     server_record_t *server_record;
65 #ifdef SERVER
66     char *query_string;
67 #endif
68
69 #ifdef _WIN32
70     int erreur = WSAStartup(MAKEWORD(2, 2), &initialisation_win32);
71     if (erreur != 0) {
72         fprintf(stderr, "Erreur initialisation Winsock error : %d %d\n", erreur,
73                 WSAGetLastError());
74     } else {
75         fprintf(stderr, "Initialisation Winsock\n");
76     }
77 #endif /*_WIN32*/
78
79     server_record = init_JPIPserver(60000, 0);
80
81 #ifdef SERVER
82     while (FCGI_Accept() >= 0)
83 #else
84
85     char query_string[128];
86     while (fgets(query_string, 128, stdin) && query_string[0] != '\n')
87 #endif
88     {
89         QR_t *qr;
90         OPJ_BOOL parse_status;
91
92 #ifdef SERVER
93         query_string = getenv("QUERY_STRING");
94 #endif /*SERVER*/
95
96         if (strcmp(query_string, QUIT_SIGNAL) == 0) {
97             break;
98         }
99
100         qr = parse_querystring(query_string);
101
102         parse_status = process_JPIPrequest(server_record, qr);
103
104 #ifndef SERVER
105         local_log(OPJ_TRUE, OPJ_TRUE, parse_status, OPJ_FALSE, qr, server_record);
106 #endif
107
108         if (parse_status) {
109             send_responsedata(server_record, qr);
110         } else {
111             fprintf(FCGI_stderr, "Error: JPIP request failed\n");
112             fprintf(FCGI_stdout, "\r\n");
113         }
114
115         end_QRprocess(server_record, &qr);
116     }
117
118     fprintf(FCGI_stderr, "JPIP server terminated by a client request\n");
119
120     terminate_JPIPserver(&server_record);
121
122 #ifdef _WIN32
123     if (WSACleanup() != 0) {
124         fprintf(stderr, "\nError in WSACleanup : %d %d", erreur, WSAGetLastError());
125     } else {
126         fprintf(stderr, "\nWSACleanup OK\n");
127     }
128 #endif
129
130     return 0;
131 }