-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.rs
179 lines (159 loc) · 5.62 KB
/
cli.rs
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
use std::{
io::{Read, Write},
net::TcpStream,
};
use clap::{Parser, Subcommand};
#[derive(Debug, Subcommand)]
enum Command {
CreateLog {
/// Log name
#[clap(long, short)]
name: String,
/// Number of partitions for the log
#[clap(long, short)]
partitions: u8,
},
Publish {
/// Log name
#[clap(long, short)]
log_name: String,
/// Partition to publish the data
#[clap(long, short)]
partition: u8,
/// Data to be published
#[clap(long, short)]
data: String,
},
Fetch {
/// Log name
#[clap(long, short)]
log_name: String,
/// Partition to publish the data
#[clap(long, short)]
partition: u8,
/// Consumer group
#[clap(long, short)]
group: String,
},
}
#[derive(Debug, Parser)]
struct Args {
/// Rog server address on the format ip:port, e.g.: 127.0.0.1:7878
#[clap(long, short)]
address: String,
#[clap(subcommand)]
command: Command,
}
fn main() {
let args = Args::parse();
let mut stream = match TcpStream::connect(args.address) {
Ok(stream) => stream,
Err(e) => panic!("Unable to stablish connection to rog server {e}"),
};
match args.command {
Command::CreateLog { name, partitions } => {
let command_byte = (0_u8).to_be_bytes();
let partitions_as_bytes = partitions.to_be_bytes();
let name_as_bytes = name.as_bytes();
let mut command = Vec::new();
command.extend(command_byte);
command.extend(partitions_as_bytes);
command.extend((name_as_bytes.len() as u8).to_be_bytes());
command.extend(name_as_bytes);
if let Err(e) = stream.write_all(&command) {
println!("Unable to create log\n{e}");
return;
}
let mut buf = [0; 1024];
match stream.read(&mut buf) {
Ok(_) => {
if buf[0] == 0 {
println!("Created log {name} with {partitions} partitions");
} else {
println!("Unable to create log");
let error_message = parse_error(&buf);
println!("{error_message}");
}
}
Err(e) => println!("Unable to create log\n{e}"),
}
}
Command::Publish {
log_name,
partition,
data,
} => {
let command_byte = (1_u8).to_be_bytes();
let partitions_as_bytes = partition.to_be_bytes();
let name_as_bytes = log_name.as_bytes();
let data_as_bytes = data.as_bytes();
let mut command = Vec::new();
command.extend(command_byte);
command.extend(partitions_as_bytes);
command.extend((name_as_bytes.len() as u8).to_be_bytes());
command.extend(name_as_bytes);
command.extend(data_as_bytes.len().to_be_bytes());
command.extend(data_as_bytes);
if let Err(e) = stream.write_all(&command) {
println!("Unable to publish to log\n{e}");
return;
}
let mut buf = [0; 1024];
match stream.read(&mut buf) {
Ok(_) => {
if buf[0] == 0 {
println!(
"Successfully published to log {log_name} to partition {partition}"
);
} else {
println!("Unable to publish to log");
let error_message = parse_error(&buf);
println!("{error_message}");
}
}
Err(e) => println!("Unable to publish to log\n{e}"),
}
}
Command::Fetch {
log_name,
partition,
group,
} => {
let command_byte = (2_u8).to_be_bytes();
let partitions_as_bytes = partition.to_be_bytes();
let name_as_bytes = log_name.as_bytes();
let group_as_bytes = group.as_bytes();
let mut command = Vec::new();
command.extend(command_byte);
command.extend(partitions_as_bytes);
command.extend((name_as_bytes.len() as u8).to_be_bytes());
command.extend(name_as_bytes);
command.extend((group_as_bytes.len() as u8).to_be_bytes());
command.extend(group_as_bytes);
if let Err(e) = stream.write_all(&command) {
println!("Unable to fetch log\n{e}");
return;
}
let mut buf = [0; 1024];
match stream.read(&mut buf) {
Ok(_) => {
if buf[0] == 0 {
let message_size = usize::from_be_bytes(buf[1..9].try_into().unwrap());
let response = String::from_utf8_lossy(&buf[9..(9 + message_size)]);
println!("{response}");
} else {
println!("Unable to fetch log");
let error_message = parse_error(&buf);
println!("{error_message}");
}
}
Err(e) => println!("Unable to fetch log\n{e}"),
}
}
}
}
fn parse_error(buf: &[u8]) -> String {
let message_size = usize::from_be_bytes(buf[1..9].try_into().unwrap());
let message = &buf[9..(9 + message_size)];
String::from_utf8_lossy(message).to_string()
}