AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Message.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_MESSAGES_MESSAGE_H_
17 #define ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_MESSAGES_MESSAGE_H_
18 
19 #include <string>
20 
21 #include <rapidjson/document.h>
22 #include <rapidjson/writer.h>
23 #include <rapidjson/stringbuffer.h>
24 
25 #include "MessageInterface.h"
26 
28 const char MSG_PAYLOAD_TAG[] = "payload";
29 
31 const char MSG_WINDOWID_TAG[] = "windowId";
32 
34 const char MSG_ENABLED_TAG[] = "enabled";
35 
37 const char MSG_TOKEN_TAG[] = "token";
38 
40 const char MSG_STATE_TAG[] = "state";
41 
43 const std::string EMPTY_JSON("{}");
44 
45 namespace alexaClientSDK {
46 namespace sampleApplications {
47 namespace ipcServerSampleApp {
48 namespace messages {
49 
53 class Message : public MessageInterface {
54 private:
55  rapidjson::Document mParsedDocument;
56 
57 public:
64  Message(const std::string& nameSpace, const int version, const std::string& name) :
65  MessageInterface(version, nameSpace, name),
66  mParsedDocument(&alloc()) {
67  }
68 
75  Message& addMember(const std::string& name, const std::string& value) {
76  mDocument.AddMember(
77  rapidjson::Value(name.c_str(), mDocument.GetAllocator()).Move(),
78  rapidjson::Value(value.c_str(), mDocument.GetAllocator()).Move(),
79  mDocument.GetAllocator());
80  return *this;
81  }
82 
89  Message& addMember(const std::string& name, unsigned value) {
90  mDocument.AddMember(
91  rapidjson::Value(name.c_str(), mDocument.GetAllocator()).Move(), value, mDocument.GetAllocator());
92  return *this;
93  }
94 
101  Message& addMemberInPayload(const std::string& name, const std::string& value) {
102  mPayload.AddMember(
103  rapidjson::Value(name.c_str(), mDocument.GetAllocator()).Move(),
104  rapidjson::Value(value.c_str(), mDocument.GetAllocator()).Move(),
105  mDocument.GetAllocator());
106  return *this;
107  }
108 
115  Message& addIntegerInPayload(const std::string& name, const int value) {
116  mPayload.AddMember(
117  rapidjson::Value(name.c_str(), mDocument.GetAllocator()).Move(), value, mDocument.GetAllocator());
118  return *this;
119  }
120 
127  Message& addMemberInPayload(const std::string& name, unsigned value) {
128  mPayload.AddMember(
129  rapidjson::Value(name.c_str(), mDocument.GetAllocator()).Move(), value, mDocument.GetAllocator());
130  return *this;
131  }
132 
138  Message& setEnabledInPayload(bool enabled) {
140  return *this;
141  }
142 
149  mPayload.AddMember(
150  MSG_STATE_TAG, rapidjson::Value(state.c_str(), mDocument.GetAllocator()).Move(), mDocument.GetAllocator());
151  return *this;
152  }
153 
160  mPayload.AddMember(MSG_TOKEN_TAG, token, mDocument.GetAllocator());
161  return *this;
162  }
163 
169  Message& setTokenInPayload(unsigned token) {
170  mPayload.AddMember(MSG_TOKEN_TAG, token, mDocument.GetAllocator());
171  return *this;
172  }
173 
180  mDocument.AddMember(MSG_PAYLOAD_TAG, mParsedDocument.Parse(payload), mDocument.GetAllocator());
181  return *this;
182  }
183 
190  mPayload.AddMember(MSG_PAYLOAD_TAG, mParsedDocument.Parse(payload), mDocument.GetAllocator());
191  return *this;
192  }
193 
200  mPayload.AddMember(MSG_WINDOWID_TAG, windowId, mDocument.GetAllocator());
201  return *this;
202  }
203 
209  mDocument.AddMember(MSG_PAYLOAD_TAG, mPayload, mDocument.GetAllocator());
210  return *this;
211  }
212 
219  mDocument.AddMember(MSG_PAYLOAD_TAG, std::move(payload), mDocument.GetAllocator());
220  return *this;
221  }
222 
227  auto alloc() -> decltype(mDocument.GetAllocator()) {
228  return mDocument.GetAllocator();
229  };
230 
235  std::string get() {
236  rapidjson::StringBuffer buffer;
237  buffer.Clear();
238  rapidjson::Writer<
239  rapidjson::StringBuffer,
240  rapidjson::UTF8<>,
241  rapidjson::UTF8<>,
242  rapidjson::CrtAllocator,
243  rapidjson::kWriteNanAndInfFlag>
244  writer(buffer);
245  if (!mDocument.Accept(writer)) {
246  return EMPTY_JSON;
247  }
248  return std::string(buffer.GetString(), buffer.GetSize());
249  }
250 
256  return std::move(mDocument);
257  };
258 };
259 } // namespace messages
260 } // namespace ipcServerSampleApp
261 } // namespace sampleApplications
262 } // namespace alexaClientSDK
263 
264 #endif // ALEXA_CLIENT_SDK_LIBIPCSERVERSAMPLEAPP_INCLUDE_IPCSERVERSAMPLEAPP_MESSAGES_MESSAGE_H_
const char MSG_TOKEN_TAG[]
The token json key in the message.
Definition: Message.h:37
const char MSG_PAYLOAD_TAG[]
The payload json key in the message.
Definition: Message.h:28
const char MSG_PAYLOAD_TAG[]
The payload json key in the message.
Definition: MessageInterface.h:34
Message & addMemberInPayload(const std::string &name, const std::string &value)
Definition: Message.h:101
Message & addMember(const std::string &name, const std::string &value)
Definition: Message.h:75
::std::string string
Definition: gtest-port.h:1097
Message & setParsedPayload(const std::string &payload)
Definition: Message.h:179
const std::string EMPTY_JSON("{}")
String representation of empty json.
const char MSG_WINDOWID_TAG[]
The windowId json key in the message.
Definition: Message.h:31
Message & addMember(const std::string &name, unsigned value)
Definition: Message.h:89
Message(const std::string &nameSpace, const int version, const std::string &name)
Definition: Message.h:64
auto alloc() -> decltype(mDocument.GetAllocator())
Definition: Message.h:227
Message & setEnabledInPayload(bool enabled)
Definition: Message.h:138
Message & setPayload(rapidjson::Value &&payload)
Definition: Message.h:218
bool Value(const T &value, M matcher)
Definition: gmock-matchers.h:4347
const char MSG_STATE_TAG[]
The state json key in the message.
Definition: Message.h:40
rapidjson::Document mDocument
Definition: MessageInterface.h:56
Message & addIntegerInPayload(const std::string &name, const int value)
Definition: Message.h:115
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
const char MSG_ENABLED_TAG[]
The enabled json key in the message.
Definition: Message.h:34
Message & setTokenInPayload(unsigned token)
Definition: Message.h:169
Message & setStateInPayload(const std::string &state)
Definition: Message.h:148
Message & addMemberInPayload(const std::string &name, unsigned value)
Definition: Message.h:127
Message & setParsedPayloadInPayload(const std::string &payload)
Definition: Message.h:189
Message & setTokenInPayload(const std::string &token)
Definition: Message.h:159
const T & move(const T &t)
Definition: gtest-port.h:1317
Message & setWindowIdInPayload(const std::string &windowId)
Definition: Message.h:199

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0