]> git.mxchange.org Git - flightgear.git/blob - src/Network/jpg-httpd.cxx
Clean up a couple bugs in the multiengine handling of the net_fdm.hxx
[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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 SG_USING_STD(string);
49
50
51 bool FGJpegHttpd::open() {
52     if ( is_enabled() ) {
53         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
54                 << "is already in use, ignoring" );
55         return false;
56     }
57
58     imageServer = new HttpdImageServer( port );
59     
60     set_hz( 5 );                // default to processing requests @ 5Hz
61     set_enabled( true );
62
63     return true;
64 }
65
66
67 bool FGJpegHttpd::process() {
68     netChannel::poll();
69
70     return true;
71 }
72
73
74 bool FGJpegHttpd::close() {
75     delete imageServer;
76
77     return true;
78 }
79
80
81 // Handle http GET requests
82 void HttpdImageChannel::foundTerminator (void) {
83
84     closeWhenDone ();
85
86     string response;
87
88     const string s = buffer.getData();
89     if ( s.find( "GET " ) == 0 ) {
90         
91         printf("echo: %s\n", s.c_str());
92
93         int ImageLen = JpgFactory->render();
94
95         if( ImageLen ) {
96             response = "HTTP/1.1 200 OK";
97             response += getTerminator();
98             response += "Content-Type: image/jpeg";
99             response += getTerminator();
100             push( response.c_str() );
101
102             char ctmp[256];
103             printf( "info->numbytes = %d\n", ImageLen );
104             sprintf( ctmp, "Content-Length: %d", ImageLen );
105             push( ctmp );
106
107             response = getTerminator();
108             response += "Connection: close";
109             response += getTerminator();
110             response += getTerminator();
111             push( response.c_str() );
112
113             /* can't use strlen on binary data */
114             bufferSend ( (char *)JpgFactory->data(), ImageLen ) ;
115         } else {
116             printf("!!! NO IMAGE !!!\n\tinfo->numbytes = %d\n", ImageLen );
117         }
118     }
119
120     buffer.remove();
121 }