• 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 #include "actor/aid.h"
18 
19 namespace mindspore {
20 constexpr int PORTMINNUMBER = 0;
21 constexpr int PORTMAXNUMBER = 65535;
22 constexpr int PROCOLLEN = 3;  // strlen("://");
23 
SetUnfixUrl()24 void AID::SetUnfixUrl() {
25   size_t index = url.find("://");
26   if (index != std::string::npos) {
27     if (url.substr(0, index) == MINDRT_TCP) {
28       url = url.substr(index + PROCOLLEN);
29     }
30   }
31 }
32 
AID(const char * tmpName)33 AID::AID(const char *tmpName) {
34   std::string sName = tmpName;
35   size_t index = sName.find("@");
36   if (index == std::string::npos) {
37     name = sName;
38     url = "";
39   } else {
40     name = sName.substr(0, index);
41     url = sName.substr(index + 1);
42     SetUnfixUrl();
43   }
44 }
45 
AID(const std::string & tmpName)46 AID::AID(const std::string &tmpName) {
47   size_t index = tmpName.find("@");
48   if (index == std::string::npos) {
49     name = tmpName;
50     url = "";
51   } else {
52     name = tmpName.substr(0, index);
53     url = tmpName.substr(index + 1);
54     SetUnfixUrl();
55   }
56 }
57 
OK() const58 bool AID::OK() const {
59   std::string proto = GetProtocol();
60 #ifdef UDP_ENABLED
61   bool protoOK = (proto == MINDRT_TCP) || (proto == MINDRT_UDP);
62 #else
63   bool protoOK = (proto == MINDRT_TCP);
64 #endif
65   int port = GetPort();
66   bool portOK = port > PORTMINNUMBER && port < PORTMAXNUMBER;
67   return protoOK && portOK && name != "";
68 }
operator =(const AID & id)69 AID &AID::operator=(const AID &id) {
70   if (&id != this) {
71     name = id.name;
72     url = id.url;
73   }
74   return *this;
75 }
76 
SetProtocol(const std::string & protocol)77 void AID::SetProtocol(const std::string &protocol) {
78   size_t index = url.find("://");
79   if (index != std::string::npos) {
80     if (protocol == MINDRT_TCP) {
81       url = url.substr(index + PROCOLLEN);
82     } else {
83       url = protocol + url.substr(index);
84     }
85   } else {
86     if (protocol == MINDRT_TCP) {
87     } else {
88       url = protocol + "://" + url;
89     }
90   }
91 }
92 
GetProtocol() const93 std::string AID::GetProtocol() const {
94   size_t index = url.find("://");
95   if (index != std::string::npos) {
96     return url.substr(0, index);
97   } else {
98     return "tcp";
99   }
100 }
101 
GetIp() const102 std::string AID::GetIp() const {
103   size_t index1 = url.find("://");
104   if (index1 == std::string::npos) {
105     index1 = 0;
106   } else {
107     index1 = index1 + PROCOLLEN;
108   }
109   size_t index2 = url.rfind(':');
110   if ((index2 == std::string::npos) || (index2 < index1)) {
111     MS_LOG(DEBUG) << "wrong url:" << url.c_str();
112     return url;
113   } else {
114     return url.substr(index1, index2 - index1);
115   }
116 }
117 
GetPort() const118 uint16_t AID::GetPort() const {
119   size_t index = url.rfind(':');
120   if (index == std::string::npos) {
121     return 0;
122   }
123   return (uint16_t)std::stoul(url.substr(index + 1));
124 }
125 };  // end of namespace mindspore
126