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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
197
198
199
200
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use libp2p::PeerId;
use crate::node::user_accounts::UserAccount;
use crate::rpc::Rpc;
use prost::Message;
use state::Storage;
use std::collections::BTreeMap;
use std::sync::RwLock;
mod rtc_managing;
mod rtc_messaging;
use super::chat::Chat;
use super::group;
use super::group::group_id::GroupId;
use super::messaging::{proto, Messaging, MessagingServiceType};
use crate::utilities::timestamp;
use rtc_managing::RtcManaging;
use rtc_messaging::RtcMessaging;
pub mod proto_rpc {
include!("qaul.rpc.rtc.rs");
}
pub mod proto_net {
include!("qaul.net.rtc.rs");
}
#[derive(Clone)]
pub struct RtcSession {
pub user_id: Vec<u8>,
pub group_id: Vec<u8>,
pub session_type: u32,
pub created_at: u64,
pub state: u8,
}
#[derive(Clone)]
pub struct RtcSessions {
pub sessions: BTreeMap<Vec<u8>, RtcSession>,
}
pub static RTCSESSIONS: Storage<RwLock<RtcSessions>> = Storage::new();
pub struct Rtc {}
impl Rtc {
pub fn init() {
let rtc_sessions = RtcSessions {
sessions: BTreeMap::new(),
};
RTCSESSIONS.set(RwLock::new(rtc_sessions));
}
pub fn get_session_from_id(group_id: &Vec<u8>) -> Option<RtcSession> {
let sessions = RTCSESSIONS.get().read().unwrap();
if sessions.sessions.contains_key(group_id) {
return Some(sessions.sessions.get(group_id).unwrap().clone());
}
None
}
pub fn update_session(session: RtcSession) {
let mut sessions = RTCSESSIONS.get().write().unwrap();
sessions.sessions.insert(session.group_id.clone(), session);
}
pub fn remove_session(session_id: &Vec<u8>) {
let mut sessions = RTCSESSIONS.get().write().unwrap();
sessions.sessions.remove(session_id);
}
pub fn send_rtc_message_through_message(
user_account: &UserAccount,
receiver: PeerId,
data: &Vec<u8>,
) {
let group_id = GroupId::from_peers(&user_account.id, &receiver);
if !group::GroupStorage::group_exists(user_account.id, group_id.to_bytes()) {
group::GroupManage::create_new_direct_chat_group(&user_account.id, &receiver);
}
let group;
match group::GroupStorage::get_group(user_account.id, group_id.to_bytes()) {
Some(v) => group = v,
None => return,
}
let my_member;
match group.get_member(&user_account.id.to_bytes()) {
Some(v) => {
my_member = v.clone();
}
_ => {
return;
}
}
let last_index = my_member.last_message_index + 1;
let message_id = Chat::generate_message_id(&group.id, &user_account.id, last_index);
let common_message = proto::CommonMessage {
message_id: message_id.clone(),
group_id: group_id.to_bytes(),
sent_at: timestamp::Timestamp::get_timestamp(),
payload: Some(proto::common_message::Payload::RtcMessage(
proto::RtcMessage {
content: data.clone(),
},
)),
};
let send_message = proto::Messaging {
message: Some(proto::messaging::Message::CommonMessage(
common_message.clone(),
)),
};
if let Err(e) = Messaging::pack_and_send_message(
user_account,
&receiver,
send_message.encode_to_vec(),
MessagingServiceType::Rtc,
&message_id,
true,
) {
log::error!("rtc message sending failed {}", e.to_string());
}
}
pub fn net(sender_id: &PeerId, receiver_id: &PeerId, data: &Vec<u8>) {
match proto_net::RtcContainer::decode(&data[..]) {
Ok(messaging) => match messaging.message {
Some(proto_net::rtc_container::Message::RtcSessionRequest(session_req)) => {
log::error!("on_session_request");
rtc_managing::RtcManaging::on_session_request(
&sender_id,
&receiver_id,
&session_req,
);
}
Some(proto_net::rtc_container::Message::RtcSessionManagement(session_mgr)) => {
log::error!("on_session_management");
rtc_managing::RtcManaging::on_session_management(
&sender_id,
&receiver_id,
&session_mgr,
);
}
_ => {
log::error!("rtc message from {} was empty", sender_id.to_base58())
}
},
Err(e) => {
log::error!(
"Error decoding Rtc Message from {} to {}: {}",
sender_id.to_base58(),
receiver_id.to_base58(),
e
);
}
}
}
pub fn rpc(data: Vec<u8>, user_id: Vec<u8>) {
let my_user_id = PeerId::from_bytes(&user_id).unwrap();
match proto_rpc::RtcRpc::decode(&data[..]) {
Ok(rtc_rpc) => {
match rtc_rpc.message {
Some(proto_rpc::rtc_rpc::Message::RtcSessionRequest(session_req)) => {
match RtcManaging::session_request(&my_user_id, &session_req) {
Err(error) => {
log::error!("rtc request error {}", error);
}
Ok(_id) => {
let proto_message = proto_rpc::RtcRpc {
message: Some(proto_rpc::rtc_rpc::Message::RtcSessionResponse(
proto_rpc::RtcSessionResponse {
group_id: session_req.group_id.clone(),
},
)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");
Rpc::send_message(
buf,
crate::rpc::proto::Modules::Rtc.into(),
"".to_string(),
Vec::new(),
);
}
}
}
Some(proto_rpc::rtc_rpc::Message::RtcSessionManagement(session_req)) => {
if let Err(error) =
RtcManaging::session_management(&my_user_id, &session_req)
{
log::error!("rtc management error {}", error);
}
}
Some(proto_rpc::rtc_rpc::Message::RtcOutgoing(session_req)) => {
if let Err(error) = RtcMessaging::send_message(&my_user_id, &session_req) {
log::error!("rtc message error {}", error);
}
}
Some(proto_rpc::rtc_rpc::Message::RtcSessionListRequest(_session_req)) => {
let res = RtcManaging::session_list(&my_user_id);
let proto_message = proto_rpc::RtcRpc {
message: Some(proto_rpc::rtc_rpc::Message::RtcSessionListResponse(res)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");
Rpc::send_message(
buf,
crate::rpc::proto::Modules::Rtc.into(),
"".to_string(),
Vec::new(),
);
}
_ => {
log::error!("Unhandled Protobuf RTC message");
}
}
}
Err(error) => {
log::error!("{:?}", error);
}
}
}
}