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
21ServiceEntry::ServiceEntry()
22{
23    unixSocketServer_ = nullptr;
24}
25ServiceEntry::~ServiceEntry() {}
26bool ServiceEntry::StartServer(const std::string& addrname)
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), false, "StartServer FAIL");
33
34    unixSocketServer_ = server;
35    return true;
36}
37
38bool ServiceEntry::RegisterService(ServiceBase& psb)
39{
40    CHECK_TRUE(serviceMap_.find(psb.serviceName_) == serviceMap_.end(), false, "RegisterService FAIL");
41    serviceMap_[psb.serviceName_] = &psb;
42    return true;
43}
44const ServiceBase* ServiceEntry::FindServiceByName(const std::string& service_name)
45{
46    CHECK_TRUE(serviceMap_.find(service_name) != serviceMap_.end(), nullptr, "FindServiceByName FAIL %s",
47               service_name.c_str());
48
49    return serviceMap_[service_name];
50}
51
52namespace {
53const int MS_PER_S = 1000;
54const int US_PER_S = 1000000;
55const int NS_PER_US = 1000;
56const int NS_PER_S = 1000000000;
57const int NS_PER_MS = 1000000;
58} // namespace
59
60uint64_t GetTimeMS()
61{
62    struct timespec ts;
63    clock_gettime(CLOCK_BOOTTIME, &ts);
64    return ts.tv_sec * MS_PER_S + ts.tv_nsec / NS_PER_MS;
65}
66
67uint64_t GetTimeUS()
68{
69    struct timespec ts;
70    clock_gettime(CLOCK_BOOTTIME, &ts);
71    return ts.tv_sec * US_PER_S + ts.tv_nsec / NS_PER_US;
72}
73
74uint64_t GetTimeNS()
75{
76    struct timespec ts;
77    clock_gettime(CLOCK_BOOTTIME, &ts);
78    return ts.tv_sec * NS_PER_S + ts.tv_nsec;
79}