????

Your IP : 18.119.110.152


Current Path : /lib/python3.6/site-packages/glances/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/glances/__pycache__/autodiscover.cpython-36.pyc

3

Eo�c}%�@s�dZddlZddlZddlmZddlmZy ddlmZ	m
Z
mZmZdZ
Wnek
rddZ
YnXe
r�dZed	d
�e	jd�D��Zejdje	��eekr�ejd
�ejd�ddZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZdS)zBManage autodiscover Glances server (thk to the ZeroConf protocol).�N)�BSD)�logger)�__version__�ServiceBrowser�ServiceInfo�ZeroconfTF�cCsg|]}t|��qS�)�int)�.0Znumr	r	�"/usr/lib/python3.6/autodiscover.py�
<listcomp>sr
�.zZeroconf version {} detected.z'Please install zeroconf 0.17 or higher.�z_%s._tcp.local.�glancesc@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�AutoDiscoveredz1Class to manage the auto discovered servers dict.cCs
g|_dS)N)�_server_list)�selfr	r	r�__init__,szAutoDiscovered.__init__cCs|jS)z.Return the current server list (list of dict).)r)rr	r	r�get_servers_list1szAutoDiscovered.get_servers_listcCs||j||<dS)zCSet the key to the value for the server_pos (position in the list).N)r)r�
server_pos�key�valuer	r	r�
set_server5szAutoDiscovered.set_serverc	CsJ||jd�d||ddddd�}|jj|�tjdt|j�|jf�d	S)
zAdd a new server to the list.�:rr�ZUNKNOWNZDYNAMIC)r�name�ip�portZusernameZpasswordZstatus�typez%Updated servers list (%s servers): %sN)�splitr�appendr�debug�len)rrrrZ
new_serverr	r	r�
add_server9szAutoDiscovered.add_servercCs|xv|jD]l}|d|kry8|jj|�tjd|�tjdt|j�|jf�Wqtk
rrtjd|�YqXqWdS)zRemove a server from the dict.rzRemove server %s from the listz%Updated servers list (%s servers): %sz%Cannot remove server %s from the listN)r�removerr"r#�
ValueError�error)rr�ir	r	r�
remove_serverHszAutoDiscovered.remove_serverN)	�__name__�
__module__�__qualname__�__doc__rrrr$r)r	r	r	rr(src@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�GlancesAutoDiscoverListenerz%Zeroconf listener for Glances server.cCst�|_dS)N)r�servers)rr	r	rrXsz$GlancesAutoDiscoverListener.__init__cCs
|jj�S)z.Return the current server list (list of dict).)r/r)rr	r	rr\sz,GlancesAutoDiscoverListener.get_servers_listcCs|jj|||�dS)zCSet the key to the value for the server_pos (position in the list).N)r/r)rrrrr	r	rr`sz&GlancesAutoDiscoverListener.set_servercCs�|tkrdStjd||f�|j||�}|r�|js:|jr�|jrJ|jdn|jd}tj|�}|j}|j	j
|||�tjd|||f�n
tjd�dS)z�Method called when a new Zeroconf client is detected.

        Note: the return code will never be used

        :return: True if the zeroconf client is a Glances server
        Fz"Check new Zeroconf server: %s / %srz+New Glances server detected (%s from %s:%s)zGNew Glances server detected, but failed to be get Zeroconf ServiceInfo T)
�
zeroconf_typerr"Zget_service_info�	addressesZparsed_addresses�socketZ	inet_ntoarr/r$�infoZwarning)r�zeroconf�srv_type�srv_namer3�addressZ
new_server_ipZnew_server_portr	r	r�add_serviceds

z'GlancesAutoDiscoverListener.add_servicecCs|jj|�tjd|�dS)z Remove the server from the list.z2Glances server %s removed from the autodetect listN)r/r)rr3)rr4r5r6r	r	r�remove_service{sz*GlancesAutoDiscoverListener.remove_serviceN)	r*r+r,r-rrrr8r9r	r	r	rr.Tsr.c@s2eZdZdZddd�Zdd�Zdd�Zd	d
�ZdS)�GlancesAutoDiscoverServerzMImplementation of the Zeroconf protocol (server side for the Glances client).NcCs�trvtjd�yt�|_Wn8tjk
rR}ztjd|�d|_WYdd}~Xq�Xt�|_	t
|jt|j	�|_d|_ntjd�d|_dS)Nz*Init autodiscover mode (Zeroconf protocol)zCannot start Zeroconf (%s)FTz>Cannot start autodiscover mode (Zeroconf lib is not installed))
�zeroconf_tagrr3rr4r2r'�zeroconf_enable_tagr.�listenerrr0Zbrowser)r�args�er	r	rr�s

z"GlancesAutoDiscoverServer.__init__cCstr|jr|jj�SgSdS)z.Return the current server list (dict of dict).N)r;r<r=r)rr	r	rr�s

z*GlancesAutoDiscoverServer.get_servers_listcCstr|jr|jj|||�dS)zCSet the key to the value for the server_pos (position in the list).N)r;r<r=r)rrrrr	r	rr�s
z$GlancesAutoDiscoverServer.set_servercCstr|jr|jj�dS)N)r;r<r4�close)rr	r	rr@�s
zGlancesAutoDiscoverServer.close)N)r*r+r,r-rrrr@r	r	r	rr:�s

r:c@s.eZdZdZd	dd�Zedd��Zdd�ZdS)
�GlancesAutoDiscoverClientzMImplementation of the zeroconf protocol (client side for the Glances server).Nc.Cs�t�rr|j}yt�|_Wn4tjk
rL}ztjdj|��WYdd}~XnXts|y|dkrd|j	�}Wnt
k
rzYnXtj|�}tj||j
�dd}y4ttdj||j
t�tj||�|j
ddi|d�|_WnHtk
�rttdj||j
t�tj||�g|j
ddi|d�|_YnXy|jj|j�Wn4tk
�r`}ztjdj|��WYdd}~XnXtdj|��n
tjd	�dS)
NzCannot start zeroconf: {}z0.0.0.0rz{}:{}.{})r7r�weight�priority�
properties�server)rr1rrBrCrDrEz)Error while announcing Glances server: {}z<Announce the Glances server on the LAN (using {} IP address)zJCannot announce Glances server on the network: zeroconf library not found.)r;Zbind_addressrr4r2r'r�formatr�find_active_ip_address�KeyErrorZ
gethostbynameZgetaddrinforrr0Z	inet_ptonr3�	TypeErrorZregister_service�	Exception�print)rZhostnamer>Zzeroconf_bind_addressr?Zaddress_familyr	r	rr�sP"

"z"GlancesAutoDiscoverClient.__init__cCs6ddl}|j�d|jd}|j|�|jddS)z$Try to find the active IP addresses.rN�defaultrZaddr)�	netifacesZgatewaysZAF_INETZifaddresses)rMZgateway_itfr	r	rrG�sz0GlancesAutoDiscoverClient.find_active_ip_addresscCs tr|jj|j�|jj�dS)N)r;r4Zunregister_servicer3r@)rr	r	rr@�szGlancesAutoDiscoverClient.close)N)r*r+r,r-r�staticmethodrGr@r	r	r	rrA�s
;
rA)rrr)r-r2�sysZglances.globalsrZglances.loggerrr4rZ__zeroconf_versionrrrr;�ImportErrorZzeroconf_min_version�tupler Zzeroconf_versionr"rFZcritical�exitr0�objectrr.r:rAr	r	r	r�<module>
s*


,-%