Program Listing for File service_handler.hpp

Return to documentation for file (include/email/service_handler.hpp)

// Copyright 2020-2021 Christophe Bedard
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef EMAIL__SERVICE_HANDLER_HPP_
#define EMAIL__SERVICE_HANDLER_HPP_

#include <map>
#include <memory>
#include <mutex>
#include <optional>  // NOLINT cpplint mistakes <optional> for a C system header
#include <string>
#include <unordered_map>
#include <utility>

#include "email/email/handler.hpp"
#include "email/email/info.hpp"
#include "email/email/polling_manager.hpp"
#include "email/gid.hpp"
#include "email/log.hpp"
#include "email/macros.hpp"
#include "email/safe_map.hpp"
#include "email/safe_queue.hpp"
#include "email/service_info.hpp"
#include "email/visibility_control.hpp"

namespace email
{


class ServiceHandler : public EmailHandler, public std::enable_shared_from_this<ServiceHandler>
{
public:
  using ResponseMap = SafeMap<SequenceNumber, std::pair<struct EmailData, ServiceInfo>>;
  using RequestQueue = SafeQueue<std::pair<struct EmailData, ServiceInfo>>;

  ServiceHandler();

  virtual ~ServiceHandler();


  void
  register_service_client(
    const Gid & gid,
    ResponseMap::WeakPtr response_map);


  void
  register_service_server(
    const std::string & service_name,
    RequestQueue::WeakPtr request_queue);

  virtual
  void
  register_handler(std::shared_ptr<PollingManager> polling_manager);


  virtual
  void
  handle(const struct EmailData & data);


  static constexpr auto HEADER_SEQUENCE_NUMBER = "Request-Sequence-Number";

private:
  EMAIL_DISABLE_COPY(ServiceHandler)

  std::shared_ptr<Logger> logger_;
  mutable std::mutex mutex_clients_;
  std::unordered_map<GidValue, ResponseMap::WeakPtr> clients_;
  std::unordered_map<GidValue, SequenceNumber> clients_last_seq_;
  mutable std::mutex mutex_servers_;
  std::unordered_multimap<std::string, RequestQueue::WeakPtr> servers_;
};

}  // namespace email

#endif  // EMAIL__SERVICE_HANDLER_HPP_