-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker3.py
182 lines (162 loc) · 7.63 KB
/
broker3.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import socket
import threading
import os
import json
#client part (zookeeper-broker)
client1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client1.connect(('10.0.2.15',5000))
# server part
ser_host='10.0.2.15'
ser_port = 5001
print(f"Running server on: {ser_host} and port: {ser_port}")
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
try:
sck.bind((ser_host,ser_port))
sck.listen(5)
except Exception as e:
raise SystemExit(f"We could not bind the server on host:{ser_host} to port: {ser_port},because {e}")
def on_new_client(client,connection):
ip = connection[0]
port = connection[1]
print(f"new connection from ip:{ip} and port: {port}")
while True:
# client1.send("Give ip_port".encode('utf-8'))
# l = client1.recv(1024).decode()
msg = client.recv(1024)
here = msg.decode()
print(f"The client said:{msg.decode()}")
if msg.decode()=='p_update':
# client.send(l.encode('utf-8'))
while True:
with open("./zookeeper/offset_metadata.txt","r") as f1:
meta = json.loads(f1.read())
i = 0;prev=''
while(i<3):
msg = client.recv(1024).decode()
msg = json.loads(msg)
topic = list(msg.keys())[0]
print(topic)
if topic not in meta:
meta[topic]={"off1":0,"off2":0,"off3":0}
off1=0;off2=0;off3=0
print(meta)
else:
d = meta[topic]
print(d)
off1 = d["off1"];off2= d["off2"];off3 = d["off3"]
if prev=='':
prev=topic
dirnames = [name for name in os.listdir("./broker1")]
if topic not in dirnames:
os.chdir('./broker1')
os.mkdir(topic)
with open('./topic/{topicName}/partition1.txt'.format(topicName = topic),'w+') as f:
f.write("--")
with open('./topic/{topicName}/partition2.txt'.format(topicName = topic),'w+') as f:
f.write("--")
with open('./topic/{topicName}/partition3.txt'.format(topicName = topic),'w+') as f:
f.write("--")
if prev!=topic:
i=0
prev=topic
if i==0:
with open(f'./broker1/{topic}/partition1.txt','a+') as f:
f.write(f"{off1+1}: {list(msg.values())[0]}\n")
meta[topic]["off1"]=meta[topic]["off1"]+1
f.close()
elif i==1:
with open(f'./broker1/{topic}/partition2.txt','a+') as f:
f.write(f"{off2+1}: {list(msg.values())[0]}\n")
meta[topic]["off2"]=meta[topic]["off2"]+1
f.close()
else:
with open(f'./broker1/{topic}/partition3.txt','a+') as f:
f.write(f"{off3+1}: {list(msg.values())[0]}\n")
meta[topic]["off3"]=meta[topic]["off3"]+1
f.close()
i+=1
with open("./zookeeper/offset_metadata.txt", 'w') as json_file:
json_file.write(json.dumps(meta,indent=4))
if here=='c_offset':
client.send("nothing".encode('utf-8'))
msg = client.recv(1024).decode('utf-8')
# msg = json.load(msg)
topic = msg
print(topic)
dirnames = [name for name in os.listdir("./broker1")]
with open("./zookeeper/offset_metadata.txt","r") as f1:
meta = json.loads(f1.read())
if topic not in dirnames:
os.chdir('./broker1')
os.mkdir(topic)
with open('./broker1/{topicName}/partition1.txt'.format(topicName = topic),'w+') as f:
f.write("--")
with open('./broker1/{topicName}/partition2.txt'.format(topicName = topic),'w+') as f:
f.write("--")
with open('./broker1/{topicName}/partition3.txt'.format(topicName = topic),'w+') as f:
f.write("--")
off1 = 0;off2=0;off3=0
else:
off1 = meta[topic]["off1"];off2 = meta[topic]["off2"];off3 = meta[topic]["off3"]
client.send(str(meta[topic]).encode('utf-8'))
d=client.recv(1024).decode('utf-8')
offset=client.recv(1024).decode('utf-8')
d=json.loads(d)
print(d)
offset = json.loads(offset)
print(offset)
topic=list(d.keys())[0]
flag = d[topic]
if flag==True:
with open('./broker1/{topicName}/partition1.txt'.format(topicName = topic),'r') as f:
v = f.readlines()
h = [i[2:] for i in v]
print(v)
client.send(str(h).encode('utf-8'))
with open('./broker1/{topicName}/partition2.txt'.format(topicName = topic),'r') as f:
v = f.readlines()
h = [i[2:] for i in v]
print(v)
client.send(str(h).encode('utf-8'))
with open('./broker1/{topicName}/partition3.txt'.format(topicName = topic),'r') as f:
v = f.readlines()
h = [i[2:] for i in v]
print(v)
client.send(str(h).encode('utf-8'))
else:
with open("./zookeeper/offset_metadata.txt","r") as f1:
meta = json.loads(f1.read())
print(offset)
if meta[topic]["off1"]-offset["off1"]:
with open('./broker1/{topicName}/partition1.txt'.format(topicName = topic),'r') as f:
v = f.readlines()
h = [i[2:] for i in v]
print(v)
client.send(str(h[offset["off1"]]).encode('utf-8'))
offset["off1"] = meta[topic]["off1"]
elif meta[topic]["off2"]-offset["off2"]:
with open('./broker1/{topicName}/partition2.txt'.format(topicName = topic),'r') as f:
v = f.readlines()
h = [i[2:] for i in v]
print(v)
client.send(h[offset["off2"]].encode('utf-8'))
offset["off2"] = meta[topic]["off2"]
elif meta[topic]["off3"]-offset["off3"]:
with open('./broker1/{topicName}/partition3.txt'.format(topicName = topic),'r') as f:
v = f.readlines()
h = [i[2:] for i in v]
print(v)
client.send(h[offset["off3"]].encode('utf-8'))
offset["off3"] = meta[topic]["off3"]
client.send("stop".encode("utf-8"))
if msg == "stop":
break
client.close()
while True:
try:
client,ip = sck.accept()
threading._start_new_thread(on_new_client,(client,ip))
except Exception as e:
print(f"This is error {e}")
sck.close()