]> git.mxchange.org Git - flightgear.git/blob - src/Network/jpg-httpd.cxx
f8ec013485a1ab299256d88599a2999218a70a9c
[flightgear.git] / src / Network / jpg-httpd.cxx
1 // httpd.hxx -- FGFS http property manager interface / external script
2 //              and control class
3 //
4 // Written by Curtis Olson, started June 2001.
5 //
6 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // Jpeg Image Support added August 2001
9 //  by Norman Vine - nhv@cape.com
10 //
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.
15 //
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.
20 //
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.
24 //
25 // $Id$
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/compiler.h>
33
34 #include <stdlib.h>             // atoi() atof()
35
36 #include STL_STRING
37
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>
42
43 #include <Main/fg_props.hxx>
44 #include <Main/globals.hxx>
45
46 #include "jpg-httpd.hxx"
47
48 #define __MAX_HTTP_BLOCK_SIZE       4096
49 #define __MAX_STRING_SIZE           2048
50 #define __TIMEOUT_COUNT             5
51 #define __HTTP_GET_STRING           "GET "
52
53 #include <osg/CameraNode>
54 extern osg::ref_ptr<osg::CameraNode> mSceneCamera;
55
56 SG_USING_STD(string);
57
58
59 bool FGJpegHttpd::open() {
60     if ( is_enabled() ) {
61         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
62                 << "is already in use, ignoring" );
63         return false;
64     }
65
66     imageServer = new HttpdImageServer( port );
67     
68     set_hz( 5 );                // default to processing requests @ 5Hz
69     set_enabled( true );
70
71     return true;
72 }
73
74
75 bool FGJpegHttpd::process() {
76     netChannel::poll();
77
78     return true;
79 }
80
81
82 bool FGJpegHttpd::close() {
83     delete imageServer;
84
85     return true;
86 }
87
88 // Handle http GET requests
89 void HttpdImageChannel :: foundTerminator( void ) {
90
91     closeWhenDone();
92
93     char      szTemp[256];
94     char      szResponse[__MAX_STRING_SIZE];
95     char      *pRequest     = buffer.getData();
96     int       nStep         = 0;
97     int       nBytesSent    = 0;
98     int       nTimeoutCount = 0;
99     int       nBufferCount  = 0;
100     int       nImageLen;
101     int       nBlockSize;
102
103
104     if ( strstr( pRequest, __HTTP_GET_STRING ) != NULL ) {
105         
106         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< HTTP Request : " << pRequest );
107
108         double left, right, bottom, top, zNear, zFar;
109         mSceneCamera->getProjectionMatrixAsFrustum( left, right, bottom, top, zNear, zFar );
110         JpgFactory->setFrustum( left, right, bottom, top, zNear, zFar );
111
112         nImageLen  = JpgFactory -> render();
113         nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
114
115         if( nImageLen ) {
116             strcpy( szResponse, "HTTP/1.1 200 OK" );
117             strcat( szResponse, getTerminator() );
118             strcat( szResponse, "Content-Type: image/jpeg" );
119             strcat( szResponse, getTerminator() );
120
121             SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
122             sprintf( szTemp, "Content-Length: %d", nImageLen );
123             strcat( szResponse, szTemp );
124
125             strcat( szResponse, getTerminator() );
126             strcat( szResponse, "Connection: close" );
127             strcat( szResponse, getTerminator() );
128             strcat( szResponse, getTerminator() );
129
130             if( getHandle() == -1 )  {
131                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
132                 buffer.remove();
133                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
134                 return;
135             }
136
137             if( send( ( char * ) szResponse, strlen( szResponse ) ) <= 0 )  {
138                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Error to send HTTP response. Ignoring request.\n" );
139                 buffer.remove();
140                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
141                 return;
142             }
143
144             /*
145              * Send block with size defined by __MAX_HTTP_BLOCK_SIZE
146              */
147             while( nStep <= nImageLen ) {
148                 nBufferCount++;
149
150                 if( getHandle() == -1 )  {
151                     SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
152                     break;
153                 }
154
155                 nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
156
157                 if( nBytesSent <= 0 )  {
158                     if( nTimeoutCount == __TIMEOUT_COUNT )  {
159                         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Timeout reached. Exiting before end of image transmission.\n" );
160                         nTimeoutCount = 0;
161                         break;
162                     }
163
164                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
165
166 #ifdef _WIN32
167                 Sleep(1000);
168 #else
169                 sleep(1);
170 #endif
171                 nTimeoutCount++;
172                 continue;
173             }
174
175             SG_LOG( SG_IO, SG_DEBUG, ">>>>>>>>> (" << nBufferCount << ") BLOCK STEP " << nStep << " - IMAGELEN " << nImageLen << " - BLOCKSIZE " << nBlockSize << " - SENT " << nBytesSent );
176
177             /*
178              * Calculate remaining image.
179              */
180             if( ( nStep + nBlockSize ) >= nImageLen ) {
181                 nBlockSize = ( nImageLen - nStep );
182                 nStep += nBlockSize;
183             }
184
185             nStep += nBytesSent;
186             nTimeoutCount = 0;
187 #ifdef _WIN32
188             Sleep(1);
189 #else
190             usleep( 1000 );
191 #endif
192         }
193
194         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
195
196         } else {
197             SG_LOG( SG_IO, SG_DEBUG, "!!! NO IMAGE !!!  info -> numbytes = " << nImageLen );
198         }
199
200         /*
201          * Release JPEG buffer.
202          */
203         JpgFactory -> destroy();
204     }
205
206     buffer.remove();
207 }