B
    q\                 @   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d0dd	Z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 )1r   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.7/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k	r$|dk	r$td|dkrR|dk	rL|jsDtdqR|j}nt }yB|d dd}t|t	j
|dd	| _|   t|| _d
| _ W nL t	jk
r } z,|jdkrtdntd|j|jW dd}~X Y nX 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 {}: {})r   lockfile
ValueErrorZ
is_runningr   Zparamsr   replacer   xmlrpcZServerProxyr   pingcopydeepcopyZProtocolErrorZerrcodeformaterrmsg)r	   hubZ
hub_paramsZ	pool_sizeZurlpr
   r
   r   connect    s,    




zSAMPHubProxy.connectc             C   s   d| _ d| _i | _dS )z7
        Disconnect from the current SAMP Hub.
        NF)r   r   r   )r	   r
   r
   r   
disconnectX   s    zSAMPHubProxy.disconnectc             C   s   | j   d S )N)r   server_close)r	   r
   r
   r   r   `   s    zSAMPHubProxy.server_closec             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_hubc   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   k   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_callbackq   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"   w   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    Z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    	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getSubscriptions)r	   r    r%   r
   r
   r   get_subscriptions   s    zSAMPHubProxy.get_subscriptionsc             C   s   | j |S )zD
        Proxy to ``getRegisteredClients`` SAMP Hub method.
        )r   ZgetRegisteredClients)r	   r    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   r/      s    zSAMPHubProxy.callc             C   s   | j |||S )z7
        Proxy to ``callAll`` SAMP Hub method.
        )r   ZcallAll)r	   r    r0   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   r3      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*   r+   r.   r/   r1   r2   r3   r
   r
   r
   r   r      s.   
8)r   Zxmlrpc.clientZclientr   errorsr   Zutilsr   Zlockfile_helpersr   __all__r   r
   r
   r
   r   <module>   s   