ActionResponse.h
Go to the documentation of this file.
1 /****
2  * ActionResponse.h
3  *
4  * Copyright 2020 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming UPnP Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Envelope.h"
24 #include "LinkedItemList.h"
25 
27 
28 namespace UPnP
29 {
30 class ActionRequest;
31 
35 class ActionResponse : public LinkedItem
36 {
37 public:
38  /*
39  * Stream to support deferred responses
40  */
41  class Stream : public MemoryDataStream
42  {
43  public:
44  Stream(HttpServerConnection& connection, Envelope* envelope) : connection(connection), envelope(envelope)
45  {
46  }
47 
49 
50  bool isFinished() override
51  {
52  return done && MemoryDataStream::isFinished();
53  }
54 
55  MimeType getMimeType() const override
56  {
57  return MimeType::XML;
58  }
59 
60  void complete(Error err);
61 
63  {
64  return envelope;
65  }
66 
67  private:
68  friend ActionResponse;
69 
70  HttpServerConnection& connection;
71  Envelope* envelope;
72  LinkedItemList responses;
73  bool done{false};
74  };
75 
76  ActionResponse(const ActionResponse& response) : ActionResponse(response.envelope, response.stream)
77  {
78  }
79 
80  ActionResponse(const ActionRequest& request);
81 
83 
85  {
86  if(stream != nullptr) {
87  stream->responses.add(this);
88  }
89  }
90 
91  template <typename T> T getArg(const FlashString& name) const
92  {
93  T value;
94  envelope.getArg(name, value);
95  return value;
96  }
97 
98  template <typename T> void setArg(const FlashString& name, const T& value) const
99  {
100  envelope.addArg(name, value);
101  }
102 
104  {
105  return envelope.actionName();
106  }
107 
109  {
110  return envelope.fault();
111  }
112 
113  /*
114  * @brief Complete host action by sending response
115  * @param err Response error
116  * @retval bool true if response was completed, false if connection's gone
117  */
118  bool complete(Error err) const;
119 
120 protected:
123 };
124 
125 } // namespace UPnP
describes a counted string stored in flash memory
Definition: String.hpp:174
Definition: HttpServerConnection.h:34
Read/write stream using expandable memory buffer.
Definition: MemoryDataStream.h:27
bool isFinished() override
Check if all data has been read.
Definition: MemoryDataStream.h:78
The String class.
Definition: WString.h:137
Definition: ActionRequest.h:27
Definition: ActionResponse.h:42
Envelope * getEnvelope() const
Definition: ActionResponse.h:62
Stream(HttpServerConnection &connection, Envelope *envelope)
Definition: ActionResponse.h:44
MimeType getMimeType() const override
Get MIME type for stream content.
Definition: ActionResponse.h:55
bool isFinished() override
Check if all data has been read.
Definition: ActionResponse.h:50
Class to handle action requests and responses.
Definition: ActionResponse.h:36
ActionResponse(const ActionRequest &request)
Stream * stream
Definition: ActionResponse.h:122
String actionName() const
Definition: ActionResponse.h:103
Envelope::Fault fault() const
Definition: ActionResponse.h:108
Envelope & envelope
Definition: ActionResponse.h:121
ActionResponse(const ActionResponse &response)
Definition: ActionResponse.h:76
T getArg(const FlashString &name) const
Definition: ActionResponse.h:91
ActionResponse(Envelope &envelope, Stream *stream)
Definition: ActionResponse.h:84
bool complete(Error err) const
void setArg(const FlashString &name, const T &value) const
Definition: ActionResponse.h:98
Definition: Envelope.h:46
Class to manage a SOAP envelope for service request/response.
Definition: Envelope.h:36
String getArg(const String &name) const
Definition: Envelope.h:197
Fault fault()
Definition: Envelope.h:175
bool addArg(const String &name, const String &value)
Definition: Envelope.h:266
String actionName() const
Get the action name.
Definition: Envelope.h:129
Singly-linked list of items.
Definition: LinkedItemList.h:31
bool add(LinkedItem *item)
Base class template for items in a list.
Definition: LinkedItem.h:32
MimeType
Definition: WebConstants.h:53
Definition: ActionRequest.h:25
Error
Definition: Libraries/UPnP/src/include/Network/UPnP/Error.h:45