-
Notifications
You must be signed in to change notification settings - Fork 1
[RTC-365] Server notifications #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
70fc756
Add protobuf
roznawsk 77e7156
wip
roznawsk c75c978
Better proto compiler
roznawsk 393c63b
Private module
roznawsk e3e3681
Notifier WIP
roznawsk 7928b8f
Notifier WIP
roznawsk 450beb7
Add notifier
roznawsk 87081b1
add missing deps
roznawsk 9a02d27
fix docker tests
roznawsk a201a3a
Update readme
roznawsk fb4bf3a
Fix CI
roznawsk 98834db
Update docstrings
roznawsk 8ceaf6f
Move events to separate package
roznawsk aa72e1e
Add testing section
roznawsk 04e11a5
Remove obnoxious cursing from the code
roznawsk 7cc9c1c
Update docs
roznawsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [submodule "protos"] | ||
| path = protos | ||
| url = https://github.com/jellyfish-dev/protos.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Terminate on errors | ||
| set -e | ||
|
|
||
|
|
||
| printf "Synchronising submodules... " | ||
| git submodule sync --recursive >> /dev/null | ||
| git submodule update --recursive --remote --init >> /dev/null | ||
| printf "DONE\n\n" | ||
|
|
||
| server_file="./protos/jellyfish/server_notifications.proto" | ||
| printf "Compiling: file $server_file" | ||
| protoc -I . --python_betterproto_out=./jellyfish/events/_protos $server_file | ||
| printf "\tDONE\n" | ||
|
|
||
| peer_file="./protos/jellyfish/peer_notifications.proto" | ||
| printf "Compiling: file $peer_file" | ||
| protoc -I . --python_betterproto_out=./tests/support/protos $peer_file | ||
| printf "\tDONE\n" | ||
sgfn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| aenum==3.1.15 | ||
| betterproto==1.2.5 | ||
| pydantic==1.10.12 | ||
| pytest==7.1.3 | ||
| pytest-asyncio==0.21.1 | ||
| python_dateutil==2.8.2 | ||
| setuptools==67.6.1 | ||
| typing_extensions==4.7.1 | ||
| urllib3 >= 1.25.3, < 2 | ||
| pylint==2.17.5 | ||
| pdoc==14.1.0 | ||
| websockets==11.0.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| ''' | ||
| Notifier listening to WebSocket events | ||
| ''' | ||
|
|
||
| import asyncio | ||
| from typing import Callable, Any | ||
|
|
||
| import betterproto | ||
|
|
||
| from websockets import client | ||
| from websockets.exceptions import ConnectionClosed | ||
|
|
||
| from jellyfish.events import ( | ||
| ServerMessage, ServerMessageAuthRequest, ServerMessageAuthenticated, | ||
| ServerMessageSubscribeRequest, ServerMessageEventType, ServerMessageSubscribeResponse, | ||
| ServerMessageMetricsReport) | ||
|
|
||
|
|
||
| class Notifier: | ||
| ''' | ||
| Allows for receiving WebSocket messages from Jellyfish. | ||
| ''' | ||
|
|
||
| def __init__(self, | ||
| server_address: str = 'localhost:5002', server_api_token: str = 'development'): | ||
| self._server_address = server_address | ||
| self._server_api_token = server_api_token | ||
| self._websocket = None | ||
| self._ready = False | ||
|
|
||
| self._ready_event: asyncio.Event = None | ||
|
|
||
| self._notification_handler: Callable = None | ||
| self._metrics_handler: Callable = None | ||
|
|
||
| def on_server_notification(self, handler: Callable[[Any], None]): | ||
| ''' | ||
| Decorator used for defining handler for ServerNotifications | ||
| i.e. all messages other than `ServerMessageMetricsReport`. | ||
| ''' | ||
| self._notification_handler = handler | ||
| return handler | ||
|
|
||
| def on_metrics(self, handler: Callable[[ServerMessageMetricsReport], None]): | ||
| ''' | ||
| Decorator used for defining handler for `ServerMessageMetricsReport`. | ||
| ''' | ||
| self._metrics_handler = handler | ||
| return handler | ||
|
|
||
| async def connect(self): | ||
| ''' | ||
| A coroutine which connects Notifier to Jellyfish and listens for all incoming | ||
| messages from the Jellyfish. | ||
|
|
||
| It runs until the connection isn't closed. | ||
|
|
||
| The incoming messages are handled by the functions defined using the | ||
| `on_server_notification` and `on_metrics` decorators. | ||
|
|
||
| The handlers have to be defined before calling `connect`, | ||
| otherwise the messages won't be received. | ||
| ''' | ||
| address = f'ws://{self._server_address}/socket/server/websocket' | ||
| async with client.connect(address) as websocket: | ||
| try: | ||
| self._websocket = websocket | ||
| await self._authenticate() | ||
|
|
||
| if self._notification_handler: | ||
| await self._subscribe_event( | ||
| event=ServerMessageEventType.EVENT_TYPE_SERVER_NOTIFICATION) | ||
|
|
||
| if self._metrics_handler: | ||
| await self._subscribe_event(event=ServerMessageEventType.EVENT_TYPE_METRICS) | ||
|
Comment on lines
+70
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does that mean that you cannot add handler after you have been connected? If so, I would mention it somewhere. |
||
|
|
||
| self._ready = True | ||
| if self._ready_event: | ||
| self._ready_event.set() | ||
|
|
||
| await self._receive_loop() | ||
| finally: | ||
| self._websocket = None | ||
|
|
||
| async def wait_ready(self) -> True: | ||
| ''' | ||
| Waits until the notifier is connected and authenticated to Jellyfish. | ||
|
|
||
| If already connected, returns `True` immediately. | ||
| ''' | ||
| if self._ready: | ||
| return True | ||
|
|
||
| if self._ready_event is None: | ||
| self._ready_event = asyncio.Event() | ||
|
|
||
| await self._ready_event.wait() | ||
|
|
||
| async def _authenticate(self): | ||
| msg = ServerMessage(auth_request=ServerMessageAuthRequest(token=self._server_api_token)) | ||
| await self._websocket.send(bytes(msg)) | ||
|
|
||
| try: | ||
| message = await self._websocket.recv() | ||
| except ConnectionClosed as exception: | ||
| if 'invalid token' in str(exception): | ||
| raise RuntimeError('Invalid server_api_token') from exception | ||
| raise | ||
|
|
||
| message = ServerMessage().parse(message) | ||
|
|
||
| _type, message = betterproto.which_one_of(message, 'content') | ||
| assert isinstance(message, ServerMessageAuthenticated) | ||
sgfn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async def _receive_loop(self): | ||
| while True: | ||
| message = await self._websocket.recv() | ||
| message = ServerMessage().parse(message) | ||
| _which, message = betterproto.which_one_of(message, 'content') | ||
|
|
||
| if isinstance(message, ServerMessageMetricsReport): | ||
| self._metrics_handler(message) | ||
| else: | ||
| self._notification_handler(message) | ||
|
|
||
| async def _subscribe_event(self, event: ServerMessageEventType): | ||
| request = ServerMessage(subscribe_request=ServerMessageSubscribeRequest(event)) | ||
|
|
||
| await self._websocket.send(bytes(request)) | ||
| message = await self._websocket.recv() | ||
| message = ServerMessage().parse(message) | ||
| _which, message = betterproto.which_one_of(message, "content") | ||
| assert isinstance(message, ServerMessageSubscribeResponse) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| ''' | ||
| Server events being sent Jellyfish | ||
| ''' | ||
|
|
||
| # Private messages | ||
| from jellyfish.events._protos.jellyfish import ( | ||
| ServerMessage, ServerMessageAuthenticated, ServerMessageAuthRequest, ServerMessageEventType, | ||
| ServerMessageSubscribeResponse, ServerMessageSubscribeRequest) | ||
|
|
||
| # Exported messages | ||
| from jellyfish.events._protos.jellyfish import ( | ||
| ServerMessageComponentCrashed, ServerMessageHlsPlayable, | ||
| ServerMessageMetricsReport, ServerMessagePeerCrashed, ServerMessagePeerConnected, | ||
| ServerMessagePeerDisconnected, ServerMessageRoomCrashed, ServerMessageRoomDeleted, | ||
| ServerMessageRoomCreated) | ||
|
|
||
| __all__ = [ | ||
| 'ServerMessageComponentCrashed', 'ServerMessageHlsPlayable', | ||
| 'ServerMessageMetricsReport', 'ServerMessagePeerCrashed', 'ServerMessagePeerConnected', | ||
| 'ServerMessagePeerDisconnected', 'ServerMessageRoomCrashed', 'ServerMessageRoomDeleted', | ||
| 'ServerMessageRoomCreated'] |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.