• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_AID_H
18 #define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_AID_H
19 
20 #include <string>
21 
22 #include "actor/log.h"
23 #include "mindapi/base/macros.h"
24 
25 namespace mindspore {
26 constexpr auto MINDRT_TCP = "tcp";
27 constexpr auto MINDRT_UDP = "udp";
28 
29 class MS_CORE_API AID {
30  public:
AID()31   AID() : name(), url() {}
32 
~AID()33   ~AID() {}
34 
35   explicit AID(const char *name);
36   explicit AID(const std::string &name);
37 
AID(const std::string & tmpName,const std::string & sUrl)38   AID(const std::string &tmpName, const std::string &sUrl) : name(tmpName), url(sUrl) { SetUnfixUrl(); }
39 
AID(const AID & id)40   AID(const AID &id) : name(id.name), url(id.url) { SetUnfixUrl(); }
41 
42   // Overloading of Assignment Operator
43   AID &operator=(const AID &id);
44 
SetUrl(const std::string & tmpUrl)45   inline void SetUrl(const std::string &tmpUrl) {
46     url = tmpUrl;
47     SetUnfixUrl();
48   }
49 
SetName(const std::string & tmpName)50   inline void SetName(const std::string &tmpName) { name = tmpName; }
51 
Name()52   inline const std::string &Name() const { return name; }
53 
Url()54   inline const std::string &Url() const { return url; }
55 
56   void SetProtocol(const std::string &protocol);
57   bool OK() const;
58 
59   std::string GetProtocol() const;
60   std::string GetIp() const;
61   uint16_t GetPort() const;
UnfixUrl()62   inline std::string UnfixUrl() const { return GetIp() + ":" + std::to_string(GetPort()); }
string()63   inline operator std::string() const { return name + "@" + url; }
64 
HashString()65   inline std::string HashString() const { return name + "@" + UnfixUrl(); }
66 
67  private:
68   void SetUnfixUrl();
69 
70   friend class Actor;
71 
72   // actor's name
73   std::string name;
74 
75   /**
76   tcp://ip:port
77   udp://ip:port
78   ip:port  (tcp)
79    **/
80   std::string url;
81 };
82 
83 inline std::ostream &operator<<(std::ostream &os, const AID &aid) {
84   os << aid.Name() << "@" << aid.Url();
85   return os;
86 }
87 
88 inline bool operator==(const AID &aid1, const AID &aid2) {
89   if (aid1.GetProtocol() == MINDRT_TCP && aid2.GetProtocol() == MINDRT_TCP) {
90     // NOTE : By default, http has no protocol filed, so we use 'UnfixUrl' to compare aids here
91     return ((aid1.Name() == aid2.Name()) && (aid1.UnfixUrl() == aid2.UnfixUrl()));
92   } else {
93     return ((aid1.Name() == aid2.Name()) && (aid1.Url() == aid2.Url()));
94   }
95 }
96 inline bool operator!=(const AID &aid1, const AID &aid2) { return !(aid1 == aid2); }
97 
98 inline bool operator>(const AID &aid1, const AID &aid2) { return aid1.HashString() > aid2.HashString(); }
99 inline bool operator<(const AID &aid1, const AID &aid2) { return aid1.HashString() < aid2.HashString(); }
100 
101 };  // namespace mindspore
102 
103 // custom specialization of std::hash can be injected in namespace std
104 namespace std {
105 template <>
106 struct hash<mindspore::AID> {
107   typedef mindspore::AID argument_type;
108   typedef std::size_t result_type;
109   result_type operator()(argument_type const &s) const noexcept { return (std::hash<std::string>{}(s.HashString())); }
110 };
111 }  // namespace std
112 
113 #endif
114