global_data.h 2.8 KB
Newer Older
1
/******************************************************************************
G
gruminions 已提交
2
 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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
 *
 * 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 CYBERTRON_COMMON_GLOBAL_DATA_H_
#define CYBERTRON_COMMON_GLOBAL_DATA_H_

#include <string>
#include <unordered_map>

#include "cybertron/base/atomic_hash_map.h"
#include "cybertron/base/atomic_rw_lock.h"
#include "cybertron/common/log.h"
#include "cybertron/common/macros.h"
#include "cybertron/proto/cyber_config.pb.h"

namespace apollo {
namespace cybertron {
namespace common {

using ::apollo::cybertron::base::AtomicHashMap;
using ::apollo::cybertron::proto::CyberConfig;

class GlobalData {
 public:
  ~GlobalData();

  int ProcessId() const;

  void SetProcessName(const std::string& process_name);
  const std::string& ProcessName() const;

  const std::string& HostIp() const;

  const std::string& HostName() const;

  const CyberConfig& Config() const;

51 52 53
  void EnableSimulationMode();
  void DisableSimulationMode();

54 55
  bool IsRealityMode() const;

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  static uint64_t RegisterNode(const std::string& node_name);
  static const std::string& GetNodeById(uint64_t id);

  static uint64_t RegisterChannel(const std::string& channel);
  static const std::string& GetChannelById(uint64_t id);

  static uint64_t RegisterService(const std::string& service);
  static const std::string& GetServiceById(uint64_t id);

  static uint64_t RegisterTaskName(const std::string& task_name);
  static const std::string& GetTaskNameById(uint64_t id);

 private:
  void InitHostInfo();
  void InitConfig();

  // global config
  CyberConfig config_;

  // host info
  std::string host_ip_;
  std::string host_name_;

  // process info
  int process_id_;
  std::string process_name_;

83 84 85
  // run mode
  bool is_reality_mode_;

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  static AtomicHashMap<uint64_t, std::string, 512> node_id_map_;
  static AtomicHashMap<uint64_t, std::string, 256> channel_id_map_;
  static AtomicHashMap<uint64_t, std::string, 256> service_id_map_;
  static AtomicHashMap<uint64_t, std::string, 256> task_id_map_;

  static const std::string dummy_str_;

  DECLARE_SINGLETON(GlobalData)
};

}  // namespace common
}  // namespace cybertron
}  // namespace apollo

#endif  // CYBERTRON_COMMON_GLOBAL_DATA_H_