Files
flipper/xplat/Sonar/SonarClient.h
John Knox fb447e4f84 Add state tracking plumbing
Summary:
The plan is to get SonarState to take update events and aggregate them into an object, that can be displayed in the UI so you can see what sonar is currently trying to do, and what if anything is failing.
This is pretty rubbish right now, as it just uses a single string to represent state, this is just the initial version I'll iterate on.
I also need to pass the SonarState into the SonarWebSocketImpl, since that's where a lot of the heavy lifting is done, but as long as something is logging state updates here, it proves the concept.
SonarStateUpdateListener is the interface that will be implemented by android and iOS implementations. These implementations will notify an activity or screen that the state has changed and they should reflect that.

Reviewed By: passy

Differential Revision: D8932114

fbshipit-source-id: fb03babfe92be53771eb4d8f10de7ecdc7f1a6d8
2018-08-03 07:42:16 -07:00

97 lines
2.5 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 <Sonar/SonarConnectionImpl.h>
#include <Sonar/SonarInitConfig.h>
#include <Sonar/SonarPlugin.h>
#include <Sonar/SonarState.h>
#include <Sonar/SonarWebSocket.h>
#include <map>
#include <mutex>
#include "SonarStep.h"
namespace facebook {
namespace sonar {
class SonarClient : public SonarWebSocket::Callbacks {
public:
/**
Call before accessing instance with SonarClient::instance(). This will set up
all the state needed to establish a Sonar connection.
*/
static void init(SonarInitConfig config);
/**
Standard accessor for the shared SonarClient instance. This returns a
singleton instance to a shared SonarClient. First call to this function will
create the shared SonarClient. Must call SonarClient::initDeviceData() before
first call to SonarClient::instance().
*/
static SonarClient* instance();
/**
Only public for testing
*/
SonarClient(std::unique_ptr<SonarWebSocket> socket)
: socket_(std::move(socket)) {
sonarState_ = std::make_unique<SonarState>();
socket_->setCallbacks(this);
}
void start() {
socket_->start();
}
void stop() {
socket_->stop();
}
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();
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 SonarClient* 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::unique_ptr<SonarState> sonarState_;
void performAndReportError(const std::function<void()>& func);
void disconnect(std::shared_ptr<SonarPlugin> plugin);
};
} // namespace sonar
} // namespace facebook