Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2016 Cisco and/or its affiliates. |
| 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. |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 15 | # |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 16 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 17 | from __future__ import print_function |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 18 | from __future__ import absolute_import |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 19 | import sys |
| 20 | import os |
| 21 | import logging |
| 22 | import collections |
| 23 | import struct |
| 24 | import json |
| 25 | import threading |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 26 | import fnmatch |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 27 | import weakref |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 28 | import atexit |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 29 | from . vpp_serializer import VPPType, VPPEnumType, VPPUnionType, BaseTypes |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 30 | from . vpp_serializer import VPPMessage, vpp_get_type, VPPTypeAlias |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 31 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame^] | 32 | logger = logging.getLogger(__name__) |
| 33 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 34 | if sys.version[0] == '2': |
| 35 | import Queue as queue |
| 36 | else: |
| 37 | import queue as queue |
| 38 | |
Ole Troan | afddd83 | 2018-02-28 14:55:20 +0100 | [diff] [blame] | 39 | |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 40 | class VppEnumType(type): |
| 41 | def __getattr__(cls, name): |
| 42 | t = vpp_get_type(name) |
| 43 | return t.enum |
| 44 | |
| 45 | |
| 46 | # Python3 |
| 47 | # class VppEnum(metaclass=VppEnumType): |
| 48 | # pass |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 49 | class VppEnum(object): |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 50 | __metaclass__ = VppEnumType |
| 51 | |
| 52 | |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 53 | def vpp_atexit(vpp_weakref): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 54 | """Clean up VPP connection on shutdown.""" |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 55 | vpp_instance = vpp_weakref() |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 56 | if vpp_instance and vpp_instance.transport.connected: |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 57 | vpp_instance.logger.debug('Cleaning up VPP on exit') |
| 58 | vpp_instance.disconnect() |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 59 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 60 | if sys.version[0] == '2': |
| 61 | def vpp_iterator(d): |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 62 | return d.iteritems() |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 63 | else: |
| 64 | def vpp_iterator(d): |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 65 | return d.items() |
| 66 | |
| 67 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame^] | 68 | def call_logger(msgdef, kwargs): |
| 69 | s = 'Calling {}('.format(msgdef.name) |
| 70 | for k, v in kwargs.items(): |
| 71 | s += '{}:{} '.format(k, v) |
| 72 | s += ')' |
| 73 | return s |
| 74 | |
| 75 | |
| 76 | def return_logger(r): |
| 77 | s = 'Return from {}'.format(r) |
| 78 | return s |
| 79 | |
| 80 | |
Klement Sekera | 8aedf5e | 2018-07-06 11:07:21 +0200 | [diff] [blame] | 81 | class VppApiDynamicMethodHolder(object): |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 82 | pass |
| 83 | |
| 84 | |
| 85 | class FuncWrapper(object): |
| 86 | def __init__(self, func): |
| 87 | self._func = func |
| 88 | self.__name__ = func.__name__ |
| 89 | |
| 90 | def __call__(self, **kwargs): |
| 91 | return self._func(**kwargs) |
| 92 | |
| 93 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 94 | class VPPApiError(Exception): |
| 95 | pass |
| 96 | |
| 97 | |
| 98 | class VPPNotImplementedError(NotImplementedError): |
| 99 | pass |
| 100 | |
| 101 | |
| 102 | class VPPIOError(IOError): |
| 103 | pass |
| 104 | |
| 105 | |
| 106 | class VPPRuntimeError(RuntimeError): |
| 107 | pass |
| 108 | |
| 109 | |
| 110 | class VPPValueError(ValueError): |
| 111 | pass |
| 112 | |
| 113 | |
Paul Vinciguerra | 7e713f1 | 2018-11-26 12:04:48 -0800 | [diff] [blame] | 114 | class VPP(object): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 115 | """VPP interface. |
| 116 | |
| 117 | This class provides the APIs to VPP. The APIs are loaded |
| 118 | from provided .api.json files and makes functions accordingly. |
| 119 | These functions are documented in the VPP .api files, as they |
| 120 | are dynamically created. |
| 121 | |
| 122 | Additionally, VPP can send callback messages; this class |
| 123 | provides a means to register a callback function to receive |
| 124 | these messages in a background thread. |
| 125 | """ |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 126 | VPPApiError = VPPApiError |
| 127 | VPPRuntimeError = VPPRuntimeError |
| 128 | VPPValueError = VPPValueError |
| 129 | VPPNotImplementedError = VPPNotImplementedError |
| 130 | VPPIOError = VPPIOError |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 131 | |
| 132 | def process_json_file(self, apidef_file): |
| 133 | api = json.load(apidef_file) |
| 134 | types = {} |
| 135 | for t in api['enums']: |
| 136 | t[0] = 'vl_api_' + t[0] + '_t' |
| 137 | types[t[0]] = {'type': 'enum', 'data': t} |
| 138 | for t in api['unions']: |
| 139 | t[0] = 'vl_api_' + t[0] + '_t' |
| 140 | types[t[0]] = {'type': 'union', 'data': t} |
| 141 | for t in api['types']: |
| 142 | t[0] = 'vl_api_' + t[0] + '_t' |
| 143 | types[t[0]] = {'type': 'type', 'data': t} |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 144 | for t, v in api['aliases'].items(): |
| 145 | types['vl_api_' + t + '_t'] = {'type': 'alias', 'data': v} |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 146 | self.services.update(api['services']) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 147 | |
| 148 | i = 0 |
| 149 | while True: |
| 150 | unresolved = {} |
| 151 | for k, v in types.items(): |
| 152 | t = v['data'] |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 153 | if not vpp_get_type(k): |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 154 | if v['type'] == 'enum': |
| 155 | try: |
| 156 | VPPEnumType(t[0], t[1:]) |
| 157 | except ValueError: |
| 158 | unresolved[k] = v |
| 159 | elif v['type'] == 'union': |
| 160 | try: |
| 161 | VPPUnionType(t[0], t[1:]) |
| 162 | except ValueError: |
| 163 | unresolved[k] = v |
| 164 | elif v['type'] == 'type': |
| 165 | try: |
| 166 | VPPType(t[0], t[1:]) |
| 167 | except ValueError: |
| 168 | unresolved[k] = v |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 169 | elif v['type'] == 'alias': |
| 170 | try: |
| 171 | VPPTypeAlias(k, t) |
| 172 | except ValueError: |
| 173 | unresolved[k] = v |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 174 | if len(unresolved) == 0: |
| 175 | break |
| 176 | if i > 3: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 177 | raise VPPValueError('Unresolved type definitions {}' |
| 178 | .format(unresolved)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 179 | types = unresolved |
| 180 | i += 1 |
| 181 | |
| 182 | for m in api['messages']: |
| 183 | try: |
| 184 | self.messages[m[0]] = VPPMessage(m[0], m[1:]) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 185 | except VPPNotImplementedError: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 186 | self.logger.error('Not implemented error for {}'.format(m[0])) |
| 187 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 188 | def __init__(self, apifiles=None, testmode=False, async_thread=True, |
Igor Mikhailov (imichail) | 5efd14f | 2018-10-30 12:17:49 -0700 | [diff] [blame] | 189 | logger=None, loglevel=None, |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 190 | read_timeout=5, use_socket=False, |
| 191 | server_address='/run/vpp-api.sock'): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 192 | """Create a VPP API object. |
| 193 | |
| 194 | apifiles is a list of files containing API |
| 195 | descriptions that will be loaded - methods will be |
| 196 | dynamically created reflecting these APIs. If not |
| 197 | provided this will load the API files from VPP's |
| 198 | default install location. |
Ian Wells | d0e812f | 2018-06-06 14:12:27 +0100 | [diff] [blame] | 199 | |
| 200 | logger, if supplied, is the logging logger object to log to. |
| 201 | loglevel, if supplied, is the log level this logger is set |
| 202 | to report at (from the loglevels in the logging module). |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 203 | """ |
Ian Wells | d0e812f | 2018-06-06 14:12:27 +0100 | [diff] [blame] | 204 | if logger is None: |
| 205 | logger = logging.getLogger(__name__) |
| 206 | if loglevel is not None: |
| 207 | logger.setLevel(loglevel) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 208 | self.logger = logger |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 209 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 210 | self.messages = {} |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 211 | self.services = {} |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 212 | self.id_names = [] |
| 213 | self.id_msgdef = [] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 214 | self.header = VPPType('header', [['u16', 'msgid'], |
| 215 | ['u32', 'client_index']]) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 216 | self.apifiles = [] |
Ole Troan | 3d31f00 | 2017-01-26 11:13:00 +0100 | [diff] [blame] | 217 | self.event_callback = None |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 218 | self.message_queue = queue.Queue() |
dongjuan | 8493752 | 2017-11-09 14:46:36 +0800 | [diff] [blame] | 219 | self.read_timeout = read_timeout |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 220 | self.async_thread = async_thread |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 221 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 222 | if use_socket: |
| 223 | from . vpp_transport_socket import VppTransport |
| 224 | else: |
| 225 | from . vpp_transport_shmem import VppTransport |
| 226 | |
Ole Troan | f5984bd | 2016-12-18 13:15:08 +0100 | [diff] [blame] | 227 | if not apifiles: |
| 228 | # Pick up API definitions from default directory |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 229 | try: |
| 230 | apifiles = self.find_api_files() |
| 231 | except RuntimeError: |
| 232 | # In test mode we don't care that we can't find the API files |
| 233 | if testmode: |
| 234 | apifiles = [] |
| 235 | else: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 236 | raise VPPRuntimeError |
Ole Troan | f5984bd | 2016-12-18 13:15:08 +0100 | [diff] [blame] | 237 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 238 | for file in apifiles: |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 239 | with open(file) as apidef_file: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 240 | self.process_json_file(apidef_file) |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 241 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 242 | self.apifiles = apifiles |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 243 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 244 | # Basic sanity check |
Ole Troan | f5984bd | 2016-12-18 13:15:08 +0100 | [diff] [blame] | 245 | if len(self.messages) == 0 and not testmode: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 246 | raise VPPValueError(1, 'Missing JSON message definitions') |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 247 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 248 | self.transport = VppTransport(self, read_timeout=read_timeout, |
| 249 | server_address=server_address) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 250 | # Make sure we allow VPP to clean up the message rings. |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 251 | atexit.register(vpp_atexit, weakref.ref(self)) |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 252 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 253 | class ContextId(object): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 254 | """Thread-safe provider of unique context IDs.""" |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 255 | def __init__(self): |
| 256 | self.context = 0 |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 257 | self.lock = threading.Lock() |
| 258 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 259 | def __call__(self): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 260 | """Get a new unique (or, at least, not recently used) context.""" |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 261 | with self.lock: |
| 262 | self.context += 1 |
| 263 | return self.context |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 264 | get_context = ContextId() |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 265 | |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 266 | def get_type(self, name): |
| 267 | return vpp_get_type(name) |
| 268 | |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 269 | @classmethod |
| 270 | def find_api_dir(cls): |
| 271 | """Attempt to find the best directory in which API definition |
| 272 | files may reside. If the value VPP_API_DIR exists in the environment |
| 273 | then it is first on the search list. If we're inside a recognized |
| 274 | location in a VPP source tree (src/scripts and src/vpp-api/python) |
| 275 | then entries from there to the likely locations in build-root are |
| 276 | added. Finally the location used by system packages is added. |
| 277 | |
| 278 | :returns: A single directory name, or None if no such directory |
| 279 | could be found. |
| 280 | """ |
| 281 | dirs = [] |
| 282 | |
| 283 | if 'VPP_API_DIR' in os.environ: |
| 284 | dirs.append(os.environ['VPP_API_DIR']) |
| 285 | |
| 286 | # perhaps we're in the 'src/scripts' or 'src/vpp-api/python' dir; |
| 287 | # in which case, plot a course to likely places in the src tree |
| 288 | import __main__ as main |
| 289 | if hasattr(main, '__file__'): |
| 290 | # get the path of the calling script |
| 291 | localdir = os.path.dirname(os.path.realpath(main.__file__)) |
| 292 | else: |
| 293 | # use cwd if there is no calling script |
Andrey "Zed" Zaikin | 68e2ffb | 2018-04-24 14:50:02 +0300 | [diff] [blame] | 294 | localdir = os.getcwd() |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 295 | localdir_s = localdir.split(os.path.sep) |
| 296 | |
| 297 | def dmatch(dir): |
| 298 | """Match dir against right-hand components of the script dir""" |
| 299 | d = dir.split('/') # param 'dir' assumes a / separator |
Ole Troan | afddd83 | 2018-02-28 14:55:20 +0100 | [diff] [blame] | 300 | length = len(d) |
| 301 | return len(localdir_s) > length and localdir_s[-length:] == d |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 302 | |
| 303 | def sdir(srcdir, variant): |
| 304 | """Build a path from srcdir to the staged API files of |
| 305 | 'variant' (typically '' or '_debug')""" |
| 306 | # Since 'core' and 'plugin' files are staged |
| 307 | # in separate directories, we target the parent dir. |
| 308 | return os.path.sep.join(( |
| 309 | srcdir, |
| 310 | 'build-root', |
| 311 | 'install-vpp%s-native' % variant, |
| 312 | 'vpp', |
| 313 | 'share', |
| 314 | 'vpp', |
| 315 | 'api', |
| 316 | )) |
| 317 | |
| 318 | srcdir = None |
| 319 | if dmatch('src/scripts'): |
| 320 | srcdir = os.path.sep.join(localdir_s[:-2]) |
| 321 | elif dmatch('src/vpp-api/python'): |
| 322 | srcdir = os.path.sep.join(localdir_s[:-3]) |
| 323 | elif dmatch('test'): |
| 324 | # we're apparently running tests |
| 325 | srcdir = os.path.sep.join(localdir_s[:-1]) |
| 326 | |
| 327 | if srcdir: |
| 328 | # we're in the source tree, try both the debug and release |
| 329 | # variants. |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 330 | dirs.append(sdir(srcdir, '_debug')) |
| 331 | dirs.append(sdir(srcdir, '')) |
| 332 | |
| 333 | # Test for staged copies of the scripts |
| 334 | # For these, since we explicitly know if we're running a debug versus |
| 335 | # release variant, target only the relevant directory |
| 336 | if dmatch('build-root/install-vpp_debug-native/vpp/bin'): |
| 337 | srcdir = os.path.sep.join(localdir_s[:-4]) |
| 338 | dirs.append(sdir(srcdir, '_debug')) |
| 339 | if dmatch('build-root/install-vpp-native/vpp/bin'): |
| 340 | srcdir = os.path.sep.join(localdir_s[:-4]) |
| 341 | dirs.append(sdir(srcdir, '')) |
| 342 | |
| 343 | # finally, try the location system packages typically install into |
| 344 | dirs.append(os.path.sep.join(('', 'usr', 'share', 'vpp', 'api'))) |
| 345 | |
| 346 | # check the directories for existance; first one wins |
| 347 | for dir in dirs: |
| 348 | if os.path.isdir(dir): |
| 349 | return dir |
| 350 | |
| 351 | return None |
| 352 | |
| 353 | @classmethod |
| 354 | def find_api_files(cls, api_dir=None, patterns='*'): |
| 355 | """Find API definition files from the given directory tree with the |
| 356 | given pattern. If no directory is given then find_api_dir() is used |
| 357 | to locate one. If no pattern is given then all definition files found |
| 358 | in the directory tree are used. |
| 359 | |
| 360 | :param api_dir: A directory tree in which to locate API definition |
| 361 | files; subdirectories are descended into. |
| 362 | If this is None then find_api_dir() is called to discover it. |
| 363 | :param patterns: A list of patterns to use in each visited directory |
| 364 | when looking for files. |
| 365 | This can be a list/tuple object or a comma-separated string of |
| 366 | patterns. Each value in the list will have leading/trialing |
| 367 | whitespace stripped. |
| 368 | The pattern specifies the first part of the filename, '.api.json' |
| 369 | is appended. |
| 370 | The results are de-duplicated, thus overlapping patterns are fine. |
| 371 | If this is None it defaults to '*' meaning "all API files". |
| 372 | :returns: A list of file paths for the API files found. |
| 373 | """ |
| 374 | if api_dir is None: |
| 375 | api_dir = cls.find_api_dir() |
| 376 | if api_dir is None: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 377 | raise VPPApiError("api_dir cannot be located") |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 378 | |
| 379 | if isinstance(patterns, list) or isinstance(patterns, tuple): |
| 380 | patterns = [p.strip() + '.api.json' for p in patterns] |
| 381 | else: |
| 382 | patterns = [p.strip() + '.api.json' for p in patterns.split(",")] |
| 383 | |
| 384 | api_files = [] |
| 385 | for root, dirnames, files in os.walk(api_dir): |
| 386 | # iterate all given patterns and de-dup the result |
| 387 | files = set(sum([fnmatch.filter(files, p) for p in patterns], [])) |
| 388 | for filename in files: |
| 389 | api_files.append(os.path.join(root, filename)) |
| 390 | |
| 391 | return api_files |
| 392 | |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 393 | @property |
| 394 | def api(self): |
| 395 | if not hasattr(self, "_api"): |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 396 | raise VPPApiError("Not connected, api definitions not available") |
Klement Sekera | 7112c54 | 2017-03-01 09:53:19 +0100 | [diff] [blame] | 397 | return self._api |
| 398 | |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 399 | def make_function(self, msg, i, multipart, do_async): |
| 400 | if (do_async): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 401 | def f(**kwargs): |
| 402 | return self._call_vpp_async(i, msg, **kwargs) |
| 403 | else: |
| 404 | def f(**kwargs): |
| 405 | return self._call_vpp(i, msg, multipart, **kwargs) |
| 406 | |
| 407 | f.__name__ = str(msg.name) |
| 408 | f.__doc__ = ", ".join(["%s %s" % |
| 409 | (msg.fieldtypes[j], k) |
| 410 | for j, k in enumerate(msg.fields)]) |
| 411 | return f |
| 412 | |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 413 | def _register_functions(self, do_async=False): |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 414 | self.id_names = [None] * (self.vpp_dictionary_maxid + 1) |
| 415 | self.id_msgdef = [None] * (self.vpp_dictionary_maxid + 1) |
Klement Sekera | 8aedf5e | 2018-07-06 11:07:21 +0200 | [diff] [blame] | 416 | self._api = VppApiDynamicMethodHolder() |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 417 | for name, msg in vpp_iterator(self.messages): |
| 418 | n = name + '_' + msg.crc[2:] |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 419 | i = self.transport.get_msg_index(n.encode()) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 420 | if i > 0: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 421 | self.id_msgdef[i] = msg |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 422 | self.id_names[i] = name |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 423 | |
| 424 | # Create function for client side messages. |
| 425 | if name in self.services: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 426 | if 'stream' in self.services[name] and \ |
| 427 | self.services[name]['stream']: |
Ole Troan | dfb984d | 2018-12-07 14:31:16 +0100 | [diff] [blame] | 428 | multipart = True |
| 429 | else: |
| 430 | multipart = False |
| 431 | f = self.make_function(msg, i, multipart, do_async) |
| 432 | setattr(self._api, name, FuncWrapper(f)) |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 433 | else: |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 434 | self.logger.debug( |
| 435 | 'No such message type or failed CRC checksum: %s', n) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 436 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 437 | def connect_internal(self, name, msg_handler, chroot_prefix, rx_qlen, |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 438 | do_async): |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 439 | pfx = chroot_prefix.encode() if chroot_prefix else None |
| 440 | |
| 441 | rv = self.transport.connect(name.encode(), pfx, msg_handler, rx_qlen) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 442 | if rv != 0: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 443 | raise VPPIOError(2, 'Connect failed') |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 444 | self.vpp_dictionary_maxid = self.transport.msg_table_max_index() |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 445 | self._register_functions(do_async=do_async) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 446 | |
| 447 | # Initialise control ping |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 448 | crc = self.messages['control_ping'].crc |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 449 | self.control_ping_index = self.transport.get_msg_index( |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 450 | ('control_ping' + '_' + crc[2:]).encode()) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 451 | self.control_ping_msgdef = self.messages['control_ping'] |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 452 | if self.async_thread: |
| 453 | self.event_thread = threading.Thread( |
| 454 | target=self.thread_msg_handler) |
| 455 | self.event_thread.daemon = True |
| 456 | self.event_thread.start() |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 457 | return rv |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 458 | |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 459 | def connect(self, name, chroot_prefix=None, do_async=False, rx_qlen=32): |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 460 | """Attach to VPP. |
| 461 | |
| 462 | name - the name of the client. |
| 463 | chroot_prefix - if VPP is chroot'ed, the prefix of the jail |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 464 | do_async - if true, messages are sent without waiting for a reply |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 465 | rx_qlen - the length of the VPP message receive queue between |
| 466 | client and server. |
| 467 | """ |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 468 | msg_handler = self.transport.get_callback(do_async) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 469 | return self.connect_internal(name, msg_handler, chroot_prefix, rx_qlen, |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 470 | do_async) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 471 | |
Ole Troan | 6bf177c | 2017-08-17 10:34:32 +0200 | [diff] [blame] | 472 | def connect_sync(self, name, chroot_prefix=None, rx_qlen=32): |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 473 | """Attach to VPP in synchronous mode. Application must poll for events. |
| 474 | |
| 475 | name - the name of the client. |
| 476 | chroot_prefix - if VPP is chroot'ed, the prefix of the jail |
| 477 | rx_qlen - the length of the VPP message receive queue between |
| 478 | client and server. |
| 479 | """ |
| 480 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 481 | return self.connect_internal(name, None, chroot_prefix, rx_qlen, |
Ole Troan | eabd607 | 2018-08-09 12:50:55 +0200 | [diff] [blame] | 482 | do_async=False) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 483 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 484 | def disconnect(self): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 485 | """Detach from VPP.""" |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 486 | rv = self.transport.disconnect() |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 487 | self.message_queue.put("terminate event thread") |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 488 | return rv |
| 489 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 490 | def msg_handler_sync(self, msg): |
| 491 | """Process an incoming message from VPP in sync mode. |
| 492 | |
| 493 | The message may be a reply or it may be an async notification. |
| 494 | """ |
| 495 | r = self.decode_incoming_msg(msg) |
| 496 | if r is None: |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 497 | return |
| 498 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 499 | # If we have a context, then use the context to find any |
| 500 | # request waiting for a reply |
| 501 | context = 0 |
| 502 | if hasattr(r, 'context') and r.context > 0: |
| 503 | context = r.context |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 504 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 505 | if context == 0: |
| 506 | # No context -> async notification that we feed to the callback |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 507 | self.message_queue.put_nowait(r) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 508 | else: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 509 | raise VPPIOError(2, 'RPC reply message received in event handler') |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 510 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame^] | 511 | def has_context(self, msg): |
| 512 | if len(msg) < 10: |
| 513 | return False |
| 514 | |
| 515 | header = VPPType('header_with_context', [['u16', 'msgid'], |
| 516 | ['u32', 'client_index'], |
| 517 | ['u32', 'context']]) |
| 518 | |
| 519 | (i, ci, context), size = header.unpack(msg, 0) |
| 520 | if self.id_names[i] == 'rx_thread_exit': |
| 521 | return |
| 522 | |
| 523 | # |
| 524 | # Decode message and returns a tuple. |
| 525 | # |
| 526 | msgobj = self.id_msgdef[i] |
| 527 | if 'context' in msgobj.field_by_name and context >= 0: |
| 528 | return True |
| 529 | return False |
| 530 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 531 | def decode_incoming_msg(self, msg, no_type_conversion=False): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 532 | if not msg: |
Ole Troan | 3cc4971 | 2017-03-08 12:02:24 +0100 | [diff] [blame] | 533 | self.logger.warning('vpp_api.read failed') |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 534 | return |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame^] | 535 | |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 536 | (i, ci), size = self.header.unpack(msg, 0) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 537 | if self.id_names[i] == 'rx_thread_exit': |
| 538 | return |
| 539 | |
| 540 | # |
| 541 | # Decode message and returns a tuple. |
| 542 | # |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 543 | msgobj = self.id_msgdef[i] |
| 544 | if not msgobj: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 545 | raise VPPIOError(2, 'Reply message undefined') |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 546 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 547 | r, size = msgobj.unpack(msg, ntc=no_type_conversion) |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 548 | return r |
| 549 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 550 | def msg_handler_async(self, msg): |
| 551 | """Process a message from VPP in async mode. |
| 552 | |
| 553 | In async mode, all messages are returned to the callback. |
| 554 | """ |
| 555 | r = self.decode_incoming_msg(msg) |
| 556 | if r is None: |
| 557 | return |
| 558 | |
| 559 | msgname = type(r).__name__ |
| 560 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 561 | if self.event_callback: |
| 562 | self.event_callback(msgname, r) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 563 | |
| 564 | def _control_ping(self, context): |
| 565 | """Send a ping command.""" |
| 566 | self._call_vpp_async(self.control_ping_index, |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 567 | self.control_ping_msgdef, |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 568 | context=context) |
| 569 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 570 | def validate_args(self, msg, kwargs): |
| 571 | d = set(kwargs.keys()) - set(msg.field_by_name.keys()) |
| 572 | if d: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 573 | raise VPPValueError('Invalid argument {} to {}' |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 574 | .format(list(d), msg.name)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 575 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame^] | 576 | def _call_vpp(self, i, msgdef, multipart, **kwargs): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 577 | """Given a message, send the message and await a reply. |
| 578 | |
| 579 | msgdef - the message packing definition |
| 580 | i - the message type index |
| 581 | multipart - True if the message returns multiple |
| 582 | messages in return. |
| 583 | context - context number - chosen at random if not |
| 584 | supplied. |
| 585 | The remainder of the kwargs are the arguments to the API call. |
| 586 | |
| 587 | The return value is the message or message array containing |
| 588 | the response. It will raise an IOError exception if there was |
| 589 | no response within the timeout window. |
| 590 | """ |
| 591 | |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 592 | if 'context' not in kwargs: |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 593 | context = self.get_context() |
| 594 | kwargs['context'] = context |
| 595 | else: |
| 596 | context = kwargs['context'] |
| 597 | kwargs['_vl_msg_id'] = i |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 598 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 599 | no_type_conversion = kwargs.pop('_no_type_conversion', False) |
| 600 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 601 | try: |
| 602 | if self.transport.socket_index: |
| 603 | kwargs['client_index'] = self.transport.socket_index |
| 604 | except AttributeError: |
| 605 | pass |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame^] | 606 | self.validate_args(msgdef, kwargs) |
| 607 | |
| 608 | logging.debug(call_logger(msgdef, kwargs)) |
| 609 | |
| 610 | b = msgdef.pack(kwargs) |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 611 | self.transport.suspend() |
| 612 | |
| 613 | self.transport.write(b) |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 614 | |
| 615 | if multipart: |
| 616 | # Send a ping after the request - we use its response |
| 617 | # to detect that we have seen all results. |
| 618 | self._control_ping(context) |
| 619 | |
| 620 | # Block until we get a reply. |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 621 | rl = [] |
| 622 | while (True): |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 623 | msg = self.transport.read() |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 624 | if not msg: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 625 | raise VPPIOError(2, 'VPP API client: read failed') |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 626 | r = self.decode_incoming_msg(msg, no_type_conversion) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 627 | msgname = type(r).__name__ |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 628 | if context not in r or r.context == 0 or context != r.context: |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 629 | # Message being queued |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 630 | self.message_queue.put_nowait(r) |
| 631 | continue |
| 632 | |
| 633 | if not multipart: |
| 634 | rl = r |
| 635 | break |
| 636 | if msgname == 'control_ping_reply': |
| 637 | break |
| 638 | |
| 639 | rl.append(r) |
| 640 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 641 | self.transport.resume() |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 642 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame^] | 643 | logger.debug(return_logger(rl)) |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 644 | return rl |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 645 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 646 | def _call_vpp_async(self, i, msg, **kwargs): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 647 | """Given a message, send the message and await a reply. |
| 648 | |
| 649 | msgdef - the message packing definition |
| 650 | i - the message type index |
| 651 | context - context number - chosen at random if not |
| 652 | supplied. |
| 653 | The remainder of the kwargs are the arguments to the API call. |
| 654 | """ |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 655 | if 'context' not in kwargs: |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 656 | context = self.get_context() |
| 657 | kwargs['context'] = context |
| 658 | else: |
| 659 | context = kwargs['context'] |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 660 | try: |
| 661 | if self.transport.socket_index: |
| 662 | kwargs['client_index'] = self.transport.socket_index |
| 663 | except AttributeError: |
| 664 | kwargs['client_index'] = 0 |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 665 | kwargs['_vl_msg_id'] = i |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 666 | b = msg.pack(kwargs) |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 667 | |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 668 | self.transport.write(b) |
Ole Troan | 7e3a875 | 2016-12-05 10:27:09 +0100 | [diff] [blame] | 669 | |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 670 | def register_event_callback(self, callback): |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 671 | """Register a callback for async messages. |
Ole Troan | a03f4ef | 2016-12-02 12:53:55 +0100 | [diff] [blame] | 672 | |
Ole Troan | 5016f99 | 2017-01-19 09:44:44 +0100 | [diff] [blame] | 673 | This will be called for async notifications in sync mode, |
| 674 | and all messages in async mode. In sync mode, replies to |
| 675 | requests will not come here. |
| 676 | |
| 677 | callback is a fn(msg_type_name, msg_type) that will be |
| 678 | called when a message comes in. While this function is |
| 679 | executing, note that (a) you are in a background thread and |
| 680 | may wish to use threading.Lock to protect your datastructures, |
| 681 | and (b) message processing from VPP will stop (so if you take |
| 682 | a long while about it you may provoke reply timeouts or cause |
| 683 | VPP to fill the RX buffer). Passing None will disable the |
| 684 | callback. |
| 685 | """ |
| 686 | self.event_callback = callback |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 687 | |
| 688 | def thread_msg_handler(self): |
Ole Troan | 94495f2 | 2018-08-02 11:58:12 +0200 | [diff] [blame] | 689 | """Python thread calling the user registered message handler. |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 690 | |
| 691 | This is to emulate the old style event callback scheme. Modern |
| 692 | clients should provide their own thread to poll the event |
| 693 | queue. |
| 694 | """ |
| 695 | while True: |
| 696 | r = self.message_queue.get() |
Klement Sekera | 180402d | 2018-02-17 10:58:37 +0100 | [diff] [blame] | 697 | if r == "terminate event thread": |
| 698 | break |
Ole Troan | dfc9b7c | 2017-03-06 23:51:57 +0100 | [diff] [blame] | 699 | msgname = type(r).__name__ |
Ole Troan | 4df9716 | 2017-07-07 16:06:08 +0200 | [diff] [blame] | 700 | if self.event_callback: |
| 701 | self.event_callback(msgname, r) |
Chris Luke | 52bf22e | 2017-11-03 23:32:38 -0400 | [diff] [blame] | 702 | |
| 703 | |
| 704 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |