Exemplo n.º 1
0
 def __receive_request(self):
     """
     Receives the full message sent by the master.
     :return: keep alive
     """
     # get the request's length
     request_size = self.__socket.recv(Commands.SIZE_LENGTH)
     # if the master sent an empty msg, then he has closed himself
     if not request_size:
         print "Master Has Been Closed"
         # TODO: close the peasant and start the run function all over again
         return False
     # fix the request's length
     request_size = int(request_size) - Commands.COMMAND_LENGTH
     # get the request's command's number
     command = int(Commands.decrypt(self.__socket.recv(Commands.COMMAND_LENGTH)))
     # if the request size's is 0, then there are not args
     args = []
     # else, there are args, read them (decrypted)
     if request_size != 0:
         args = Commands.decrypt(self.__socket.recv(request_size)).split(Commands.SEPARATE_CHAR)
     if self.__DEBUG:
         print args
     # handle the command and add the command number and return value to the responses list
     self.__responses.append(str(command) + Commands.handle_command_request(command, args))
     return True
Exemplo n.º 2
0
 def __recv_from_socket(self, s):
     """
     Gets a data from the given socket. First the length of the data
     (DATA_LENGTH digits), and then the data itself.
     :param s: the socket to get the data from
     :return: the data got from the socket
     """
     # decrease the counter
     self.__counter -= 1
     # get the socket address
     address = self.__get_address_by_socket(s)
     # get the data's length
     length = s.recv(Commands.SIZE_LENGTH)
     # if the socket is closing the connection
     if not length:
         if self.__DEBUG:
             print >> sys.__stdout__, "DEBUG: <%s : %s> Is Disconnecting" % (
                 address[0], address[1])
         # delete the socket from the connected dictionary
         del self.__connected[self.__get_address_by_socket(s)]
         # close the socket
         s.close()
         # return the exit command
         return "EXIT"
     if not length.isdigit():
         return "ERROR: Got Length Not Int"
     # else, there is data. convert the length to int
     length = int(length)
     # get the data itself (decrypted)
     data = Commands.decrypt(s.recv(length))
     # TODO: DECRYPT
     # if DEBUG MODE on then print the data we got
     if self.__DEBUG:
         print >> sys.__stdout__, "Got the following response from <%s : %s>:\n%s" % (
             address[0], str(address[1]), data)
     # return the data we got
     return data