Skip to content
Snippets Groups Projects
server.hh 1.87 KiB
Newer Older
  • Learn to ignore specific revisions
  • #ifndef __VEREIGN_GRPC_SERVER_API_HH
    #define __VEREIGN_GRPC_SERVER_API_HH
    
    
    #include <string>
    #include <memory>
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    namespace vereign::grpc {
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    /**
     * BindError is thrown when the Server::Server could not start listening.
     */
    
    class BindError: public std::exception {
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
    public:
      auto what() const noexcept -> const char* override {
        return "gRPC listen failed";
      }
    };
    
    
    /**
     * Server is a grpc server that provides the Vereign services.
     *
     * It bootstraps the grpc server, http client, http client session and
     * Vereign services.
     */
    class Server {
    public:
      /**
       * Constructs and bootstraps the server.
       *
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
       * When the gRPC could not start listening a BindError is thrown.
       * This can happen for number of reasons like invalid hostname or the listen port is already in use.
       *
    
       * @param listenAddress gRPC listen address, for example "localhost:".
       * @param vereignHost Vereign restapi host.
       * @param vereignPort Vereign restapi port - https, 443...
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
       * @throws BindError when the gRPC server could not start listening.
    
       */
      explicit Server(
        const std::string& listenAddress,
        const std::string& vereignHost,
    
    Daniel Lyubomirov's avatar
    Daniel Lyubomirov committed
        const std::string& vereignPort
    
      );
    
      /**
       * Shutdowns the server.
       *
       * @see Server::Shutdown
       */
      ~Server();
    
      // Disable copying
      Server(const Server&) = delete;
    
      auto operator=(const Server&) -> Server& = delete;
    
    
      /**
       * Shutdown the server.
       *
       * It will cancel all pending http requests to the Vereign restapi.
       * Blocks for all pending gRPC handlers to finish.
       */
      void Shutdown();
    
      /**
       * Returns the port that the gRPC server listens to.
       *
       * This is useful if you construct the server with ephemeral port.
       * Then this method will return the port that the OS assigned to the gRPC
       * socket.
       */
    
      auto SelectedPort() const -> int;
    
      class Impl;
      std::unique_ptr<Impl> impl_;
    
    } // namespace vereign