2012-11-14 23:44:35 +01:00
|
|
|
// jpg-httpd.cxx -- FGFS jpg-http interface
|
2002-01-16 23:02:52 +00:00
|
|
|
//
|
|
|
|
// 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>
|
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
#include <cstdlib> // atoi() atof()
|
2002-01-16 23:02:52 +00:00
|
|
|
|
2010-10-24 15:21:26 +02:00
|
|
|
#include <cstring>
|
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>
|
2012-11-14 23:44:35 +01:00
|
|
|
#include <simgear/io/sg_netChat.hxx>
|
|
|
|
#include <simgear/screen/jpgfactory.hxx>
|
2002-01-16 23:02:52 +00:00
|
|
|
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
#include <Main/globals.hxx>
|
2012-04-25 23:28:00 +02:00
|
|
|
#include <Viewer/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
|
|
|
|
2012-11-14 23:44:35 +01:00
|
|
|
/* simple httpd server that makes an hasty stab at following the http
|
|
|
|
1.1 rfc. */
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// class HttpdImageChannel
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class HttpdImageChannel : public simgear::NetChat
|
|
|
|
{
|
|
|
|
|
|
|
|
simgear::NetBuffer buffer;
|
|
|
|
trJpgFactory *JpgFactory;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
HttpdImageChannel() : buffer(512) {
|
|
|
|
|
|
|
|
int nWidth = fgGetInt( "/sim/startup/xsize", 800 );
|
|
|
|
int nHeight = fgGetInt( "/sim/startup/ysize", 600 );
|
|
|
|
|
|
|
|
setTerminator("\r\n");
|
|
|
|
JpgFactory = new trJpgFactory();
|
|
|
|
int error = JpgFactory -> init( nWidth, nHeight );
|
|
|
|
if (0 != error)
|
|
|
|
{
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Failed to initialize JPEG-factory, error: " << error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~HttpdImageChannel() {
|
|
|
|
JpgFactory -> destroy();
|
|
|
|
delete JpgFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void collectIncomingData (const char* s, int n) {
|
|
|
|
buffer.append(s,n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the actual http request
|
|
|
|
virtual void foundTerminator (void);
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// class HttpdImageServer
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class HttpdImageServer : private simgear::NetChannel
|
|
|
|
{
|
|
|
|
virtual bool writable (void) { return false; }
|
|
|
|
|
|
|
|
virtual void handleAccept (void) {
|
|
|
|
simgear::IPAddress addr;
|
|
|
|
int handle = accept ( &addr );
|
|
|
|
SG_LOG( SG_IO, SG_INFO, "Client " << addr.getHost() << ":" << addr.getPort() << " connected" );
|
|
|
|
|
|
|
|
HttpdImageChannel *hc = new HttpdImageChannel;
|
|
|
|
hc->setHandle ( handle );
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
HttpdImageServer ( int port )
|
|
|
|
{
|
|
|
|
if (!open())
|
|
|
|
{
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Failed to open HttpdImage port.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != bind( "", port ))
|
|
|
|
{
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Failed to bind HttpdImage port.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != listen( 5 ))
|
|
|
|
{
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Failed to listen on HttpdImage port.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_LOG(SG_IO, SG_ALERT, "HttpdImage server started on port " << port);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
// class FGJpegHttpd
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
FGJpegHttpd::FGJpegHttpd( int p ) :
|
|
|
|
port(p),
|
|
|
|
imageServer(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FGJpegHttpd::~FGJpegHttpd()
|
|
|
|
{
|
|
|
|
delete imageServer;
|
|
|
|
}
|
2002-01-16 23:02:52 +00:00
|
|
|
|
|
|
|
bool FGJpegHttpd::open() {
|
|
|
|
if ( is_enabled() ) {
|
2012-11-14 23:27:18 +01:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
|
|
|
|
<< "is already in use, ignoring" );
|
|
|
|
return false;
|
2002-01-16 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-11-14 23:44:35 +01:00
|
|
|
imageServer = NULL;
|
2002-01-16 23:02:52 +00:00
|
|
|
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
|
|
|
|
2012-11-14 23:27:18 +01: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;
|
2012-11-14 23:27:18 +01: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();
|
2012-11-14 23:27:18 +01:00
|
|
|
nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
if( nImageLen )
|
|
|
|
{
|
|
|
|
strcpy( szResponse, "HTTP/1.1 200 OK" );
|
|
|
|
strcat( szResponse, getTerminator() );
|
|
|
|
strcat( szResponse, "Content-Type: image/jpeg" );
|
2006-10-24 17:51:56 +00:00
|
|
|
strcat( szResponse, getTerminator() );
|
|
|
|
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
|
|
|
|
sprintf( szTemp, "Content-Length: %d", nImageLen );
|
2012-11-14 23:27:18 +01:00
|
|
|
strcat( szResponse, szTemp );
|
2006-10-24 17:51:56 +00:00
|
|
|
|
|
|
|
strcat( szResponse, getTerminator() );
|
2012-11-14 23:27:18 +01:00
|
|
|
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 )
|
|
|
|
{
|
2006-10-24 17:51:56 +00:00
|
|
|
nBufferCount++;
|
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
if( getHandle() == -1 )
|
|
|
|
{
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
|
|
|
|
break;
|
|
|
|
}
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
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;
|
|
|
|
}
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
|
2006-10-24 17:51:56 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-11-14 23:27:18 +01:00
|
|
|
Sleep(1000);
|
2006-10-24 17:51:56 +00:00
|
|
|
#else
|
2012-11-14 23:27:18 +01:00
|
|
|
sleep(1);
|
2006-10-24 17:51:56 +00:00
|
|
|
#endif
|
2012-11-14 23:27:18 +01:00
|
|
|
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;
|
2006-10-24 17:51:56 +00:00
|
|
|
#ifdef _WIN32
|
2012-11-14 23:27:18 +01:00
|
|
|
Sleep(1);
|
2006-10-24 17:51:56 +00:00
|
|
|
#else
|
2012-11-14 23:27:18 +01:00
|
|
|
usleep( 1000 );
|
2006-10-24 17:51:56 +00:00
|
|
|
#endif
|
2012-11-14 23:27:18 +01:00
|
|
|
}
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2002-01-16 23:02:52 +00:00
|
|
|
} else {
|
2012-11-14 23:44:35 +01:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Failed to generate JPEG image data. Error: " << nImageLen);
|
2002-01-16 23:02:52 +00:00
|
|
|
}
|
2006-10-24 17:51:56 +00:00
|
|
|
|
2012-11-14 23:27:18 +01:00
|
|
|
/*
|
|
|
|
* Release JPEG buffer.
|
|
|
|
*/
|
|
|
|
JpgFactory -> destroy();
|
2002-01-16 23:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer.remove();
|
|
|
|
}
|