| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Telefonica S.A. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 17 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 18 | from http import HTTPStatus |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame] | 19 | import logging |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 20 | from threading import Lock |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 21 | |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame] | 22 | from osm_common.common_utils import FakeLock |
| 23 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 24 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 25 | |
| 26 | |
| 27 | class MsgException(Exception): |
| 28 | """ |
| 29 | Base Exception class for all msgXXXX exceptions |
| 30 | """ |
| 31 | |
| tierno | a4eaefe | 2018-06-20 17:37:17 +0200 | [diff] [blame] | 32 | def __init__(self, message, http_code=HTTPStatus.SERVICE_UNAVAILABLE): |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 33 | """ |
| 34 | General exception |
| 35 | :param message: descriptive text |
| 36 | :param http_code: <http.HTTPStatus> type. It contains ".value" (http error code) and ".name" (http error name |
| 37 | """ |
| 38 | self.http_code = http_code |
| 39 | Exception.__init__(self, "messaging exception " + message) |
| 40 | |
| 41 | |
| 42 | class MsgBase(object): |
| 43 | """ |
| 44 | Base class for all msgXXXX classes |
| 45 | """ |
| 46 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 47 | def __init__(self, logger_name="msg", lock=False): |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 48 | """ |
| 49 | Constructor of FsBase |
| 50 | :param logger_name: logging name |
| 51 | :param lock: Used to protect simultaneous access to the same instance class by several threads: |
| 52 | False, None: Do not protect, this object will only be accessed by one thread |
| 53 | True: This object needs to be protected by several threads accessing. |
| 54 | Lock object. Use thi Lock for the threads access protection |
| 55 | """ |
| 56 | self.logger = logging.getLogger(logger_name) |
| 57 | if not lock: |
| 58 | self.lock = FakeLock() |
| 59 | elif lock is True: |
| 60 | self.lock = Lock() |
| 61 | elif isinstance(lock, Lock): |
| 62 | self.lock = lock |
| 63 | else: |
| 64 | raise ValueError("lock parameter must be a Lock class or boolean") |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 65 | |
| 66 | def connect(self, config): |
| 67 | pass |
| 68 | |
| 69 | def disconnect(self): |
| 70 | pass |
| 71 | |
| 72 | def write(self, topic, key, msg): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 73 | raise MsgException( |
| 74 | "Method 'write' not implemented", http_code=HTTPStatus.INTERNAL_SERVER_ERROR |
| 75 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 76 | |
| 77 | def read(self, topic): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 78 | raise MsgException( |
| 79 | "Method 'read' not implemented", http_code=HTTPStatus.INTERNAL_SERVER_ERROR |
| 80 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 81 | |
| Gulsum Atici | a06b854 | 2023-05-09 13:42:13 +0300 | [diff] [blame] | 82 | async def aiowrite(self, topic, key, msg): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 83 | raise MsgException( |
| 84 | "Method 'aiowrite' not implemented", |
| 85 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 86 | ) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 87 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 88 | async def aioread( |
| Gulsum Atici | a06b854 | 2023-05-09 13:42:13 +0300 | [diff] [blame] | 89 | self, topic, callback=None, aiocallback=None, group_id=None, **kwargs |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 90 | ): |
| 91 | raise MsgException( |
| 92 | "Method 'aioread' not implemented", |
| 93 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 94 | ) |