Skip to content

Commit

Permalink
context: Enhancements to transport setup
Browse files Browse the repository at this point in the history
Merges: #270
  • Loading branch information
chrysn committed Aug 15, 2022
2 parents 97c1f3f + 2d196d9 commit 0b35b6e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions aiocoap/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import asyncio
import weakref
import time
from typing import Optional, List

from . import defaults
from .credentials import CredentialsMap
Expand Down Expand Up @@ -161,7 +162,7 @@ async def _append_tokenmanaged_transport(self, token_interface_constructor):

@classmethod
@AwaitOrAenter.decorate
async def create_client_context(cls, *, loggername="coap", loop=None):
async def create_client_context(cls, *, loggername="coap", loop=None, transports: Optional[List[str]] = None):
"""Create a context bound to all addresses on a random listening port.
This is the easiest way to get a context suitable for sending client
Expand All @@ -178,8 +179,10 @@ async def create_client_context(cls, *, loggername="coap", loop=None):

self = cls(loop=loop, serversite=None, loggername=loggername)

selected_transports = transports or defaults.get_default_clienttransports(loop=loop)

# FIXME make defaults overridable (postponed until they become configurable too)
for transportname in defaults.get_default_clienttransports(loop=loop):
for transportname in selected_transports:
if transportname == 'udp6':
from .transports.udp6 import MessageInterfaceUDP6
await self._append_tokenmanaged_messagemanaged_transport(
Expand Down Expand Up @@ -216,7 +219,7 @@ async def create_client_context(cls, *, loggername="coap", loop=None):

@classmethod
@AwaitOrAenter.decorate
async def create_server_context(cls, site, bind=None, *, loggername="coap-server", loop=None, _ssl_context=None, multicast=[], server_credentials=None):
async def create_server_context(cls, site, bind=None, *, loggername="coap-server", loop=None, _ssl_context=None, multicast=[], server_credentials=None, transports: Optional[List[str]] = None):
"""Create a context, bound to all addresses on the CoAP port (unless
otherwise specified in the ``bind`` argument).
Expand Down Expand Up @@ -250,7 +253,9 @@ async def create_server_context(cls, site, bind=None, *, loggername="coap-server

multicast_done = not multicast

for transportname in defaults.get_default_servertransports(loop=loop):
selected_transports = transports or defaults.get_default_servertransports(loop=loop)

for transportname in selected_transports:
if transportname == 'udp6':
from .transports.udp6 import MessageInterfaceUDP6

Expand Down
2 changes: 1 addition & 1 deletion aiocoap/transports/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async def create_transport(cls, tman: interfaces.TokenManager, log, loop, *, cli
host, port = server_bind
if port is None:
port = 8683
else:
elif port != 0:
# FIXME see module documentation
port = port + 3000

Expand Down

0 comments on commit 0b35b6e

Please sign in to comment.