-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
157 lines (137 loc) · 5.44 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <aws/crt/Api.h>
#include <aws/ipc/GGIpcClient.h>
#include <cstdio>
#include <iostream>
using namespace Aws::Eventstreamrpc;
class TestLifecycleHandler : public ConnectionLifecycleHandler {
public:
TestLifecycleHandler() {
isConnected = false;
lastErrorCode = AWS_OP_ERR;
}
void OnConnectCallback() override {
std::cout << "OnConnectCallback" << std::endl;
isConnected = true;
}
void OnDisconnectCallback(int errorCode) override {
std::cout << "OnDisconnectCallback: " << errorCode << std::endl;
lastErrorCode = errorCode;
}
bool OnErrorCallback(int errorCode) override {
std::cout << "OnErrorCallback: " << errorCode << std::endl;
std::cout << aws_error_str(errorCode) << std::endl;
lastErrorCode = errorCode;
return true;
}
public:
int isConnected;
int lastErrorCode;
};
class CustomSubscribeHandler : public Ipc::SubscribeToTopicStreamHandler {
void OnStreamEvent(Ipc::SubscriptionResponseMessage *response) override {
auto binaryMessage = response->GetBinaryMessage();
Aws::Crt::JsonObject serializedResponse;
if (binaryMessage.has_value()) {
binaryMessage.value().SerializeToJsonObject(serializedResponse);
}
std::cout << "This is the response "
<< serializedResponse.View().WriteReadable() << std::endl;
}
};
void test_publish(Aws::Crt::String topic) {
Aws::Crt::Allocator *allocator = Aws::Crt::g_allocator;
Aws::Crt::ApiHandle apiHandle(allocator);
Aws::Crt::Io::TlsContextOptions tlsCtxOptions =
Aws::Crt::Io::TlsContextOptions::InitDefaultClient();
Aws::Crt::Io::TlsContext tlsContext(tlsCtxOptions,
Aws::Crt::Io::TlsMode::CLIENT, allocator);
Aws::Crt::Io::TlsConnectionOptions tlsConnectionOptions =
tlsContext.NewConnectionOptions();
Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator);
UnixSocketResolver unixSocketResolver(eventLoopGroup, 8, allocator);
Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup,
unixSocketResolver, allocator);
clientBootstrap.EnableBlockingShutdown();
TestLifecycleHandler lifecycleHandler;
std::cout << "creating client" << std::endl;
Ipc::GreengrassIpcClient client(clientBootstrap);
std::cout << "attempting connect" << std::endl;
auto connectedStatus =
client.Connect(lifecycleHandler);
std::cout << "get connect status" << std::endl;
connectedStatus.get();
std::cout << "create operation" << std::endl;
// Ipc::PublishToTopicOperation operation = client.NewPublishToTopic();
Aws::Crt::String messageString("Hello World");
Aws::Crt::Vector<uint8_t> messageData(
{messageString.begin(), messageString.end()}, allocator);
Ipc::BinaryMessage binaryMessage(allocator);
binaryMessage.SetMessage(messageData);
Ipc::PublishMessage publishMessage(allocator);
publishMessage.SetBinaryMessage(binaryMessage);
// Ipc::PublishMessage publishMessage(allocator);
std::cout << "create request" << std::endl;
Ipc::PublishToTopicRequest publishRequest(allocator);
publishRequest.SetTopic(topic);
publishRequest.SetPublishMessage(publishMessage);
CustomSubscribeHandler streamHandler;
Ipc::SubscribeToTopicOperation subscribeOperation =
client.NewSubscribeToTopic(streamHandler);
// Ipc::PublishMessage publishMessage(allocator);
Ipc::SubscribeToTopicRequest subscribeRequest(allocator);
subscribeRequest.SetTopic(topic);
std::cout << "subscribing" << std::endl;
auto subscribeActivate =
subscribeOperation.Activate(subscribeRequest, nullptr);
subscribeActivate.wait();
std::cout << "getting response" << std::endl;
auto futureResponse = subscribeOperation.GetOperationResult();
auto response = futureResponse.get();
if (response) {
std::cout << "Sent successfully" << std::endl;
Ipc::SubscribeToTopicResponse *subscribeToTopicResponse =
static_cast<Ipc::SubscribeToTopicResponse *>(
response.GetOperationResponse());
} else {
auto errorType = response.GetResultType();
if (errorType == OPERATION_ERROR) {
/* User can cast this to whatever "Error" type they desire
e.g. auto *error = static_cast<Ipc::UnauthorizedError*>(response.GetOperationError());
Although, normally, we are only concerned about the error message.
*/
auto *error = response.GetOperationError();
std::cout << error->GetMessage().value() << std::endl;
} else {
std::cout << response.GetRpcError() << std::endl;
}
}
auto publishOperation = client.NewPublishToTopic();
std::cout << "publishing" << std::endl;
auto publishActivate = publishOperation.Activate(publishRequest, nullptr);
publishActivate.wait();
std::cout << "getting response" << std::endl;
auto publishFutureResponse = publishOperation.GetOperationResult();
auto publishResponse = publishFutureResponse.get();
if (publishResponse) {
std::cout << "Sent successfully" << std::endl;
} else {
auto errorType = publishResponse.GetResultType();
if (errorType == RPC_ERROR) {
std::cout << publishResponse.GetRpcError() << std::endl;
} else {
auto *error = publishResponse.GetOperationError();
(void)error;
}
}
// Sleep so that we give some time to receive
std::this_thread::sleep_for(std::chrono::seconds(20));
client.Close();
}
int main(int argc, char *argv[]) {
printf("arguments:\n");
for (int i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
test_publish(Aws::Crt::String("topic"));
return 0;
}