[1.5][JPIP] new feature to target JP2 files from www (libcurl required)
[openjpeg.git] / applications / jpip / libopenjpip / imgsock_manager.h
1 /*
2  * $Id: imgsock_manager.h 54 2011-05-10 13:22:47Z 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 #ifndef         IMGSOCK_MANAGER_H_
32 # define        IMGSOCK_MANAGER_H_
33
34 #include "bool.h"
35 #include "byte_manager.h"
36
37 #ifdef _WIN32
38 #include <winsock2.h>
39 #else
40 typedef int SOCKET;
41 #endif //_WIN32
42
43 /**
44  * open listening socket
45  *
46  * @return new socket
47  */
48 SOCKET open_listeningsocket();
49
50 /**
51  * accept a new connection to the listenning socket
52  *
53  * @param listening_socket listenning socket
54  * @return                 connected socket (-1 if error occurs)
55  */
56 SOCKET accept_socket( SOCKET listening_socket);
57
58
59 #define NUM_OF_MSGTYPES 9
60 typedef enum eMSGTYPE{ JPIPSTREAM, PNMREQ, XMLREQ, TIDREQ, CIDREQ, CIDDST, SIZREQ, JP2SAVE, QUIT, MSGERROR} msgtype_t;
61
62 /**
63  * indeitify client message type
64  *
65  * @param [in] connected_socket file descriptor of the connected socket
66  * @return                      message type
67  */
68 msgtype_t identify_clientmsg( SOCKET connected_socket);
69
70 /**
71  * receive a JPT- JPP- stream from client
72  *
73  * @param [in]  connected_socket file descriptor of the connected socket
74  * @param [out] target           address of received target file name string pointer ( malloced, if not received, NULL)
75  * @param [out] tid              address of received target identifier string pointer ( malloced, if not received, null string)
76  * @param [out] cid              address of received channel identifier string pointer ( malloced, if not received, null string)
77  * @param [out] streamlen        length of the received codestream
78  * @return                       JPT- JPP- codestream
79  */
80 Byte_t * receive_JPIPstream( SOCKET connected_socket, char **target, char **tid, char **cid, int *streamlen);
81
82 /**
83  * send PGM/PPM image stream to the client
84  *
85  * @param [in]  connected_socket file descriptor of the connected socket
86  * @param [in]  pnmstream        PGM/PPM image codestream
87  * @param [in]  width            width  of the PGM/PPM image (different from the original image)
88  * @param [in]  height           height of the PGM/PPM image
89  * @param [in]  numofcomp        number of components of the image
90  * @param [in]  maxval           maximum value of the image (only 255 supported)
91  */
92 void send_PNMstream( SOCKET connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval);
93
94 /**
95  * send XML data stream to the client
96  *
97  * @param [in]  connected_socket file descriptor of the connected socket
98  * @param [in]  xmlstream        xml data stream
99  * @param [in]  length           legnth of the xml data stream
100  */
101 void send_XMLstream( SOCKET connected_socket, Byte_t *xmlstream, int length);
102
103 /**
104  * send TID data stream to the client
105  *
106  * @param [in]  connected_socket file descriptor of the connected socket
107  * @param [in]  tid              tid string
108  * @param [in]  tidlen           legnth of the tid string
109  */
110 void send_TIDstream( SOCKET connected_socket, char *tid, int tidlen);
111
112 /**
113  * send CID data stream to the client
114  *
115  * @param [in]  connected_socket file descriptor of the connected socket
116  * @param [in]  cid              cid string
117  * @param [in]  cidlen           legnth of the cid string
118  */
119 void send_CIDstream( SOCKET connected_socket, char *cid, int cidlen);
120
121 /**
122  * send SIZ data stream to the client
123  *
124  * @param [in]  connected_socket file descriptor of the connected socket
125  * @param [in]  width            original width  of the image
126  * @param [in]  height           original height of the image
127  */
128 void send_SIZstream( SOCKET connected_socket, unsigned int width, unsigned int height);
129
130 /**
131  * send response signal to the client
132  *
133  * @param [in]  connected_socket file descriptor of the connected socket
134  * @param [in]  succeed          whether if the requested process succeeded
135  */
136 void response_signal( SOCKET connected_socket, bool succeed);
137
138 /**
139  * receive a string line (ending with '\n') from client
140  *
141  * @param [in]  connected_socket file descriptor of the connected socket
142  * @param [out] buf              string to be stored
143  * @return                       red size
144  */
145 int receive_line(SOCKET connected_socket, char *buf);
146
147 /**
148  * receive a string line (ending with '\n') from client, return malloc string
149  *
150  * @param [in]  connected_socket file descriptor of the connected socket
151  * @return                       pointer to the string (memory allocated)
152  */
153 char * receive_string( SOCKET connected_socket);
154
155 /**
156  * close socket
157  *
158  * @param [in] sock closing socket
159  * @return     0 if succeed, -1 if failed
160  */
161 int close_socket( SOCKET sock);
162
163 #endif /* !IMGSOCK_MANAGER_H_ */
164
165 /*! \file
166  * PROTOCOL specification to communicate with opj_dec_server
167  *
168  *\section sec1 JPIP-stream
169  * Cache JPT- JPP- stream in server
170  *
171  * client -> server: JPIP-stream\\n version 1.1\\n (optional for cid registration: targetnamestring\\n  tidstring\\n  cidstring\\n) bytelengthvalue\\n data \n
172  * server -> client: 1 or 0 (of 1Byte response signal)
173  * 
174  *\section sec2 PNM request
175  * Get decoded PGM/PPM image
176  *
177  * client -> server: PNM request\\n [cid/tid]string\\n fw\\n fh\\n \n
178  * server -> client: P6 or P5 (2Byte) width (2Byte Big endian) height (2Byte Big endian) maxval (1Byte) data
179  *
180  *\section sec3 XML request
181  * Get XML data
182  *
183  * client -> server: XML request\\n \n
184  * server -> client: XML (3Byte) length (2Byte Big endian) data
185  *
186  *\section sec4 TID request
187  * Get target ID of target image
188  *
189  * client -> server: TID request\\n targetname\\n \n
190  * server -> client: TID (3Byte) length (1Byte) tiddata
191  *
192  *\section sec5 CID request
193  * Get Channel ID of identical target image
194  *
195  * client -> server: CID request\\n targetname\\n \n
196  * server -> client: CID (3Byte) length (1Byte) ciddata
197  *
198  *\section sec6 CID destroy
199  * Close Channel ID
200  *
201  * client -> server: CID destroy\\n ciddata \n
202  * server -> client: 1 or 0 (of 1Byte response signal)
203  *
204  *\section sec7 SIZ request
205  * Get original size of image
206  *
207  * client -> server: SIZ request\\n  tidstring\\n  cidstring\\n \n
208  * server -> client: SIZ (3Byte) width (3Byte Big endian) height (3Byte Big endian)
209  *
210  *\section sec8 JP2 save
211  * Save in JP2 file format
212  *
213  * client -> server: JP2 save\\n ciddata \n
214  * server -> client: 1 or 0 (of 1Byte response signal)
215  *
216  *\section sec9 QUIT
217  * Quit the opj_dec_server program
218  *
219  * client -> server: quit or QUIT
220  */