~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/lib/message.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Helpers for reliable persistent message queues."""
 
2
 
 
3
ANCIENT = 1
 
4
 
 
5
 
 
6
def got_next_expected(store, next_expected):
 
7
    """Our peer has told us what it expects our next message's sequence to be.
 
8
 
 
9
    Call this with the message store and sequence number that the peer
 
10
    wants next; this will do various things based on what *this* side
 
11
    has in its outbound queue store.
 
12
 
 
13
    1. The peer expects a sequence greater than what we last
 
14
       sent. This is the common case and generally it should be
 
15
       expecting last_sent_sequence+len(messages_sent)+1.
 
16
 
 
17
    2. The peer expects a sequence number our side has already sent,
 
18
       and we no longer have that message. In this case, just send
 
19
       *all* messages we have, including the previous generation,
 
20
       starting at the sequence number the peer expects (meaning that
 
21
       messages have probably been lost).
 
22
 
 
23
    3. The peer expects a sequence number we already sent, and we
 
24
       still have that message cached. In this case, we send starting
 
25
       from that message.
 
26
 
 
27
    If the next expected sequence from the server refers to a message
 
28
    older than we have, then L{ANCIENT} will be returned.
 
29
    """
 
30
    ret = None
 
31
    old_sequence = store.get_sequence()
 
32
    if next_expected > old_sequence:
 
33
        store.delete_old_messages()
 
34
        pending_offset = next_expected - old_sequence
 
35
    elif next_expected < (old_sequence - store.get_pending_offset()):
 
36
        # "Ancient": The other side wants messages we don't have,
 
37
        # so let's just reset our counter to what it expects.
 
38
        pending_offset = 0
 
39
        ret = ANCIENT
 
40
    else:
 
41
        # No messages transferred, or
 
42
        # "Old": We'll try to send these old messages that the
 
43
        # other side still wants.
 
44
        pending_offset = (store.get_pending_offset() + next_expected
 
45
                          - old_sequence)
 
46
 
 
47
    store.set_pending_offset(pending_offset)
 
48
    store.set_sequence(next_expected)
 
49
    return ret