def add_request_socket(self, ws): sock = ClientSocket(ws) while not sock.is_closed(): data = sock.receive() if not data: sock.close() return data = self.command.parse(data) # Register if data and data.type == self.command_id.register and sock.get_id( ) is None: client_id = self.get_new_client_id() sock.set_id(client_id) if client_id: sock.send( self.command.build( dict(type=self.command_id.register, id=client_id)), True) else: sock.send( self.command.build(dict(type=self.command_id.error)), True) # Position Update elif data.type == self.command_id.update: self.update_player_position(sock.client_id, data.x, data.y)
class SendingQueue(): ''' Class to sending queue process ''' def __init__(self, server_address): ''' Create SendingQueue object ''' self.threads = [] # gevent provides a synchronized queue (locking is handled) self.queue = queue.Queue() self.socket_mgr = ClientSocket(server_address) def add(self, segments): ''' A list of segments to the sending queue''' for sgmt in segments: self.queue.put(sgmt) def send(self): ''' Iterate continuously looking to send entries in queue ''' logger.debug("Started sending thread") while True: if self.queue.qsize(): self.socket_mgr.send(self.queue.get()) time.sleep(1) # send is non-blocking, don't over send else: time.sleep(3) def start_sending(self, filename, port_list, segments, num_threads): ''' Start a separate thread to begin sending from the send queue. Should be started before breaking up files. As segments are added to queue, it will send, until stop_sending is called. ''' self.add(segments) self.socket_mgr.connect(filename, port_list) for i in range(num_threads): self.threads.append(gevent.spawn(self.send)) return def complete_sending(self): ''' Join all threads created during this send process. This should be done between searching for new files ''' # Wait till rest of send queue is empty # ASSUME no more is being added at this point logger.debug("Waiting for all segments to send before completing send") while self.queue.qsize(): time.sleep(3) # Wait till sending finished while self.socket_mgr.num_currently_sending: logger.debug("Waiting for (%d) segments to finish sending", self.socket_mgr.num_currently_sending) time.sleep(3) # Kill any threads created gevent.killall(self.threads, timeout=5) self.threads = [] self.socket_mgr.disconnect()
print(fName[len(PATH):]) #print #file name except IndexError: #no file found print("Error") with open(eName, 'a') as f: print("Error no File Found for Upload: Exiting. Time: %i" % time.time(), file=f) f.close() exit(-1) #error con = ClientSocket() #start client socket with open(fName, 'r') as f: #open up the file in read mode data = f.read() con.sendName(fName[len(PATH):] ) #lets the server know which file is to be appended to con.send(data) #send the file f.close() #close the file if (con.rec() == '200'): #All clear condition print("Data Uploaded and received correctly") os.remove(fName) #remove the file from the client else: with open(eName, 'a') as f: print("Error Uploading: Keeping Data. Time: %i" % time.time(), file=f) #Serever did not recive the data, keeping data f.close() con.close()