Current File : //lib/python2.7/site-packages/lsm/_transport.pyo
�
-�P`c@s�ddlZddlZddlZddlZddlZddlZddlmZmZddlm	Z
ddlmZ
ddlmZdefd��YZd�Zd	ejfd
��YZedkr�ej�ndS(i����N(tLsmErrortErrorNumber(t	SocketEOF(tDataDecoder(tDataEncodert	TransPortcBs�eZdZdZd�Zd�Zd�Zd�Zed��Z	d�Z
d�Zd	�Zd
�Z
dd�Zdd
�Zd�ZRS(s�
    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.
    i
cCs�|dkrtd��nt�}xNt|�|krt|jj|t|��}|sgt��n||7}q'W|jd�S(s�
        Reads l number of bytes before returning.  Will raise a SocketEOF
        if socket returns zero bytes (i.e. socket no longer connected)
        is Trying to read less than 1 byte!sutf-8(t
ValueErrort	bytearraytlentstrecvt
_SocketEOFtdecode(tselftltdatatr((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt	_read_all-s	cCsu|dkst|�dkr-td��ntjtt|��|j�|}|jjt|j	d���dS(s]
        Sends the json formatted message by pre-appending the length
        first.
        isMsg argument emptysutf-8N(
tNoneRRtstrtzfilltHDR_LENR	tsendalltbytestencode(R
tmsgR	((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt	_send_msg?s%cCscy+|j|j�}|jt|��}Wn1tjk
r^}ttjdt|���nX|S(sk
        Reads header first to get the length and then the remaining
        bytes of the message.
        s.Error while reading a message from the plug-in(	RRtinttsocketterrorRRtTRANSPORT_COMMUNICATIONR(R
RRte((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt	_recv_msgMs	cCs
||_dS(N(R	(R
tsocket_descriptor((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt__init__\scCs�y�tjtjtj�}tjj|�rntj|tjtjB�rY|j	|�q�t
tjd��nt
tj
d��Wn&tjk
r�t
tjd��nX|S(sE
        Returns a connected socket from the passed in path.
        s-Permissions are incorrect for IPC socket filesPlug-in appears to not exists*Unable to connect to lsmd, daemon started?(RtAF_UNIXtSOCK_STREAMtostpathtexiststaccesstR_OKtW_OKtconnectRRtPLUGIN_SOCKET_PERMISSIONtPLUGIN_NOT_EXISTRtPLUGIN_IPC_FAIL(R&R	((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt
get_socket_s		
	
cCs|jj�dS(s@
        Closes the transport and the underlying socket
        N(R	tclose(R
((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyR0wscCsyyAi|d6dd6|d6}tj|dt�}|j|�Wn1tjk
rt}ttjdt	|���nXdS(s�
        Sends a request given a method and arguments.
        Note: arguments must be in the form that can be automatically
        serialized to json
        tmethodidtidtparamstclss,Error while sending a message to the plug-inN(
tjsontdumpst_DataEncoderRRRRRRR(R
R1targsRRtse((s2/usr/lib/python2.7/site-packages/lsm/_transport.pytsend_req}s	cCs/|j�}t|�r+tj|dt�SdS(sG
        Reads a message and returns the parsed version of it.
        R4N(R RR5tloadst_DataDecoder(R
R((s2/usr/lib/python2.7/site-packages/lsm/_transport.pytread_req�scCs&|j||�|j�\}}|S(s;
        Sends a request and waits for a response.
        (R:t	read_resp(R
R1R8treplytmsg_id((s2/usr/lib/python2.7/site-packages/lsm/_transport.pytrpc�scCsIi|d6i|d6|d6|d6d6}|jtj|dt��dS(s,
        Used to transmit an error.
        R2tcodetmessageRRR4N(RR5R6R7(R
R@t
error_codeRRR((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt
send_error�sidcCs4i|d6|d6}|jtj|dt��dS(s-
        Used to transmit a response
        R2tresultR4N(RR5R6R7(R
RFR@R((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt	send_resp�scCsY|j�}tj|dt�}d|kr?|d|dfS|d}t|��dS(NR4RFR2R(R R5R;R<R(R
RtrespR((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyR>�s
N(t__name__t
__module__t__doc__RRRR R"tstaticmethodR/R0R:R=RARRERGR>(((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyRs										cCs�t|�}|j�}z�xk|ddkr�|ddkrh|j|d|dd|dd�n|j|d�|j�}qW|j|d�Wd|j�XdS(	s)
    Test echo server for test case.
    R1tdoneRR2R3t	errorcodeterrormsgN(RR=RERGR0(R	tsrvR((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt_server�st_TestTransportcBs5eZd�Zd�Zd�Zd�Zd�ZRS(cCshtjtjtj�\|_|_t|j�|_tj	dt
d|jf�|_|jj�dS(NttargetR8(
Rt
socketpairR#R$tcR	Rtclientt	threadingtThreadRQtservertstart(R
((s2/usr/lib/python2.7/site-packages/lsm/_transport.pytsetUp�s
!cCs{ddddddg}x\|D]T}|jjd|�|jj�\}}|j|dk�|j||k�qWdS(	Nt0t s   s{}:""sSome text messagetDEADBEEFttestid(RVR:R>t
assertTrue(R
ttcttR?R@((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyttest_simple�s
cCs�d}d}|jjdi|d6|d6�|jt|jj�y2|jjdi|d6|d6�|jj�Wn?tk
r�}|j|j|k�|j|j|k�nXdS(NsTest error messageidRRNRO(RVR:tassertRaisesRR>R`RBR(R
te_msgte_codeR((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyttest_exceptions�s!c	Cs�x�tddd�D]�}d|}idd6dd6|d	6}tj|d
t�}tjt|�tj�|}|j	t|�dk�x|D]}|j
j|�q�W|jj
�\}}|j	||k�qWdS(Niii
txtdripR1idR2R3R4(trangeR5R6R7tstringRRRRR`RUtsendRVR>(	R
RtpayloadRRtwiretiR?R@((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt	test_slow�s

cCsL|jjdd�|jj�\}}|j|dk�|jj�dS(NRM(RVR:RR>R`RYtjoin(R
RHR@((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyttearDowns(RIRJR[RcRgRpRr(((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyRR�s
						t__main__(R5RRkR%tunittestRWtlsm._commonRRRRt	lsm._dataRR<RR7tobjectRRQtTestCaseRRRItmain(((s2/usr/lib/python2.7/site-packages/lsm/_transport.pyt<module>s�	>