FlightGear next
RemoteXMLRequest.hxx
Go to the documentation of this file.
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
24#include <simgear/structure/exception.hxx>
25#include <simgear/io/HTTPMemoryRequest.hxx>
26#include <simgear/props/props_io.hxx>
27
29 public simgear::HTTP::MemoryRequest
30{
31public:
32 SGPropertyNode_ptr _complete;
33 SGPropertyNode_ptr _status;
34 SGPropertyNode_ptr _failed;
35 SGPropertyNode_ptr _target;
36
37 RemoteXMLRequest(const std::string& url, SGPropertyNode* targetNode) :
38 simgear::HTTP::MemoryRequest(url),
39 _target(targetNode)
40 {
41 }
42
43 void setCompletionProp(SGPropertyNode_ptr p)
44 {
45 _complete = p;
46 }
47
48 void setStatusProp(SGPropertyNode_ptr p)
49 {
50 _status = p;
51 }
52
53 void setFailedProp(SGPropertyNode_ptr p)
54 {
55 _failed = p;
56 }
57
58protected:
59
60 virtual void onFail()
61 {
62 SG_LOG(SG_IO, SG_INFO, "network level failure in RemoteXMLRequest");
63 if (_failed) {
64 _failed->setBoolValue(true);
65 }
66 }
67
68 virtual void onDone()
69 {
70 int response = responseCode();
71 bool failed = false;
72 if (response == 200) {
73 try {
74 const char* buffer = responseBody().c_str();
75 readProperties(buffer, responseBody().size(), _target, true);
76 } catch (const sg_exception &e) {
77 SG_LOG(SG_IO, SG_WARN, "parsing XML from remote, failed: " << e.getFormattedMessage());
78 failed = true;
79 response = 406; // 'not acceptable', anything better?
80 }
81 } else {
82 failed = true;
83 }
84 // now the response data is output, signal Nasal / listeners
85 if (_complete) _complete->setBoolValue(true);
86 if (_status) _status->setIntValue(response);
87 if (_failed && failed) _failed->setBoolValue(true);
88 }
89};
90
91
92#endif //REMOTEXMLREQUEST_HXXH
#define p(x)
SGPropertyNode_ptr _complete
virtual void onFail()
void setStatusProp(SGPropertyNode_ptr p)
RemoteXMLRequest(const std::string &url, SGPropertyNode *targetNode)
SGPropertyNode_ptr _target
SGPropertyNode_ptr _status
void setCompletionProp(SGPropertyNode_ptr p)
virtual void onDone()
void setFailedProp(SGPropertyNode_ptr p)
SGPropertyNode_ptr _failed