Ejemplo n.º 1
0
	def receive_results(self, client_socket):
		"""This function sends the results file to the client by receiving the jobID from the client get the worker ip that runs this job create a connection to this worker download the results file and send it to the client
		
		Args:
			client_socket (class): socket of the client that request the results file. 
		"""

		sockTools.send_and_encode(client_socket, "please provide job ID")
		requestedJobID = sockTools.recv_and_decode(client_socket)
		jobStatus = (self.db.get_job_data(requestedJobID, "status"))[0]
		if jobStatus not in ["COMPLETED", "ERROR"]:
			sockTools.send_and_encode(client_socket,"Your job is still in progress")
		else:
			worker_socket = sockTools.create_socket(isTCP = True) 
			workerHostname = (self.db.get_job_data(requestedJobID,"hostname"))[0]	
			workerIP = (self.db.get_host_data(workerHostname, "ipaddr"))[0] 
			worker_socket.connect((workerIP, 4444))
			sockTools.send_and_encode(worker_socket, "receiveFile")
			response = sockTools.recv_and_decode(worker_socket)
			if response[:5] == "going":
				sockTools.send_and_encode(worker_socket, requestedJobID)
			resultsFile = sockTools.receiveFile(worker_socket)
			if jobStatus == "ERROR":
				sockTools.send_and_encode(client_socket,"You job has failed do you want to download err log(y/n)")	
				response = sockTools.recv_and_decode(client_socket)
			else:
				sockTools.send_and_encode(client_socket, "Are you ready to download the results file(y/n)")
				response = sockTools.recv_and_decode(client_socket)
			print("Going to send {} to client".format(resultsFile))
			if response[:1] == "y":
				sockTools.sendFile(client_socket, resultsFile)
Ejemplo n.º 2
0
	def listen_to_server(self, server_socket, address):
		"""
		this functions listen to server waiting to receive new information and act accordingly.
		
		Arguments:
			server_socket (class) : the socket that transfers the server data.
			address (tuple) : server address for filenames and debugging.
			

		Important:
			server can send several words through socket for various functions.

			**sendFile** : when this word is received the worker start receiving the job file.

			**receiveFile** : when this word is received the worker sends the results file to the manager. 
		"""
		while True:
			try:
				data_from_server = sockTools.recv_and_decode(server_socket)
				if data_from_server == "":
					print("{} Disconnected".format(address))
					server_socket.close()
					return False
				if data_from_server == "sendFile":
					self.receive_file_from_server(server_socket, address)
				if data_from_server == "receiveFile":
					self.send_requested_file(server_socket)
			except socket.error:
				print("{} Disconnected".format(address))
				server_socket.close()
				return False
Ejemplo n.º 3
0
def send_file_to_server(arguments, sock):
    """This function sends the file to the server.
	
	Args:
		arguments (dict): All arguments from command line. 
		sock (class): socket that file should be transfer on. 
	"""

    print("Sending {} to manager: {}".format(arguments.file, arguments.server))
    sockTools.send_and_encode(sock, "sendFile")
    jobID = sockTools.recv_and_decode(sock)
    print("Your jobID is {}".format(jobID))
    sockTools.sendFile(sock, arguments.file, "./")
Ejemplo n.º 4
0
	def send_requested_file(self, server_socket):
		"""This function sends the results file to the manager by receiving the jobID from the manager get the filename from the database and sends the relevant result file.
		
		Arguments:
			server_socket (class): The socket that the results file should be transfer by 
		"""

		sockTools.send_and_encode(server_socket, "going to send Results file")
		requestedJobID = sockTools.recv_and_decode(server_socket)
		requestedFileName = (self.db.get_job_data(requestedJobID, "fileName"))[0]
		filename = "results-" + requestedFileName
		sockTools.sendFile(server_socket, filename)
		server_socket.close()
Ejemplo n.º 5
0
def add_new_worker(sock):
    """This function adds the specific machine that uses it as a new worker by sending newWorker message to server than pass the information and run the workerservice.
	
	Args:
		sock (class): The socket that used for transferring the messages and information. 
	
	Raises:
		SystemExit: Raises when the user wants to become a worker than the service replace the interface. 
	"""
    workerInformation = system.gatherInformation()
    sockTools.send_and_encode(sock, "newWorker")
    response = sockTools.recv_and_decode(sock)
    if response[:6] == "please":
        sockTools.send_and_encode(sock, workerInformation)
    response = sockTools.recv_and_decode(sock)
    print(response)
    if response[:3] == "new":
        print("Running worker service")
        try:
            subprocess.call(["python", "../worker/workerService.py"])
            raise SystemExit()
        except KeyboardInterrupt:
            print("Quit")
Ejemplo n.º 6
0
def get_results_file(sock, arguments, db, pp):
    """This function request the results file from the server prompt the user for the downloading and store the results file in jobs folder.
	
	Args:
		sock (class): socket that file should be transfer on. 
		arguments (dict): all arguments from the command line. 
		db (class): db class handler for database operations. 
		pp (class): pretty print handler for printing results. 
	"""

    sockTools.send_and_encode(sock, "receiveResults")
    response = sockTools.recv_and_decode(sock)
    if response[:6] == "please":
        sockTools.send_and_encode(sock, arguments.getResults)
    response = sockTools.recv_and_decode(sock)
    print(response)
    if response[:4] == "Your":
        get_job_info(db, pp, arguments.getResults)
        exit(0)
    else:
        a = input()
        if a[:1] == "y":
            sockTools.send_and_encode(sock, a)
            sockTools.receiveFile(sock)
Ejemplo n.º 7
0
	def send_file_to_worker(self, workerHostname, filename):
		"""This function resolves the worker ip and then open a socket between the manager and the worker and send the file to the worker.
		
		Arguments:
			workerHostname (string): The hostname of the worker that should run the job 
			filename (string): fileName of the job. 
		"""

		workerIP = (self.db.get_host_data(workerHostname, "ipaddr"))[0] 
		print("\nSelected {} ,worker ip is: {}".format(workerHostname, workerIP))
		print("Sending job file to {}".format(workerHostname))
		worker_socket = sockTools.create_socket(isTCP = True) 
		worker_socket.connect((workerIP, 4444))
		sockTools.send_and_encode(worker_socket,"sendFile")	
		response = sockTools.recv_and_decode(worker_socket)
		sockTools.sendFile(worker_socket, filename) #send the file to worker
Ejemplo n.º 8
0
	def add_new_worker(self, client_socket, address):
		"""
		this function add new worker to the system and to the database if worker already exists the function sends an ERROR"

		Arguments:
			client_socket (class) : the socket that transfers the client data. 
		"""
		print("New worker request accepted")
		sockTools.send_and_encode(client_socket, "please send your info")
		newWorkerInformation = sockTools.recv_and_decode(client_socket)
		workerInformationList = newWorkerInformation.split(":")
		print("New worker information is: {}".format(workerInformationList))
		dbResult = self.db.get_host_data_by_ip(address[0])	
		if dbResult:
			sockTools.send_and_encode(client_socket,"{} is already a worker".format(address[0]))
		else:
			self.db.insert_to_db('hosts', workerInformationList)
			sockTools.send_and_encode(client_socket,"new worker has been added info is:{}".format(workerInformationList))
			self.workersList = self.db.get_workers_list() #store workers list for local use
			self.numberOfWorkers += 1
Ejemplo n.º 9
0
	def listen_to_client(self, client_socket, address):
		"""
		This function listen to client waiting to receive new information and act accordingly.

		Arguments:
			client_socket (class) : the socket that transfers the client data.
			address (tuple) : client address for filenames and debugging.
		 
		Important:
			client/workers can sends several words through socket for various functions.

			**sendFile** : when this word is received the manager start receiving the job file.

			**newWorker** : when this word is received the manager calls the udpPacket methods and worker will send the discover packet.

			**receiveResults** : when this word is received the manager sends the results file to the client.
		"""
		action = True
		while action:
			try:
				data_from_client = sockTools.recv_and_decode(client_socket) #recv message from client
				if data_from_client == "":
					print("{} Disconnected".format(address))
					client_socket.close()
					return False
				if data_from_client == "sendFile":
					self.receive_file_from_client(client_socket, address)
				if data_from_client == "newWorker":
					self.add_new_worker(client_socket, address)
				if data_from_client == "receiveResults":
					self.receive_results(client_socket)
				action = False
			except socket.error:
				print("{} Disconnected".format(address))
				print(traceback.format_exc())
				client_socket.close()
				return False