#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2022, Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Provide a set of objects to represent different stages of a connectionto a Bokeh server.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromabcimportABCMeta,abstractmethodfromenumimportEnum,autofromtypingimportTYPE_CHECKING,Any# Bokeh importsfrom..core.typesimportIDifTYPE_CHECKING:from..protocol.messageimportMessagefrom.connectionimportClientConnection#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('CONNECTED_BEFORE_ACK','CONNECTED_AFTER_ACK','DISCONNECTED','ErrorReason','NOT_YET_CONNECTED','WAITING_FOR_REPLY',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------
[docs]classNOT_YET_CONNECTED(State):''' The ``ClientConnection`` is not yet connected. '''asyncdefrun(self,connection:ClientConnection)->None:awaitconnection._connect_async()
[docs]classCONNECTED_BEFORE_ACK(State):''' The ``ClientConnection`` connected to a Bokeh server, but has not yet received an ACK from it. '''asyncdefrun(self,connection:ClientConnection)->None:awaitconnection._wait_for_ack()
[docs]classCONNECTED_AFTER_ACK(State):''' The ``ClientConnection`` connected to a Bokeh server, and has received an ACK from it. '''asyncdefrun(self,connection:ClientConnection)->None:awaitconnection._handle_messages()
[docs]classDISCONNECTED(State):''' The ``ClientConnection`` was connected to a Bokeh server, but is now disconnected. '''
[docs]def__init__(self,reason:ErrorReason=ErrorReason.NO_ERROR,error_code:int|None=None,error_detail:str="")->None:''' Constructs a DISCONNECT-State with given reason (``ErrorReason`` enum), error id and additional information provided as string. '''self._error_code=error_codeself._error_detail=error_detailself._error_reason=reason
@propertydeferror_reason(self)->ErrorReason:''' The reason for the error encoded as an enumeration value. '''returnself._error_reason@propertydeferror_code(self)->int|None:''' Holds the error code, if any. None otherwise. '''returnself._error_code@propertydeferror_detail(self)->str:''' Holds the error message, if any. Empty string otherwise. '''returnself._error_detailasyncdefrun(self,connection:ClientConnection)->None:return
[docs]classWAITING_FOR_REPLY(State):''' The ``ClientConnection`` has sent a message to the Bokeh Server which should generate a paired reply, and is waiting for the reply. '''_reply:Message[Any]|Nonedef__init__(self,reqid:ID)->None:self._reqid=reqidself._reply=None@propertydefreply(self)->Message[Any]|None:''' The reply from the server. (``None`` until the reply arrives) '''returnself._reply@propertydefreqid(self)->ID:''' The request ID of the originating message. '''returnself._reqidasyncdefrun(self,connection:ClientConnection)->None:message=awaitconnection._pop_message()ifmessageisNone:awaitconnection._transition_to_disconnected(DISCONNECTED(ErrorReason.NETWORK_ERROR))elif'reqid'inmessage.headerandmessage.header['reqid']==self.reqid:self._reply=messageawaitconnection._transition(CONNECTED_AFTER_ACK())else:awaitconnection._next()