Diff
Not logged in

Differences From Artifact [b06c78cec8]:

To Artifact [300f45a0e5]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
..
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
..
82
83
84
85
86
87
88
89





90
91
92
93
94
95
96
...
104
105
106
107
108
109
110



111
112
113
114
115
116
117
...
167
168
169
170
171
172
173

174
175
176
177
178
179
180
181
...
182
183
184
185
186
187
188

189
190
191
192
193
194
195
196
...
201
202
203
204
205
206
207
208






209
210
211
212
213

214
215
216
217

218
219
220
221
222
223
224
225
...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.1.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// 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 ack_manager::{Ack, AckManager};
use action::Action;
use crust::{PeerId, Service};
use crust::Event as CrustEvent;
use error::{InterfaceError, RoutingError};
use event::Event;
use id::{FullId, PublicId};
................................................................................
use routing_message_filter::{FilteringResult, RoutingMessageFilter};
use routing_table::Authority;
use state_machine::Transition;
use stats::Stats;
use std::collections::BTreeSet;
use std::fmt::{self, Debug, Formatter};
use std::time::Duration;
use super::common::{Base, Bootstrapped, USER_MSG_CACHE_EXPIRY_DURATION_SECS};
use timer::Timer;
use xor_name::XorName;

/// A node connecting a user to the network, as opposed to a routing / data storage node.
///
/// Each client has a _proxy_: a node through which all requests are routed.
pub struct Client {
................................................................................

        outbox.send_event(Event::Connected);
        client
    }

    pub fn handle_action(&mut self, action: Action) -> Transition {
        match action {
            Action::ClientSendRequest { content, dst, priority, result_tx } => {





                let src = Authority::Client {
                    client_key: *self.full_id.public_id().signing_public_key(),
                    proxy_node_name: *self.proxy_public_id.name(),
                    peer_id: self.crust_service.id(),
                };

                let user_msg = UserMessage::Request(content);
................................................................................
            Action::NodeSendMessage { result_tx, .. } => {
                let _ = result_tx.send(Err(InterfaceError::InvalidState));
            }
            Action::Name { result_tx } => {
                let _ = result_tx.send(*self.name());
            }
            Action::Timeout(token) => self.handle_timeout(token),



            Action::Terminate => {
                return Transition::Terminate;
            }
        }

        Transition::Stay
    }
................................................................................

    fn handle_hop_message(&mut self,
                          hop_msg: HopMessage,
                          peer_id: PeerId,
                          outbox: &mut EventBox)
                          -> Result<Transition, RoutingError> {
        if self.proxy_peer_id == peer_id {

            hop_msg.verify(self.proxy_public_id.signing_public_key())?;
        } else {
            return Err(RoutingError::UnknownConnection(peer_id));
        }

        let signed_msg = hop_msg.content;
        signed_msg.check_integrity(self.min_section_size())?;

................................................................................
        let routing_msg = signed_msg.routing_message();
        let in_authority = self.in_authority(&routing_msg.dst);
        if in_authority {
            self.send_ack(routing_msg, 0);
        }

        // Prevents us repeatedly handling identical messages sent by a malicious peer.

        match self.routing_msg_filter.filter_incoming(routing_msg, hop_msg.route) {
            FilteringResult::KnownMessage |
            FilteringResult::KnownMessageAndRoute => return Err(RoutingError::FilterCheckFailed),
            FilteringResult::NewMessage => (),
        }

        if !in_authority {
            return Ok(Transition::Stay);
................................................................................

    fn dispatch_routing_message(&mut self,
                                routing_msg: RoutingMessage,
                                outbox: &mut EventBox)
                                -> Transition {
        match routing_msg.content {
            MessageContent::Ack(ack, _) => self.handle_ack_response(ack),
            MessageContent::UserMessagePart { hash, part_count, part_index, payload, .. } => {






                trace!("{:?} Got UserMessagePart {:02x}{:02x}{:02x}.., {}/{} from {:?} to {:?}.",
                       self,
                       hash[0],
                       hash[1],
                       hash[2],

                       part_count,
                       part_index,
                       routing_msg.src,
                       routing_msg.dst);

                if let Some(msg) = self.user_msg_cache.add(hash, part_count, part_index, payload) {
                    self.stats().count_user_message(&msg);
                    outbox.send_event(msg.into_event(routing_msg.src, routing_msg.dst));
                }
                Transition::Stay
            }
            content => {
                debug!("{:?} - Unhandled routing message: {:?} from {:?} to {:?}",
................................................................................
    fn timer(&mut self) -> &mut Timer {
        &mut self.timer
    }
}

#[cfg(feature = "use-mock-crust")]
impl Client {
    /// Resends all unacknowledged messages.
    pub fn resend_unacknowledged(&mut self) -> bool {
        let timer_tokens = self.ack_mgr.timer_tokens();
        for timer_token in &timer_tokens {
            self.resend_unacknowledged_timed_out_msgs(*timer_token);
        }
        !timer_tokens.is_empty()
    }

    /// Are there any unacknowledged messages?
    pub fn has_unacknowledged(&self) -> bool {
        self.ack_mgr.has_pending()
    }
}

impl Debug for Client {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(formatter, "Client({})", self.name())
    }
}







|
|








>







 







<







 







|
>
>
>
>
>







 







>
>
>







 







>
|







 







>
|







 







|
>
>
>
>
>
>





>

<


>
|







 







|
|
<
<
<
<
<
<
<
<
<
<








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
..
30
31
32
33
34
35
36

37
38
39
40
41
42
43
..
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

232
233
234
235
236
237
238
239
240
241
242
...
384
385
386
387
388
389
390
391
392










393
394
395
396
397
398
399
400
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// 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 super::common::{Base, Bootstrapped, USER_MSG_CACHE_EXPIRY_DURATION_SECS};
use ack_manager::{Ack, AckManager};
use action::Action;
use crust::{PeerId, Service};
use crust::Event as CrustEvent;
use error::{InterfaceError, RoutingError};
use event::Event;
use id::{FullId, PublicId};
................................................................................
use routing_message_filter::{FilteringResult, RoutingMessageFilter};
use routing_table::Authority;
use state_machine::Transition;
use stats::Stats;
use std::collections::BTreeSet;
use std::fmt::{self, Debug, Formatter};
use std::time::Duration;

use timer::Timer;
use xor_name::XorName;

/// A node connecting a user to the network, as opposed to a routing / data storage node.
///
/// Each client has a _proxy_: a node through which all requests are routed.
pub struct Client {
................................................................................

        outbox.send_event(Event::Connected);
        client
    }

    pub fn handle_action(&mut self, action: Action) -> Transition {
        match action {
            Action::ClientSendRequest {
                content,
                dst,
                priority,
                result_tx,
            } => {
                let src = Authority::Client {
                    client_key: *self.full_id.public_id().signing_public_key(),
                    proxy_node_name: *self.proxy_public_id.name(),
                    peer_id: self.crust_service.id(),
                };

                let user_msg = UserMessage::Request(content);
................................................................................
            Action::NodeSendMessage { result_tx, .. } => {
                let _ = result_tx.send(Err(InterfaceError::InvalidState));
            }
            Action::Name { result_tx } => {
                let _ = result_tx.send(*self.name());
            }
            Action::Timeout(token) => self.handle_timeout(token),
            Action::ResourceProofResult(..) => {
                error!("Action::ResourceProofResult received by Client state");
            }
            Action::Terminate => {
                return Transition::Terminate;
            }
        }

        Transition::Stay
    }
................................................................................

    fn handle_hop_message(&mut self,
                          hop_msg: HopMessage,
                          peer_id: PeerId,
                          outbox: &mut EventBox)
                          -> Result<Transition, RoutingError> {
        if self.proxy_peer_id == peer_id {
            hop_msg
                .verify(self.proxy_public_id.signing_public_key())?;
        } else {
            return Err(RoutingError::UnknownConnection(peer_id));
        }

        let signed_msg = hop_msg.content;
        signed_msg.check_integrity(self.min_section_size())?;

................................................................................
        let routing_msg = signed_msg.routing_message();
        let in_authority = self.in_authority(&routing_msg.dst);
        if in_authority {
            self.send_ack(routing_msg, 0);
        }

        // Prevents us repeatedly handling identical messages sent by a malicious peer.
        match self.routing_msg_filter
                  .filter_incoming(routing_msg, hop_msg.route) {
            FilteringResult::KnownMessage |
            FilteringResult::KnownMessageAndRoute => return Err(RoutingError::FilterCheckFailed),
            FilteringResult::NewMessage => (),
        }

        if !in_authority {
            return Ok(Transition::Stay);
................................................................................

    fn dispatch_routing_message(&mut self,
                                routing_msg: RoutingMessage,
                                outbox: &mut EventBox)
                                -> Transition {
        match routing_msg.content {
            MessageContent::Ack(ack, _) => self.handle_ack_response(ack),
            MessageContent::UserMessagePart {
                hash,
                part_count,
                part_index,
                payload,
                ..
            } => {
                trace!("{:?} Got UserMessagePart {:02x}{:02x}{:02x}.., {}/{} from {:?} to {:?}.",
                       self,
                       hash[0],
                       hash[1],
                       hash[2],
                       part_index + 1,
                       part_count,

                       routing_msg.src,
                       routing_msg.dst);
                if let Some(msg) = self.user_msg_cache
                       .add(hash, part_count, part_index, payload) {
                    self.stats().count_user_message(&msg);
                    outbox.send_event(msg.into_event(routing_msg.src, routing_msg.dst));
                }
                Transition::Stay
            }
            content => {
                debug!("{:?} - Unhandled routing message: {:?} from {:?} to {:?}",
................................................................................
    fn timer(&mut self) -> &mut Timer {
        &mut self.timer
    }
}

#[cfg(feature = "use-mock-crust")]
impl Client {
    pub fn get_timed_out_tokens(&mut self) -> Vec<u64> {
        self.timer.get_timed_out_tokens()










    }
}

impl Debug for Client {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(formatter, "Client({})", self.name())
    }
}