common/peer/jax_protocol/
messages.rs1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4use crate::linked_data::Link;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub enum Request {
9 Ping(PingRequest),
11 FetchBucket(FetchBucketRequest),
13 Announce(AnnounceMessage),
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
19pub enum Response {
20 Ping(PingResponse),
22 FetchBucket(FetchBucketResponse),
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct PingRequest {
29 pub bucket_id: Uuid,
31 pub current_link: Link,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
37pub enum SyncStatus {
38 NotFound,
40 Behind,
42 InSync,
44 Ahead,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct PingResponse {
51 pub status: SyncStatus,
52}
53
54impl PingResponse {
55 pub fn new(status: SyncStatus) -> Self {
56 Self { status }
57 }
58
59 pub fn not_found() -> Self {
60 Self {
61 status: SyncStatus::NotFound,
62 }
63 }
64
65 pub fn behind() -> Self {
66 Self {
67 status: SyncStatus::Behind,
68 }
69 }
70
71 pub fn in_sync() -> Self {
72 Self {
73 status: SyncStatus::InSync,
74 }
75 }
76
77 pub fn unsynced() -> Self {
78 Self {
79 status: SyncStatus::Ahead,
80 }
81 }
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct AnnounceMessage {
87 pub bucket_id: Uuid,
89 pub new_link: Link,
91 pub previous_link: Option<Link>,
93}
94
95impl AnnounceMessage {
96 pub fn new(bucket_id: Uuid, new_link: Link, previous_link: Option<Link>) -> Self {
97 Self {
98 bucket_id,
99 new_link,
100 previous_link,
101 }
102 }
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct FetchBucketRequest {
108 pub bucket_id: Uuid,
110}
111
112impl FetchBucketRequest {
113 pub fn new(bucket_id: Uuid) -> Self {
114 Self { bucket_id }
115 }
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct FetchBucketResponse {
121 pub current_link: Option<Link>,
123}
124
125impl FetchBucketResponse {
126 pub fn new(current_link: Option<Link>) -> Self {
127 Self { current_link }
128 }
129
130 pub fn not_found() -> Self {
131 Self { current_link: None }
132 }
133
134 pub fn found(link: Link) -> Self {
135 Self {
136 current_link: Some(link),
137 }
138 }
139}