1 // httpd.hxx -- FGFS http property manager interface / external script
4 // Written by Curtis Olson, started June 2001.
6 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
8 // Jpeg Image Support added August 2001
9 // by Norman Vine - nhv@cape.com
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.
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.
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.
32 #include <simgear/compiler.h>
34 #include <cstdlib> // atoi() atof()
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>
43 #include <Main/fg_props.hxx>
44 #include <Main/globals.hxx>
45 #include <Main/renderer.hxx>
47 #include "jpg-httpd.hxx"
49 #define __MAX_HTTP_BLOCK_SIZE 4096
50 #define __MAX_STRING_SIZE 2048
51 #define __TIMEOUT_COUNT 5
52 #define __HTTP_GET_STRING "GET "
54 #include <osgUtil/SceneView>
55 #include <osgViewer/Viewer>
56 extern osg::ref_ptr<osgUtil::SceneView> sceneView;
61 bool FGJpegHttpd::open() {
63 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
64 << "is already in use, ignoring" );
68 imageServer = new HttpdImageServer( port );
70 set_hz( 5 ); // default to processing requests @ 5Hz
77 bool FGJpegHttpd::process() {
78 simgear::NetChannel::poll();
84 bool FGJpegHttpd::close() {
90 // Handle http GET requests
91 void HttpdImageChannel :: foundTerminator( void ) {
96 char szResponse[__MAX_STRING_SIZE];
97 char *pRequest = buffer.getData();
100 int nTimeoutCount = 0;
101 int nBufferCount = 0;
106 if ( strstr( pRequest, __HTTP_GET_STRING ) != NULL ) {
108 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< HTTP Request : " << pRequest );
110 double left, right, bottom, top, zNear, zFar;
111 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
112 viewer->getCamera()->getProjectionMatrixAsFrustum(left, right,
115 JpgFactory->setFrustum( left, right, bottom, top, zNear, zFar );
117 nImageLen = JpgFactory -> render();
118 nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
121 strcpy( szResponse, "HTTP/1.1 200 OK" );
122 strcat( szResponse, getTerminator() );
123 strcat( szResponse, "Content-Type: image/jpeg" );
124 strcat( szResponse, getTerminator() );
126 SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
127 sprintf( szTemp, "Content-Length: %d", nImageLen );
128 strcat( szResponse, szTemp );
130 strcat( szResponse, getTerminator() );
131 strcat( szResponse, "Connection: close" );
132 strcat( szResponse, getTerminator() );
133 strcat( szResponse, getTerminator() );
135 if( getHandle() == -1 ) {
136 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
138 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
142 if( send( ( char * ) szResponse, strlen( szResponse ) ) <= 0 ) {
143 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Error to send HTTP response. Ignoring request.\n" );
145 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
150 * Send block with size defined by __MAX_HTTP_BLOCK_SIZE
152 while( nStep <= nImageLen ) {
155 if( getHandle() == -1 ) {
156 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
160 nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
162 if( nBytesSent <= 0 ) {
163 if( nTimeoutCount == __TIMEOUT_COUNT ) {
164 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Timeout reached. Exiting before end of image transmission.\n" );
169 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
180 SG_LOG( SG_IO, SG_DEBUG, ">>>>>>>>> (" << nBufferCount << ") BLOCK STEP " << nStep << " - IMAGELEN " << nImageLen << " - BLOCKSIZE " << nBlockSize << " - SENT " << nBytesSent );
183 * Calculate remaining image.
185 if( ( nStep + nBlockSize ) >= nImageLen ) {
186 nBlockSize = ( nImageLen - nStep );
199 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
202 SG_LOG( SG_IO, SG_DEBUG, "!!! NO IMAGE !!! info -> numbytes = " << nImageLen );
206 * Release JPEG buffer.
208 JpgFactory -> destroy();