
    Oh                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKr\R                  " \
5      rSrSS1r " S S\R                  5      r " S S	5      r " S
 S\5      r " S S\5      r " S S5      r " S S5      r\SS4S jrg)z
Simplistic RPC implementation.
Exposes all functions of a Server object.

This code is for demonstration purposes only, and does not include certain
security protections. It is not meant to be run on an untrusted network or
in a production environment.
    Ni /  numpyznumpy.core.multiarrayc                       \ rS rSrS rSrg)RestrictedUnpickler#   c                     U[         ;   a   [        [        R                  " U5      U5      $ [        R
                  " SU< SU< S35      e)Nzglobal '.z' is forbidden)safe_modulesgetattr	importlibimport_modulepickleUnpicklingError)selfmodulenames      I/var/www/html/shao/venv/lib/python3.13/site-packages/faiss/contrib/rpc.py
find_classRestrictedUnpickler.find_class%   s?    \!9226:DAA$$&,d&4 5 	5     N)__name__
__module____qualname____firstlineno__r   __static_attributes__r   r   r   r   r   #   s    5r   r   c                   4    \ rS rSrSrS rS rS	S jrS rSr	g)
FileSock.   z6wraps a socket so that it is usable by pickle/cPickle c                     Xl         SU l        g )Nr   )socknr)r   r    s     r   __init__FileSock.__init__1   s    	r   c                     SnSnU[        U5      :  a5  U R                  R                  XX2-    5      nX4-  nU[        U5      :  a  M4  g g )N   r   )lenr    send)r   bufbsnssents        r   writeFileSock.write5   sE     3s8m99>>#/2DJB 3s8mr   c                    U =R                   S-  sl         / nSn[        U5      U:  aU  U R                  R                  X-
  5      nU(       d  O0UR	                  U5        U[        U5      -  n[        U5      U:  a  MU  SR                  U5      $ )N   r   r   )r!   r&   r    recvappendjoin)r   r)   bnbrbs        r   readFileSock.read?   sr    
!fRi(BuHHRL#b'MB !fRi xx{r   c                 F    [        5       n U R                  S5      nX-  nM  )zmay be optimized...r/   )bytesr6   r&   chr)r   scs      r   readlineFileSock.readlineM   s'     'iilADA r   )r!   r    N)r%   )
r   r   r   r   __doc__r"   r,   r6   r=   r   r   r   r   r   r   .   s    =r   r   c                       \ rS rSrSrg)
ClientExitW   r   Nr   r   r   r   r   r   r   r   rA   rA   W       r   rA   c                       \ rS rSrSrg)ServerExceptionZ   r   NrC   r   r   r   rF   rF   Z   rD   r   rF   c                   X    \ rS rSrSr\R                  S4S jrS rS r	S r
S rS	 rS
rg)Server^   zf
server protocol. Methods from classes that subclass Server can be called
transparently from a client
 c                 H    X l         X0l        Xl        [        U5      U l        g N)logf
log_prefixconnr   fs)r   r;   rN   rO   s       r   r"   Server.__init__d   s    	$ 	1+r   c                 `    U R                   R                  SU R                  < SU< S35        g )Nz
Sever log z: 
)rN   r,   rO   )r   r;   s     r   log
Server.logn   s    		CDr   c                 *    [        U R                  5      R                  5       u  pU R                  SU-  5        SnSn [        X5      n W" U6 n[$        R'                  S
5         [(        R*                  " X44U R                  SS9  g! [         a    [	        S5      ef = f! [         a"    [        SU-   5      nU R                  S5         Nf = f! [         a  nSR                  [        R                  " [        R                  " 5       S   5      5      [        U5      -   nU R                  S5        [        R                  " S	U R                   5        U R                   R#                  5          SnAGN#SnAff = f! [         a    [	        S5      ef = f)ay  
Executes a single function with associated I/O.
Protocol:
- the arguments and results are serialized with the pickle protocol
- client sends : (fname,args)
    fname = method name to call
    args = tuple of arguments
- server sends result: (rid,st,ret)
    rid = request id
    st = None, or exception if there was during execution
    ret = return value or None if st!=None
z	read argszexecuting method %sNzunknown method zunknown methodrK      zexception in method2   return   protocolzfunction return)r   rQ   loadEOFErrorrA   rU   r
   AttributeError	Exceptionr2   	traceback	format_tbsysexc_infostr	print_excrN   flushLOGinfor   dump)r   fnameargsstretfes          r   one_functionServer.one_functionq   s[   	*/8==?MU 	&./	'd!A
	T(C 		0KK	477Q77  	*[))	*  	' 1% 78BHH%&	'  
	 wwy**3<<>!+<=>s1vEBHH*+499-IIOO
	  	0.//	0sB   %B	  B" C '!E< 	B")CC
E9BE44E9<Fc                 v   U R                  S5          U R                  5         M  ! [         a  nU R                  SU-  5         SnAOSnAf[        R                   a?  nU R                  SU-  5        [
        R                  " SU R                  5         SnAOSnAf[         a5    U R                  S5        [
        R                  " SU R                  5         OJ[         a>    [
        R                  " S[        R                  5        [        R                  " S5         Of = f[        R                  S5        g)	z2main execution loop. Loops and handles exit stateszin exec_loopzClientExit %sNzsocket error %srY   zEOF during communicationr/   z
exit sever)rU   rr   rA   socketerrorrb   rg   rN   r_   BaseExceptionrd   stderrexitri   rj   )r   rq   s     r   	exec_loopServer.exec_loop   s     	 	!!#  	(HH_Q&''|| 	.HH&q()499-- 	.HH/0499- 	3::.HHQK	
 	s-   & 
D"A		D" 5B?D"AD"!D"c                     g rM   r   )r   s    r   exec_loop_cleanupServer.exec_loop_cleanup   s    r   c                     Sn[         R                  " SS[         R                  " 5       -  -   S-   S-   5      nU H  nX-  nM	     U$ )NrK   zFecho ============ `hostname` uptime:; uptime;echo ============ self:; z*ps -p %d -o pid,vsize,rss,%%cpu,nlwp,psr; zecho ============ run queue:;z?ps ar -o user,pid,%cpu,%mem,ni,nlwp,psr,vsz,rss,cputime,command)ospopengetpid)r   ro   rp   ls       r   get_ps_statsServer.get_ps_stats   s]    
(( /?		KL 33 U	U V
 AFC 
r   )rP   rQ   rO   rN   N)r   r   r   r   r?   rd   rx   r"   rU   rr   rz   r}   r   r   r   r   r   rI   rI   ^   s2    
  #zzb E-0^,	r   rI   c                   8    \ rS rSrSr\S4S jrS rS rS r	Sr
g	)
Client   zU
Methods of the server object can be called transparently. Exceptions are
re-raised.
Fc                    U(       a  [         R                  O[         R                  n[         R                   " U[         R                  5      n[        R                  SXU5        UR                  X45        XPl        [        U5      U l	        g )Nz$connecting to %s:%d, socket type: %s)
ru   AF_INET6AF_INETSOCK_STREAMri   rj   connectr    r   rQ   )r   HOSTportv6socktyper    s         r   r"   Client.__init__   sX    &(6??fnn}}Xv'9'9:7XNd\"	4.r   c                 d    [         R                  " X4U R                  SS9  U R                  5       $ )Nr[   r\   )r   rk   rQ   
get_result)r   rl   rm   s      r   generic_funClient.generic_fun   s%    UM477Q7  r   c                 r    [        U R                  5      R                  5       u  pUS :w  a  [        U5      eU$ rM   )r   rQ   r^   rF   )r   rn   ro   s      r   r   Client.get_result   s2    '0557	t8!"%%Jr   c                    ^ ^ UU 4S j$ )Nc                  (   > TR                  TU 5      $ rM   )r   )xr   r   s    r   <lambda>$Client.__getattr__.<locals>.<lambda>   s    $**42r   r   )r   r   s   ``r   __getattr__Client.__getattr__   s	    22r   )rQ   r    N)r   r   r   r   r?   PORTr"   r   r   r   r   r   r   r   r   r      s"     #'5 !!
3r   r   Fc                 ^   SnU(       a  [         R                  O[         R                  n[         R                   " U[         R                  5      nUR	                  [         R
                  [         R                  S5        [        R                  SXA5        UR                  XA45        UR                  S5        [        R                  S5        UbI  [        R                  SU5        [        US5      R                  S[         R                  " 5       U4-  5          UR                  5       u  px[        R                  S
U5        U " U5      n
[         R"                  " U
R$                  S5      n[        R'                  SU5        Mj  ! [         R                   a  n	U	S   S	:X  a   S n	A	M  e S n	A	ff = f)NrK   r/   z
bind %s:%d   zaccepting connectionszstoring host+port in %swz%s:%d zInterrupted system callzConnected to %sr   zThread ID: %d)ru   r   r   r   
setsockopt
SOL_SOCKETSO_REUSEADDRri   rj   bindlistenopenr,   gethostnameacceptrv   _threadstart_new_threadrz   debug)new_handlerr   report_to_filer   r   r   r;   rP   addrrq   ibstids               r   
run_serverr      s<   D"$v&..Hh 2 23ALL""F$7$7;HH\4&FFD<HHQKHH$%!*N;^S!''F4F4F4H$3O(OP
	JD
 	"D)$&&s}}R8		/3'  || 	t..	s   F F,	F'&F''F,)r?   r   r   r   rd   r   rb   ru   logging	getLoggerr   ri   r   r	   	Unpicklerr   r   ra   rA   rF   rI   r   r   r   r   r   <module>r      s     	  
    !  5&** 5' 'R	 		i 	g gR3 3< "&du (r   