-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache-routine.py
executable file
·44 lines (35 loc) · 1.28 KB
/
cache-routine.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
from lrucache import LRUCache
import socket, json, sys
if len ( sys.argv ) >= 5 :
server_name = sys.argv[1]
port_number = int ( sys.argv[2] )
number_of_connections = int ( sys.argv[3] )
size_of_cache = int ( sys.argv[4] )
else:
print "Usage: \"python cache-routine.py server_name port_number number_of_connections size_of_cache\": Insufficient parameters"
exit()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# for reusing the same port without waiting for TIME_WAIT to expire
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((server_name, port_number))
# Will listen to only one connection at a time
s.listen( number_of_connections )
LRU = LRUCache( size_of_cache )
while True:
conn, addr = s.accept()
data = conn.recv(1024)
print "DEBUG: " + data
# convert the received data into JSON
json_data = json.loads(data)
"""
fetch the request method
GET denotes a GET cache request to check for hit/miss
POST denotes a SAVE request once a miss happens
"""
request_method = json_data["method"]
if request_method == "GET":
lru_response = LRU.get(json_data["username"])
conn.send(str(lru_response))
elif request_method == "POST":
LRU.set(json_data["username"], json_data["userdata"])
conn.close()