1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use maidsafe_utilities::event_sender::MaidSafeObserver;
#[cfg(any(test, feature = "use-mock-crust"))]
use maidsafe_utilities::SeededRng;
#[cfg(all(not(test), not(feature = "use-mock-crust")))]
use rand;
#[cfg(any(test, feature = "use-mock-crust"))]
use rand::Rng;
use xor_name::XorName;

pub type RoutingActionSender = MaidSafeObserver<::action::Action>;

/// Unique ID for messages
///
/// This is used for deduplication: Since the network sends messages redundantly along different
/// routes, the same message will usually arrive more than once at any given node. A message with
/// an ID that is already in the cache will be ignored.
#[derive(Ord, PartialOrd, Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Hash)]
pub struct MessageId(XorName);

impl MessageId {
    /// Generate a new `MessageId` with random content.
    #[cfg(any(test, feature = "use-mock-crust"))]
    pub fn new() -> MessageId {
        let mut rng = SeededRng::thread_rng();
        MessageId(rng.gen())
    }

    /// Generate a new `MessageId` with random content.
    #[cfg(all(not(test), not(feature = "use-mock-crust")))]
    pub fn new() -> MessageId {
        MessageId(rand::random())
    }

    /// Generate a `MessageId` with value 0. This should only be used for messages where there is
    /// no danger of duplication.
    pub fn zero() -> MessageId {
        MessageId(XorName([0; 32]))
    }

    /// Generate a new `MessageId` with contents extracted from lost node.
    pub fn from_lost_node(mut name: XorName) -> MessageId {
        name.0[0] = b'L';
        MessageId(name)
    }

    /// Generate a new `MessageId` with contents extracted from new node.
    pub fn from_added_node(mut name: XorName) -> MessageId {
        name.0[0] = b'A';
        MessageId(name)
    }

    /// Generate the reverse of the given `MessageId`.
    pub fn from_reverse(name: &MessageId) -> MessageId {
        let MessageId(XorName(mut name_mut)) = *name;
        name_mut.reverse();
        MessageId(XorName(name_mut))
    }

    /// Generate the increment (on the MSB only) of the given `MessageId`.
    pub fn increment_first_byte(message_id: &MessageId) -> MessageId {
        let MessageId(XorName(mut vec_mut)) = *message_id;
        vec_mut[0] = vec_mut[0].wrapping_add(1);
        MessageId(XorName(vec_mut))
    }

    /// Generate the decrement (on the MSB only) of the given `MessageId`.
    pub fn decrement_first_byte(message_id: &MessageId) -> MessageId {
        let MessageId(XorName(mut vec_mut)) = *message_id;
        vec_mut[0] = vec_mut[0].wrapping_sub(1);
        MessageId(XorName(vec_mut))
    }
}

impl Default for MessageId {
    fn default() -> MessageId {
        MessageId::zero()
    }
}

#[cfg(test)]
#[cfg_attr(feature = "cargo-clippy", allow(indexing_slicing))]
mod tests {
    use super::MessageId;
    use xor_name::{XorName, XOR_NAME_LEN};

    #[test]
    fn increment() {
        let message_id = MessageId::increment_first_byte(&MessageId(XorName([255; XOR_NAME_LEN])));
        let MessageId(XorName(vec_bytes)) = message_id;
        assert_eq!(vec_bytes[0], 0);
    }

    #[test]
    fn decrement() {
        let message_id = MessageId::decrement_first_byte(&MessageId(XorName([0; XOR_NAME_LEN])));
        let MessageId(XorName(vec_bytes)) = message_id;
        assert_eq!(vec_bytes[0], 255);
    }
}