o
    jz                     @   sl   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 G dd deZ	G dd	 d	eZ
G d
d deZdS )zmThe ``spyne.server.zeromq`` module contains a server implementation that
uses ZeroMQ (zmq.REP) as transport.
    N)process_contexts)MethodContext)
ServerBasec                       s   e Zd Z fddZ  ZS )ZmqMethodContextc                    s    t t| |tj d| j_d S )Nzmq)superr   __init__r   SERVER	transporttype)selfapp	__class__ S/var/www/html/LicenciasMiG/venv/lib/python3.10/site-packages/spyne/server/zeromq.pyr       s   zZmqMethodContext.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c                       sD   e Zd ZdZdZd fdd	Zdd Zd fd	d
	Zdd Z  Z	S )ZeroMQServerzThe ZeroMQ server transport.zhttp://rfc.zeromq.org/Nc                    s   |r|r||j urtdtt| | || _|| _|r!|| _n|r(|j | _nt	 | _|r4|| _
d S | jtj| _
| j
| d S )N(ctx should be the same as socket.context)context
ValueErrorr   r   r   app_urlwsdl_urlctxr   Context
zmq_socketsocketREPbind)r   r   r   r   r   r   r   r   r   r   )   s   


zZeroMQServer.__init__c                 C   s   | j | jS )N)r   get_interface_documenturl)r   r   r   r   __handle_wsdl_request>   s   z"ZeroMQServer.__handle_wsdl_requestutf8c                    s   t t| j||dS )N)in_string_charset)r   r   generate_contexts)r   r   r&   r   r   r   r'   B   s   zZeroMQServer.generate_contextsc                 C   s   	 d}t | }| j g|_| |}|d |dd }}d|_|jr,|j|_|j}n| | |jr<|j|_|j}n| 	| |j
rK|j
|_|j
}| | t| || | jd|j |  q)Runs the ZeroMQ server.TNr          )r   r   recv	in_stringr'   activein_error
out_objectget_in_objectget_out_object	out_errorget_out_stringr   sendjoin
out_stringclose)r   errorinitial_ctxcontextsp_ctxothersr   r   r   serve_foreverF   s.   



zZeroMQServer.serve_foreverNNN)r%   )
r   r   r   __doc__r
   r   "_ZeroMQServer__handle_wsdl_requestr'   r=   r   r   r   r   r   r   %   s    r   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
ZeroMQThreadPoolServerzCreate a ZeroMQ server transport with several background workers,
    allowing asynchronous calls.

    More details on the pattern http://zguide.zeromq.org/page:all#Shared-Queue-DEALER-and-ROUTER-socketsNc                 C   s   |r|r||j urtd|| _|r|| _n|r|j | _nt | _|r(|| _n| jtj| _| j	| dj
| jj| jjd}g | _g | _t|D ]}| ||\}	}
| j|	 | j|
 qK| jtj| _| j	| d S )Nr   zinproc://{tns}.{name})tnsname)r   r   r   r   r   r   frontendr   ROUTERr!   formatrB   rC   poolbackground_jobsrangecreate_workerappendDEALERbackend)r   r   r   	pool_sizer   r   r   be_urliworkerjobr   r   r   r   t   s*   

zZeroMQThreadPoolServer.__init__c                 C   sD   | j tj}|| t| j||d}tj|j	d}d|_
||fS )N)r   )targetT)r   r   r   r    connectr   r   	threadingThreadr=   daemon)r   rP   rO   r   rQ   rR   r   r   r   rJ      s   
z$ZeroMQThreadPoolServer.create_workerc                 C   s@   | j D ]}|  qttj| j| j | j  | j  dS )r(   N)rH   startr   deviceQUEUErD   rM   r7   )r   rR   r   r   r   r=      s
   


z$ZeroMQThreadPoolServer.serve_foreverr>   )r   r   r   r?   r   rJ   r=   r   r   r   r   rA   n   s
    
rA   )r?   rU   r   spyne.auxprocr   spyne.contextr   spyne.serverr   r   r   objectrA   r   r   r   r   <module>   s   I