Store online peers when joining a worktree

This commit is contained in:
Antonio Scandurra
2021-06-30 15:07:33 +02:00
parent 7704291432
commit 1d53d43d6f
3 changed files with 27 additions and 18 deletions
+7 -4
View File
@@ -47,14 +47,12 @@ message OpenWorktree {
message OpenWorktreeResponse { message OpenWorktreeResponse {
Worktree worktree = 1; Worktree worktree = 1;
uint32 replica_id = 2; uint32 replica_id = 2;
uint32 host_peer_id = 3; repeated Peer peers = 3;
} }
message AddGuest { message AddGuest {
uint64 worktree_id = 1; uint64 worktree_id = 1;
uint32 peer_id = 2; Peer guest = 2;
uint32 replica_id = 3;
User user = 4;
} }
message RemoveGuest { message RemoveGuest {
@@ -62,6 +60,11 @@ message RemoveGuest {
uint32 peer_id = 2; uint32 peer_id = 2;
} }
message Peer {
uint32 peer_id = 1;
uint32 replica_id = 2;
}
message OpenBuffer { message OpenBuffer {
uint64 worktree_id = 1; uint64 worktree_id = 1;
string path = 2; string path = 2;
+1 -1
View File
@@ -26,7 +26,7 @@ type BoxedWriter = Pin<Box<dyn AsyncWrite + 'static + Send>>;
type BoxedReader = Pin<Box<dyn AsyncRead + 'static + Send>>; type BoxedReader = Pin<Box<dyn AsyncRead + 'static + Send>>;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ConnectionId(u32); pub struct ConnectionId(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct PeerId(pub u32); pub struct PeerId(pub u32);
+19 -13
View File
@@ -37,7 +37,6 @@ use std::{
fmt, fs, fmt, fs,
future::Future, future::Future,
io, io,
iter::FromIterator,
ops::Deref, ops::Deref,
os::unix::fs::MetadataExt, os::unix::fs::MetadataExt,
path::{Path, PathBuf}, path::{Path, PathBuf},
@@ -103,14 +102,14 @@ impl Worktree {
.worktree .worktree
.ok_or_else(|| anyhow!("empty worktree"))?; .ok_or_else(|| anyhow!("empty worktree"))?;
let replica_id = open_worktree_response.replica_id; let replica_id = open_worktree_response.replica_id;
let host_peer_id = PeerId(open_worktree_response.host_peer_id); let peers = open_worktree_response.peers;
let worktree = cx.update(|cx| { let worktree = cx.update(|cx| {
cx.add_model(|cx| { cx.add_model(|cx| {
Worktree::Remote(RemoteWorktree::new( Worktree::Remote(RemoteWorktree::new(
id, id,
replica_id as ReplicaId, replica_id as ReplicaId,
host_peer_id,
worktree_message, worktree_message,
peers,
rpc.clone(), rpc.clone(),
languages, languages,
cx, cx,
@@ -442,10 +441,12 @@ impl LocalWorktree {
envelope: TypedEnvelope<proto::AddGuest>, envelope: TypedEnvelope<proto::AddGuest>,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
) -> Result<()> { ) -> Result<()> {
self.peers.insert( let guest = envelope
PeerId(envelope.payload.peer_id), .payload
envelope.payload.replica_id as ReplicaId, .guest
); .ok_or_else(|| anyhow!("empty peer"))?;
self.peers
.insert(PeerId(guest.peer_id), guest.replica_id as ReplicaId);
Ok(()) Ok(())
} }
@@ -760,8 +761,8 @@ impl RemoteWorktree {
fn new( fn new(
remote_id: u64, remote_id: u64,
replica_id: ReplicaId, replica_id: ReplicaId,
host_peer_id: PeerId,
worktree: proto::Worktree, worktree: proto::Worktree,
peers: Vec<proto::Peer>,
rpc: rpc::Client, rpc: rpc::Client,
languages: Arc<LanguageRegistry>, languages: Arc<LanguageRegistry>,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
@@ -818,7 +819,10 @@ impl RemoteWorktree {
rpc, rpc,
replica_id, replica_id,
open_buffers: Default::default(), open_buffers: Default::default(),
peers: HashMap::from_iter(Some((host_peer_id, 0))), peers: peers
.into_iter()
.map(|p| (PeerId(p.peer_id), p.replica_id as ReplicaId))
.collect(),
languages, languages,
} }
} }
@@ -883,10 +887,12 @@ impl RemoteWorktree {
envelope: TypedEnvelope<proto::AddGuest>, envelope: TypedEnvelope<proto::AddGuest>,
cx: &mut ModelContext<Worktree>, cx: &mut ModelContext<Worktree>,
) -> Result<()> { ) -> Result<()> {
self.peers.insert( let guest = envelope
PeerId(envelope.payload.peer_id), .payload
envelope.payload.replica_id as ReplicaId, .guest
); .ok_or_else(|| anyhow!("empty peer"))?;
self.peers
.insert(PeerId(guest.peer_id), guest.replica_id as ReplicaId);
Ok(()) Ok(())
} }