ó
―-'Nc           @   s   d  Z  d d l Z y d d l Z Wn! e k
 rE d d l m Z n Xd d l m Z d e f d     YZ	 d e f d     YZ
 d	   Z d S(
   sE   Provide access to the persistent data used by the L{MessageExchange}.iĸĸĸĸN(   t   dbapi2(   t   with_cursort   MessageContextc           B   s&   e  Z d  Z d   Z e d    Z RS(   sr  Stores a context for incoming messages that require a response.

    The context consists of

      - the "operation-id" value
      - the secure ID that was in effect when the message was received
      - the message type
      - the time when the message was received

    This data will be used to detect secure ID changes between the time at
    which the request message came in and the completion of the request.
    If the secure ID did change the result message is obolete and will not be
    sent to the server.

    @param db: the sqlite database handle.
    @param id: the database key value for this instance.
    c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   _dbt   operation_idt	   secure_idt   message_typet	   timestamp(   t   selft   dbR   R   R   R   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyt   __init__   s
    				c         C   s   | j  d |  j f  d  S(   Ns0   DELETE FROM message_context WHERE operation_id=?(   t   executeR   (   R   t   cursor(    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyt   remove&   s    (   t   __name__t
   __module__t   __doc__R
   R   R   (    (    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyR      s   	t   ExchangeStorec           B   sS   e  Z d  Z d Z d   Z d   Z e d    Z e d    Z	 e d    Z
 RS(   s/  Message meta data required by the L{MessageExchange}.

    The implementation uses a SQLite database as backend, with a single table
    called "message_context", whose schema is defined in
    L{ensure_exchange_schema}.

    @param filename: The name of the file that contains the sqlite database.
    c         C   s   | |  _  d  S(   N(   t	   _filename(   R   t   filename(    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyR
   8   s    c         C   s   t  |  j  d  S(   N(   t   ensure_exchange_schemaR   (   R   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyt   _ensure_schema;   s    c         C   s8   | | | t  j    f } | j d |  t |  j |  S(   s,   Add a L{MessageContext} with the given data.se   INSERT INTO message_context    (operation_id, secure_id, message_type, timestamp)    VALUES (?,?,?,?)(   t   timeR   R   R   (   R   R   R   R   R   t   params(    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyt   add_message_context>   s
    c         C   s=   | j  d | f  | j   } | r5 t |  j |  Sd Sd S(   s?   The L{MessageContext} for the given C{operation_id} or C{None}.sa   SELECT operation_id, secure_id, message_type, timestamp FROM message_context WHERE operation_id=?N(   R   t   fetchoneR   R   t   None(   R   R   R   t   row(    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyt   get_message_contextI   s    
c         C   s4   | j  d  | j   } g  | D] } | d ^ q  S(   s@   Return all operation IDs currently stored in C{message_context}.s(   SELECT operation_id FROM message_contexti    (   R   t   fetchall(   R   R   t   resultR   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyt   all_operation_idsU   s    N(   R   R   R   R   R   R
   R   R   R   R   R   (    (    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyR   -   s   		c         C   sv   |  j    } y | j d  | j d  Wn1 t j t j f k
 r] | j   |  j   n X| j   |  j   d S(   sg   Create all tables needed by a L{ExchangeStore}.

    @param db: A connection to a SQLite database.
    sĒ   CREATE TABLE message_context (id INTEGER PRIMARY KEY, timestamp TIMESTAMP,   secure_id TEXT NOT NULL, operation_id INTEGER NOT NULL,   message_type text NOT NULL)sK   CREATE UNIQUE INDEX msgctx_operationid_idx ON message_context(operation_id)N(   R   R   t   sqlite3t   OperationalErrort   DatabaseErrort   closet   rollbackt   commit(   R	   R   (    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyR   ]   s    

(   R   R   R    t   ImportErrort	   pysqlite2R    t   landscape.lib.storeR   t   objectR   R   R   (    (    (    sB   /usr/lib/python2.7/dist-packages/landscape/broker/exchangestore.pyt   <module>   s   !0