Skip to content

Commit 8ee76e5

Browse files
authored
Merge pull request #55 from CoLearn-Dev/rename-set-send
Rename variable transfer functions (set/get -> send/receive)
2 parents b5b552e + e1435d0 commit 8ee76e5

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

examples/protocol_variable_transfer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl ProtocolEntry for Initiator {
1111
participants: Vec<Participant>,
1212
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
1313
println!("initiator");
14-
cl.set_variable("output", &param, &[participants[1].clone()])
14+
cl.send_variable("output", &param, &[participants[1].clone()])
1515
.await?;
1616
Ok(())
1717
}
@@ -26,7 +26,7 @@ impl ProtocolEntry for Receiver {
2626
param: Vec<u8>,
2727
participants: Vec<Participant>,
2828
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
29-
let msg = cl.get_variable("output", &participants[0]).await?;
29+
let msg = cl.recv_variable("output", &participants[0]).await?;
3030
println!("{}", String::from_utf8_lossy(&msg));
3131
cl.create_entry(&format!("tasks:{}:output", cl.get_task_id()?), &msg)
3232
.await?;

src/extensions/variable_transfer.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ mod tls_utils;
77
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
88

99
impl crate::application::CoLink {
10+
#[deprecated(note = "please use `send_variable` instead")]
1011
pub async fn set_variable(
1112
&self,
1213
key: &str,
1314
payload: &[u8],
1415
receivers: &[Participant],
16+
) -> Result<(), Error> {
17+
self.send_variable(key, payload, receivers).await
18+
}
19+
20+
pub async fn send_variable(
21+
&self,
22+
key: &str,
23+
payload: &[u8],
24+
receivers: &[Participant],
1525
) -> Result<(), Error> {
1626
if self.task_id.is_empty() {
1727
Err("task_id not found".to_string())?;
@@ -24,11 +34,11 @@ impl crate::application::CoLink {
2434
let receiver = receiver.clone();
2535
tokio::spawn(async move {
2636
if cl
27-
._set_variable_p2p(&key, &payload, &receiver)
37+
._send_variable_p2p(&key, &payload, &receiver)
2838
.await
2939
.is_err()
3040
{
31-
cl.set_variable_with_remote_storage(&key, &payload, &[receiver.clone()])
41+
cl.send_variable_with_remote_storage(&key, &payload, &[receiver.clone()])
3242
.await?;
3343
}
3444
Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
@@ -37,14 +47,19 @@ impl crate::application::CoLink {
3747
Ok(())
3848
}
3949

50+
#[deprecated(note = "please use `recv_variable` instead")]
4051
pub async fn get_variable(&self, key: &str, sender: &Participant) -> Result<Vec<u8>, Error> {
52+
self.recv_variable(key, sender).await
53+
}
54+
55+
pub async fn recv_variable(&self, key: &str, sender: &Participant) -> Result<Vec<u8>, Error> {
4156
if self.task_id.is_empty() {
4257
Err("task_id not found".to_string())?;
4358
}
44-
if let Ok(res) = self._get_variable_p2p(key, sender).await {
59+
if let Ok(res) = self._recv_variable_p2p(key, sender).await {
4560
return Ok(res);
4661
}
47-
let res = self.get_variable_with_remote_storage(key, sender).await?;
62+
let res = self.recv_variable_with_remote_storage(key, sender).await?;
4863
Ok(res)
4964
}
5065
}

src/extensions/variable_transfer/p2p_inbox.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub(crate) struct VtP2pCtx {
151151
}
152152

153153
impl crate::application::CoLink {
154-
pub(crate) async fn _set_variable_p2p(
154+
pub(crate) async fn _send_variable_p2p(
155155
&self,
156156
key: &str,
157157
payload: &[u8],
@@ -165,7 +165,7 @@ impl crate::application::CoLink {
165165
.contains_key(&receiver.user_id)
166166
{
167167
let inbox = self
168-
.get_variable_with_remote_storage("inbox", receiver)
168+
.recv_variable_with_remote_storage("inbox", receiver)
169169
.await?;
170170
let inbox: VTInbox = serde_json::from_slice(&inbox)?;
171171
let inbox = if inbox.addr.is_empty() {
@@ -218,7 +218,7 @@ impl crate::application::CoLink {
218218
Ok(())
219219
}
220220

221-
pub(crate) async fn _get_variable_p2p(
221+
pub(crate) async fn _recv_variable_p2p(
222222
&self,
223223
key: &str,
224224
sender: &Participant,
@@ -287,7 +287,7 @@ impl crate::application::CoLink {
287287
.clone(),
288288
}
289289
};
290-
self.set_variable_with_remote_storage(
290+
self.send_variable_with_remote_storage(
291291
"inbox",
292292
&serde_json::to_vec(&vt_inbox)?,
293293
&[sender.clone()],

src/extensions/variable_transfer/remote_storage.rs

+20
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ mod colink_remote_storage {
88
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
99

1010
impl crate::application::CoLink {
11+
#[deprecated(note = "please use `send_variable_with_remote_storage` instead")]
1112
pub async fn set_variable_with_remote_storage(
1213
&self,
1314
key: &str,
1415
payload: &[u8],
1516
receivers: &[Participant],
17+
) -> Result<(), Error> {
18+
self.send_variable_with_remote_storage(key, payload, receivers)
19+
.await
20+
}
21+
22+
pub async fn send_variable_with_remote_storage(
23+
&self,
24+
key: &str,
25+
payload: &[u8],
26+
receivers: &[Participant],
1627
) -> Result<(), Error> {
1728
let mut new_participants = vec![Participant {
1829
user_id: self.get_user_id()?,
@@ -49,10 +60,19 @@ impl crate::application::CoLink {
4960
Ok(())
5061
}
5162

63+
#[deprecated(note = "please use `recv_variable_with_remote_storage` instead")]
5264
pub async fn get_variable_with_remote_storage(
5365
&self,
5466
key: &str,
5567
sender: &Participant,
68+
) -> Result<Vec<u8>, Error> {
69+
self.recv_variable_with_remote_storage(key, sender).await
70+
}
71+
72+
pub async fn recv_variable_with_remote_storage(
73+
&self,
74+
key: &str,
75+
sender: &Participant,
5676
) -> Result<Vec<u8>, Error> {
5777
let key = format!(
5878
"_remote_storage:private:{}:_variable_transfer:{}:{}",

tests/test_protocol_variable_transfer.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ impl ProtocolEntry for Initiator {
1515
for i in 0..8 {
1616
let key = &format!("output{}", i);
1717
let key2 = &format!("output_remote_storage{}", i);
18-
cl.set_variable(key, &param, &participants[1..participants.len()])
19-
.await?;
20-
cl.set_variable_with_remote_storage(key2, &param, &participants[1..participants.len()])
18+
cl.send_variable(key, &param, &participants[1..participants.len()])
2119
.await?;
20+
cl.send_variable_with_remote_storage(
21+
key2,
22+
&param,
23+
&participants[1..participants.len()],
24+
)
25+
.await?;
2226
}
2327
Ok(())
2428
}
@@ -36,11 +40,11 @@ impl ProtocolEntry for Receiver {
3640
for i in 0..8 {
3741
let key = &format!("output{}", i);
3842
let key2 = &format!("output_remote_storage{}", i);
39-
let msg = cl.get_variable(key, &participants[0]).await?;
43+
let msg = cl.recv_variable(key, &participants[0]).await?;
4044
cl.create_entry(&format!("tasks:{}:output{}", cl.get_task_id()?, i), &msg)
4145
.await?;
4246
let msg = cl
43-
.get_variable_with_remote_storage(key2, &participants[0])
47+
.recv_variable_with_remote_storage(key2, &participants[0])
4448
.await?;
4549
cl.create_entry(
4650
&format!("tasks:{}:output_remote_storage{}", cl.get_task_id()?, i),

0 commit comments

Comments
 (0)