-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable HTTP connection between Client/Server
Re ECFLOW-1957
- Loading branch information
1 parent
d4f1b79
commit 7839841
Showing
12 changed files
with
242 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2009- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
#include "ecflow/base/HttpClient.hpp" | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
#include <stdexcept> | ||
|
||
#include "ecflow/base/stc/StcCmd.hpp" | ||
#include "ecflow/core/Converter.hpp" | ||
|
||
HttpClient::HttpClient(Cmd_ptr cmd_ptr, const std::string& host, const std::string& port, int timeout) | ||
: stopped_(false), | ||
host_(host), | ||
port_(port), | ||
client_(host, ecf::convert_to<int>(port)) { | ||
|
||
if (!cmd_ptr.get()) { | ||
throw std::runtime_error("Client::Client: No request specified !"); | ||
} | ||
|
||
outbound_request_.set_cmd(cmd_ptr); | ||
} | ||
|
||
void HttpClient::run() { | ||
std::string outbound; | ||
ecf::save_as_string(outbound, outbound_request_); | ||
|
||
auto result = client_.Post("/v1/ecflow", outbound, "application/json"); | ||
auto response = result.value(); | ||
|
||
ecf::restore_from_string(response.body, inbound_response_); | ||
}; | ||
|
||
bool HttpClient::handle_server_response(ServerReply& server_reply, bool debug) const { | ||
if (debug) { | ||
std::cout << " Client::handle_server_response" << std::endl; | ||
} | ||
server_reply.set_host_port(host_, port_); // client context, needed by some commands, ie. SServerLoadCmd | ||
return inbound_response_.handle_server_response(server_reply, outbound_request_.get_cmd(), debug); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2009- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
#ifndef ecflow_base_HttpClient_HPP | ||
#define ecflow_base_HttpClient_HPP | ||
|
||
#include <httplib.h> | ||
|
||
#include "ecflow/base/ClientToServerRequest.hpp" | ||
#include "ecflow/base/Connection.hpp" | ||
#include "ecflow/base/ServerToClientResponse.hpp" | ||
|
||
/// | ||
/// \brief This class acts as the client part. ( in client/server architecture) | ||
/// | ||
/// \note The plug command can move a node to another server hence the server | ||
/// itself will NEED to ACT as a client. This is why client lives in Base and | ||
/// not the Client project | ||
/// | ||
|
||
class HttpClient { | ||
public: | ||
/// Constructor starts the asynchronous connect operation. | ||
HttpClient(Cmd_ptr cmd_ptr, const std::string& host, const std::string& port, int timout = 0); | ||
|
||
void run(); | ||
|
||
/// Client side, get the server response, handles reply from server | ||
/// Returns true if all is ok, else false if further client action is required | ||
/// will throw std::runtime_error for errors | ||
bool handle_server_response(ServerReply&, bool debug) const; | ||
|
||
private: | ||
bool stopped_; | ||
std::string host_; /// the servers name | ||
std::string port_; /// the port on the server | ||
httplib::Client client_; | ||
ClientToServerRequest outbound_request_; /// The request we will send to the server | ||
ServerToClientResponse inbound_response_; /// The response we get back from the server | ||
}; | ||
|
||
#endif /* ecflow_base_HttpClient_HPP */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.