client.h 2.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#ifndef AIRMAP_MONITOR_CLIENT_H_
#define AIRMAP_MONITOR_CLIENT_H_

#include <airmap/do_not_copy_or_move.h>
#include <airmap/error.h>
#include <airmap/traffic.h>

#include <memory>
#include <vector>

namespace airmap {
/// namespace monitor bundles up types and functions to
/// interact with the AirMap monitor daemon.
namespace monitor {

/// Client provides access to the AirMap monitor service.
class Client : DoNotCopyOrMove {
 public:
  /// Configuration bundles up creation-time parameters of a Client.
  struct Configuration {
    std::string endpoint;            ///< The remote endpoint hosting the service.
    std::shared_ptr<Logger> logger;  ///< The logger instance.
  };

  /// Updates models updates delivered to clients.
  struct Update {
    std::vector<Traffic::Update> traffic;  ///< Traffic updates.
  };

  /// UpdateStream abstracts a source of incoming updates.
  class UpdateStream : DoNotCopyOrMove {
   public:
    /// Reveiver models an entity interested in receiving updates.
    class Receiver : DoNotCopyOrMove {
     public:
      /// handle_update is invoked for every update sent out by the service.
      virtual void handle_update(const Update& update) = 0;
    };

    /// subscribe connects 'receiver' to the stream of updates.
    virtual void subscribe(const std::shared_ptr<Receiver>& receiver) = 0;

    /// unsubscribe disconnects 'receiver' from the stream of updates.
    virtual void unsubscribe(const std::shared_ptr<Receiver>& receiver) = 0;

   protected:
    UpdateStream() = default;
  };

  /// ConnectToUpdates bundles up types for calls to Client::connect_to_updates.
  struct ConnectToUpdates {
    /// Result models the outcome of calling Client::connect_to_updates.
    using Result = Outcome<std::shared_ptr<UpdateStream>, Error>;
    /// Callback models the async receiver for a call to Client::connect_to_updates.
    using Callback = std::function<void(const Result&)>;
  };

  /// connect_to_updates connects to incoming updates.
  virtual void connect_to_updates(const ConnectToUpdates::Callback& cb) = 0;

 protected:
  Client() = default;
};

}  // namespace monitor
}  // namespace airmap

/// @example monitor/client.cpp
/// Illustrates how to use airmap::monitor::Client to connect
/// to an AirMap monitor instance.

#endif  // AIRMAP_MONITOR_CLIENT_H_