|
| 1 | +import sys |
| 2 | + |
| 3 | +from ipv8.lazy_community import lazy_wrapper |
| 4 | +from ipv8.messaging.lazy_payload import VariablePayload, vp_compile |
| 5 | +from tribler_core.version import version_id |
| 6 | + |
| 7 | + |
| 8 | +@vp_compile |
| 9 | +class VersionRequest(VariablePayload): |
| 10 | + msg_id = 101 |
| 11 | + |
| 12 | + |
| 13 | +@vp_compile |
| 14 | +class VersionResponse(VariablePayload): |
| 15 | + msg_id = 102 |
| 16 | + format_list = ['varlenI', 'varlenI'] |
| 17 | + names = ['version', 'platform'] |
| 18 | + |
| 19 | + def fix_pack_version(self, value): |
| 20 | + return value.encode('utf-8') |
| 21 | + |
| 22 | + def fix_pack_platform(self, value): |
| 23 | + return value.encode('utf-8') |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def fix_unpack_version(cls, value): |
| 27 | + return value.decode('utf-8') |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def fix_unpack_platform(cls, value): |
| 31 | + return value.decode('utf-8') |
| 32 | + |
| 33 | + |
| 34 | +class VersionCommunityMixin: |
| 35 | + """ |
| 36 | + This mixin add the protocol messages to ask and receive version of Tribler and community the |
| 37 | + peer is currently running. |
| 38 | +
|
| 39 | + Knowing the version of Tribler or the individual community is not critical for normal operation |
| 40 | + of Tribler but is useful in doing network experiments and monitoring of the network behavior |
| 41 | + because of a new feature/algorithm deployment. |
| 42 | + """ |
| 43 | + |
| 44 | + def init_version_community(self): |
| 45 | + self.add_message_handler(VersionRequest, self.on_version_request) |
| 46 | + self.add_message_handler(VersionResponse, self.on_version_response) |
| 47 | + |
| 48 | + def send_version_request(self, peer): |
| 49 | + self.logger.info(f"Sending version request to {peer.address}") |
| 50 | + self.ez_send(peer, VersionRequest()) |
| 51 | + |
| 52 | + @lazy_wrapper(VersionRequest) |
| 53 | + async def on_version_request(self, peer, _): |
| 54 | + self.logger.info(f"Received version request from {peer.address}") |
| 55 | + version_response = VersionResponse(version_id, sys.platform) |
| 56 | + self.ez_send(peer, version_response) |
| 57 | + |
| 58 | + @lazy_wrapper(VersionResponse) |
| 59 | + async def on_version_response(self, peer, payload): |
| 60 | + self.logger.info(f"Received version response from {peer.address}") |
| 61 | + self.process_version_response(peer, payload.version, payload.platform) |
| 62 | + |
| 63 | + def process_version_response(self, peer, version, platform): |
| 64 | + """ |
| 65 | + This is the method the implementation community or the experiment will implement |
| 66 | + to process the version and platform information. |
| 67 | + """ |
0 commit comments