2002-01-16 23:02:52 +00:00
|
|
|
// httpd.hxx -- FGFS http property manager interface / external script
|
|
|
|
// and control class
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started June 2001.
|
|
|
|
//
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
|
2002-01-16 23:02:52 +00:00
|
|
|
//
|
|
|
|
// Jpeg Image Support added August 2001
|
|
|
|
// by Norman Vine - nhv@cape.com
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-01-16 23:02:52 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
|
|
|
#include <stdlib.h> // atoi() atof()
|
|
|
|
|
2008-07-25 18:38:29 +00:00
|
|
|
#include <string>
|
2002-01-16 23:02:52 +00:00
|
|
|
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/io/iochannel.hxx>
|
|
|
|
#include <simgear/math/sg_types.hxx>
|
2003-05-07 17:37:54 +00:00
|
|
|
#include <simgear/props/props.hxx>
|
2002-01-16 23:02:52 +00:00
|
|
|
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
#include <Main/globals.hxx>
|
2007-05-21 17:50:02 +00:00
|
|
|
#include <Main/renderer.hxx>
|
2002-01-16 23:02:52 +00:00
|
|
|
|
|
|
|
#include "jpg-httpd.hxx"
|
|
|
|
|
2006-10-24 17:51:56 +00:00
|
|
|
#define __MAX_HTTP_BLOCK_SIZE 4096
|
|
|
|
#define __MAX_STRING_SIZE 2048
|
|
|
|
#define __TIMEOUT_COUNT 5
|
|
|
|
#define __HTTP_GET_STRING "GET "
|
|
|
|
|
2007-02-10 16:32:21 +00:00
|
|
|
#include <osgUtil/SceneView>
|
2008-11-19 01:09:29 +00:00
|
|
|
#include <osgViewer/Viewer>
|
2007-02-10 16:32:21 +00:00
|
|
|
extern osg::ref_ptr<osgUtil::SceneView> sceneView;
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2008-07-27 16:25:13 +00:00
|
|
|
using std::string;
|
2002-01-16 23:02:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool FGJpegHttpd::open() {
|
|
|
|
if ( is_enabled() ) {
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
|
|
|
|
<< "is already in use, ignoring" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
imageServer = new HttpdImageServer( port );
|
|
|
|
|
|
|
|
set_hz( 5 ); // default to processing requests @ 5Hz
|
|
|
|
set_enabled( true );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FGJpegHttpd::process() {
|
2010-10-23 20:37:26 +01:00
|
|
|
simgear::NetChannel::poll();
|
2002-01-16 23:02:52 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FGJpegHttpd::close() {
|
|
|
|
delete imageServer;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle http GET requests
|
2006-10-24 17:51:56 +00:00
|
|
|
void HttpdImageChannel :: foundTerminator( void ) {
|
2002-01-16 23:02:52 +00:00
|
|
|
|
2006-10-24 17:51:56 +00:00
|
|
|
closeWhenDone();
|
2002-01-16 23:02:52 +00:00
|
|
|
|
2006-10-24 17:51:56 +00:00
|
|
|
char szTemp[256];
|
|
|
|
char szResponse[__MAX_STRING_SIZE];
|
|
|
|
char *pRequest = buffer.getData();
|
|
|
|
int nStep = 0;
|
|
|
|
int nBytesSent = 0;
|
|
|
|
int nTimeoutCount = 0;
|
|
|
|
int nBufferCount = 0;
|
|
|
|
int nImageLen;
|
|
|
|
int nBlockSize;
|
2002-01-16 23:02:52 +00:00
|
|
|
|
2006-10-24 17:51:56 +00:00
|
|
|
|
|
|
|
if ( strstr( pRequest, __HTTP_GET_STRING ) != NULL ) {
|
2002-01-16 23:02:52 +00:00
|
|
|
|
2006-10-24 17:51:56 +00:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< HTTP Request : " << pRequest );
|
|
|
|
|
2006-12-16 17:44:46 +00:00
|
|
|
double left, right, bottom, top, zNear, zFar;
|
2007-05-21 17:50:02 +00:00
|
|
|
osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
|
2007-12-22 23:26:56 +00:00
|
|
|
viewer->getCamera()->getProjectionMatrixAsFrustum(left, right,
|
|
|
|
bottom, top,
|
|
|
|
zNear, zFar);
|
2006-12-16 17:44:46 +00:00
|
|
|
JpgFactory->setFrustum( left, right, bottom, top, zNear, zFar );
|
|
|
|
|
2006-10-24 17:51:56 +00:00
|
|
|
nImageLen = JpgFactory -> render();
|
|
|
|
nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
|
|
|
|
|
|
|
|
if( nImageLen ) {
|
|
|
|
strcpy( szResponse, "HTTP/1.1 200 OK" );
|
|
|
|
strcat( szResponse, getTerminator() );
|
|
|
|
strcat( szResponse, "Content-Type: image/jpeg" );
|
|
|
|
strcat( szResponse, getTerminator() );
|
|
|
|
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
|
|
|
|
sprintf( szTemp, "Content-Length: %d", nImageLen );
|
|
|
|
strcat( szResponse, szTemp );
|
|
|
|
|
|
|
|
strcat( szResponse, getTerminator() );
|
|
|
|
strcat( szResponse, "Connection: close" );
|
|
|
|
strcat( szResponse, getTerminator() );
|
|
|
|
strcat( szResponse, getTerminator() );
|
|
|
|
|
|
|
|
if( getHandle() == -1 ) {
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
|
|
|
|
buffer.remove();
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( send( ( char * ) szResponse, strlen( szResponse ) ) <= 0 ) {
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Error to send HTTP response. Ignoring request.\n" );
|
|
|
|
buffer.remove();
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send block with size defined by __MAX_HTTP_BLOCK_SIZE
|
|
|
|
*/
|
|
|
|
while( nStep <= nImageLen ) {
|
|
|
|
nBufferCount++;
|
|
|
|
|
|
|
|
if( getHandle() == -1 ) {
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
|
|
|
|
|
|
|
|
if( nBytesSent <= 0 ) {
|
|
|
|
if( nTimeoutCount == __TIMEOUT_COUNT ) {
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Timeout reached. Exiting before end of image transmission.\n" );
|
|
|
|
nTimeoutCount = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
Sleep(1000);
|
|
|
|
#else
|
|
|
|
sleep(1);
|
|
|
|
#endif
|
|
|
|
nTimeoutCount++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, ">>>>>>>>> (" << nBufferCount << ") BLOCK STEP " << nStep << " - IMAGELEN " << nImageLen << " - BLOCKSIZE " << nBlockSize << " - SENT " << nBytesSent );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate remaining image.
|
|
|
|
*/
|
|
|
|
if( ( nStep + nBlockSize ) >= nImageLen ) {
|
|
|
|
nBlockSize = ( nImageLen - nStep );
|
|
|
|
nStep += nBlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
nStep += nBytesSent;
|
|
|
|
nTimeoutCount = 0;
|
|
|
|
#ifdef _WIN32
|
|
|
|
Sleep(1);
|
|
|
|
#else
|
|
|
|
usleep( 1000 );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
|
|
|
|
|
2002-01-16 23:02:52 +00:00
|
|
|
} else {
|
2006-10-24 17:51:56 +00:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "!!! NO IMAGE !!! info -> numbytes = " << nImageLen );
|
2002-01-16 23:02:52 +00:00
|
|
|
}
|
2006-10-24 17:51:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Release JPEG buffer.
|
|
|
|
*/
|
|
|
|
JpgFactory -> destroy();
|
2002-01-16 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer.remove();
|
|
|
|
}
|