]> git.mxchange.org Git - flightgear.git/blob - src/Network/jpg-httpd.cxx
jpg-httpd: consistent indentation and white-spaces
[flightgear.git] / src / Network / jpg-httpd.cxx
1 // httpd.hxx -- FGFS http property manager interface / external script
2 //              and control class
3 //
4 // Written by Curtis Olson, started June 2001.
5 //
6 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // Jpeg Image Support added August 2001
9 //  by Norman Vine - nhv@cape.com
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of the
14 // License, or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 //
25 // $Id$
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/compiler.h>
33
34 #include <cstdlib>        // atoi() atof()
35
36 #include <cstring>
37
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/io/iochannel.hxx>
40 #include <simgear/math/sg_types.hxx>
41 #include <simgear/props/props.hxx>
42
43 #include <Main/fg_props.hxx>
44 #include <Main/globals.hxx>
45 #include <Viewer/renderer.hxx>
46
47 #include "jpg-httpd.hxx"
48
49 #define __MAX_HTTP_BLOCK_SIZE       4096
50 #define __MAX_STRING_SIZE           2048
51 #define __TIMEOUT_COUNT             5
52 #define __HTTP_GET_STRING           "GET "
53
54 #include <osgUtil/SceneView>
55 #include <osgViewer/Viewer>
56 extern osg::ref_ptr<osgUtil::SceneView> sceneView;
57
58 using std::string;
59
60
61 bool FGJpegHttpd::open() {
62     if ( is_enabled() ) {
63     SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
64         << "is already in use, ignoring" );
65     return false;
66     }
67
68     imageServer = new HttpdImageServer( port );
69     
70     set_hz( 5 );                // default to processing requests @ 5Hz
71     set_enabled( true );
72
73     return true;
74 }
75
76
77 bool FGJpegHttpd::process() {
78     simgear::NetChannel::poll();
79
80     return true;
81 }
82
83
84 bool FGJpegHttpd::close() {
85     delete imageServer;
86
87     return true;
88 }
89
90 // Handle http GET requests
91 void HttpdImageChannel :: foundTerminator( void ) {
92
93     closeWhenDone();
94
95     char      szTemp[256];
96     char      szResponse[__MAX_STRING_SIZE];
97     char      *pRequest     = buffer.getData();
98     int       nStep         = 0;
99     int       nBytesSent    = 0;
100     int       nTimeoutCount = 0;
101     int       nBufferCount  = 0;
102     int       nImageLen;
103     int       nBlockSize;
104
105
106     if ( strstr( pRequest, __HTTP_GET_STRING ) != NULL )
107     {
108         
109         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< HTTP Request : " << pRequest );
110
111         double left, right, bottom, top, zNear, zFar;
112         osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
113         viewer->getCamera()->getProjectionMatrixAsFrustum(left, right,
114                                                           bottom, top,
115                                                           zNear, zFar);
116         JpgFactory->setFrustum( left, right, bottom, top, zNear, zFar );
117
118         nImageLen  = JpgFactory -> render();
119         nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
120
121         if( nImageLen )
122         {
123             strcpy( szResponse, "HTTP/1.1 200 OK" );
124             strcat( szResponse, getTerminator() );
125             strcat( szResponse, "Content-Type: image/jpeg" );
126             strcat( szResponse, getTerminator() );
127
128             SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
129             sprintf( szTemp, "Content-Length: %d", nImageLen );
130             strcat( szResponse, szTemp );
131
132             strcat( szResponse, getTerminator() );
133             strcat( szResponse, "Connection: close" );
134             strcat( szResponse, getTerminator() );
135             strcat( szResponse, getTerminator() );
136
137             if( getHandle() == -1 )
138             {
139                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
140                 buffer.remove();
141                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
142                 return;
143             }
144
145             if( send( ( char * ) szResponse, strlen( szResponse ) ) <= 0 )
146             {
147                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Error to send HTTP response. Ignoring request.\n" );
148                 buffer.remove();
149                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
150                 return;
151             }
152
153             /*
154              * Send block with size defined by __MAX_HTTP_BLOCK_SIZE
155              */
156             while( nStep <= nImageLen )
157             {
158                 nBufferCount++;
159
160                 if( getHandle() == -1 )
161                 {
162                     SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
163                     break;
164                 }
165
166                 nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
167
168                 if( nBytesSent <= 0 )
169                 {
170                     if( nTimeoutCount == __TIMEOUT_COUNT )
171                     {
172                         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Timeout reached. Exiting before end of image transmission.\n" );
173                         nTimeoutCount = 0;
174                         break;
175                     }
176
177                     SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
178
179 #ifdef _WIN32
180                     Sleep(1000);
181 #else
182                     sleep(1);
183 #endif
184                     nTimeoutCount++;
185                     continue;
186                 }
187
188                 SG_LOG( SG_IO, SG_DEBUG, ">>>>>>>>> (" << nBufferCount << ") BLOCK STEP " << nStep << " - IMAGELEN " << nImageLen << " - BLOCKSIZE " << nBlockSize << " - SENT " << nBytesSent );
189
190                 /*
191                  * Calculate remaining image.
192                  */
193                 if( ( nStep + nBlockSize ) >= nImageLen )
194                 {
195                     nBlockSize = ( nImageLen - nStep );
196                     nStep += nBlockSize;
197                 }
198
199                 nStep += nBytesSent;
200                 nTimeoutCount = 0;
201 #ifdef _WIN32
202                 Sleep(1);
203 #else
204                 usleep( 1000 );
205 #endif
206             }
207
208             SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
209
210         } else {
211             SG_LOG( SG_IO, SG_DEBUG, "!!! NO IMAGE !!!  info -> numbytes = " << nImageLen );
212         }
213
214         /*
215          * Release JPEG buffer.
216          */
217         JpgFactory -> destroy();
218     }
219
220     buffer.remove();
221 }