1 // jpg-httpd.cxx -- FGFS jpg-http interface
3 // Written by Curtis Olson, started June 2001.
5 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Jpeg Image Support added August 2001
8 // by Norman Vine - nhv@cape.com
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <simgear/compiler.h>
33 #include <cstdlib> // atoi() atof()
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/io/iochannel.hxx>
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/props/props.hxx>
41 #include <simgear/io/sg_netChat.hxx>
42 #include <simgear/screen/jpgfactory.hxx>
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
46 #include <Viewer/renderer.hxx>
48 #include "jpg-httpd.hxx"
50 #define __MAX_HTTP_BLOCK_SIZE 4096
51 #define __MAX_STRING_SIZE 2048
52 #define __TIMEOUT_COUNT 5
53 #define __HTTP_GET_STRING "GET "
55 #include <osgUtil/SceneView>
56 #include <osgViewer/Viewer>
57 extern osg::ref_ptr<osgUtil::SceneView> sceneView;
61 /* simple httpd server that makes an hasty stab at following the http
64 //////////////////////////////////////////////////////////////
65 // class HttpdImageChannel
66 //////////////////////////////////////////////////////////////
68 class HttpdImageChannel : public simgear::NetChat
71 simgear::NetBuffer buffer;
72 trJpgFactory *JpgFactory;
76 HttpdImageChannel() : buffer(512) {
78 int nWidth = fgGetInt( "/sim/startup/xsize", 800 );
79 int nHeight = fgGetInt( "/sim/startup/ysize", 600 );
81 setTerminator("\r\n");
82 JpgFactory = new trJpgFactory();
83 int error = JpgFactory -> init( nWidth, nHeight );
86 SG_LOG( SG_IO, SG_ALERT, "Failed to initialize JPEG-factory, error: " << error);
90 ~HttpdImageChannel() {
91 JpgFactory -> destroy();
95 virtual void collectIncomingData (const char* s, int n) {
99 // Handle the actual http request
100 virtual void foundTerminator (void);
103 //////////////////////////////////////////////////////////////
104 // class HttpdImageServer
105 //////////////////////////////////////////////////////////////
107 class HttpdImageServer : private simgear::NetChannel
109 virtual bool writable (void) { return false; }
111 virtual void handleAccept (void) {
112 simgear::IPAddress addr;
113 int handle = accept ( &addr );
114 SG_LOG( SG_IO, SG_INFO, "Client " << addr.getHost() << ":" << addr.getPort() << " connected" );
116 HttpdImageChannel *hc = new HttpdImageChannel;
117 hc->setHandle ( handle );
119 poller.addChannel( hc );
122 simgear::NetChannelPoller poller;
125 HttpdImageServer ( int port )
129 SG_LOG( SG_IO, SG_ALERT, "Failed to open HttpdImage port.");
133 if (0 != bind( "", port ))
135 SG_LOG( SG_IO, SG_ALERT, "Failed to bind HttpdImage port.");
139 if (0 != listen( 5 ))
141 SG_LOG( SG_IO, SG_ALERT, "Failed to listen on HttpdImage port.");
145 poller.addChannel(this);
146 SG_LOG(SG_IO, SG_ALERT, "HttpdImage server started on port " << port);
155 //////////////////////////////////////////////////////////////
157 //////////////////////////////////////////////////////////////
159 FGJpegHttpd::FGJpegHttpd( int p ) :
165 FGJpegHttpd::~FGJpegHttpd()
170 bool FGJpegHttpd::open() {
171 if ( is_enabled() ) {
172 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
173 << "is already in use, ignoring" );
177 imageServer = new HttpdImageServer( port );
179 set_hz( 5 ); // default to processing requests @ 5Hz
186 bool FGJpegHttpd::process() {
193 bool FGJpegHttpd::close() {
199 // Handle http GET requests
200 void HttpdImageChannel :: foundTerminator( void ) {
205 char szResponse[__MAX_STRING_SIZE];
206 char *pRequest = buffer.getData();
209 int nTimeoutCount = 0;
210 int nBufferCount = 0;
215 if ( strstr( pRequest, __HTTP_GET_STRING ) != NULL )
218 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< HTTP Request : " << pRequest );
220 double left, right, bottom, top, zNear, zFar;
221 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
222 viewer->getCamera()->getProjectionMatrixAsFrustum(left, right,
225 JpgFactory->setFrustum( left, right, bottom, top, zNear, zFar );
227 nImageLen = JpgFactory -> render();
228 nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
232 strcpy( szResponse, "HTTP/1.1 200 OK" );
233 strcat( szResponse, getTerminator() );
234 strcat( szResponse, "Content-Type: image/jpeg" );
235 strcat( szResponse, getTerminator() );
237 SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
238 sprintf( szTemp, "Content-Length: %d", nImageLen );
239 strcat( szResponse, szTemp );
241 strcat( szResponse, getTerminator() );
242 strcat( szResponse, "Connection: close" );
243 strcat( szResponse, getTerminator() );
244 strcat( szResponse, getTerminator() );
246 if( getHandle() == -1 )
248 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
250 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
254 if( send( ( char * ) szResponse, strlen( szResponse ) ) <= 0 )
256 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Error to send HTTP response. Ignoring request.\n" );
258 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
263 * Send block with size defined by __MAX_HTTP_BLOCK_SIZE
265 while( nStep <= nImageLen )
269 if( getHandle() == -1 )
271 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
275 nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
277 if( nBytesSent <= 0 )
279 if( nTimeoutCount == __TIMEOUT_COUNT )
281 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Timeout reached. Exiting before end of image transmission.\n" );
286 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
297 SG_LOG( SG_IO, SG_DEBUG, ">>>>>>>>> (" << nBufferCount << ") BLOCK STEP " << nStep << " - IMAGELEN " << nImageLen << " - BLOCKSIZE " << nBlockSize << " - SENT " << nBytesSent );
300 * Calculate remaining image.
302 if( ( nStep + nBlockSize ) >= nImageLen )
304 nBlockSize = ( nImageLen - nStep );
317 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
320 SG_LOG( SG_IO, SG_ALERT, "Failed to generate JPEG image data. Error: " << nImageLen);
324 * Release JPEG buffer.
326 JpgFactory -> destroy();