def NotifySrc(Srcip, Srcport, Desport, Desip, Filename, Userid): #---Srcip to connect ----------------------- print("Hi notify Src with file", Filename) masterSlaveReplicaPorts = [9200, 9201, 9202] context = zmq.Context() socket = context.socket(zmq.REQ) for port in masterSlaveReplicaPorts: socket.connect("tcp://" + str(Srcip) + ":%s" % port) msg = np.array(["1", Filename, Userid, Desport, Desip]) print("Send msg to Src slave", msg) send_array(socket, msg) socket.close()
def NotifyDest(Filename, Userid, Destip, Desport): #---Destip to connect --------------------- print("Hi notify dest with file", Filename) masterSlaveReplicaPorts = [9203, 9204, 9205] context = zmq.Context() socket = context.socket(zmq.REQ) for port in masterSlaveReplicaPorts: socket.connect("tcp://" + str(Destip) + ":%s" % port) msg = np.array(["2", Filename, Userid, Desport, Desip]) print("Send msg to des slave", msg) send_array(socket, msg) socket.close()
def DoneDownloading(slavePort): print("sendind done downloading to master ..") masterSlavePorts = [9100, 9101, 9102, 9103, 9104] context = zmq.Context() print("Connecting to server with ports %s" % masterSlavePorts) masterSlavesocket = context.socket(zmq.REQ) for port in masterSlavePorts: masterSlavesocket.connect("tcp://192.168.1.6:%s" % port) print("i'm slave port ", slavePort) msg = np.array( ["2", slavePort, str(socket.gethostbyname(socket.gethostname()))]) #modify # IP send_array(masterSlavesocket, msg)
def SendingDone(slavePort, filename, clientID): print("sendind done to master ..") masterSlavePorts = [9100, 9101, 9102, 9103, 9104] context = zmq.Context() print("Connecting to server with ports %s" % masterSlavePorts) masterSlavesocket = context.socket(zmq.REQ) for port in masterSlavePorts: masterSlavesocket.connect("tcp://192.168.1.6:%s" % port) dirpath = os.getcwd() msg = np.array([ "1", filename, slavePort, clientID, str(socket.gethostbyname(socket.gethostname())), dirpath ]) #modify # IP, FilePath send_array(masterSlavesocket, msg)
def Getting_Replica_Req_Dest(port): while True: context = zmq.Context() slaveReplicasocket = context.socket(zmq.REP) slaveReplicasocket.bind("tcp://*:%s" % port) print("Running Slave Replica on port: ", port) request = recv_array(slaveReplicasocket) if request[0] == "2": #--- Dest print("Getting Dest request", request) Filename = request[1] Userid = request[2] myPort = request[3] myip = request[4] #sisterPorts = [9300,9301,9302,9303] #for port in sisterPorts: print("Download Replica Function ", Filename) context = zmq.Context() Downsocket = context.socket(zmq.REP) Downsocket.bind("tcp://*:%s" % myPort) filedata = Downsocket.recv() file = open("replica.mp4", "wb") file.write(filedata) file.close() newname = Filename[:-4] chunk = math.ceil(len(filedata) / 6) f = open(Filename, "rb") for i in range(0, 6): part = f.read(chunk) subfile = open(newname + str(i) + ".mp4", "wb") subfile.write(part) subfile.close() masterSlavePorts = [9100, 9101, 9102, 9103, 9104] mastercontext = zmq.Context() print("Connecting to server with ports %s" % masterSlavePorts) masterSlavesocket = mastercontext.socket(zmq.REQ) for mport in masterSlavePorts: masterSlavesocket.connect("tcp://192.168.1.6:%s" % mport) dirpath = os.getcwd() msg = np.array(["1", Filename, myPort, Userid, myip, dirpath]) #modify # IP, FilePath send_array(masterSlavesocket, msg)
def InitSendingRequests(ports): context = zmq.Context() print("Connecting to server with ports %s" % ports) socket = context.socket(zmq.REQ) for port in ports: socket.connect("tcp://192.168.1.6:%s" % port) while (1): ID = input("Enter your ID: ") requestnum = input("choose one option: 1-Upload 2-Download 3-list: ") print("Sending request to master requestnum: ", requestnum) # donemessage=socket.recv_string() msg = np.array([str(requestnum)]) if requestnum == "1": send_array(socket, msg) Upload(socket, ID) elif requestnum == "2": fileName = input("Enter the file name you want to download: ") msg = np.append(msg, ID) #ID #modify msg = np.append(msg, fileName) send_array(socket, msg) Download(socket, fileName) elif requestnum == "3": msg = np.append(msg, ID) #ID #modify send_array(socket, msg) List(socket)
def Getting_requests(port): print("Hi from port %s", port) context = zmq.Context() socket = context.socket(zmq.REP) #host_name = socket.gethostname() #host_ip = socket.gethostbyname(host_name) socket.bind("tcp://*:%s" % port) print("Running MasterNode on port: ", port) while (1): # Wait for next request from client #message = socket.recv_string() context = zmq.Context() socket = context.socket(zmq.REP) #host_name = socket.gethostname() #host_ip = socket.gethostbyname(host_name) socket.bind("tcp://*:%s" % port) message = recv_array(socket) print(message) #socket.send_string("request recieved in master") if message[0] == "1": result = Upload() send_array(socket, result) elif message[0] == "2": userID = message[1] fileName = message[2] result = Download(userID, fileName) send_array(socket, result) elif message[0] == "3": result = List(message[1]) send_array(socket, result)
def Download(socket, fileName): # Get the reply. dataNode = recv_array(socket) #6 --> IP,Port print("Received reply datanode ", dataNode) if (len(dataNode) == 0): print( "Sorry we are busy...Try again Later and Double check the file name" ) return myFile = bytearray() for i in range(0, 12, 2): print("In Download .. Connecting to dataNode port", i) DataNodeIP = dataNode[i] # for connecting on different machines DataNodePort = dataNode[i + 1] DataNodeContext = zmq.Context() DataNodeSocket = DataNodeContext.socket(zmq.REQ) string = "tcp://" + str(DataNodeIP) + ":%s" #DataNodeSocket.connect ("tcp://localhost:%s" % DataNodePort) DataNodeSocket.connect(string % DataNodePort) #DataNodeSocket.send_string("2") #--Requesting Download #message=DataNodeSocket.recv_string() newname = fileName[:-4] print(newname + str(int(i / 2)) + ".mp4") #print(message) #if message=="send FN": msg = np.array(["2", newname + str(int(i / 2)) + ".mp4"]) send_array(DataNodeSocket, msg) chunk = DataNodeSocket.recv() myFile += chunk DataNodeSocket.close() fileDir = input("enter the directory you want to download the file in:") file = open(fileDir + '/' + fileName, "wb") file.write(myFile) print("Done Downloading ;)")
def Upload(socket, myID): # Get the reply. dataNode = recv_array(socket) if (len(dataNode) == 0): print("Sorry we are busy...Try again Later") return dataNodeIP = dataNode[0] #[0]: IP #modify dataNodePort = int(dataNode[1]) print("Received reply datanode port: ", dataNodePort) print("Connecting to data node...") dataNodeContext = zmq.Context() dataNodeSocket = dataNodeContext.socket(zmq.REQ) #dataNodeSocket.connect ("tcp://localhost:%s" % dataNodePort) string = "tcp://" + str(dataNodeIP) + ":%s" #DataNodeSocket.connect ("tcp://localhost:%s" % DataNodePort) dataNodeSocket.connect(string % dataNodePort) #dataNodeSocket.send_string("1")#req,file and file name and ID print("connection established between client and data node") #message=dataNodeSocket.recv_string() #print(message) #if message=="send ID": #myID = 1 #dataNodeSocket.send_string(str(myID)) #replay=dataNodeSocket.recv_string() #print(replay) fileName = input("enter the file name :") #dataNodeSocket.send_string(fileName) msg = np.array(["1", str(myID), fileName]) send_array(dataNodeSocket, msg) replay = dataNodeSocket.recv_string() print(replay) fileDir = input("enter the file directory :") SendingFile(dataNodeSocket, fileName, fileDir) dataNodeSocket.close()