Newer
Older
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
#!/usr/bin/env python3
# -----------------------------------------------------------------------------
# Distributed Systems (TDDD25)
# -----------------------------------------------------------------------------
# Author: Sergiu Rafiliu (sergiu.rafiliu@liu.se)
# Modified: 16 March 2017
#
# Copyright 2012-2017 Linkoping University
# -----------------------------------------------------------------------------
"""Client reader/writer for a fortune database."""
import sys
import socket
import json
import argparse
# -----------------------------------------------------------------------------
# Initialize and read the command line arguments
# -----------------------------------------------------------------------------
def address(path):
addr = path.split(":")
if len(addr) == 2 and addr[1].isdigit():
return((addr[0], int(addr[1])))
else:
msg = "{} is not a correct server address.".format(path)
raise argparse.ArgumentTypeError(msg)
description = """\
Client for a fortune database. It reads a random fortune from the database.\
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"-w", "--write", metavar="FORTUNE", dest="fortune",
help="Write a new fortune to the database."
)
parser.add_argument(
"-i", "--interactive", action="store_true", dest="interactive",
default=False, help="Interactive session with the fortune database."
)
parser.add_argument(
"address", type=address, nargs=1, metavar="addr:port",
help="Server address."
)
opts = parser.parse_args()
server_address = opts.address[0]
# -----------------------------------------------------------------------------
# Auxiliary classes
# -----------------------------------------------------------------------------
class CommunicationError(Exception):
pass
class DatabaseProxy(object):
"""Class that simulates the behavior of the database class."""
def __init__(self, server_address):
self.address = server_address
def read(self):
#
# Your code here.
#
}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(self.address)
worker = s.makefile(mode="rw")
worker.write(json.dumps(msg) + '\n')
worker.flush()
res = json.loads(worker.readline())
pass
def write(self, fortune):
#
# Your code here.
#
msg = {
"method": "write" ,
"args": fortune
}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(self.address)
worker = s.makefile(mode="rw")
worker.write(json.dumps(msg) + '\n')
worker.flush()
res = json.loads(worker.readline())
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
pass
# -----------------------------------------------------------------------------
# The main program
# -----------------------------------------------------------------------------
# Create the database object.
db = DatabaseProxy(server_address)
if not opts.interactive:
# Run in the normal mode.
if opts.fortune is not None:
db.write(opts.fortune)
else:
print(db.read())
else:
# Run in the interactive mode.
def menu():
print("""\
Choose one of the following commands:
r :: read a random fortune from the database,
w <FORTUNE> :: write a new fortune into the database,
h :: print this menu,
q :: exit.\
""")
command = ""
menu()
while command != "q":
sys.stdout.write("Command> ")
command = input()
if command == "r":
print(db.read())
elif (len(command) > 1 and command[0] == "w" and
command[1] in [" ", "\t"]):
db.write(command[2:].strip())
elif command == "h":
menu()