a Vd@sddlZddlZddlZddlZddlZddlmZmZddlmZ ddl m Z ddl m ZGdddeZdd ZGd d d ejZed kredS) N)LsmError ErrorNumber) SocketEOF) DataDecoder) DataEncoderc@s|eZdZdZdZddZddZddZd d Ze d d Z d dZ ddZ ddZ ddZdddZdddZddZdS) 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. | |qXt t j dn t t j dWn tjyzt t jdYn0|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_STREAMospathexistsaccessR_OKW_OKZconnectrrZPLUGIN_SOCKET_PERMISSIONZPLUGIN_NOT_EXISTr!ZPLUGIN_IPC_FAIL)r)rrrr get_socketYs"   zTransPort.get_socketcCs|jdS)z@ Closes the transport and the underlying socket N)rcloserrrrr/rszTransPort.closec Csfz(|d|d}tj|td}||Wn8tjy`}zttjdt |WYd}~n d}~00dS)z Sends a request given a method and arguments. Note: arguments must be in the form that can be automatically serialized to json dmethodidparamsclsz,Error while sending a message to the plug-inN) jsondumps _DataEncoderrr r!rrr"r)rr3argsrrZserrrsend_reqxs zTransPort.send_reqcCs"|}t|rtj|tdSdS)zG Reads a message and returns the parsed version of it. r6N)r$r r8loads _DataDecoder)rrrrrread_reqszTransPort.read_reqcCs||||\}}|S)z; Sends a request and waits for a response. )r< read_resp)rr3r;replymsg_idrrrrpcs  z TransPort.rpcNcCs*||||dd}|tj|tddS)z, Used to transmit an error. )codemessager)r4r!r6Nrr8r9r:)rrBZ error_coderrr#rrr send_errorszTransPort.send_errorr1cCs"||d}|tj|tddS)z- Used to transmit a response )r4resultr6NrF)rrHrBrrrr send_resps zTransPort.send_respcCsH|}tj|td}d|vr.|d|dfS|d}tfi|dS)Nr6rHr4r!)r$r8r=r>r)rrrespr#rrrr@s zTransPort.read_resp)N)r1)__name__ __module__ __qualname____doc__rrrr$r% staticmethodr.r/r<r?rCrGrIr@rrrrrs     rcCst|}|}zn|ddkrf|ddkrN||d|dd|ddn||d|}q||dW|n |0dS) z) Test echo server for test case. r3doner!r4r5 errorcodeerrormsgN)rr?rGrIr/)rZsrvrrrr_servers    rSc@s4eZdZddZddZddZddZd d Zd S) _TestTransportcCsFttjtj\|_|_t|j|_tj t |jfd|_ |j dS)N)targetr;) r Z socketpairr&r'crrclient threadingThreadrSserverstartr0rrrsetUps   z_TestTransport.setUpcCsNgd}|D]<}|jd||j\}}||dk|||kq dS)N)0 z z{}:""zSome text messageZDEADBEEFtestr1)rWr<r@ assertTrue)rZtctrArBrrr test_simples z_TestTransport.test_simplec Csd}d}|jd||d|t|jjz"|jd||d|jWnDty}z,||j|k||j|kWYd}~n d}~00dS)NzTest error messager1r!)rQrR)rWr<Z assertRaisesrr@r`rDr)rZe_msgZe_coder#rrrtest_exceptionssz_TestTransport.test_exceptionsc CstdddD]}d|}dd|d}tj|td}tt|}|tj|}| t|dk|D]}|j t |d qd|j \}} | ||kq dS) Nr irxZdripr1r2r6r )ranger8r9r:rr rrrr`rVsendrrWr@) rrZpayloadrrZhdrZwireirArBrrr test_slows  z_TestTransport.test_slowcCsB|jdd|j\}}||du|j|jdS)NrP)rWr<r@r`rZjoinr/)rrJrBrrrtearDown s  z_TestTransport.tearDownN)rKrLrMr\rbrcrhrjrrrrrTs   rT__main__)r8r r(ZunittestrXZ lsm._commonrrrrZ lsm._datarr>rr:objectrrSZTestCaserTrKmainrrrrs   'C