-
Notifications
You must be signed in to change notification settings - Fork 0
/
xMaster.py
208 lines (193 loc) · 6.7 KB
/
xMaster.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import zmq
import time
import sys
from zmq import Socket
import pandas as pd
import sysv_ipc as ss
import json
from io import StringIO
import numpy as np
def initMemoryMain(id):
try:
memory = ss.SharedMemory(id, ss.IPC_CREAT)
except:
memory = ss.SharedMemory(id, ss.IPC_CREX)
return memory
def initSemaphoreMain(id):
try:
semaphore = ss.Semaphore(id,ss.IPC_CREAT)
except:
semaphore = ss.Semaphore(id,ss.IPC_CREX)
return semaphore
def readSendVideo(Videopath:str,socket:Socket):
messagevid={}
with open(Videopath,'rb') as vfile:
file=vfile.read()
messagevid['VIDEO']=file
socket.send_pyobj(messagevid)
return True
#this willl be in the download to require ports of the datakeepers ..
def randports_acquire(n_needed=1,x='D'):
#you dont have to use semaphores in reading (ya mokhtar)...
global lu1,mem1,sem1
sem1.acquire() #you don't need to
mybytes=mem1.read()
s=str(mybytes,'utf-8')
s=s[1:]
TESTDATA = StringIO(s)
table = pd.read_csv(TESTDATA,sep=',')
acq_count = 0
acquired = []
for i in range (len(table)):
if (table.iloc[i]['Alive']==1):
ports = json.loads(table.iloc[i][x+'Port'])
isfree = json.loads(table.iloc[i][x+'free'])
for port in range(len(isfree)):
if (acq_count >= n_needed):
break
if(isfree[port] == 1):
#acquire and set
isfree[port] = 0
acquired.append([table.iloc[i]["IPv4"] ,ports[port]])
acq_count += 1
table.loc[table['ID']==table.iloc[i]['ID'],x+'free'] = json.dumps(isfree)
csvfile=table.to_csv()
mem1.write(str(csvfile))
sem1.release()
return acquired
##why ?!
def port_freeing(data):
ipv4 = data['IPv4']
port = data['Port']
x = data['Type']
global lu1,mem1,sem1
sem1.acquire()
mybytes=mem1.read()
s=str(mybytes,'utf-8')
s=s[1:]
TESTDATA = StringIO(s)
table = pd.read_csv(TESTDATA,sep=',')
isfree = json.loads((pd.DataFrame(table.loc[table['IPv4']==ipv4]))[x+'free'].iloc[0])
ports = json.loads((pd.DataFrame(table.loc[table['IPv4']==ipv4]))[x+'Port'].iloc[0])
for i in range(len(ports)):
#print(ports[i])
if(ports[i] == port):
isfree[i] = 1
table.loc[table['IPv4']==ipv4,x+'free'] = json.dumps(isfree)
csvfile=table.to_csv()
mem1.write(str(csvfile))
sem1.release()
return
#??????????????????
def port_acquire(data):
ipv4 = data['IPv4']
port = data['Port']
x = data['Type']
#print(ipv4,port,x)
global lu1,mem1,sem1
sem1.acquire()
mybytes=mem1.read()
s=str(mybytes,'utf-8')
s=s[1:]
TESTDATA = StringIO(s)
table = pd.read_csv(TESTDATA,sep=',')
isfree = json.loads((pd.DataFrame(table.loc[table['IPv4']==ipv4]))[x+'free'].iloc[0])
ports = json.loads((pd.DataFrame(table.loc[table['IPv4']==ipv4]))[x+'Port'].iloc[0])
for i in range(len(ports)):
if(ports[i] == port):
isfree[i] = 0
table.loc[table['IPv4']==ipv4,x+'free'] = json.dumps(isfree)
csvfile=table.to_csv()
mem1.write(str(csvfile))
sem1.release()
return
def replica_check():
pass
def upload():
return randports_acquire(n_needed=1,x='U')
def download(filename='zmo.mo4'):
#search for alive maci
global lu1,mem1,sem1
sem1.acquire()
mybytes=mem1.read()
s=str(mybytes,'utf-8')
s=s[1:]
TESTDATA = StringIO(s)
download_from_machine = []
found = 0
table = pd.read_csv(TESTDATA,sep=',')
for i in range(len(table)):
if (found == 1):
break
if (table.iloc[i]['Alive']==1):
files = json.loads(table.iloc[i]['Files'])
if (filename in files):
ports = json.loads(table.iloc[i]['DPort'])
isfree = json.loads(table.iloc[i]['Dfree'])
for port in range(len(isfree)):
if(isfree[port] == 1):
isfree[port] = 0
download_from_machine.append([table.iloc[i]["IPv4"] ,ports[port]])
found = 1
break
if (found == 1):
table.loc[table['ID']==table.iloc[i]['ID'],'Dfree'] = json.dumps(isfree)
csvfile=table.to_csv()
mem1.write(str(csvfile))
sem1.release()
#print(download_from_machine)
return download_from_machine
if __name__ == "__main__":
id = 1334
global lu1,mem1,sem1
[lu1,mem1,sem1] = init(id)
masterContext = zmq.Context()
masterSocket = masterContext.socket(zmq.REP)
with open('master_tracker_config.json') as f:
replica_factor = json.load(f)['R']
portMaster = sys.argv[1]
#print(replica_factor)
#masterSocket.bind("tcp://*:%s" % portMaster)
#randports_acquire()
#free_test = {'IPv4':'192.168.17.5','Type':'D','Port':6001}
#port_freeing(free_test)
#free_test = {'IPv4':'192.168.17.5','Type':'D','Port':6000}
#port_freeing(free_test)
#append_file_todk({'IPv4':'192.168.17.3','Type':'D','Port':6000,'Filename':'zmq.mp4'})
i = 0
#download()
masterContext = zmq.Context()
masterSocket = masterContext.socket(zmq.REP)
masterSocket.bind("tcp://*:%s" % portMaster)
print(portMaster)
while True:
messageClient = masterSocket.recv_pyobj()
print(messageClient)
#print("Received request: ", messageClient)
if messageClient['REQ_TYPE']=='upload':
uploadpath = upload()
#uploadpath = None
if len(uploadpath) > 0:
sendMessege={'PORT_NUMBER':uploadpath[0][1],'IP':uploadpath[0][0]}
print(sendMessege)
masterSocket.send_pyobj(sendMessege)
# dkPort='6556'
# dkContext = zmq.Context()
# consumer_receiver = dkContext.socket(zmq.PULL)
# consumer_receiver.bind("tcp://*:6556")
# video=consumer_receiver.recv_pyobj()
#print(video)
elif messageClient['REQ_TYPE']=='download':
fileName=messageClient['FILE_NAME']
#search for dk which has the this file(video)
downloadpath = (download(fileName))
#print(downloadpath)
if len(downloadpath) > 0:
sendMessege={'DK_INFO':[(downloadpath[0][0],downloadpath[0][1])]}
masterSocket.send_pyobj(sendMessege)
# dkContext = zmq.Context()
# dk_download = dkContext.socket(zmq.REP)
# dk_download.bind("tcp://*:6557")
# message = dk_download.recv_string()
# print("Received request: ", message)
#readSendVideo('cat.mp4',dk_download)