]> git.mxchange.org Git - flightgear.git/blob - src/Network/RemoteXMLRequest.hxx
New PathsDialog, for managing locations.
[flightgear.git] / src / Network / RemoteXMLRequest.hxx
1 // RemoteXMLRequest.hxx -- xml http requests
2 //
3 // Written by James Turner
4 //
5 // Copyright (C) 2012  James Turner
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifndef REMOTEXMLREQUEST_HXXH
22 #define REMOTEXMLREQUEST_HXXH
23 #include <simgear/structure/exception.hxx>
24 #include <simgear/io/HTTPMemoryRequest.hxx>
25 class RemoteXMLRequest:
26   public simgear::HTTP::MemoryRequest
27 {
28 public:
29     SGPropertyNode_ptr _complete;
30     SGPropertyNode_ptr _status;
31     SGPropertyNode_ptr _failed;
32     SGPropertyNode_ptr _target;
33
34     RemoteXMLRequest(const std::string& url, SGPropertyNode* targetNode) :
35       simgear::HTTP::MemoryRequest(url),
36       _target(targetNode)
37     {
38     }
39     
40     void setCompletionProp(SGPropertyNode_ptr p)
41     {
42         _complete = p;
43     }
44     
45     void setStatusProp(SGPropertyNode_ptr p)
46     {
47         _status = p;
48     }
49     
50     void setFailedProp(SGPropertyNode_ptr p)
51     {
52         _failed = p;
53     }
54     
55 protected:
56     
57     virtual void onFail()
58     {
59         SG_LOG(SG_IO, SG_INFO, "network level failure in RemoteXMLRequest");
60         if (_failed) {
61             _failed->setBoolValue(true);
62         }
63     }
64     
65     virtual void onDone()
66     {
67         int response = responseCode();
68         bool failed = false;
69         if (response == 200) {
70             try {
71                 const char* buffer = responseBody().c_str();
72                 readProperties(buffer, responseBody().size(), _target, true);
73             } catch (const sg_exception &e) {
74                 SG_LOG(SG_IO, SG_WARN, "parsing XML from remote, failed: " << e.getFormattedMessage());
75                 failed = true;
76                 response = 406; // 'not acceptable', anything better?
77             }
78         } else {
79             failed = true;
80         }
81     // now the response data is output, signal Nasal / listeners
82         if (_complete) _complete->setBoolValue(true);
83         if (_status) _status->setIntValue(response);
84         if (_failed && failed) _failed->setBoolValue(true);
85     }
86 };
87
88
89 #endif //REMOTEXMLREQUEST_HXXH