예제 #1
0
    def open(self):
        self._client = ""
        self._tmp_files = {}
        self._downloaded = []
        global _clients

        while True:
            print _clients
            tmp_id = str(random.randint(0,999999)).zfill(10)
            print tmp_id
            if id not in _clients: 
                self._client = Client(tmp_id)
                _clients[tmp_id] = self
                break
        
        print _clients
        
        self._client._files = {}
        tmp_msg = Msg(None, self._client.client_id)
        tmp_msg.set_type(MsgCon.TYPE_CONN_DETAILS)
        tmp_msg.set_body(self._client.get_details())
        self.write_message(str(tmp_msg))
예제 #2
0
def process(conn, msg):
    print msg
    msg_obj = Msg(msg)
    #print msg_obj
        
    if msg_obj.get_type() == MsgCon.TYPE_FILE_INFO:
        body = tornado.escape.json_decode(msg_obj.get_body())
        print body
        conn._tmp_files[body["file_id"]] = File(body["filename"], body["file_id"], body["file_chunk_count"])
        #print "File Info: ID-%s Name-%s Length-%d"% (body["file_id"], body["filename"], body["file_chunk_count"])
        tmp_msg = Msg(None, "0000000000")        
        tmp_msg.set_value("msg_id", msg_obj.get_msg_id())
        tmp_msg.set_type(MsgCon.TYPE_ACK)
        tmp_msg.set_body({"status": 200})
        conn.write_message(str(tmp_msg))
        
    elif msg_obj.get_type() == MsgCon.TYPE_FILE_PUSH:
        body = tornado.escape.json_decode(msg_obj.get_body())
        print "PUSH"
        
        for file_obj in conn._client._files.itervalues():
            if file_obj.file_id not in _clients[body["client_id"]]._downloaded:
                tmp_msg = Msg(None, "0000000000")
                tmp_msg.set_type(MsgCon.TYPE_FILE_INFO)
                tmp_msg.set_body(file_obj.get_dict())
                _clients[body["client_id"]].write_message(str(tmp_msg))
                     
                     
                print file_obj.file_id
                print file_obj.file_ref
                file_obj.inflate()
                   
                for chunk in file_obj._chunks.itervalues():
                    tmp_msg1 = Msg(None, "0000000000")
                    tmp_msg1.set_type(MsgCon.TYPE_FILE_CHUNK)
    
                    print chunk.file_id
                    print chunk.chunk_id
                    tmp_msg1.set_body(chunk.__dict__)
                    _clients[body["client_id"]].write_message(str(tmp_msg1))
                    
                _clients[body["client_id"]]._downloaded.append(file_obj.file_id);
                print "DONE"
            
            print conn._client._files

        
    elif msg_obj.get_type() == MsgCon.TYPE_FILE_REQUEST:
        body = tornado.escape.json_decode(msg_obj.get_body())
        
        print _clients[body["client_id"]]._client._files

        for file_obj in _clients[body["client_id"]]._client._files.itervalues():
            if file_obj.file_id not in conn._downloaded:

                tmp_msg = Msg(None, "0000000000")
                tmp_msg.set_type(MsgCon.TYPE_FILE_INFO)
                tmp_msg.set_body(file_obj.get_dict())
                conn.write_message(str(tmp_msg))
                     
                     
                print file_obj.file_id
                print file_obj.file_ref
                file_obj.inflate()
                   
                for chunk in file_obj._chunks.itervalues():
                    tmp_msg1 = Msg(None, "0000000000")
                    tmp_msg1.set_type(MsgCon.TYPE_FILE_CHUNK)
    
                    print chunk.file_id
                    print chunk.chunk_id
                    tmp_msg1.set_body(chunk.__dict__)
                    conn.write_message(str(tmp_msg1))
                    
                conn._downloaded.append(file_obj.file_id);
                print "DONE"
            
            print conn._client._files
                
        
    elif msg_obj.get_type() == MsgCon.TYPE_FILE_CHUNK:
        body = tornado.escape.json_decode(msg_obj.get_body())
        # body
        tmp_file = conn._tmp_files[body["file_id"]]
        tmp_file.add_chunk(Chunk(body))
        
        tmp_msg = Msg(None, "0000000000")
        tmp_msg.set_value("msg_id", msg_obj.get_msg_id())
        tmp_msg.set_type(MsgCon.TYPE_ACK)
        tmp_msg.set_body({"status": 200})
        conn.write_message(str(tmp_msg))

        #print "File Chunk: ID-%s Chunk-%d"% (body["file_id"], body["chunk_id"])
        
        if tmp_file.check_chunks() == 0:
            print "File Received: ID-%s"% (body["file_id"])
            if tmp_file.validate():
                print "File Validated: ID-%s"% (body["file_id"])
                tmp_file.deflate(TMP_FILE_DIR)
                
                conn._client.add_file(tmp_file)
                conn._tmp_files[body["file_id"]] = None
                del conn._tmp_files[body["file_id"]]
                
                tmp_msg = Msg(None, "0000000000")
                tmp_msg.set_value("msg_id", msg_obj.get_msg_id())
                tmp_msg.set_type(MsgCon.TYPE_ACK)
                tmp_msg.set_body({"status": 200})
                conn.write_message(str(tmp_msg))
                
                
                
                
                
                
                
                
                
            else:
                print "FILE WENT WRONG" 
        
    """