-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
134 lines (104 loc) · 4.31 KB
/
cache.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
import time
import json
import socket
from datetime import datetime, timedelta
from servidor1 import HOST as HOST_1, PORT as PORT_1
from servidor2 import HOST as HOST_2, PORT as PORT_2
from servidor3 import HOST as HOST_3, PORT as PORT_3
from utils import create_connection
MESSAGE_SIZE_IN_BYTES = 1024
CACHE_TABLE = []
CACHE_DURATION_IN_SECONDS = 30
CACHE_HOST = 'localhost'
CACHE_PORT = 8000
def make_connection_to_server(host, port):
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
time.sleep(1)
return client
def make_server_row(temperature, timestamp, server_name, connection):
row = {
"temperature": temperature,
"timestamp": timestamp,
"server_name": server_name,
"initialized": False,
"connection": connection
}
return row
def init_cache_table(servers):
print("Initializing cache table")
time.sleep(1)
cache_table = []
for i in range (0, 3):
row = make_server_row(
0,
datetime.now(),
"server {}".format(i + 1),
servers[i]
)
cache_table.append(row)
return cache_table
def already_expired_cache_server_row(row):
current_time = datetime.now()
cache_time = row.get("timestamp")
if not row.get("initialized") or current_time > cache_time + timedelta(seconds=CACHE_DURATION_IN_SECONDS):
return True
return False
def request_temperature_from_server(server_row):
server_row.get("connection").sendall(b"Get temperature")
temperature = server_row.get("connection").recv(MESSAGE_SIZE_IN_BYTES)
temperature = temperature.decode("utf-8")
timestamp = datetime.now()
return {
"temperature": temperature,
"timestamp": timestamp,
"server_name": server_row["server_name"],
"initialized": True,
"connection": server_row.get("connection")
}
def update_cache_table(new_row):
global CACHE_TABLE
new_cache_table = []
for row in CACHE_TABLE:
if row.get("server_name") == new_row.get("server_name"):
new_cache_table.append(new_row)
else:
new_cache_table.append(row)
CACHE_TABLE = new_cache_table
def print_result(temperature_informations):
is_from_cache = "WAS" if temperature_informations.get("is_from_cache") else "WAS NOT"
temperature = temperature_informations.get("temperature")
server = temperature_informations.get("server_name")
print("Temperature on {} is {} and {} received from cache".format(server, temperature, is_from_cache))
if __name__ == "__main__":
servers = []
print("Establishing connection with server 1")
servers.append(make_connection_to_server(HOST_1, PORT_1))
print("Establishing connection with server 2")
servers.append(make_connection_to_server(HOST_2, PORT_2))
print("Establishing connection with server 3")
servers.append(make_connection_to_server(HOST_3, PORT_3))
CACHE_TABLE = init_cache_table(servers)
connection, address = create_connection(CACHE_HOST, CACHE_PORT)
print("Connected by {}".format(address))
while(True):
data = connection.recv(MESSAGE_SIZE_IN_BYTES)
temperature_from_servers = []
for cache_row in CACHE_TABLE:
is_from_cache = True
if already_expired_cache_server_row(cache_row):
is_from_cache = False
print("Cache from {} is expired, requesting a new temperature".format(cache_row.get("server_name")))
cache_row = request_temperature_from_server(cache_row)
print("Updating row {} from cache table".format(cache_row.get("server_name")))
update_cache_table(cache_row)
else:
print("Cache from {} stil valid, getting temperature from cache".format(cache_row.get("server_name")))
print()
temperature_from_servers.append({
"temperature": cache_row.get("temperature"),
"is_from_cache": is_from_cache,
"server_name": cache_row.get("server_name")
})
connection.sendall(str(json.dumps(temperature_from_servers)).encode("utf-8"))
time.sleep(5)