Skip to content

Commit

Permalink
flipper | separate implementation of FlipperConnectionImpl to cpp
Browse files Browse the repository at this point in the history
Summary: the headers are using try{}catch, which will break the compilation of units with disabled exceptions, move out impl of functions to C++ file, so it will not break the compilation of no-exception buck units

Differential Revision: D65280989

fbshipit-source-id: 162ade90246318f44e9fa5d4f2424d93e0aacc30
  • Loading branch information
arhelmus authored and facebook-github-bot committed Oct 31, 2024
1 parent 2fbff87 commit 06d60d0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 55 deletions.
89 changes: 89 additions & 0 deletions xplat/Flipper/FlipperConnectionImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "FlipperConnectionImpl.h"

namespace facebook {
namespace flipper {

FlipperConnectionImpl::FlipperConnectionImpl(
FlipperConnectionManager* socket,
const std::string& name)
: socket_(socket), name_(name) {}

void FlipperConnectionImpl::call(
const std::string& method,
const folly::dynamic& params,
std::shared_ptr<FlipperResponder> responder) {
if (receivers_.find(method) == receivers_.end()) {
std::string errorMessage = "Receiver " + method + " not found.";
log("Error: " + errorMessage);
responder->error(folly::dynamic::object("message", errorMessage));
return;
}
try {
receivers_.at(method)(params, responder);
} catch (const std::exception& ex) {
std::string errorMessage = "Receiver " + method + " failed with error. ";
std::string reason = ex.what();
errorMessage += "Error: '" + reason + "'.";
log("Error: " + errorMessage);
responder->error(folly::dynamic::object("message", errorMessage));
}
}

void FlipperConnectionImpl::send(
const std::string& method,
const folly::dynamic& params) {
folly::dynamic message = folly::dynamic::object("method", "execute")(
"params",
folly::dynamic::object("api", name_)("method", method)("params", params));
socket_->sendMessage(message);
}

void FlipperConnectionImpl::sendRaw(
const std::string& method,
const std::string& params) {
std::stringstream ss;
ss << "{"
"\"method\": \"execute\","
"\"params\": {"
"\"api\": \""
<< name_
<< "\","
"\"method\": \""
<< method
<< "\","
"\"params\":"
<< params << "}}";
auto message = ss.str();
socket_->sendMessageRaw(message);
}

void FlipperConnectionImpl::error(
const std::string& message,
const std::string& stacktrace) {
socket_->sendMessage(folly::dynamic::object(
"error",
folly::dynamic::object("message", message)("stacktrace", stacktrace)));
}

void FlipperConnectionImpl::receive(
const std::string& method,
const FlipperReceiver& receiver) {
receivers_[method] = receiver;
}

/**
Runtime check which receivers are supported for this app
*/
bool FlipperConnectionImpl::hasReceiver(const std::string& method) {
return receivers_.find(method) != receivers_.end();
}

} // namespace flipper
} // namespace facebook
64 changes: 9 additions & 55 deletions xplat/Flipper/FlipperConnectionImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,73 +21,27 @@ class FlipperConnectionImpl : public FlipperConnection {
public:
FlipperConnectionImpl(
FlipperConnectionManager* socket,
const std::string& name)
: socket_(socket), name_(name) {}
const std::string& name);

void call(
const std::string& method,
const folly::dynamic& params,
std::shared_ptr<FlipperResponder> responder) {
if (receivers_.find(method) == receivers_.end()) {
std::string errorMessage = "Receiver " + method + " not found.";
log("Error: " + errorMessage);
responder->error(folly::dynamic::object("message", errorMessage));
return;
}
try {
receivers_.at(method)(params, responder);
} catch (const std::exception& ex) {
std::string errorMessage = "Receiver " + method + " failed with error. ";
std::string reason = ex.what();
errorMessage += "Error: '" + reason + "'.";
log("Error: " + errorMessage);
responder->error(folly::dynamic::object("message", errorMessage));
}
}
std::shared_ptr<FlipperResponder> responder);

void send(const std::string& method, const folly::dynamic& params) override {
folly::dynamic message = folly::dynamic::object("method", "execute")(
"params",
folly::dynamic::object("api", name_)("method", method)(
"params", params));
socket_->sendMessage(message);
}
void send(const std::string& method, const folly::dynamic& params) override;

void sendRaw(const std::string& method, const std::string& params) override {
std::stringstream ss;
ss << "{"
"\"method\": \"execute\","
"\"params\": {"
"\"api\": \""
<< name_
<< "\","
"\"method\": \""
<< method
<< "\","
"\"params\":"
<< params << "}}";
auto message = ss.str();
socket_->sendMessageRaw(message);
}
void sendRaw(const std::string& method, const std::string& params) override;

void error(const std::string& message, const std::string& stacktrace)
override {
socket_->sendMessage(folly::dynamic::object(
"error",
folly::dynamic::object("message", message)("stacktrace", stacktrace)));
}
override;

void receive(const std::string& method, const FlipperReceiver& receiver)
override {
receivers_[method] = receiver;
}
override;

/**
Runtime check which receivers are supported for this app
*/
bool hasReceiver(const std::string& method) {
return receivers_.find(method) != receivers_.end();
}
* Runtime check which receivers are supported for this app
*/
bool hasReceiver(const std::string& method);

private:
FlipperConnectionManager* socket_;
Expand Down

0 comments on commit 06d60d0

Please sign in to comment.