
    rh                        d Z ddlZddlZddlZddlZddlZddlZddlZddlZ ej                  e
      ZdZddhZ G d dej                        Z G d d	      Z G d
 de      Z G d de      Z G d d      Z G d d      ZeddfdZy)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                       e Zd Zd Zy)RestrictedUnpicklerc                     |t         v rt        t        j                  |      |      S t	        j
                  d|d|d      )Nzglobal '.z' is forbidden)safe_modulesgetattr	importlibimport_modulepickleUnpicklingError)selfmodulenames      d/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/faiss/contrib/rpc.py
find_classzRestrictedUnpickler.find_class%   s?    \!9226:DAA$$&,d&4 5 	5    N)__name__
__module____qualname__r    r   r   r   r   #   s    5r   r   c                   *    e Zd ZdZd Zd ZddZd Zy)FileSockz7 wraps a socket so that it is usable by pickle/cPickle c                      || _         d| _        y )Nr   )socknr)r   r   s     r   __init__zFileSock.__init__1   s    	r   c                     d}d}|t        |      k  r6| j                  j                  ||||z          }||z  }|t        |      k  r5y y )N   r   )lenr   send)r   bufbsnssents        r   writezFileSock.write5   sK     3s8m99>>#bb/2D$JB 3s8mr   c                    | xj                   dz  c_         g }d}t        |      |k  rO| j                  j                  ||z
        }|sn.|j	                  |       |t        |      z  }t        |      |k  rOdj                  |      S )N   r   r   )r   r    r   recvappendjoin)r   r#   bnbrbs        r   readzFileSock.read?   st    
!fRiR(BuHHRL#b'MB !fRi xx{r   c                 F    t               }	 | j                  d      }||z  })zmay be optimized...r(   )bytesr/   r    chr)r   scs      r   readlinezFileSock.readlineM   s)     'iilAqDA r   N)r   )r   r   r   __doc__r   r&   r/   r5   r   r   r   r   r   .   s    =r   r   c                       e Zd Zy)
ClientExitNr   r   r   r   r   r   r8   r8   W       r   r8   c                       e Zd Zy)ServerExceptionNr9   r   r   r   r<   r<   Z   r:   r   r<   c                   N    e Zd ZdZej
                  dfdZd Zd Zd Z	d Z
d Zy	)
Serverzr
    server protocol. Methods from classes that subclass Server can be called
    transparently from a client
     c                 N    || _         || _        || _        t        |      | _        y N)logf
log_prefixconnr   fs)r   r3   rB   rC   s       r   r   zServer.__init__d   s$    	$ 	1+r   c                 \    | j                   j                  d| j                  d|d       y )Nz
Sever log z: 
)rB   r&   rC   )r   r3   s     r   logz
Server.logn   s    		CDr   c                 &   	 t        | j                        j                         \  }}| j                  d|z         d}d}	 t        | |      }	  | }t$        j'                  d
       	 t)        j*                  ||f| j                  d       y# t        $ r t	        d      w xY w# t        $ r" t        d|z         }| j                  d       Y w xY w# t        $ r}dj                  t        j                  t        j                         d               t        |      z   }| j                  d       t        j                  d	| j                          | j                   j#                          Y d}~ d}~ww xY w# t        $ r t	        d      w xY w)a  
        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 methodr?      zexception in method2   return   protocolzfunction return)r   rE   loadEOFErrorr8   rH   r	   AttributeError	Exceptionr+   	traceback	format_tbsysexc_infostr	print_excrB   flushLOGinfor   dump)r   fnameargsstretfes          r   one_functionzServer.one_functionq   sa   	*/8==?MUD 	&./	'd5!A
	T(C 		0KKS	477Q77  	*[))	*  	' 1% 78BHH%&	'  
	 wwy**3<<>!+<=>s1vEBHH*+499-IIOO
	  	0.//	0sB   &B B& C )$E; B#&(CC	E8BE33E8;Fc                 d   | j                  d       	 	 | j                          # t        $ r}| j                  d|z         Y d}~nd}~wt        j                  $ r>}| j                  d|z         t        j                  d| j                         Y d}~nd}~wt        $ r4 | j                  d       t        j                  d| j                         Y nGt        $ r< t        j                  dt        j                         t        j                  d       Y nw xY wt        j                  d       y)	z3 main execution loop. Loops and handles exit stateszin exec_loopzClientExit %sNzsocket error %srK   zEOF during communicationr(   z
exit sever)rH   rd   r8   socketerrorrT   rY   rB   rQ   BaseExceptionrV   stderrexitr[   r\   )r   rc   s     r   	exec_loopzServer.exec_loop   s     	 	!!#  	(HH_Q&''|| 	.HH&q()499-- 	.HH/0499- 	3::.HHQK	
 	s-   % 	DAD4B=DADDc                      y rA   r   )r   s    r   exec_loop_cleanupzServer.exec_loop_cleanup   s    r   c                     d}t        j                  ddt        j                         z  z   dz   dz         }|D ]  }||z  }	 |S )Nr?   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   ra   rb   ls       r   get_ps_statszServer.get_ps_stats   sd    
(( /?		KL 33 U	U V
  	AFC	
r   N)r   r   r   r6   rV   ri   r   rH   rd   rk   rm   rs   r   r   r   r>   r>   ^   s2    
  #zzb E-0^,	r   r>   c                   .    e Zd ZdZedfdZd Zd Zd Zy)Clientza
    Methods of the server object can be called transparently. Exceptions are
    re-raised.
    Fc                    |rt         j                  nt         j                  }t        j                   |t         j                        }t        j                  d|||       |j                  ||f       || _        t        |      | _	        y )Nz$connecting to %s:%d, socket type: %s)
rf   AF_INET6AF_INETSOCK_STREAMr[   r\   connectr   r   rE   )r   HOSTportv6socktyper   s         r   r   zClient.__init__   s^    &(6??fnn}}Xv'9'9:7tXNdD\"	4.r   c                 j    t        j                  ||f| j                  d       | j                         S )NrM   rN   )r   r]   rE   
get_result)r   r^   r_   s      r   generic_funzClient.generic_fun   s'    UDM477Q7  r   c                 r    t        | j                        j                         \  }}|d k7  rt        |      |S rA   )r   rE   rP   r<   )r   r`   ra   s      r   r   zClient.get_result   s4    '0557	St8!"%%Jr   c                       fdS )Nc                  (    j                  |       S rA   )r   )xr   r   s    r   <lambda>z$Client.__getattr__.<locals>.<lambda>   s    $**42 r   r   )r   r   s   ``r   __getattr__zClient.__getattr__   s	    22r   N)	r   r   r   r6   PORTr   r   r   r   r   r   r   ru   ru      s"     #'5 !!
3r   ru   Fc                 J   d}|rt         j                  nt         j                  }t        j                   |t         j                        }|j	                  t         j
                  t         j                  d       t        j                  d||       |j                  ||f       |j                  d       t        j                  d       |Ht        j                  d|       t        |d      j                  dt        j                         |fz         	 	 |j                         \  }}t        j                  d
|        | |      }
t!        j"                  |
j$                  d      }t        j'                  d|       i# t         j                  $ r}	|	d   d	k(  rY d }	~	 d }	~	ww xY w)Nr?   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)rf   rw   rx   ry   
setsockopt
SOL_SOCKETSO_REUSEADDRr[   r\   bindlistenopenr&   gethostnameacceptrg   _threadstart_new_threadrk   debug)new_handlerr|   report_to_filer}   r{   r~   r3   rD   addrrc   ibstids               r   
run_serverr      sB   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   E< <F"FFF")r6   r
   ro   r   rV   r   rT   rf   logging	getLoggerr   r[   r   r   	Unpicklerr   r   rS   r8   r<   r>   ru   r   r   r   r   <module>r      s     	  
    g!  5&** 5' 'R	 		i 	g gR3 3< "&du (r   