Feature/ws send confirmation removal (#280)

* Removed send confirmation

* Updated websocket examples
This commit is contained in:
Jędrzej Stuczyński
2020-07-13 12:07:06 +01:00
committed by GitHub
parent 5ef96aa241
commit 6cc2bc6f91
5 changed files with 32 additions and 46 deletions
@@ -56,14 +56,6 @@ func main() {
if err = conn.WriteMessage(websocket.BinaryMessage, payload); err != nil {
panic(err)
}
sendConfirmationJSON := make(map[string]interface{})
err = conn.ReadJSON(&sendConfirmationJSON)
if err != nil {
panic(err)
}
if sendConfirmationJSON["type"].(string) != "send" {
panic("invalid send confirmation")
}
fmt.Printf("waiting to receive a message from the mix network...\n")
_, receivedMessage, err := conn.ReadMessage()
@@ -49,15 +49,6 @@ func main() {
panic(err)
}
sendConfirmationJSON := make(map[string]interface{})
err = conn.ReadJSON(&sendConfirmationJSON)
if err != nil {
panic(err)
}
if sendConfirmationJSON["type"].(string) != "send" {
panic("invalid send confirmation")
}
fmt.Printf("waiting to receive a message from the mix network...\n")
_, receivedMessage, err := conn.ReadMessage()
if err != nil {
@@ -26,8 +26,6 @@ async def send_file():
print("sending content of 'dummy_file' over the mix network...")
await websocket.send(bin_payload)
msg_send_confirmation = json.loads(await websocket.recv())
assert msg_send_confirmation["type"], "send"
print("waiting to receive the 'dummy_file' from the mix network...")
received_data = await websocket.recv()
@@ -24,8 +24,6 @@ async def send_text():
print("sending '{}' over the mix network...".format(message))
await websocket.send(text_send)
msg_send_confirmation = json.loads(await websocket.recv())
assert msg_send_confirmation["type"], "send"
print("waiting to receive a message from the mix network...")
received_message = await websocket.recv()
+32 -25
View File
@@ -91,14 +91,18 @@ impl Handler {
}
}
fn handle_text_send(&mut self, msg: String, full_recipient_address: String) -> ServerResponse {
fn handle_text_send(
&mut self,
msg: String,
full_recipient_address: String,
) -> Option<ServerResponse> {
let message_bytes = msg.into_bytes();
let recipient = match Recipient::try_from_string(full_recipient_address) {
Ok(address) => address,
Err(e) => {
trace!("failed to parse received Recipient: {:?}", e);
return ServerResponse::new_error("malformed recipient address");
return Some(ServerResponse::new_error("malformed recipient address"));
}
};
@@ -107,8 +111,7 @@ impl Handler {
self.msg_input.unbounded_send(input_msg).unwrap();
self.received_response_type = ReceivedResponseType::Text;
ServerResponse::Send
None
}
async fn handle_text_get_clients(&mut self) -> ServerResponse {
@@ -129,52 +132,58 @@ impl Handler {
}
}
async fn handle_text_message(&mut self, msg: String) -> Message {
async fn handle_text_message(&mut self, msg: String) -> Option<Message> {
debug!("Handling text message request");
trace!("Content: {:?}", msg.clone());
match ClientRequest::try_from(msg) {
Err(e) => ServerResponse::Error {
message: format!("received invalid request. err: {:?}", e),
}
.into(),
Ok(req) => match req {
ClientRequest::Send { message, recipient } => {
self.handle_text_send(message, recipient)
Err(e) => Some(
ServerResponse::Error {
message: format!("received invalid request. err: {:?}", e),
}
ClientRequest::GetClients => self.handle_text_get_clients().await,
ClientRequest::SelfAddress => self.handle_text_self_address(),
}
.into(),
.into(),
),
Ok(req) => match req {
ClientRequest::Send { message, recipient } => self
.handle_text_send(message, recipient)
.map(|resp| resp.into()),
ClientRequest::GetClients => Some(self.handle_text_get_clients().await.into()),
ClientRequest::SelfAddress => Some(self.handle_text_self_address().into()),
},
}
}
async fn handle_binary_send(&mut self, recipient: Recipient, data: Vec<u8>) -> ServerResponse {
async fn handle_binary_send(
&mut self,
recipient: Recipient,
data: Vec<u8>,
) -> Option<ServerResponse> {
// the ack control is now responsible for chunking, etc.
let input_msg = InputMessage::new(recipient, data);
self.msg_input.unbounded_send(input_msg).unwrap();
self.received_response_type = ReceivedResponseType::Binary;
ServerResponse::Send
None
}
// if it's binary we assume it's a sphinx packet formatted the same way as we'd have sent
// it to the gateway
async fn handle_binary_message(&mut self, msg: Vec<u8>) -> Message {
async fn handle_binary_message(&mut self, msg: Vec<u8>) -> Option<Message> {
debug!("Handling binary message request");
self.received_response_type = ReceivedResponseType::Binary;
// make sure it is correctly formatted
let binary_request = BinaryClientRequest::try_from_bytes(&msg);
if binary_request.is_none() {
return ServerResponse::new_error("invalid binary request").into();
return Some(ServerResponse::new_error("invalid binary request").into());
}
match binary_request.unwrap() {
BinaryClientRequest::Send { recipient, data } => {
self.handle_binary_send(recipient, data).await
}
}
.into()
.map(|resp| resp.into())
}
async fn handle_request(&mut self, raw_request: Message) -> Option<Message> {
@@ -182,10 +191,8 @@ impl Handler {
// them and let's test that claim. If that's not the case, just copy code from
// old version of this file.
match raw_request {
Message::Text(text_message) => Some(self.handle_text_message(text_message).await),
Message::Binary(binary_message) => {
Some(self.handle_binary_message(binary_message).await)
}
Message::Text(text_message) => self.handle_text_message(text_message).await,
Message::Binary(binary_message) => self.handle_binary_message(binary_message).await,
_ => None,
}
}