
    zhi&                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlZd dlZd	Z G d
 de          Z G d de	          Z G d de          Z G d d          ZdS )    )SMPPServerProtocol)	pdu_types)	Interface)ServerFactory)UsernamePassword)error)deferNzsmpp.twisted.serverc                       e Zd ZdS )IAuthenticatedSMPPN)__name__
__module____qualname__     c/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/smpp/twisted/server.pyr   r      s        Dr   r   c                       e Zd Zd ZdS )UsernameAndPasswordAndIPc                 0    || _         || _        || _        d S N)usernamepasswordclient_ip_address)selfr   r   r   s       r   __init__z!UsernameAndPasswordAndIP.__init__   s      !2r   N)r   r   r   r   r   r   r   r   r      s#        3 3 3 3 3r   r   c                   Z    e Zd ZeZd Zd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd ZdS )SMPPServerFactoryc                 l    || _         t          j        t                    | _        i | _        || _        d S r   )configlogging	getLoggerLOG_CATEGORYlogbound_connections_auth_portal)r   r   auth_portals      r   r   zSMPPServerFactory.__init__!   s3    $\22 "$'r   c                     | j         S r   )r   r   s    r   	getConfigzSMPPServerFactory.getConfig)   s
    {r   c                 V    || j         v r| j         |                                         S dS )Nr   )r#   getMaxTransmitReceiveBindCountr   	system_ids     r   getBoundConnectionCountz)SMPPServerFactory.getBoundConnectionCount,   s/    ...))4SSUUU1r   c                     || j         v rl| j         |                                         }g }|                                D ]\  }}|                    d||fz              d                    |          }|S dS )Nz%s: %dz, 0)r#   getBindingCountByTypeitemsappendjoin)r   r,   bind_countsbound_connections_countkeyvaluebound_connections_strs          r   getBoundConnectionCountsStrz-SMPPServerFactory.getBoundConnectionCountsStr2   s    ...0;QQSSK&(#)//11 H H
U'..x3,/FGGGG$(II.E$F$F!((3r   c                 z   |j         }| j                            d|z             || j        vrt	          |          | j        |<   | j        |                             |           |j        }| j                            d|d|d|                     |          d| j	        j
        |         d                    dS )z
        Add a protocol instance to the list of current connections.
        @param connection: An instance of SMPPServerProtocol
        zAdding SMPP binding for %szAdded  bind for ''. Active binds: . Max binds: max_bindingsN)r,   r"   debugr#   SMPPBindManager
addBinding	bind_typeinfor9   r   systemsr   
connectionr,   rB   s       r   addBoundConnectionz$SMPPServerFactory.addBoundConnection=   s   
 (	3i?@@@D2220?	0J0JD"9-y)44Z@@@(	S\S\S\^g^g^gim  jJ  jJ  KT  jU  jU  jU  jU  W[  Wb  Wj  kt  Wu  vD  WE  WE  F  	G  	G  	G  	G  	Gr   c                    |j         | j                            d           dS |j         }|j        }| j        |                             |           | j                            d|d|d|                     |          d| j        j	        |         d                    | j        |         
                                dk    r| j                            |           dS dS )	z
        Remove a protocol instance (SMPP binding) from the list of current connections.
        @param connection: An instance of SMPPServerProtocol
        Nz/SMPP connection attempt failed without binding.zDropped r;   r<   r=   r>   r   )r,   r"   r?   rB   r#   removeBindingrC   r9   r   rD   getBindingCountpoprE   s       r   removeConnectionz"SMPPServerFactory.removeConnectionJ   s:   
 'HNNLMMMMM",I",I"9-;;JGGGHMMMYbYbYbdmdmdmos  pP  pP  QZ  p[  p[  p[  p[  ]a  ]h  ]p  qz  ]{  |J  ]K  ]K  L  M  M  M%i0@@BBaGG&**955555 HGr   c                 6    | j                             |          S r   )r#   getr+   s     r   getBoundConnectionsz%SMPPServerFactory.getBoundConnectionsZ   s    %)))444r   c                     | j         0| j                             t          |||          d t                    S t	          j                    r   )r$   loginr   r   r   UnauthorizedLogin)r   r,   r   r   s       r   rQ   zSMPPServerFactory.login]   sM    ($**(H>OPP"  
 %'''r   c                     |                      |          }|r1|                    |          }|| j        j        |         d         k     S | j        j        |         d         dk    S )z
        Checks if the gateway with the specified system_id can open a new
        connection, as it hasn't exceeded its maximum number of bindings.
        @param bind_type: One of smpp.pdu.pdu_types.CommandId
        r>   r   )rO   getBindingCountForTyper   rD   )r   r,   rB   existing_bindings_for_idconnections_counts        r   canOpenNewConnectionz&SMPPServerFactory.canOpenNewConnectionf   sl     $(#;#;I#F#F # 	F 8 O OPY Z Z$t{':9'En'UUU ;&y1.AAEEr   c                 
   |                      |          }|rWg }|D ]=}|                    |                                           |                                 >t	          j        |          }nt	          j        d          }|S )zD Unbinds and disconnects all the bindings for the given system_id.  N)rO   r2   getDisconnectedDeferredunbindAndDisconnectr	   DeferredListsucceed)r   r,   bind_mgrunbinds_listbindds         r   unbindGatewayzSMPPServerFactory.unbindGatewayt   s    ++I66 	$L  + +##D$@$@$B$BCCC((****"<00AAd##Ar   c                     d| j         j        |         d<   |                     |          }|                    | j        |           |S )z
        Removes a running gateway from the config so they will be unable to rebind.
        Any attempt to bind while unbinding will receive a ESME_RBINDFAIL error.
        r   r>   )r   rD   ra   addCallbackremoveGatewayFromConfig)r   r,   r`   s      r   unbindAndRemoveGatewayz(SMPPServerFactory.unbindAndRemoveGateway   sG    
 :;I&~6y))	d2I>>>r   c                 D    | j         j                            |           |S r   )r   rD   rK   )r   deferred_resr,   s      r   rd   z)SMPPServerFactory.removeGatewayFromConfig   s!    	***r   N)r   r   r   r   protocolr   r(   r-   r9   rG   rL   rO   rQ   rW   ra   re   rd   r   r   r   r   r      s        !H( ( (    	 	 	G G G6 6 6 5 5 5( ( (F F F        r   r   c                   J    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )r@   c                     || _         t          j        j        g t          j        j        g t          j        j        g i| _        t          j                    | _	        d S r   )
r,   r   	CommandIdbind_transceiverbind_transmitterbind_receiver_bindscollectionsdeque_delivery_binding_historyr+   s     r   r   zSMPPBindManager.__init__   sL    " *;R *;R *8"> *5):)<)<&&&r   c                 T    |j         }| j        |                             |           dS z6 @param connection: An instance of SMPPServerProtocol N)rB   ro   r2   r   rF   rB   s      r   rA   zSMPPBindManager.addBinding   s-     (	I%%j11111r   c                 T    |j         }| j        |                             |           dS rt   )rB   ro   removeru   s      r   rI   zSMPPBindManager.removeBinding   s+    (	I%%j11111r   c                    t          | j        t          j        j                           t          t          | j        t          j        j                           t          | j        t          j        j                                     z   S r   )lenro   r   rk   rl   maxrm   rn   r'   s    r   r*   z.SMPPBindManager.getMaxTransmitReceiveBindCount   sd    4;y2CDEE3t{9#6#GHIIt{9#6#DEFFH HH 	Hr   c                 b    t          d | j                                        D                       S )Nc              3   4   K   | ]}t          |          V  d S r   )ry   ).0vs     r   	<genexpr>z2SMPPBindManager.getBindingCount.<locals>.<genexpr>   s(      88a3q66888888r   )sumro   valuesr'   s    r   rJ   zSMPPBindManager.getBindingCount   s-    884;#5#5#7#7888888r   c                 l    i }| j                                         D ]\  }}t          |          ||<   |S r   )ro   r1   ry   )r   retr6   r7   s       r   r0   z%SMPPBindManager.getBindingCountByType   s>    +++-- 	" 	"JC5zzCHH
r   c                 *    |                                  S r   )rJ   r'   s    r   __len__zSMPPBindManager.__len__   s    ##%%%r   c                 z    g fd| j                                         D                                              S )Nc                 :    g | ]}                     |          S r   )extend)r}   typevalss     r   
<listcomp>z,SMPPBindManager.__iter__.<locals>.<listcomp>   s%    <<<tT		<<<r   )ro   r   __iter__)r   r   s    @r   r   zSMPPBindManager.__iter__   s>    <<<<t{'9'9';';<<<<}}r   c                      |t           j        j        k    r                                 }n,t	           fdt           j        j        |fD                       }|S )z
        Sum transceiver binds plus receiver or transmitter depending on this type
        @param bind_type: One of smpp.pdu.pdu_types.CommandId
        c                 D    g | ]}t          j        |                   S r   )ry   ro   )r}   btr   s     r   r   z:SMPPBindManager.getBindingCountForType.<locals>.<listcomp>   s'    $v$v$vbSR%9%9$v$v$vr   )r   rk   rl   r*   r   )r   rB   rV   s   `  r   rT   z&SMPPBindManager.getBindingCountForType   sb    
 	+<<< $ C C E E !$$v$v$v$vYEXEiktDu$v$v$v w w  r   c                    d}t          | j                  |                     t          j        j                  k     rI| j        t          j        j                 | j        t          j        j                 z   D ]}|| j        vr nd}|?| j        r8| j                                        }|| j        |j	                 v r|}|| j        8|| j        
                    |           |S )aG  
        Messages inbound (MO) that are to be forwarded to
        the client systems can be sent via transceiver and
        receiver bindings. Call this method to determine which
        binding to send down next so that traffic travels equally
        down the different binds.
        @return smpp protocol or None
        N)ry   rr   rT   r   rk   rn   ro   rl   popleftrB   r2   )r   binding_bindings      r   getNextBindingForDeliveryz)SMPPBindManager.getNextBindingForDelivery   s      t-..1L1LYM`Mn1o1ooo;y':'HIDKXaXkX|L}}  $"@@@E A  o$"@o5==??H4;x'9:::" o$"@o *11':::r   N)r   r   r   r   rA   rI   r*   rJ   r0   r   r   rT   r   r   r   r   r@   r@      s        = = =2 2 22 2 2
H H H
9 9 9  & & &  
! ! !         r   r@   )smpp.twisted.protocolr   smpp.pdur   zope.interfacer   twisted.internet.protocolr   twisted.cred.credentialsr   twisted.credr   twisted.internetr	   r   rp   r!   r   r   r   r@   r   r   r   <module>r      sc   4 4 4 4 4 4       $ $ $ $ $ $ 3 3 3 3 3 3 5 5 5 5 5 5       " " " " " "      #	 	 	 	 	 	 	 	3 3 3 3 3/ 3 3 3q q q q q q q qfX X X X X X X X X Xr   