1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "service_entry.h"
17 #include "logging.h"
18 #include "service_base.h"
19 #include "unix_socket_server.h"
20
ServiceEntry()21 ServiceEntry::ServiceEntry()
22 {
23 unixSocketServer_ = nullptr;
24 }
~ServiceEntry()25 ServiceEntry::~ServiceEntry() {}
StartServer(const std::string & addrname,void (* callback)(int))26 bool ServiceEntry::StartServer(const std::string& addrname, void (*callback)(int))
27 {
28 CHECK_TRUE(unixSocketServer_ == nullptr, false, "Servier Already Started");
29
30 auto server = std::make_shared<UnixSocketServer>();
31
32 CHECK_TRUE(server->StartServer(addrname, *this, callback), false, "StartServer FAIL");
33
34 unixSocketServer_ = server;
35 return true;
36 }
37
RemoveContext(int fd)38 void ServiceEntry::RemoveContext(int fd)
39 {
40 if (unixSocketServer_ != nullptr) {
41 unixSocketServer_->RemoveContext(fd);
42 }
43 }
44
RegisterService(ServiceBase & psb)45 bool ServiceEntry::RegisterService(ServiceBase& psb)
46 {
47 CHECK_TRUE(serviceMap_.find(psb.serviceName_) == serviceMap_.end(), false, "RegisterService FAIL");
48 serviceMap_[psb.serviceName_] = &psb;
49 return true;
50 }
51
FindServiceByName(const std::string & service_name)52 const ServiceBase* ServiceEntry::FindServiceByName(const std::string& service_name)
53 {
54 CHECK_TRUE(serviceMap_.find(service_name) != serviceMap_.end(), nullptr, "FindServiceByName FAIL %s",
55 service_name.c_str());
56
57 return serviceMap_[service_name];
58 }
59
60 namespace {
61 const int MS_PER_S = 1000;
62 const int US_PER_S = 1000000;
63 const int NS_PER_US = 1000;
64 const int NS_PER_S = 1000000000;
65 const int NS_PER_MS = 1000000;
66 } // namespace
67
GetTimeMS()68 uint64_t GetTimeMS()
69 {
70 struct timespec ts;
71 clock_gettime(CLOCK_BOOTTIME, &ts);
72 return ts.tv_sec * MS_PER_S + ts.tv_nsec / NS_PER_MS;
73 }
74
GetTimeUS()75 uint64_t GetTimeUS()
76 {
77 struct timespec ts;
78 clock_gettime(CLOCK_BOOTTIME, &ts);
79 return ts.tv_sec * US_PER_S + ts.tv_nsec / NS_PER_US;
80 }
81
GetTimeNS()82 uint64_t GetTimeNS()
83 {
84 struct timespec ts;
85 clock_gettime(CLOCK_BOOTTIME, &ts);
86 return ts.tv_sec * NS_PER_S + ts.tv_nsec;
87 }