Skip to content

Commit

Permalink
Moved HTTP client behaviour to client library. Also added GET and POS… (
Browse files Browse the repository at this point in the history
#549)

* Moved HTTP client behaviour to client library. Also added GET and POST-specific versions of the smae HTTP client behaviour to make them easier to use

* Added copyrights

* Fixed typos
  • Loading branch information
yassiezar authored Jul 5, 2024
1 parent 682e73c commit a2da057
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 38 deletions.
2 changes: 1 addition & 1 deletion smacc2_client_library/http_client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ add_library(http_session
)

add_library(${PROJECT_NAME}
src/http_client/http_client.cpp
src/http_client/cl_http_client.cpp
)

target_link_libraries(${PROJECT_NAME}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
#include <boost/beast/version.hpp>
#include <http_client/http_session.hpp>
#include <http_client/ssl_http_session.hpp>
#include <iostream>
#include <memory>
#include <optional>
#include <smacc2/smacc.hpp>
#include <smacc2/smacc_client.hpp>
#include <string>
#include <thread>
#include <unordered_map>

namespace cl_http
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
* Authors: Jaycee Lock
*
******************************************************************************************************************/

#pragma once

#include <http_client/cl_http_client.hpp>
#include <http_client/client_behaviors/cb_http_request.hpp>
#include <smacc2/smacc.hpp>

namespace cl_http
{
class CbHttpGetRequest : public CbHttpRequestBase
{
public:
CbHttpGetRequest() : CbHttpRequestBase(ClHttp::kHttpRequestMethod::GET) {}
};
} // namespace cl_http
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
* Authors: Jaycee Lock
*
******************************************************************************************************************/

#pragma once

#include <http_client/cl_http_client.hpp>
#include <http_client/client_behaviors/cb_http_request.hpp>
#include <smacc2/smacc.hpp>

namespace cl_http
{
class CbHttpPostRequest : public CbHttpRequestBase
{
public:
CbHttpPostRequest() : CbHttpRequestBase(ClHttp::kHttpRequestMethod::POST) {}
};
} // namespace cl_http
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
* Authors: Jaycee Lock
*
******************************************************************************************************************/

#pragma once

#include <cstring>
#include <http_client/cl_http_client.hpp>
#include <smacc2/smacc.hpp>

namespace cl_http
{

class CbHttpRequestBase : public smacc2::SmaccClientBehavior
{
public:
CbHttpRequestBase(const ClHttp::kHttpRequestMethod http_request_type)
: kRequestType(http_request_type)
{
}

template <typename TOrthogonal, typename TSourceObject>
void onOrthogonalAllocation()
{
}

virtual void runtimeConfigure() override
{
this->requiresClient(cl_http_);
cl_http_->onResponseReceived(&CbHttpRequestBase::onResponseReceived, this);
}

virtual void onResponseReceived(const ClHttp::TResponse & response) {}

virtual void onEntry() override { cl_http_->makeRequest(kRequestType); }

virtual void onExit() override {}

private:
const ClHttp::kHttpRequestMethod kRequestType;

ClHttp * cl_http_;
};
} // namespace cl_http
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
******************************************************************************************************************/

#include <http_client/http_client.hpp>
#include <http_client/cl_http_client.hpp>

namespace cl_http
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,39 @@
#pragma once

#include <cstring>
#include <http_client/http_client.hpp>
#include <http_client/client_behaviors/cb_http_get_request.hpp>
#include <smacc2/smacc.hpp>

namespace sm_atomic_http {
namespace sm_atomic_http
{

template <typename TSource, typename TOrthogonal>
struct EvHttp : sc::event<EvHttp<TSource, TOrthogonal>> {};
struct EvHttp : sc::event<EvHttp<TSource, TOrthogonal>>
{
};

class CbHttpRequest : public smacc2::SmaccClientBehavior {
public:
class CbHttpRequest : public cl_http::CbHttpGetRequest
{
public:
template <typename TOrthogonal, typename TSourceObject>
void onOrthogonalAllocation() {
triggerTranstition = [this]() {
void onOrthogonalAllocation()
{
triggerTranstition = [this]()
{
auto event = new EvHttp<TSourceObject, TOrthogonal>();
this->postEvent(event);
};
}

void runtimeConfigure() override {
this->requiresClient(cl_http_);
cl_http_->onResponseReceived(&CbHttpRequest::onResponseReceived, this);
}

void onResponseReceived(const cl_http::ClHttp::TResponse& response) {
void onResponseReceived(const cl_http::ClHttp::TResponse & response)
{
RCLCPP_INFO_STREAM(this->getLogger(), "ON RESPONSE");
RCLCPP_INFO_STREAM(this->getLogger(), response.body());
triggerTranstition();
}

void onEntry() override {
RCLCPP_INFO(this->getLogger(), "On Entry!");

cl_http_->makeRequest(cl_http::ClHttp::kHttpRequestMethod::GET);
}

void onExit() override { RCLCPP_INFO(this->getLogger(), "Cb on exit!"); }

private:
cl_http::ClHttp* cl_http_;
private:
cl_http::ClHttp * cl_http_;

std::function<void()> triggerTranstition;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#pragma once

#include <http_client/http_client.hpp>
#include <http_client/cl_http_client.hpp>
#include <smacc2/smacc.hpp>

namespace sm_atomic_http {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@

#include <smacc2/smacc.hpp>

namespace sm_atomic_http {
namespace sm_atomic_http
{

// STATE DECLARATION
struct State2 : smacc2::SmaccState<State2, SmAtomicHttp> {
struct State2 : smacc2::SmaccState<State2, SmAtomicHttp>
{
using SmaccState::SmaccState;

// TRANSITION TABLE
typedef mpl::list<
Transition<EvHttp<CbHttpRequest, OrHttp>, State1, SUCCESS> >
reactions;
typedef mpl::list<Transition<EvHttp<CbHttpRequest, OrHttp>, State1, SUCCESS>>
reactions;

// STATE FUNCTIONS
static void staticConfigure() {
configure_orthogonal<OrHttp, CbHttpRequest>();
}
static void staticConfigure() { configure_orthogonal<OrHttp, CbHttpRequest>(); }

void runtimeConfigure() { RCLCPP_INFO(getLogger(), "Entering State2"); }

Expand Down

0 comments on commit a2da057

Please sign in to comment.