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
|
/// hyperdrive-specific helper for discovering the public key for the "content" feed
/// from the "metadata" feed , and sending a Feed message to initialize on this connection.
pub fn init_data_feed(&mut self) -> Result<()> {
if self.feeds.len() > 1 {
return Ok(());
}
let data_key = self.get_drive_data_key()?;
self.add_feed(&data_key[0..32])
}
/// hyperdrive-specific helper for returning the "data" feed public key (aka, index=1)
pub fn get_drive_data_key(&mut self) -> Result<Key> {
if self.feeds.len() > 1 {
// we already have the key
let key = self.feeds[1].clone();
return Ok(key);
}
// Info: downloading, not uploading
let mut im = Info::new();
im.set_uploading(false);
im.set_downloading(true);
self.conn.send_msg(&DatNetMessage::Info(im), 0)?;
// Have: nothing (so far)
let mut hm = Have::new();
hm.set_start(0);
hm.set_length(0);
self.conn.send_msg(&DatNetMessage::Have(hm), 0)?;
// UnHave: still nothing
let mut uhm = Unhave::new();
uhm.set_start(0);
self.conn.send_msg(&DatNetMessage::Unhave(uhm), 0)?;
// Want: just the first element
let mut wm = Want::new();
wm.set_start(0);
wm.set_length(1);
self.conn.send_msg(&DatNetMessage::Want(wm), 0)?;
// listen for Have
loop {
let (msg, feed_index) = self.conn.recv_msg()?;
if feed_index == 1 {
continue;
}
if let DatNetMessage::Have(_) = msg {
break;
} else {
info!("Expected Have message, got: {:?}", &msg);
continue;
}
}
// Request
let mut rm = Request::new();
rm.set_index(0);
self.conn.send_msg(&DatNetMessage::Request(rm), 0)?;
loop {
let (msg, feed_index) = self.conn.recv_msg()?;
if feed_index == 1 {
info!("Expected other message channel");
continue;
}
if let DatNetMessage::Data(dm) = msg {
info!("Got metadata: {}", dm.get_index());
if dm.get_index() == 0 {
let index_msg = parse_from_bytes::<Index>(&mut dm.get_value())?;
if index_msg.get_field_type() == "hyperdrive" {
let data_key = index_msg.get_content();
if data_key.len() != 32 {
bail!("Received data key had wrong length: {}", data_key.len());
}
// TODO: ok_or(), but what?
return Ok(Key::from_slice(&data_key[0..32]).unwrap());
} else {
bail!("non-hyperdrive Index type: {}", index_msg.get_field_type());
}
}
} else {
info!("Expected Data message, got: {:?}", &msg);
continue;
}
}
}
|