1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
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 | * \author Haichao.chc |
17 | * \date Oct 2020 |
18 | * \brief Abstract class of service which includes |
19 | * init->start->stop->cleanup |
20 | */ |
21 | |
22 | #pragma once |
23 | |
24 | #include <memory> |
25 | #include "common/error_code.h" |
26 | #include "common/logger.h" |
27 | #include "common/macro_define.h" |
28 | |
29 | namespace proxima { |
30 | namespace be { |
31 | |
32 | class Service { |
33 | public: |
34 | //! Destructor |
35 | virtual ~Service() = default; |
36 | |
37 | public: |
38 | int init() { |
39 | if (status_ != CREATED) { |
40 | LOG_ERROR("Service status error. status[%d] expect[%d]" , status_, |
41 | CREATED); |
42 | return ErrorCode_StatusError; |
43 | } |
44 | |
45 | int ret = this->init_impl(); |
46 | if (ret == 0) { |
47 | status_ = INITIALIZED; |
48 | } |
49 | |
50 | return ret; |
51 | } |
52 | |
53 | int cleanup() { |
54 | if (status_ != INITIALIZED) { |
55 | LOG_ERROR("Service status error. status[%d] expect[%d]" , status_, |
56 | INITIALIZED); |
57 | return ErrorCode_StatusError; |
58 | } |
59 | |
60 | int ret = this->cleanup_impl(); |
61 | if (ret == 0) { |
62 | status_ = CREATED; |
63 | } |
64 | return ret; |
65 | } |
66 | |
67 | int start() { |
68 | if (status_ != INITIALIZED) { |
69 | LOG_ERROR("Service status error. status[%d] expect[%d]" , status_, |
70 | INITIALIZED); |
71 | return ErrorCode_StatusError; |
72 | } |
73 | |
74 | int ret = this->start_impl(); |
75 | if (ret == 0) { |
76 | status_ = STARTED; |
77 | } |
78 | |
79 | return ret; |
80 | } |
81 | |
82 | int stop() { |
83 | if (status_ != STARTED) { |
84 | LOG_ERROR("Service status error. status[%d] expect[%d]" , status_, |
85 | STARTED); |
86 | return ErrorCode_StatusError; |
87 | } |
88 | |
89 | int ret = this->stop_impl(); |
90 | if (ret == 0) { |
91 | status_ = INITIALIZED; |
92 | } |
93 | |
94 | return ret; |
95 | } |
96 | |
97 | int status() const { |
98 | return status_; |
99 | } |
100 | |
101 | protected: |
102 | virtual int init_impl() = 0; |
103 | |
104 | virtual int cleanup_impl() = 0; |
105 | |
106 | virtual int start_impl() = 0; |
107 | |
108 | virtual int stop_impl() = 0; |
109 | |
110 | protected: |
111 | enum Status { CREATED = 0, INITIALIZED = 1, STARTED = 2 }; |
112 | |
113 | Status status_{CREATED}; |
114 | }; |
115 | |
116 | using ServicePtr = std::shared_ptr<Service>; |
117 | |
118 | } // namespace be |
119 | } // end namespace proxima |
120 | |