]> git.mxchange.org Git - flightgear.git/blob - src/Network/jpg-httpd.cxx
Erik Hofman:
[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 #include STL_STRSTREAM
38
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/io/iochannel.hxx>
41 #include <simgear/math/sg_types.hxx>
42 #include <simgear/misc/props.hxx>
43
44 #include <Main/fg_props.hxx>
45 #include <Main/globals.hxx>
46
47 #include "jpg-httpd.hxx"
48
49 SG_USING_STD(string);
50 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
51 SG_USING_STD(cout);
52 SG_USING_STD(istrstream);
53 #endif
54
55
56 bool FGJpegHttpd::open() {
57     if ( is_enabled() ) {
58         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
59                 << "is already in use, ignoring" );
60         return false;
61     }
62
63     imageServer = new HttpdImageServer( port );
64     
65     set_hz( 5 );                // default to processing requests @ 5Hz
66     set_enabled( true );
67
68     return true;
69 }
70
71
72 bool FGJpegHttpd::process() {
73     netChannel::poll();
74
75     return true;
76 }
77
78
79 bool FGJpegHttpd::close() {
80     delete imageServer;
81
82     return true;
83 }
84
85
86 // Handle http GET requests
87 void HttpdImageChannel::foundTerminator (void) {
88
89     closeWhenDone ();
90
91     string response;
92
93     const string s = buffer.getData();
94     if ( s.find( "GET " ) == 0 ) {
95         
96         printf("echo: %s\n", s.c_str());
97
98         int ImageLen = JpgFactory->render();
99
100         if( ImageLen ) {
101             response = "HTTP/1.1 200 OK";
102             response += getTerminator();
103             response += "Content-Type: image/jpeg";
104             response += getTerminator();
105             push( response.c_str() );
106
107             char ctmp[256];
108             printf( "info->numbytes = %d\n", ImageLen );
109             sprintf( ctmp, "Content-Length: %d", ImageLen );
110             push( ctmp );
111
112             response = getTerminator();
113             response += "Connection: close";
114             response += getTerminator();
115             response += getTerminator();
116             push( response.c_str() );
117
118             /* can't use strlen on binary data */
119             bufferSend ( (char *)JpgFactory->data(), ImageLen ) ;
120         } else {
121             printf("!!! NO IMAGE !!!\n\tinfo->numbytes = %d\n", ImageLen );
122         }
123     }
124
125     buffer.remove();
126 }