base_client.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from dht_common import compute_document_ID, SIZE_OF_HASH, SIZE_OF_IP_ADDRESS
  2. class Base_Client(object):
  3. def __init__(self, DHT, knownNode, documentSize, numNodes=1):
  4. self.DHT = DHT
  5. self.knownNode = knownNode
  6. self.actualNodeEntry = self.DHT.access_node(self.knownNode)
  7. self.documentSize = documentSize
  8. self.numRounds = 0
  9. self.numMessagesSent = 0
  10. self.numMessagesRecv = 0
  11. self.numBytesSent = 0
  12. self.numBytesRecv = 0
  13. self.lastNumRounds = 0
  14. self.lastNumMessagesSent = 0
  15. self.lastNumMessagesRecv = 0
  16. self.lastNumBytesSent = 0
  17. self.lastNumBytesRecv = 0
  18. def test_iterative_search(self):
  19. if self.DHT.get_num_nodes() == 10:
  20. return self.__iterative_search__(b'\xb5\xe0\xf4\xd8\xe9\x1e\xf6i\xc4=^\xc2Y\xac\xdc\x88p\x8a+\x10t\x90\xa5\xc9\x1f\x98\xc61\xda\x7fH\x92') == 7
  21. else:
  22. print("Assumed testing conditions not met; no tests run.")
  23. return False
  24. # implements a simulation of iteratively searching the DHT
  25. # (captured as the dht_simulator, or here "DHT")
  26. def __iterative_search__(self, documentID):
  27. whichNode = self.knownNode
  28. flagFound = False
  29. while not flagFound:
  30. recv = self.DHT.access_node(whichNode).get_finger_table_val(documentID)
  31. self.numRounds += 1
  32. self.numMessagesSent += 1
  33. self.numMessagesRecv += 1
  34. self.numBytesSent += SIZE_OF_HASH
  35. self.numBytesRecv += SIZE_OF_HASH + SIZE_OF_IP_ADDRESS
  36. _, nextFind = recv
  37. if nextFind == -1:
  38. flagFound = True
  39. else:
  40. whichNode = nextFind
  41. return whichNode
  42. # Searches the DHT appropriately until it finds the correct node,
  43. # and inserts the document there
  44. def insert_file(self, document):
  45. documentID = compute_document_ID(document)
  46. whichNode = self.__iterative_search__(documentID)
  47. self.DHT.access_node(whichNode).insert()
  48. self.numRounds += 1
  49. self.numMessagesSent += 1
  50. self.numMessagesRecv += 1
  51. self.numBytesSent += self.documentSize
  52. self.numBytesRecv += SIZE_OF_HASH
  53. return documentID
  54. # Searches the DHT appropriately until it finds the correct node,
  55. # and retrieves the document there
  56. def retrieve_file(self, documentID):
  57. whichNode = self.__iterative_search__(documentID)
  58. self.DHT.access_node(whichNode).retrieve()
  59. self.numRounds += 1
  60. self.numMessagesSent += 1
  61. self.numMessagesRecv += 1
  62. self.numBytesSent += SIZE_OF_HASH
  63. self.numBytesRecv += self.documentSize
  64. def get_num_rounds(self):
  65. self.lastNumRounds = self.numRounds
  66. return self.numRounds
  67. def get_recent_num_rounds(self):
  68. retval = self.numRounds - self.lastNumRounds
  69. self.lastNumRounds = self.numRounds
  70. return retval
  71. def get_num_messages_sent(self):
  72. self.lastNumMessagesSent = self.numMessagesSent
  73. return self.numMessagesSent
  74. def get_recent_num_messages_sent(self):
  75. retval = self.numMessagesSent - self.lastNumMessagesSent
  76. self.lastNumMessagesSent = self.numMessagesSent
  77. return retval
  78. def get_num_messages_recv(self):
  79. self.lastNumMessagesRecv = self.numMessagesRecv
  80. return self.numMessagesRecv
  81. def get_recent_num_messages_recv(self):
  82. retval = self.numMessagesRecv - self.lastNumMessagesRecv
  83. self.lastNumMessagesRecv = self.numMessagesRecv
  84. return retval
  85. def get_num_bytes_sent(self):
  86. self.lastNumBytesSent = self.numBytesSent
  87. return self.numBytesSent
  88. def get_recent_num_bytes_sent(self):
  89. retval = self.numBytesSent - self.lastNumBytesSent
  90. self.lastNumBytesSent = self.numBytesSent
  91. return retval
  92. def get_num_bytes_recv(self):
  93. self.lastNumBytesRecv = self.numBytesRecv
  94. return self.numBytesRecv
  95. def get_recent_num_bytes_recv(self):
  96. retval = self.numBytesRecv - self.lastNumBytesRecv
  97. self.lastNumBytesRecv = self.numBytesRecv
  98. return retval
  99. # Normally this file is a class to be used elsewhere,
  100. # but if you run it directly it performs some rudimentary unit tests
  101. # TODO: Add unit tests for size calculations
  102. if __name__ == "__main__":
  103. from dht_simulator import DHT_Simulator
  104. from base_node import Base_Node
  105. NUM_NODES_IN_TEST = 10
  106. SIZE_OF_DOCUMENTS_IN_TEST = 1024
  107. # The 1 here isn't used at all within the simulator,
  108. # but it is customary to invoke DHT_Simulator with that value there for Base_Node
  109. testbed = DHT_Simulator(Base_Node, NUM_NODES_IN_TEST, SIZE_OF_DOCUMENTS_IN_TEST, 1)
  110. client = Base_Client(testbed, 0, SIZE_OF_DOCUMENTS_IN_TEST)
  111. assert client.test_iterative_search()
  112. print("Iterative search functioning correctly.")
  113. # A random file (not actually 1024 bytes, but that's not really a major concern;
  114. # the file size is a relic of an older, buggy implementation of generate_file())
  115. file = b'\xc3\xa7\x0c\xc3\x97!\xc3\xb2offy\x13\xc2\x81~\xc2\x9f\xc3\xb5\xc3\xa1\xc3\xb9C\xc2\x92f\xc2\x968\x01\xc2\x9183D\xc3\x84\xc3\xa9\xc3\x8e\t\x0f\xc2\xa1<\xc2\xad%G\xc3\x9dq\xc2\xb7\x1b\x7f\xc2\x82\xc3\x84\xc3\xb4\xc3\x81\x1c\xc3\x8dqgH(H\\C&\x14\xc2\x98\xc3\x93`\xc3\xbe\xc3\x95\xc3\xad\xc3\x8d\xc3\xb5\xc2\x8d.\xc2\x94D\x13IK\xc3\x9a\xc2\x8dS\xc3\x8b\xc2\xa7\xc2\xb6\xc2\x9d\x1fY\xc3\xbe\xc3\x85[\r\xc2\x85\x0e#\xc2\xb85B4\x15\xc3\xaf\xc2\x81_g\xc3\xb3;!|\xc3\xbc\xc3\xaa;\xc3\xbd(m\xc2\xbd\xc3\x96b\xc2\x88\xc3\x9d\xc2\x87N\xc3\xb9\xc3\xb5\xc3\xa54\xc2\xb6(\xc2\xa7\xc3\x950\xc3\x82\x1c\xc3\x95\xc2\x85\xc2\x983\xc3\xbfy\xc2\x89lQ\xc2\x8aN\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xc3\xba\xc3\xa0\x17\xc3\xab!An\x7f\xc2\x89Rrh\xc3\x80\xc2\xaf\x1f*}j\xc2\xaf\xc2\x84\xc2\xa4\xc2\x92\xc2\x97\xc2\x83\xc2\x8cf\xc3\xb6\x1f\xc3\xadfb\xc2\xb1\xc2\xa3\xc3\x8f7\xc3\x82 \xc3\xbb\xc2\x84\xc3\xb0sh\xc2\x9d|\x7f\xc3\xbda\xc3\x84\xc2\x80\x08\xc3\x88\xc2\xa8mBi\xc2\xae)\x02}K\xc3\x9f\xc2\xa7\xc3\x95\xc3\x94[[D\n\xc2\x98AQH\xc2\xb9\xc2\xb3\xc3\xb3"\x08\xc2\x9f\xc3\x98\xc3\x91B[\xc2\x8b\xc2\x86xhMK\xc3\xbb\xc2\x83`\xc2\xa7\x02\xc3\x8a\xc3\x89\xc3\x8f\xc3\xb0\xc2\xa9\xc3\x99\xc3\x9b\xc2\xb2\xc3\x9a\xc3\xaa5\xc2\xb7T\xc2\x81\x0f\xc3\x90\x0c\xc3\xb3\x05\xc3\x8d\xc3\x85S\xc2\xbb\x17\xc3\x86\xc3\xb2\xc3\xa3\xc3\xb0V\x07\x0e\xc2\xb0y\xc2\x8e\xc2\xb1\xc3\x8f;\xc2\xb9Tc\xc3\xadM\xc2\xad\xc2\x98#\xc2\xb0\x01`\x1c\xc2\x84d'
  116. fileID = b'\xb5\xe0\xf4\xd8\xe9\x1e\xf6i\xc4=^\xc2Y\xac\xdc\x88p\x8a+\x10t\x90\xa5\xc9\x1f\x98\xc61\xda\x7fH\x92'
  117. client.insert_file(file)
  118. client.retrieve_file(fileID)
  119. print("Basic insertion/retrieval fires correctly.")