|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# NDN Repo putfile client. |
| 3 | +# |
| 4 | +# @Author jonnykong@cs.ucla.edu |
| 5 | +# susmit@cs.colostate.edu |
| 6 | +# @Date 2019-10-18 |
| 7 | +# ----------------------------------------------------------------------------- |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +sys.path.insert(1, os.path.join(sys.path[0], '..')) |
| 12 | + |
| 13 | +import asyncio as aio |
| 14 | +from .command_checker import CommandChecker |
| 15 | +from ..command import RepoCommandParam, SyncParam, EmbName, RepoStatCode |
| 16 | +from ..utils import PubSub |
| 17 | +import logging |
| 18 | +import multiprocessing |
| 19 | +from ndn.app import NDNApp |
| 20 | +from ndn.encoding import Name, NonStrictName, Component, Links |
| 21 | +import os |
| 22 | +import platform |
| 23 | +from hashlib import sha256 |
| 24 | +from typing import Optional, List |
| 25 | + |
| 26 | +class SyncClient(object): |
| 27 | + |
| 28 | + def __init__(self, app: NDNApp, prefix: NonStrictName, repo_name: NonStrictName): |
| 29 | + """ |
| 30 | + A client to sync the repo. |
| 31 | +
|
| 32 | + :param app: NDNApp. |
| 33 | + :param prefix: NonStrictName. The name of this client |
| 34 | + :param repo_name: NonStrictName. Routable name to remote repo. |
| 35 | + """ |
| 36 | + self.app = app |
| 37 | + self.prefix = prefix |
| 38 | + self.repo_name = Name.normalize(repo_name) |
| 39 | + self.encoded_packets = {} |
| 40 | + self.pb = PubSub(self.app, self.prefix) |
| 41 | + self.pb.base_prefix = self.prefix |
| 42 | + |
| 43 | + # https://bugs.python.org/issue35219 |
| 44 | + if platform.system() == 'Darwin': |
| 45 | + os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES' |
| 46 | + |
| 47 | + async def join_sync(self, sync_prefix: NonStrictName, register_prefix: NonStrictName = None, |
| 48 | + data_name_dedupe: bool = False, reset: bool = False) -> bytes: |
| 49 | + |
| 50 | + # construct insert cmd msg |
| 51 | + cmd_param = RepoCommandParam() |
| 52 | + cmd_sync = SyncParam() |
| 53 | + cmd_sync.sync_prefix = sync_prefix |
| 54 | + cmd_sync.register_prefix = register_prefix |
| 55 | + cmd_sync.data_name_dedupe = data_name_dedupe |
| 56 | + cmd_sync.reset = reset |
| 57 | + |
| 58 | + cmd_param.sync_groups = [cmd_sync] |
| 59 | + cmd_param_bytes = bytes(cmd_param.encode()) |
| 60 | + |
| 61 | + # publish msg to repo's join topic |
| 62 | + await self.pb.wait_for_ready() |
| 63 | + is_success = await self.pb.publish(self.repo_name + Name.from_str('sync/join'), cmd_param_bytes) |
| 64 | + if is_success: |
| 65 | + logging.info('Published an join msg and was acknowledged by a subscriber') |
| 66 | + else: |
| 67 | + logging.info('Published an join msg but was not acknowledged by a subscriber') |
| 68 | + return sha256(cmd_param_bytes).digest() |
| 69 | + |
| 70 | + async def leave_sync(self, sync_prefix: NonStrictName) -> bytes: |
| 71 | + # construct insert cmd msg |
| 72 | + cmd_param = RepoCommandParam() |
| 73 | + cmd_sync = SyncParam() |
| 74 | + cmd_sync.sync_prefix = sync_prefix |
| 75 | + cmd_param.sync_groups = [cmd_sync] |
| 76 | + cmd_param_bytes = bytes(cmd_param.encode()) |
| 77 | + |
| 78 | + # publish msg to repo's leave topic |
| 79 | + await self.pb.wait_for_ready() |
| 80 | + is_success = await self.pb.publish(self.repo_name + Name.from_str('sync/leave'), cmd_param_bytes) |
| 81 | + if is_success: |
| 82 | + logging.info('Published an leave msg and was acknowledged by a subscriber') |
| 83 | + else: |
| 84 | + logging.info('Published an leave msg but was not acknowledged by a subscriber') |
| 85 | + return sha256(cmd_param_bytes).digest() |
0 commit comments