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