1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "common/entity_name.h"
16 #include "include/msgr.h"
24 extern const char *ceph_entity_type_name(int type
);
26 struct str_to_entity_type_t
{
31 static const str_to_entity_type_t STR_TO_ENTITY_TYPE
[] = {
32 { CEPH_ENTITY_TYPE_AUTH
, "auth" },
33 { CEPH_ENTITY_TYPE_MON
, "mon" },
34 { CEPH_ENTITY_TYPE_OSD
, "osd" },
35 { CEPH_ENTITY_TYPE_MDS
, "mds" },
36 { CEPH_ENTITY_TYPE_MGR
, "mgr" },
37 { CEPH_ENTITY_TYPE_CLIENT
, "client" },
46 const std::string
& EntityName::
52 const char* EntityName::
55 return type_id
.c_str();
59 from_str(const string
& s
)
61 size_t pos
= s
.find('.');
63 if (pos
== string::npos
)
66 string type_
= s
.substr(0, pos
);
67 string id_
= s
.substr(pos
+ 1);
74 set(uint32_t type_
, const std::string
&id_
)
79 std::ostringstream oss
;
80 oss
<< ceph_entity_type_name(type_
) << "." << id_
;
85 set(const std::string
&type_
, const std::string
&id_
)
87 uint32_t t
= str_to_ceph_entity_type(type_
.c_str());
88 if (t
== CEPH_ENTITY_TYPE_ANY
)
95 set_type(uint32_t type_
)
101 set_type(const char *type_
)
103 return set(type_
, id
);
107 set_id(const std::string
&id_
)
112 void EntityName::set_name(entity_name_t n
)
115 sprintf(s
, "%lld", (long long)n
.num());
119 const char* EntityName::
122 return ceph_entity_type_name(type
);
125 const char *EntityName::
126 get_type_name() const
128 return ceph_entity_type_name(type
);
131 const std::string
&EntityName::
138 has_default_id() const
140 return (id
== "admin");
143 std::string
EntityName::
144 get_valid_types_as_str()
149 for (i
= 0; i
< sizeof(STR_TO_ENTITY_TYPE
)/sizeof(STR_TO_ENTITY_TYPE
[0]); ++i
) {
151 out
+= STR_TO_ENTITY_TYPE
[i
].str
;
157 bool operator<(const EntityName
& a
, const EntityName
& b
)
159 return (a
.type
< b
.type
) || (a
.type
== b
.type
&& a
.id
< b
.id
);
162 std::ostream
& operator<<(std::ostream
& out
, const EntityName
& n
)
164 return out
<< n
.to_str();
167 uint32_t str_to_ceph_entity_type(const char * str
)
170 for (i
= 0; i
< sizeof(STR_TO_ENTITY_TYPE
)/sizeof(STR_TO_ENTITY_TYPE
[0]); ++i
) {
171 if (strcmp(str
, STR_TO_ENTITY_TYPE
[i
].str
) == 0)
172 return STR_TO_ENTITY_TYPE
[i
].type
;
174 return CEPH_ENTITY_TYPE_ANY
;