]> git.mxchange.org Git - flightgear.git/blob - src/Network/jpg-httpd.cxx
- rename fgcommand "set-mouse" to "set-cursor"
[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 #include <Main/renderer.hxx>
46
47 #include "jpg-httpd.hxx"
48
49 #define __MAX_HTTP_BLOCK_SIZE       4096
50 #define __MAX_STRING_SIZE           2048
51 #define __TIMEOUT_COUNT             5
52 #define __HTTP_GET_STRING           "GET "
53
54 #include <osgUtil/SceneView>
55 extern osg::ref_ptr<osgUtil::SceneView> sceneView;
56
57 SG_USING_STD(string);
58
59
60 bool FGJpegHttpd::open() {
61     if ( is_enabled() ) {
62         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
63                 << "is already in use, ignoring" );
64         return false;
65     }
66
67     imageServer = new HttpdImageServer( port );
68     
69     set_hz( 5 );                // default to processing requests @ 5Hz
70     set_enabled( true );
71
72     return true;
73 }
74
75
76 bool FGJpegHttpd::process() {
77     netChannel::poll();
78
79     return true;
80 }
81
82
83 bool FGJpegHttpd::close() {
84     delete imageServer;
85
86     return true;
87 }
88
89 // Handle http GET requests
90 void HttpdImageChannel :: foundTerminator( void ) {
91
92     closeWhenDone();
93
94     char      szTemp[256];
95     char      szResponse[__MAX_STRING_SIZE];
96     char      *pRequest     = buffer.getData();
97     int       nStep         = 0;
98     int       nBytesSent    = 0;
99     int       nTimeoutCount = 0;
100     int       nBufferCount  = 0;
101     int       nImageLen;
102     int       nBlockSize;
103
104
105     if ( strstr( pRequest, __HTTP_GET_STRING ) != NULL ) {
106         
107         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< HTTP Request : " << pRequest );
108
109         double left, right, bottom, top, zNear, zFar;
110         osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
111         viewer->getCamera()->getProjectionMatrixAsFrustum(left, right,
112                                                           bottom, top,
113                                                           zNear, zFar);
114         JpgFactory->setFrustum( left, right, bottom, top, zNear, zFar );
115
116         nImageLen  = JpgFactory -> render();
117         nBlockSize = ( nImageLen < __MAX_HTTP_BLOCK_SIZE ? nImageLen : __MAX_HTTP_BLOCK_SIZE );
118
119         if( nImageLen ) {
120             strcpy( szResponse, "HTTP/1.1 200 OK" );
121             strcat( szResponse, getTerminator() );
122             strcat( szResponse, "Content-Type: image/jpeg" );
123             strcat( szResponse, getTerminator() );
124
125             SG_LOG( SG_IO, SG_DEBUG, "info->numbytes = " << nImageLen );
126             sprintf( szTemp, "Content-Length: %d", nImageLen );
127             strcat( szResponse, szTemp );
128
129             strcat( szResponse, getTerminator() );
130             strcat( szResponse, "Connection: close" );
131             strcat( szResponse, getTerminator() );
132             strcat( szResponse, getTerminator() );
133
134             if( getHandle() == -1 )  {
135                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
136                 buffer.remove();
137                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
138                 return;
139             }
140
141             if( send( ( char * ) szResponse, strlen( szResponse ) ) <= 0 )  {
142                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Error to send HTTP response. Ignoring request.\n" );
143                 buffer.remove();
144                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
145                 return;
146             }
147
148             /*
149              * Send block with size defined by __MAX_HTTP_BLOCK_SIZE
150              */
151             while( nStep <= nImageLen ) {
152                 nBufferCount++;
153
154                 if( getHandle() == -1 )  {
155                     SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Invalid socket handle. Ignoring request.\n" );
156                     break;
157                 }
158
159                 nBytesSent = send( ( char * ) JpgFactory -> data() + nStep, nBlockSize );
160
161                 if( nBytesSent <= 0 )  {
162                     if( nTimeoutCount == __TIMEOUT_COUNT )  {
163                         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Timeout reached. Exiting before end of image transmission.\n" );
164                         nTimeoutCount = 0;
165                         break;
166                     }
167
168                 SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< Zero bytes sent.\n" );
169
170 #ifdef _WIN32
171                 Sleep(1000);
172 #else
173                 sleep(1);
174 #endif
175                 nTimeoutCount++;
176                 continue;
177             }
178
179             SG_LOG( SG_IO, SG_DEBUG, ">>>>>>>>> (" << nBufferCount << ") BLOCK STEP " << nStep << " - IMAGELEN " << nImageLen << " - BLOCKSIZE " << nBlockSize << " - SENT " << nBytesSent );
180
181             /*
182              * Calculate remaining image.
183              */
184             if( ( nStep + nBlockSize ) >= nImageLen ) {
185                 nBlockSize = ( nImageLen - nStep );
186                 nStep += nBlockSize;
187             }
188
189             nStep += nBytesSent;
190             nTimeoutCount = 0;
191 #ifdef _WIN32
192             Sleep(1);
193 #else
194             usleep( 1000 );
195 #endif
196         }
197
198         SG_LOG( SG_IO, SG_DEBUG, "<<<<<<<<< End of image Transmission.\n" );
199
200         } else {
201             SG_LOG( SG_IO, SG_DEBUG, "!!! NO IMAGE !!!  info -> numbytes = " << nImageLen );
202         }
203
204         /*
205          * Release JPEG buffer.
206          */
207         JpgFactory -> destroy();
208     }
209
210     buffer.remove();
211 }