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 );
122 HttpdImageServer ( int port )
126 SG_LOG( SG_IO, SG_ALERT, "Failed to open HttpdImage port.");
130 if (0 != bind( "", port ))
132 SG_LOG( SG_IO, SG_ALERT, "Failed to bind HttpdImage port.");
136 if (0 != listen( 5 ))
138 SG_LOG( SG_IO, SG_ALERT, "Failed to listen on HttpdImage port.");
142 SG_LOG(SG_IO, SG_ALERT, "HttpdImage server started on port " << port);
147 //////////////////////////////////////////////////////////////
149 //////////////////////////////////////////////////////////////
151 FGJpegHttpd::FGJpegHttpd( int p ) :
157 FGJpegHttpd::~FGJpegHttpd()
162 bool FGJpegHttpd::open() {
163 if ( is_enabled() ) {
164 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
165 << "is already in use, ignoring" );
169 imageServer = new HttpdImageServer( port );
171 set_hz( 5 ); // default to processing requests @ 5Hz
178 bool FGJpegHttpd::process() {
179 simgear::NetChannel::poll();
185 bool FGJpegHttpd::close() {
191 // Handle http GET requests
192 void HttpdImageChannel :: foundTerminator( void ) {
197 char szResponse[__MAX_STRING_SIZE];
198 char *pRequest = buffer.getData();
201 int nTimeoutCount = 0;
202 int nBufferCount = 0;
207 if ( strstr( pRequest, __HTTP_GET_STRING ) != NULL )
210 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< HTTP Request : " << pRequest );
212 double left, right, bottom, top, zNear, zFar;
213 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
214 viewer->getCamera()->getProjectionMatrixAsFrustum(left, right,
217 JpgFactory->setFrustum( left, right, bottom, top, zNear, zFar );
219 nImageLen = JpgFactory -> render();
220 nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
224 strcpy( szResponse, "HTTP/1.1 200 OK" );
225 strcat( szResponse, getTerminator() );
226 strcat( szResponse, "Content-Type: image/jpeg" );
227 strcat( szResponse, getTerminator() );
229 SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
230 sprintf( szTemp, "Content-Length: %d", nImageLen );
231 strcat( szResponse, szTemp );
233 strcat( szResponse, getTerminator() );
234 strcat( szResponse, "Connection: close" );
235 strcat( szResponse, getTerminator() );
236 strcat( szResponse, getTerminator() );
238 if( getHandle() == -1 )
240 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
242 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
246 if( send( ( char * ) szResponse, strlen( szResponse ) ) <= 0 )
248 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Error to send HTTP response. Ignoring request.\n" );
250 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
255 * Send block with size defined by __MAX_HTTP_BLOCK_SIZE
257 while( nStep <= nImageLen )
261 if( getHandle() == -1 )
263 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
267 nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
269 if( nBytesSent <= 0 )
271 if( nTimeoutCount == __TIMEOUT_COUNT )
273 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Timeout reached. Exiting before end of image transmission.\n" );
278 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
289 SG_LOG( SG_IO, SG_DEBUG, ">>>>>>>>> (" << nBufferCount << ") BLOCK STEP " << nStep << " - IMAGELEN " << nImageLen << " - BLOCKSIZE " << nBlockSize << " - SENT " << nBytesSent );
292 * Calculate remaining image.
294 if( ( nStep + nBlockSize ) >= nImageLen )
296 nBlockSize = ( nImageLen - nStep );
309 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
312 SG_LOG( SG_IO, SG_ALERT, "Failed to generate JPEG image data. Error: " << nImageLen);
316 * Release JPEG buffer.
318 JpgFactory -> destroy();