-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTDBClient.py
executable file
·152 lines (129 loc) · 3.79 KB
/
TTDBClient.py
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
#!/usr/bin/python2
import argparse
import socket
import sys
def main():
parser = argparse.ArgumentParser(description='TTDB database client.')
parser.add_argument('--socket', default='./ttdb_socket', help='location of Unix socket to connect to (default: ./ttdb_socket)')
args = parser.parse_args()
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
sock.connect(args.socket)
except socket.error, msg:
print >>sys.stderr, msg
sys.exit(1)
while True:
line = sys.stdin.readline().split()
if len(line) == 0:
continue
elif line[0].upper() == 'END' and len(line) == 1:
sock.close()
break
elif line[0].upper() == 'SET' and len(line) == 3:
do_set(line[1], line[2], sock)
elif line[0].upper() == 'GET' and len(line) == 2:
do_get(line[1], sock)
elif line[0].upper() == 'UNSET' and len(line) == 2:
do_unset(line[1], sock)
elif line[0].upper() == 'NUMEQUALTO' and len(line) == 2:
do_numequalto(line[1], sock)
elif line[0].upper() == 'BEGIN' and len(line) == 1:
do_begin(sock, 'RW')
elif line[0].upper() == 'BEGIN' and len(line) == 2 and line[1].upper() in ['RW', 'RO']:
do_begin(sock, line[1])
elif line[0].upper() == 'ROLLBACK' and len(line) == 1:
do_rollback(sock)
elif line[0].upper() == 'COMMIT' and len(line) == 1:
do_commit(sock)
elif line[0].upper() == 'RESET' and len(line) == 1:
do_reset(sock)
elif line[0].upper() == 'DEBUG' and len(line) == 1:
do_debug(sock)
else:
print 'Invalid syntax for command %s' % line[0]
def do_set(variable, value, sock):
"""Send SET command to server.
Args:
variable: variable to set
value: value to which to set variable
sock: socket connection where to send command
"""
sock.sendall(" ".join(('SET', variable, value, '|')))
msg = sock.recv(64)
if msg != 'success':
print msg
def do_get(variable, sock):
"""Send GET command to server.
Args:
variable: variable whose value to get
sock: socket connection where to send command
"""
sock.sendall(" ".join(('GET', variable, '|')))
print sock.recv(64)
def do_unset(variable, sock):
"""Send UNSET command to server.
Args:
variable: variable to unset
sock: socket connection where to send command
"""
sock.sendall(" ".join(('UNSET', variable, '|')))
msg = sock.recv(64)
if msg != 'success': print msg
def do_numequalto(value, sock):
"""Send NUMEQUALTO command to server.
Args:
value: the value to count
sock: socket connection where to send command
"""
sock.sendall(" ".join(('NUMEQUALTO', value, '|')))
print sock.recv(64)
def do_begin(sock, transaction_type):
"""Send BEGIN command to server.
Args:
sock: socket connection where to send command
"""
if transaction_type.upper() == 'RW':
sock.sendall('BEGIN RW |')
elif transaction_type.upper() == 'RO':
sock.sendall('BEGIN RO |')
msg = sock.recv(64)
if msg != 'success':
print msg
def do_rollback(sock):
"""Send ROLLBACK command to server.
Args:
sock: socket connection where to send command
"""
sock.sendall('ROLLBACK |')
msg = sock.recv(64)
if msg != 'success':
print msg
def do_commit(sock):
"""Send COMMIT command to server.
Args:
sock: socket connection where to send command
"""
sock.sendall('COMMIT |')
msg = sock.recv(64)
if msg != 'success':
print msg
def do_reset(sock):
"""Send RESET command to server.
Args:
sock: socket connection where to send command
"""
sock.sendall('RESET |')
msg = sock.recv(64)
if msg != 'success':
print msg
def do_debug(sock):
"""Send DEBUG command to server.
Args:
sock: socket connection where to send command
"""
sock.sendall('DEBUG |')
msg = sock.recv(64)
if msg != 'success':
print msg
if __name__ == '__main__':
main()