1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-01 12:48:14 +00:00
gns3-server/gns3server/endpoints/compute/notifications.py

93 lines
3.0 KiB
Python
Raw Normal View History

2020-10-02 06:37:50 +00:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 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/>.
"""
API endpoints for compute notifications.
"""
import asyncio
from fastapi import APIRouter, WebSocket
2020-10-02 06:37:50 +00:00
from gns3server.compute.notification_manager import NotificationManager
from starlette.endpoints import WebSocketEndpoint
2020-10-02 06:37:50 +00:00
import logging
log = logging.getLogger(__name__)
router = APIRouter()
@router.websocket_route("/notifications/ws")
class ComputeWebSocketNotifications(WebSocketEndpoint):
"""
Receive compute notifications about the controller from WebSocket stream.
"""
2020-10-02 06:37:50 +00:00
async def on_connect(self, websocket: WebSocket) -> None:
2020-10-02 06:37:50 +00:00
await websocket.accept()
log.info(f"New client {websocket.client.host}:{websocket.client.port} has connected to compute WebSocket")
self._notification_task = asyncio.ensure_future(self._stream_notifications(websocket))
2020-10-02 06:37:50 +00:00
async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:
2020-10-02 06:37:50 +00:00
self._notification_task.cancel()
log.info(f"Client {websocket.client.host}:{websocket.client.port} has disconnected from controller WebSocket"
f" with close code {close_code}")
2020-10-02 06:37:50 +00:00
async def _stream_notifications(self, websocket: WebSocket) -> None:
2020-10-02 06:37:50 +00:00
with NotificationManager.instance().queue() as queue:
2020-10-02 06:37:50 +00:00
while True:
notification = await queue.get_json(5)
await websocket.send_text(notification)
if __name__ == '__main__':
import uvicorn
from fastapi import FastAPI
from starlette.responses import HTMLResponse
app = FastAPI()
app.include_router(router)
html = """
<!DOCTYPE html>
<html>
<body>
<ul id='messages'>
</ul>
<script>
var ws = new WebSocket("ws://localhost:8000/notifications/ws");
ws.onmessage = function(event) {
var messages = document.getElementById('messages')
var message = document.createElement('li')
var content = document.createTextNode(event.data)
message.appendChild(content)
messages.appendChild(message)
};
</script>
</body>
</html>
"""
@app.get("/")
async def get() -> HTMLResponse:
return HTMLResponse(html)
uvicorn.run(app, host="localhost", port=8000)