예제 #1
0
 def __init__(self, bootstrap_address = None):
     CacheServent.__init__(self, bootstrap_address)
     # number of transmitted message include flood[_ex], forward, and send_message 
     self.num_tx_message = 0
     # number of received message
     self.num_rx_message = 0
     # expect query message_id
     self.query_message_id = None
     # send the query to the network between 1 and 3 seconds later
     CallLater(uniform(1,3), self.send_query_to_network)
예제 #2
0
def main(args):
    # create a network
    # if len(args)<3:
    #     usage();
    #     return
    # ip = args[0]
    # port = int(args[1])
    # num_node = int(args[2])
    # address = (ip, port)
    # create_network([CacheServent]*num_node, address)

    # implement the network
    servent1 = CacheServent()
    servent2 = CacheServent()
    servent3 = CacheServent()
    servent4 = CacheServent()
    servent5 = CacheServent()
    servent3.set_files(
        [FileInfo(1, "first file", 600), FileInfo(2, "second file", 2500), FileInfo(3, "third file", 5000)]
    )
    servent1.reactor.gnutella_connect(servent2.reactor.address)
    servent2.reactor.gnutella_connect(servent3.reactor.address)
    servent3.reactor.gnutella_connect(servent4.reactor.address)
    servent5.reactor.gnutella_connect(servent2.reactor.address)
    try:
        scheduler_loop(timeout=1, count=10)
    finally:
        # flood is query to neighbors
        servent1.search_queryhit("first file")
        scheduler_loop(timeout=1, count=10)
        # servent2.search_queryhit('first file')
        # scheduler_loop(timeout=1,count=10)
        servent5.search_queryhit("first file")
        scheduler_loop(timeout=1, count=15)
        close_all()
예제 #3
0
 def on_receive(self, connection_handler, message):
     # log to show the network is still working
     self.log("on_receive() -> %s", message)
     # increase received message by one
     self.num_rx_message += 1
     # create fake download if we sent the query to the network
     if (message.payload_descriptor == GnutellaBodyId.QUERYHIT and 
         message.message_id == self.query_message_id and
         self.search('ABC') == []):
         self.add_single_file(FileInfo(1,"ABC",1))            
     CacheServent.on_receive(self, connection_handler, message)
예제 #4
0
 def search(self, criteria):
     result = CacheServent.search(self, criteria)
     if result:
         self.log("Servent has the file with name: %s", criteria)
     else:
         self.log("Servent does not have the file")
     return result
예제 #5
0
 def search_queryhit(self, criteria):
     result = CacheServent.search_queryhit(self, criteria)
     if result:
         self.log("Cache found results(%d)", len(result))
     else:
         self.log("Not in cache")
     return result
예제 #6
0
def test_cache():
    test_servent = CacheServent()

    ip = dotted_quad_to_num('127.0.0.1')
    port = 59850
    speed = 100 
    result_set = [{
            'file_index': 3435,
            'file_size': 100,
            'file_name': 'A'
        },
        {
            'file_index': 3535,
            'file_size': 200,
            'file_name': 'B'
        }]
    servent_id = 'thisisservent_id'
    
    fake_queryhit = create_message(GnutellaBodyId.QUERYHIT,
                                   ip = ip,
                                   port = port,
                                   result_set = result_set,
                                   servent_id = servent_id,
                                   speed = 1)

    test_servent.save_queryhit(fake_queryhit)
    cache_result = test_servent.search_queryhit("A")
    # test the result
    assert_equal(len(cache_result), 1)
    assert_equal(cache_result[0][3][0]['file_index'], 3435)
    
    # test the merge
    result_set2 = [{
            'file_index': 3435,
            'file_size': 100,
            'file_name': 'A'
        },
        {
            'file_index': 35345,
            'file_size': 200,
            'file_name': 'C'
        }]    
    fake_queryhit2 = create_message(GnutellaBodyId.QUERYHIT,
                                   ip = ip,
                                   port = port,
                                   result_set = result_set2,
                                   servent_id = servent_id,
                                   speed = 1)
    
    test_servent.save_queryhit(fake_queryhit2)
    # check if it actually merge
    assert_equal(len(test_servent.queryhit_cache), 1)
    
    cache_result = test_servent.search_queryhit("C")
    # test the result
    assert_equal(len(cache_result), 1)
    assert_equal(cache_result[0][3][0]['file_index'], 35345)
    # complex test, use two servent
    servent_id2 = 'thisisserventid1'
    fake_queryhit3 = create_message(GnutellaBodyId.QUERYHIT,
                                   ip = ip,
                                   port = port,
                                   result_set = result_set,
                                   servent_id = servent_id2,
                                   speed = 1)
    test_servent.save_queryhit(fake_queryhit3)
    # test if it does not merge
    assert_equal(len(test_servent.queryhit_cache), 2)
    # test the result
    cache_result = test_servent.search_queryhit("A")
    assert_equal(len(cache_result), 2)
예제 #7
0
 def flood_ex(self, message):
     ret = CacheServent.flood_ex(self, message)
     self.num_tx_message += ret
     return ret
예제 #8
0
 def flood(self, connection_handler, message):
     ret = CacheServent.flood(self, connection_handler, message) 
     self.num_tx_message += ret
     return ret
예제 #9
0
 def forward(self, message):
     ret = CacheServent.forward(self, message)
     if ret:
         self.num_tx_message += 1
     return ret
예제 #10
0
 def send_message(self, message, handler):
     self.num_tx_message += 1
     CacheServent.send_message(self, message, handler)        
예제 #11
0
 def save_queryhit(self, message):
     self.log("save in cache %s", message)
     CacheServent.save_queryhit(self, message)