Skip to content

Commit

Permalink
improve ISession/ITransportHandler and implementations (#1557)
Browse files Browse the repository at this point in the history
improve ISession/ITransportHandler and implementations
  • Loading branch information
oberstet committed May 2, 2022
1 parent 655b04f commit 06b3161
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion autobahn/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
#
###############################################################################

__version__ = '22.4.1.dev8'
__version__ = '22.4.1.dev9'

__build__ = '00000000-0000000'
34 changes: 22 additions & 12 deletions autobahn/wamp/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,6 @@ def abort(self):
@public
class ITransportHandler(abc.ABC):

@public
@property
@abc.abstractmethod
def transport(self) -> Optional[ITransport]:
"""
When the transport this handler is attached to is currently open, this property
can be read from. The property should be considered read-only. When the transport
is gone, this property is set to None.
"""

@public
@abc.abstractmethod
def onOpen(self, transport: ITransport):
Expand Down Expand Up @@ -329,11 +319,31 @@ class ISession(_ABC):
Interface for WAMP sessions.
"""

@public
@property
@abc.abstractmethod
def __init__(self, config: Optional[ComponentConfig] = None):
def config(self) -> ComponentConfig:
"""
Configuration for session.
"""

:param config: Configuration for session.
@public
@property
@abc.abstractmethod
def transport(self) -> Optional[ITransport]:
"""
When the transport this session is attached to is currently open, this property
can be read from. The property should be considered read-only. When the transport
is gone, this property is set to None.
"""

@public
@property
@abc.abstractmethod
def session_details(self) -> Optional[SessionDetails]:
"""
Return details about the session, the same as initially provided to the
:meth:`ISession.onJoin` callback on an implementation.
"""

@public
Expand Down
80 changes: 47 additions & 33 deletions autobahn/wamp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def __init__(self):
ApplicationError.PAYLOAD_SIZE_EXCEEDED: PayloadExceededError,
}

# WAMP ITransport (_not_ a Twisted protocol, which is self.transport - when using Twisted)
self._transport: Optional[ITransport] = None

# session authentication information
self._realm: Optional[str] = None
self._session_id: Optional[int] = None
Expand All @@ -104,12 +107,48 @@ def __init__(self):
self._authprovider: Optional[str] = None
self._authextra: Optional[Dict[str, Any]] = None

# set client role features supported and announced
self._session_roles: Dict[str, role.RoleFeatures] = role.DEFAULT_CLIENT_ROLES

# complete session details
self._session_details: Optional[SessionDetails] = None

# payload transparency codec
self._payload_codec: Optional[IPayloadCodec] = None

# generator for WAMP request IDs
self._request_id_gen = IdGenerator()

@property
def transport(self) -> Optional[ITransport]:
"""
Implements :func:`autobahn.wamp.interfaces.ITransportHandler.transport`
"""
# Note: self._transport (which is a WAMP ITransport) is different from self.transport (which
# is a Twisted protocol when using Twisted)!
return self._transport

@public
def is_connected(self) -> bool:
"""
Implements :func:`autobahn.wamp.interfaces.ISession.is_connected`
"""
return self._transport is not None and self._transport.isOpen()

@public
def is_attached(self) -> bool:
"""
Implements :func:`autobahn.wamp.interfaces.ISession.is_attached`
"""
return self._session_id is not None and self.is_connected()

@property
def session_details(self) -> Optional[SessionDetails]:
"""
Implements :func:`autobahn.wamp.interfaces.ISession.session_details`
"""
return self._session_details

@property
def realm(self) -> Optional[str]:
return self._realm
Expand Down Expand Up @@ -331,16 +370,9 @@ def __init__(self, config: Optional[types.ComponentConfig] = None):
Implements :func:`autobahn.wamp.interfaces.ISession`
"""
BaseSession.__init__(self)
self.config: types.ComponentConfig = config or types.ComponentConfig(realm="realm1")

# set client role features supported and announced
self._session_roles: Dict[str, role.RoleFeatures] = role.DEFAULT_CLIENT_ROLES

# WAMP ITransport (_not_ a Twisted protocol, which is self.transport - when using Twisted)
self._transport: Optional[ITransport] = None
self._session_id: Optional[int] = None
self._realm: Optional[str] = None
self._config: types.ComponentConfig = config or types.ComponentConfig(realm="realm1")

# for keeping transport-closing state
self._goodbye_sent: bool = False
self._transport_is_closing: bool = False

Expand All @@ -365,13 +397,8 @@ def __init__(self, config: Optional[types.ComponentConfig] = None):
self._invocations = {}

@property
def transport(self) -> Optional[ITransport]:
"""
Implements :func:`autobahn.wamp.interfaces.ITransportHandler.transport`
"""
# Note: self._transport (which is a WAMP ITransport) is different from self.transport (which
# is a Twisted protocol when using Twisted)!
return self._transport
def config(self) -> types.ComponentConfig:
return self._config

@public
def set_payload_codec(self, payload_codec: Optional[IPayloadCodec]):
Expand Down Expand Up @@ -463,20 +490,6 @@ def disconnect(self):
if self._transport:
self._transport.close()

@public
def is_connected(self) -> bool:
"""
Implements :func:`autobahn.wamp.interfaces.ISession.is_connected`
"""
return self._transport is not None and self._transport.isOpen()

@public
def is_attached(self) -> bool:
"""
Implements :func:`autobahn.wamp.interfaces.ISession.is_attached`
"""
return self._session_id is not None and self.is_connected()

@public
def onUserError(self, fail, msg):
"""
Expand Down Expand Up @@ -580,7 +593,7 @@ def success(res):
self._authextra = msg.authextra
self._router_roles = msg.roles

details = SessionDetails(
self._session_details = SessionDetails(
realm=self._realm,
session=self._session_id,
authid=self._authid,
Expand All @@ -594,12 +607,13 @@ def success(res):
resumable=msg.resumable,
resume_token=msg.resume_token,
)

# firing 'join' *before* running onJoin, so that
# the idiom where you "do stuff" in onJoin --
# possibly including self.leave() -- works
# properly. Besides, there's "ready" that fires
# after 'join' and onJoin have all completed...
d = self.fire('join', self, details)
d = self.fire('join', self, self._session_details)
# add a logging errback first, which will ignore any
# errors from fire()
txaio.add_callbacks(
Expand All @@ -609,7 +623,7 @@ def success(res):
# this should run regardless
txaio.add_callbacks(
d,
lambda _: txaio.as_future(self.onJoin, details),
lambda _: txaio.as_future(self.onJoin, self._session_details),
None
)
# ignore any errors from onJoin (XXX or, should that be fatal?)
Expand Down

0 comments on commit 06b3161

Please sign in to comment.