o
    va                     @   s`   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	Z	ddl
mZ G dd deZdS )	zZeroMQ interface class.    N)b)logger)GlancesExport)asbytesc                       s:   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Z  ZS )Exportz,This class manages the ZeroMQ export module.Nc                    sT   t t| j||d d| _| jdg dg d| _| js td d| _| 	 | _
dS )zInit the ZeroMQ export IF.)configargsNZzeromq)hostportprefix)Zmandatoriesoptions   )superr   __init__r   Z	load_confexport_enablesysexitcontextinitclient)selfr   r   	__class__ @/usr/lib/python3/dist-packages/glances/exports/glances_zeromq.pyr   %   s   
zExport.__init__c              
   C   s   | j sdS d| j| j}zt | _| jtj}|	| W n! t
yA } ztd||f  td W Y d}~|S d}~ww td|  |S )z*Init the connection to the CouchDB server.Nztcp://{}:{}z'Cannot connect to ZeroMQ server %s (%s)r   z!Connected to the ZeroMQ server %s)r   formatr	   r
   zmqZContextr   ZsocketZPUBZbind	Exceptionr   Zcriticalr   r   info)r   Z
server_uriZ	publisherer   r   r   r   :   s   
zExport.initc                 C   s0   | j dur
| j   | jdur| j  dS dS )zClose the socket and contextN)r   closer   Zdestroy)r   r   r   r   r   M   s
   


zExport.exitc              
   C   s   t d| tt||}|i krdS t| jt|tt	|g}z	| j
| W dS  tyI } zt d|| W Y d}~dS d}~ww )z&Write the points to the ZeroMQ server.zExport {} stats to ZeroMQFz%Cannot export {} stats to ZeroMQ ({})NT)r   debugr   dictzipr   r   r   jsondumpsr   Zsend_multipartr   error)r   namecolumnsZpointsdatamessager   r   r   r   exportT   s    zExport.export)NN)	__name__
__module____qualname____doc__r   r   r   r+   __classcell__r   r   r   r   r   !   s    r   )r/   r   r$   Zglances.compatr   Zglances.loggerr   Zglances.exports.glances_exportr   r   Zzmq.utils.strtypesr   r   r   r   r   r   <module>   s   