Files
flipper/xplat/Flipper/FlipperClient.h
John Knox 3c656de7fa Rename SonarClient to FlipperClient
Summary: Part of the Sonar -> Flipper rename

Reviewed By: passy

Differential Revision: D9871902

fbshipit-source-id: 4b64953cb1a64235d47caaf0fabfb560bb85f7a2
2018-09-20 17:06:53 -07:00

105 lines
2.7 KiB
C++

/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
#pragma once
#include "SonarConnectionImpl.h"
#include "SonarInitConfig.h"
#include "SonarPlugin.h"
#include "SonarState.h"
#include "SonarWebSocket.h"
#include <map>
#include <mutex>
#include "SonarStep.h"
#include <vector>
namespace facebook {
namespace flipper {
class FlipperClient : public SonarWebSocket::Callbacks {
public:
/**
Call before accessing instance with FlipperClient::instance(). This will set up
all the state needed to establish a Sonar connection.
*/
static void init(SonarInitConfig config);
/**
Standard accessor for the shared FlipperClient instance. This returns a
singleton instance to a shared FlipperClient. First call to this function will
create the shared FlipperClient. Must call FlipperClient::initDeviceData() before
first call to FlipperClient::instance().
*/
static FlipperClient* instance();
/**
Only public for testing
*/
FlipperClient(std::unique_ptr<SonarWebSocket> socket, std::shared_ptr<SonarState> state)
: socket_(std::move(socket)), sonarState_(state) {
auto step = sonarState_->start("Create client");
socket_->setCallbacks(this);
step->complete();
}
void start() {
auto step = sonarState_->start("Start client");
socket_->start();
step->complete();
}
void stop() {
auto step = sonarState_->start("Stop client");
socket_->stop();
step->complete();
}
void onConnected() override;
void onDisconnected() override;
void onMessageReceived(const folly::dynamic& message) override;
void addPlugin(std::shared_ptr<SonarPlugin> plugin);
void removePlugin(std::shared_ptr<SonarPlugin> plugin);
void refreshPlugins();
void setStateListener(
std::shared_ptr<SonarStateUpdateListener> stateListener);
std::shared_ptr<SonarPlugin> getPlugin(const std::string& identifier);
std::string getState();
std::vector<StateElement> getStateElements();
template <typename P>
std::shared_ptr<P> getPlugin(const std::string& identifier) {
return std::static_pointer_cast<P>(getPlugin(identifier));
}
bool hasPlugin(const std::string& identifier);
private:
static FlipperClient* instance_;
bool connected_ = false;
std::unique_ptr<SonarWebSocket> socket_;
std::map<std::string, std::shared_ptr<SonarPlugin>> plugins_;
std::map<std::string, std::shared_ptr<SonarConnectionImpl>> connections_;
std::mutex mutex_;
std::shared_ptr<SonarState> sonarState_;
void performAndReportError(const std::function<void()>& func);
void disconnect(std::shared_ptr<SonarPlugin> plugin);
};
} // namespace flipper
} // namespace facebook