-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.du
95 lines (76 loc) · 1.97 KB
/
client.du
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
import Socket;
import "maple.du" as maple;
class Client {
init(var server, var port) {}
/**
* close
*/
private close() {
this.socket.close();
}
/**
* connect
*/
connect() {
var res = Socket.create(Socket.AF_INET, Socket.SOCK_STREAM);
if (not res.success()) {
return Error(res.unwrapError());
}
this.socket = res.unwrap();
res = this.socket.connect(this.server, this.port);
if (not res.success()) {
return Error(res.unwrapError());
}
return Success(nil);
}
/**
* get
*/
get(key) {
const cmd = maple.getCmd + " " + key + maple.crlf;
print(cmd);
var res = this.socket.write(cmd);
if (not res.success()) {
return Error(res.unwrapError());
}
res = this.socket.recv(maple.readBuffer);
if (not res.success()) {
return Error(res.unwrapError());
}
print(res.unwrap());
return Success(res.unwrap());
}
/**
* set
*/
set(key, value) {
const cmd = "{} {} {}{}".format(maple.setCmd, key, value, maple.crlf);
var res = this.socket.write(cmd);
if (not res.success()) {
return Error(res.unwrapError());
}
res = this.socket.recv(maple.readBuffer);
if (not res.success()) {
return Error(res.unwrapError());
}
print(res.unwrap());
return Success(res.unwrap());
}
/**
* del
*/
del(key) {
const cmd = maple.delCmd + " " + key + maple.crlf;
print(cmd);
var res = this.socket.write(cmd);
if (not res.success()) {
return Error(res.unwrapError());
}
res = this.socket.recv(maple.readBuffer);
if (not res.success()) {
return Error(res.unwrapError());
}
print(res.unwrap());
return Success(res.unwrap());
}
}