MCPcopy Create free account
hub / github.com/evilsocket/cake / handle_one

Method handle_one

cake-core/tests/protocol.rs:63–112  ·  view source on GitHub ↗

Handle exactly one client connection, then return.

(self, cluster_key: Option<&str>)

Source from the content-addressed store, hash-verified

61
62 /// Handle exactly one client connection, then return.
63 async fn handle_one(self, cluster_key: Option<&str>) -> anyhow::Result<()> {
64 let (mut socket, _client) = self.listener.accept().await?;
65
66 // Auth
67 if let Some(key) = cluster_key {
68 cake_core::cake::auth::authenticate_as_worker(&mut socket, key).await?;
69 }
70
71 // First message
72 let (_, first) = Message::from_reader(&mut socket).await?;
73 match first {
74 Message::Hello => {
75 Message::WorkerInfo(mock_worker_info(0))
76 .to_writer(&mut socket)
77 .await?;
78 }
79 other => return Err(anyhow::anyhow!("unexpected first message: {:?}", other)),
80 }
81
82 // Message loop
83 loop {
84 let result = Message::from_reader(&mut socket).await;
85 let (_, msg) = match result {
86 Ok(m) => m,
87 Err(_) => break, // client disconnected
88 };
89
90 match msg {
91 Message::SingleOp { x, .. } => {
92 log::debug!("single op");
93 Message::Tensor(x).to_writer(&mut socket).await?;
94 }
95 Message::Batch { x, .. } => {
96 log::debug!("batch");
97 Message::Tensor(x).to_writer(&mut socket).await?;
98 }
99 Message::Goodbye => {
100 log::debug!("goodbye");
101 Message::WorkerInfo(mock_worker_info(0))
102 .to_writer(&mut socket)
103 .await?;
104 }
105 other => {
106 return Err(anyhow::anyhow!("unhandled in mock loop: {:?}", other));
107 }
108 }
109 }
110
111 Ok(())
112 }
113}
114
115/// Helper: connect to addr, do Hello handshake, return stream + WorkerInfo.

Callers 10

test_handshake_with_authFunction · 0.80
test_single_op_echoFunction · 0.80
test_batch_echoFunction · 0.80
bench_tcp_roundtripFunction · 0.80

Calls 4

authenticate_as_workerFunction · 0.85
to_writerMethod · 0.80
mock_worker_infoFunction · 0.70
WorkerInfoClass · 0.50

Tested by

no test coverage detected