a
    ߙfb                     @   sP   d dl Z d dlmZ ddlmZ ddlmZ ddlm	Z	 dgZ
G dd dZdS )    N   )SAMPHubError)ServerProxyPool)get_main_running_hubSAMPHubProxyc                   @   s   e Zd ZdZdd Zedd Zd.dd	Zd
d Zedd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- ZdS )/r   zh
    Proxy class to simplify the client interaction with a SAMP hub (via the
    standard profile).
    c                 C   s   d | _ d| _d S )NF)proxy
_connectedself r   5lib/python3.9/site-packages/astropy/samp/hub_proxy.py__init__   s    zSAMPHubProxy.__init__c                 C   s   | j S )zH
        Whether the hub proxy is currently connected to a hub.
        )r   r	   r   r   r   is_connected   s    zSAMPHubProxy.is_connectedN   c              
   C   s   d| _ i | _|dur$|dur$td|du rR|durL|jsDtdqR|j}nt }zB|d dd}t|t	j
|dd	| _|   t|| _d
| _ W nR t	jy } z8|jdkrtdntd|j d|j W Y d}~n
d}~0 0 dS )a  
        Connect to the current SAMP Hub.

        Parameters
        ----------
        hub : `~astropy.samp.SAMPHubServer`, optional
            The hub to connect to.

        hub_params : dict, optional
            Optional dictionary containing the lock-file content of the Hub
            with which to connect. This dictionary has the form
            ``{<token-name>: <token-string>, ...}``.

        pool_size : int, optional
            The number of socket connections opened to communicate with the
            Hub.
        FNz&Cannot specify both hub and hub_paramszHub is not runningzsamp.hub.xmlrpc.url\ r   )Z
allow_noneTi  z=Unauthorized access. Basic Authentication required or failed.zProtocol Error z: )r   lockfile
ValueErrorZ
is_runningr   paramsr   replacer   xmlrpcZServerProxyr   pingcopydeepcopyZProtocolErrorZerrcodeerrmsg)r
   hubZ
hub_paramsZ	pool_sizeZurlpr   r   r   connect    s,    



zSAMPHubProxy.connectc                 C   s*   | j dur| j   d| _ d| _i | _dS )z7
        Disconnect from the current SAMP Hub.
        NF)r   Zshutdownr   r   r	   r   r   r   
disconnectW   s
    

zSAMPHubProxy.disconnectc                 C   s
   | j jjS )z
        Property to abstract away the path to the hub, which allows this class
        to be used for other profiles.
        )r   Zsampr   r	   r   r   r   	_samp_huba   s    zSAMPHubProxy._samp_hubc                 C   s
   | j  S )zL
        Proxy to ``ping`` SAMP Hub method (Standard Profile only).
        )r   r   r	   r   r   r   r   i   s    zSAMPHubProxy.pingc                 C   s   | j ||S )zY
        Proxy to ``setXmlrpcCallback`` SAMP Hub method (Standard Profile only).
        )r   ZsetXmlrpcCallback)r
   private_keyZxmlrpc_addrr   r   r   set_xmlrpc_callbacko   s    z SAMPHubProxy.set_xmlrpc_callbackc                 C   s   | j |S )z8
        Proxy to ``register`` SAMP Hub method.
        )r   register)r
   Zsecretr   r   r   r"   u   s    zSAMPHubProxy.registerc                 C   s   | j |S )z:
        Proxy to ``unregister`` SAMP Hub method.
        )r   
unregisterr
   r    r   r   r   r#   {   s    zSAMPHubProxy.unregisterc                 C   s   | j ||S )z?
        Proxy to ``declareMetadata`` SAMP Hub method.
        )r   ZdeclareMetadata)r
   r    metadatar   r   r   declare_metadata   s    zSAMPHubProxy.declare_metadatac                 C   s   | j ||S )z;
        Proxy to ``getMetadata`` SAMP Hub method.
        )r   ZgetMetadatar
   r    Z	client_idr   r   r   get_metadata   s    zSAMPHubProxy.get_metadatac                 C   s   | j ||S )zD
        Proxy to ``declareSubscriptions`` SAMP Hub method.
        )r   ZdeclareSubscriptions)r
   r    Zsubscriptionsr   r   r   declare_subscriptions   s    z"SAMPHubProxy.declare_subscriptionsc                 C   s   | j ||S )z@
        Proxy to ``getSubscriptions`` SAMP Hub method.
        )r   ZgetSubscriptionsr'   r   r   r   get_subscriptions   s    zSAMPHubProxy.get_subscriptionsc                 C   s   | j |S )zD
        Proxy to ``getRegisteredClients`` SAMP Hub method.
        )r   ZgetRegisteredClientsr$   r   r   r   get_registered_clients   s    z#SAMPHubProxy.get_registered_clientsc                 C   s   | j ||S )zD
        Proxy to ``getSubscribedClients`` SAMP Hub method.
        )r   ZgetSubscribedClients)r
   r    Zmtyper   r   r   get_subscribed_clients   s    z#SAMPHubProxy.get_subscribed_clientsc                 C   s   | j |||S )z6
        Proxy to ``notify`` SAMP Hub method.
        )r   notify)r
   r    recipient_idmessager   r   r   r-      s    zSAMPHubProxy.notifyc                 C   s   | j ||S )z9
        Proxy to ``notifyAll`` SAMP Hub method.
        )r   Z	notifyAll)r
   r    r/   r   r   r   
notify_all   s    zSAMPHubProxy.notify_allc                 C   s   | j ||||S )z4
        Proxy to ``call`` SAMP Hub method.
        )r   call)r
   r    r.   msg_tagr/   r   r   r   r1      s    zSAMPHubProxy.callc                 C   s   | j |||S )z7
        Proxy to ``callAll`` SAMP Hub method.
        )r   ZcallAll)r
   r    r2   r/   r   r   r   call_all   s    zSAMPHubProxy.call_allc                 C   s   | j ||||S )z;
        Proxy to ``callAndWait`` SAMP Hub method.
        )r   ZcallAndWait)r
   r    r.   r/   Ztimeoutr   r   r   call_and_wait   s    zSAMPHubProxy.call_and_waitc                 C   s   | j |||S )z5
        Proxy to ``reply`` SAMP Hub method.
        )r   reply)r
   r    Zmsg_idZresponser   r   r   r5      s    zSAMPHubProxy.reply)NNr   )__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r!   r"   r#   r&   r(   r)   r*   r+   r,   r-   r0   r1   r3   r4   r5   r   r   r   r   r      s0   

7

)r   Zxmlrpc.clientZclientr   errorsr   Zutilsr   Zlockfile_helpersr   __all__r   r   r   r   r   <module>   s   