Sindbad~EG File Manager

Current Path : /lib64/python3.6/site-packages/lsm/__pycache__/
Upload File :
Current File : //lib64/python3.6/site-packages/lsm/__pycache__/_transport.cpython-36.opt-1.pyc

3

�.'e� �@s�ddlZddlZddlZddlZddlZddlZddlmZmZddlm	Z
ddlmZ
ddlmZGdd�de�Zdd	�ZGd
d�dej�Zedkr�ej�dS)
�N)�LsmError�ErrorNumber)�	SocketEOF)�DataDecoder)�DataEncoderc@s|eZdZdZdZdd�Zdd�Zdd�Zd	d
�Ze	dd��Z
d
d�Zdd�Zdd�Z
dd�Zddd�Zddd�Zdd�ZdS)�	TransPorta�
    Provides wire serialization by using json.  Loosely conforms to json-rpc,
    however a length header was added so that we would have the ability to use
    non sax like json parsers, which are more abundant.

    <Zero padded 10 digit number [1..2**32] for the length followed by
    valid json.

    Notes:
    id field (json-rpc) is present but currently not being used.
    This is available to be expanded on later.
    �
cCsX|dkrtd��t�}x6t|�|krL|jj|t|��}|sBt��||7}qW|jd�S)z�
        Reads l number of bytes before returning.  Will raise a SocketEOF
        if socket returns zero bytes (i.e. socket no longer connected)
        �z Trying to read less than 1 byte!zutf-8)�
ValueError�	bytearray�len�sZrecv�
_SocketEOF�decode)�self�l�data�r�r�"/usr/lib64/python3.6/_transport.py�	_read_all-szTransPort._read_allcCsP|dkst|�dkrtd��tjtt|��|j�|}|jjt|jd���dS)z]
        Sends the json formatted message by pre-appending the length
        first.
        Nr	zMsg argument emptyzutf-8)	rr
�str�zfill�HDR_LENr
Zsendall�bytes�encode)r�msgr
rrr�	_send_msg?szTransPort._send_msgcCsZy|j|j�}|jt|��}Wn6tjk
rT}zttjdt|���WYdd}~XnX|S)zk
        Reads header first to get the length and then the remaining
        bytes of the message.
        z.Error while reading a message from the plug-inN)	rr�int�socket�errorrr�TRANSPORT_COMMUNICATIONr)rrr�errr�	_recv_msgMszTransPort._recv_msgcCs
||_dS)N)r
)rZsocket_descriptorrrr�__init__\szTransPort.__init__cCs�yZtjtjtj�}tjj|�rLtj|tjtjB�r>|j	|�qXt
tjd��nt
tj
d��Wn"tjk
r|t
tjd��YnX|S)zE
        Returns a connected socket from the passed in path.
        z-Permissions are incorrect for IPC socket filezPlug-in appears to not existz*Unable to connect to lsmd, daemon started?)r�AF_UNIX�SOCK_STREAM�os�path�exists�access�R_OK�W_OKZconnectrrZPLUGIN_SOCKET_PERMISSIONZPLUGIN_NOT_EXISTr ZPLUGIN_IPC_FAIL)r(r
rrr�
get_socket_s
zTransPort.get_socketcCs|jj�dS)z@
        Closes the transport and the underlying socket
        N)r
�close)rrrrr.wszTransPort.closecCsdy(|d|d�}tj|td�}|j|�Wn6tjk
r^}zttjdt	|���WYdd}~XnXdS)z�
        Sends a request given a method and arguments.
        Note: arguments must be in the form that can be automatically
        serialized to json
        �d)�method�id�params)�clsz,Error while sending a message to the plug-inN)
�json�dumps�_DataEncoderrrr rrr!r)rr0�argsrrZserrr�send_req}szTransPort.send_reqcCs"|j�}t|�rtj|td�SdS)zG
        Reads a message and returns the parsed version of it.
        )r3N)r#rr4�loads�_DataDecoder)rrrrr�read_req�szTransPort.read_reqcCs|j||�|j�\}}|S)z;
        Sends a request and waits for a response.
        )r8�	read_resp)rr0r7�reply�msg_idrrr�rpc�sz
TransPort.rpcNcCs*||||d�d�}|jtj|td��dS)z,
        Used to transmit an error.
        )�code�messager)r1r )r3N)rr4r5r6)rr>Z
error_coderrr"rrr�
send_error�szTransPort.send_errorr/cCs"||d�}|jtj|td��dS)z-
        Used to transmit a response
        )r1�result)r3N)rr4r5r6)rrCr>rrrr�	send_resp�s
zTransPort.send_respcCsD|j�}tj|td�}d|kr.|d|dfS|d}tf|��dS)N)r3rCr1r )r#r4r9r:r)rr�respr"rrrr<�szTransPort.read_resp)N)r/)�__name__�
__module__�__qualname__�__doc__rrrr#r$�staticmethodr-r.r8r;r?rBrDr<rrrrrs		

rcCs�t|�}|j�}zjxV|ddkrh|ddkrP|j|d|dd|dd�n|j|d�|j�}qW|j|d�Wd|j�XdS)	z)
    Test echo server for test case.
    r0�doner r1r2�	errorcode�errormsgN)rr;rBrDr.)r
Zsrvrrrr�_server�s
rNc@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�_TestTransportcCsFtjtjtj�\|_|_t|j�|_tj	t
|jfd�|_|jj�dS)N)�targetr7)
rZ
socketpairr%r&�cr
r�client�	threadingZThreadrN�server�start)rrrr�setUp�s
z_TestTransport.setUpcCsZddddddg}xD|D]<}|jjd|�|jj�\}}|j|dk�|j||k�qWdS)	N�0� z   z{}:""zSome text messageZDEADBEEFZtestr/)rRr8r<�
assertTrue)rZtc�tr=r>rrr�test_simple�s
z_TestTransport.test_simplecCs�d}d}|jjd||d��|jt|jj�y"|jjd||d��|jj�WnBtk
r�}z&|j|j|k�|j|j|k�WYdd}~XnXdS)NzTest error messager/r )rLrM)rRr8ZassertRaisesrr<rYr@r)rZe_msgZe_coder"rrr�test_exceptions�s

z_TestTransport.test_exceptionsc	Cs�x�tddd�D]�}d|}dd|d�}tj|td�}tjt|�tj�|}|j	t|�dk�x|D]}|j
j|�qbW|jj
�\}}|j	||k�qWdS)	Nr	ir�xZdripr/)r0r1r2)r3)�ranger4r5r6�stringrrrrrYrQ�sendrRr<)	rrZpayloadrrZwire�ir=r>rrr�	test_slow�s
z_TestTransport.test_slowcCs8|jjdd�|jj�\}}|j|dk�|jj�dS)NrK)rRr8r<rYrT�join)rrEr>rrr�tearDownsz_TestTransport.tearDownN)rFrGrHrVr[r\rbrdrrrrrO�s
		rO�__main__)r4rr_r'ZunittestrSZlsm._commonrrrrZ	lsm._datarr:rr6�objectrrNZTestCaserOrF�mainrrrr�<module>s>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists