1 /** @file
2 
3   Some mgmt definitions for relatively general use.
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 
26 /*
27  * Type definitions.
28  */
29 #include <functional>
30 #include <string_view>
31 
32 #include "tscore/ink_defs.h"
33 #include "tscpp/util/MemSpan.h"
34 #include "tscpp/util/TextView.h"
35 
36 typedef int64_t MgmtIntCounter;
37 typedef int64_t MgmtInt;
38 typedef int8_t MgmtByte;
39 typedef float MgmtFloat;
40 typedef char *MgmtString;
41 
42 enum MgmtType {
43   MGMT_INVALID  = -1,
44   MGMT_INT      = 0,
45   MGMT_FLOAT    = 1,
46   MGMT_STRING   = 2,
47   MGMT_COUNTER  = 3,
48   MGMT_TYPE_MAX = 4,
49 };
50 
51 /// Management callback signature.
52 /// The memory span is the message payload for the callback.
53 /// This can be a lambda, which should be used if additional context information is needed.
54 using MgmtCallback = std::function<void(ts::MemSpan<void>)>;
55 
56 //-------------------------------------------------------------------------
57 // API conversion functions.
58 //-------------------------------------------------------------------------
59 /** Conversion functions to and from an arbitrary type and Management types.
60  *
61  * A type that wants to support conversion in the TS API should create a static instance of this
62  * class and fill in the appropriate members. The TS API set/get functions can then check for a
63  * @c nullptr to see if the conversion is supported and if so, call a function to do that. The
64  * @c void* argument is a raw pointer to the typed object. For instance, if this is for transaction
65  * overrides the pointer will be to the member in the transaction override configuration structure.
66  * Support for the management types is built in, this is only needed for types that aren't defined
67  * in this header.
68  */
69 struct MgmtConverter {
70   /** Load a native type into a @c MgmtInt
71    *
72    * This is passed a @c void* which is a pointer to the member in the configuration instance.
73    * This function must return a @c MgmtInt converted from that value.
74    */
75   MgmtInt (*load_int)(const void *) = nullptr;
76 
77   /** Store a @c MgmtInt into a native type.
78    *
79    * This function is passed a @c void* which is a pointer to the member in the configuration
80    * instance and a @c MgmtInt. The member should be updated to correspond to the @c MgmtInt value.
81    */
82   void (*store_int)(void *, MgmtInt) = nullptr;
83 
84   /** Load a @c MgmtFloat from a native type.
85    *
86    * This is passed a @c void* which is a pointer to the member in the configuration instance.
87    * This function must return a @c MgmtFloat converted from that value.
88    */
89   MgmtFloat (*load_float)(const void *) = nullptr;
90 
91   /** Store a @c MgmtFloat into a native type.
92    *
93    * This function is passed a @c void* which is a pointer to the member in the configuration
94    * instance and a @c MgmtFloat. The member should be updated to correspond to the @c MgmtFloat value.
95    */
96   void (*store_float)(void *, MgmtFloat) = nullptr;
97 
98   /** Load a native type into view.
99    *
100    * This is passed a @c void* which is a pointer to the member in the configuration instance.
101    * This function must return a @c string_view which contains the text for the member.
102    */
103   std::string_view (*load_string)(const void *) = nullptr;
104 
105   /** Store a view in a native type.
106    *
107    * This is passed a @c void* which is a pointer to the member in the configuration instance.
108    * This function must return a @c string_view which contains the text for the member.
109    */
110   void (*store_string)(void *, std::string_view) = nullptr;
111 
112   // Convenience constructors because generally only one pair is valid.
113   MgmtConverter(MgmtInt (*load)(const void *), void (*store)(void *, MgmtInt));
114   MgmtConverter(MgmtFloat (*load)(const void *), void (*store)(void *, MgmtFloat));
115   MgmtConverter(std::string_view (*load)(const void *), void (*store)(void *, std::string_view));
116 
117   MgmtConverter(MgmtInt (*_load_int)(const void *), void (*_store_int)(void *, MgmtInt), MgmtFloat (*_load_float)(const void *),
118                 void (*_store_float)(void *, MgmtFloat), std::string_view (*_load_string)(const void *),
119                 void (*_store_string)(void *, std::string_view));
120 };
121 
MgmtConverter(MgmtInt (* load)(const void *),void (* store)(void *,MgmtInt))122 inline MgmtConverter::MgmtConverter(MgmtInt (*load)(const void *), void (*store)(void *, MgmtInt))
123   : load_int(load), store_int(store)
124 {
125 }
126 
MgmtConverter(MgmtFloat (* load)(const void *),void (* store)(void *,MgmtFloat))127 inline MgmtConverter::MgmtConverter(MgmtFloat (*load)(const void *), void (*store)(void *, MgmtFloat))
128   : load_float(load), store_float(store)
129 {
130 }
131 
MgmtConverter(std::string_view (* load)(const void *),void (* store)(void *,std::string_view))132 inline MgmtConverter::MgmtConverter(std::string_view (*load)(const void *), void (*store)(void *, std::string_view))
133   : load_string(load), store_string(store)
134 {
135 }
136 
MgmtConverter(MgmtInt (* _load_int)(const void *),void (* _store_int)(void *,MgmtInt),MgmtFloat (* _load_float)(const void *),void (* _store_float)(void *,MgmtFloat),std::string_view (* _load_string)(const void *),void (* _store_string)(void *,std::string_view))137 inline MgmtConverter::MgmtConverter(MgmtInt (*_load_int)(const void *), void (*_store_int)(void *, MgmtInt),
138                                     MgmtFloat (*_load_float)(const void *), void (*_store_float)(void *, MgmtFloat),
139                                     std::string_view (*_load_string)(const void *), void (*_store_string)(void *, std::string_view))
140   : load_int(_load_int),
141     store_int(_store_int),
142     load_float(_load_float),
143     store_float(_store_float),
144     load_string(_load_string),
145     store_string(_store_string)
146 {
147 }
148 
149 constexpr ts::TextView LM_CONNECTION_SERVER{"processerver.sock"};
150