1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-25 01:38:08 +00:00
gns3-server/gns3server/compute/notification_manager.py

74 lines
2.1 KiB
Python
Raw Normal View History

2016-03-17 14:15:30 +00:00
#!/usr/bin/env python
#
# Copyright (C) 2016 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-05-14 00:48:10 +00:00
from contextlib import contextmanager
from gns3server.utils.notification_queue import NotificationQueue
2016-03-17 14:15:30 +00:00
class NotificationManager:
"""
Manage the notification queue where the controller
will connect to get notifications from computes
2016-03-17 14:15:30 +00:00
"""
def __init__(self):
self._listeners = set()
@contextmanager
def queue(self):
"""
Get a queue of notifications
Use it with Python with
"""
2016-03-17 14:15:30 +00:00
queue = NotificationQueue()
self._listeners.add(queue)
try:
yield queue
finally:
self._listeners.remove(queue)
2016-03-17 14:15:30 +00:00
def emit(self, action, event, **kwargs):
"""
Send an event to all the client listening for notifications
:param action: Action name
:param event: Event to send
2016-05-14 00:48:10 +00:00
:param kwargs: Add this meta to the notification (project_id for example)
2016-03-17 14:15:30 +00:00
"""
2016-03-17 14:15:30 +00:00
for listener in self._listeners:
listener.put_nowait((action, event, kwargs))
@staticmethod
def reset():
NotificationManager._instance = None
@staticmethod
def instance():
"""
Singleton to return only on instance of NotificationManager.
2018-03-15 07:17:39 +00:00
2016-03-17 14:15:30 +00:00
:returns: instance of NotificationManager
"""
2021-04-13 09:16:50 +00:00
if not hasattr(NotificationManager, "_instance") or NotificationManager._instance is None:
2016-03-17 14:15:30 +00:00
NotificationManager._instance = NotificationManager()
return NotificationManager._instance