SNode.C
Loading...
Searching...
No Matches
jsonclient.cpp File Reference
Include dependency graph for jsonclient.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 51 of file jsonclient.cpp.

51 {
52 core::SNodeC::init(argc, argv);
53
55 using MasterRequest = Client::MasterRequest;
56 using Request = Client::Request;
57 using Response = Client::Response;
58 using SocketAddress = Client::SocketAddress;
59
60 const Client jsonClient(
61 "legacy",
62 [](const std::shared_ptr<MasterRequest>& req) {
63 VLOG(1) << "-- OnRequest";
64 req->method = "POST";
65 req->url = "/index.html";
66 req->type("application/json");
67 req->set("Connection", "close");
68 req->send(
69 R"({"userId":1,"schnitzel":"good","hungry":false})",
70 []([[maybe_unused]] const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
71 VLOG(1) << "-- OnResponse";
72 VLOG(1) << " Status:";
73 VLOG(1) << " " << res->httpVersion;
74 VLOG(1) << " " << res->statusCode;
75 VLOG(1) << " " << res->reason;
76
77 VLOG(1) << " Headers:";
78 for (const auto& [field, value] : res->headers) {
79 VLOG(1) << " " << field + " = " + value;
80 }
81
82 VLOG(1) << " Cookies:";
83 for (const auto& [name, cookie] : res->cookies) {
84 VLOG(1) << " " + name + " = " + cookie.getValue();
85 for (const auto& [option, value] : cookie.getOptions()) {
86 VLOG(1) << " " + option + " = " + value;
87 }
88 }
89
90 res->body.push_back(0);
91 VLOG(1) << " Body:\n----------- start body -----------" << res->body.data() << "------------ end body ------------";
92 },
93 [](const std::shared_ptr<Request>&, const std::string& message) {
94 VLOG(1) << "legacy: Request parse error: " << message;
95 });
96 },
97 []([[maybe_unused]] const std::shared_ptr<MasterRequest>& req) {
98 LOG(INFO) << " -- OnRequestEnd";
99 });
100
101 jsonClient.connect("localhost",
102 8080,
103 [instanceName = jsonClient.getConfig().getInstanceName()](
104 const SocketAddress& socketAddress,
105 const core::socket::State& state) { // example.com:81 simulate connect timeout
106 switch (state) {
107 case core::socket::State::OK:
108 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
109 break;
110 case core::socket::State::DISABLED:
111 VLOG(1) << instanceName << ": disabled";
112 break;
113 case core::socket::State::ERROR:
114 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
115 break;
116 case core::socket::State::FATAL:
117 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
118 break;
119 }
120 });
121 /*
122 jsonClient.connect("localhost",
123 8080,
124 [instanceName = jsonClient.getConfig().getInstanceName()](
125 const SocketAddress& socketAddress,
126 const core::socket::State& state) { // example.com:81 simulate connnect timeout
127 switch (state) {
128 case core::socket::State::OK:
129 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
130 break;
131 case core::socket::State::DISABLED:
132 VLOG(1) << instanceName << ": disabled";
133 break;
134 case core::socket::State::ERROR:
135 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
136 break;
137 case core::socket::State::FATAL:
138 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
139 break;
140 }
141 });
142 */
143 /*
144 jsonClient.post("localhost", 8080, "/index.html", "{\"userId\":1,\"schnitzel\":\"good\",\"hungry\":false}", [](int err) {
145 if (err != 0) {
146 PLOG(ERROR) << "OnError: " << err;
147 }
148 });
149 */
150
151 return core::SNodeC::start();
152}
static void init(int argc, char *argv[])
Definition SNodeC.cpp:54
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:60
Client::Response Response
Definition clients.h:93
web::http::legacy::NET::Client Client
Definition clients.h:90
Client::MasterRequest MasterRequest
Definition clients.h:91
Client::Request Request
Definition clients.h:92
SocketClient::SocketAddress SocketAddress
web::http::client::Client< net::in::stream::legacy::SocketClient > Client
Definition Client.h:54

References web::http::client::Response::body, web::http::client::Client< SocketClientT >::Client(), net::in::stream::SocketClient< SocketConnectorT, ConfigSocketClientT, SocketContextFactoryT, Args >::connect(), web::http::client::Response::cookies, core::socket::State::DISABLED, core::socket::State::ERROR, core::socket::State::FATAL, core::socket::Socket< ConfigT >::getConfig(), net::config::ConfigInstance::getInstanceName(), web::http::CookieOptions::getOptions(), web::http::CookieOptions::getValue(), web::http::client::Response::headers, web::http::client::Response::httpVersion, core::SNodeC::init(), web::http::client::Request::method, core::socket::State::OK, web::http::client::Response::reason, web::http::client::MasterRequest::send(), web::http::client::Request::set(), core::SNodeC::start(), web::http::client::Response::statusCode, net::in::SocketAddress::toString(), web::http::client::Request::type(), web::http::client::Request::url, and core::socket::State::what().

Here is the call graph for this function: