Sindbad~EG File Manager
3
�.'e� � @ s� d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dlm Z
d dlmZ
d dlmZ G dd� de�Zdd � ZG d
d� dej�Zedkr�ej� dS )
� N)�LsmError�ErrorNumber)� SocketEOF)�DataDecoder)�DataEncoderc @ s| e Zd ZdZdZdd� Zdd� Zdd� Zd d
� Ze dd� �Z
d
d� Zdd� Zdd� 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.
�
c C sX |dk rt d��t� }x6t|�|k rL| 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_allc C sP |dkst |�dk rtd��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
r r r � _send_msg? s zTransPort._send_msgc C sZ y| j | j�}| j t|��}W n6 tjk
rT } zttjdt|���W Y dd}~X nX |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) r r �int�socket�errorr r �TRANSPORT_COMMUNICATIONr )r r r �er r r � _recv_msgM s zTransPort._recv_msgc C s
|| _ d S )N)r
)r Zsocket_descriptorr r r �__init__\ s zTransPort.__init__c C s� yZt j t jt j�}tjj| �rLtj| tjtjB �r>|j | � qXt
tjd��nt
tj
d��W n" t jk
r| t
tjd��Y nX |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_OKZconnectr r ZPLUGIN_SOCKET_PERMISSIONZPLUGIN_NOT_EXISTr ZPLUGIN_IPC_FAIL)r( r
r r r �
get_socket_ s
zTransPort.get_socketc C s | j j� dS )z@
Closes the transport and the underlying socket
N)r
�close)r r r r r. w s zTransPort.closec C sd y(|d|d�}t j|td�}| j|� W n6 tjk
r^ } zttjdt |���W Y dd}~X nX dS )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�_DataEncoderr r r r r r! r )r r0 �argsr r Zser r r �send_req} s zTransPort.send_reqc C s"