Skip to content

Commit

Permalink
Sync: fix implementation and document
Browse files Browse the repository at this point in the history
  • Loading branch information
zjkmxy committed Dec 23, 2024
1 parent e6a4f10 commit e3d031e
Show file tree
Hide file tree
Showing 8 changed files with 775 additions and 666 deletions.
2 changes: 1 addition & 1 deletion docs/src/specification/check.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ RepoCommandRes
However, fetched objects or segments are still inserted into the repo.
Only the objects with ``insert_num=0`` are not inserted.

* For each ``ObjectResult`` contained in the ``RepoCommandRes``, the status code can be one of the following:
* For each ``ObjStatus`` contained in the ``RepoCommandRes``, the status code can be one of the following:

* ``ROGER``: The whole command is received and the operation on this object will be started in the future.
* ``MALFORMED``: If the object has wrong parameter.
Expand Down
8 changes: 4 additions & 4 deletions docs/src/specification/delete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Repo deletion process makes use of the :doc:`../misc_pkgs/pub_sub`.
1. The repo subscribes to the topic ``/<repo_name>/delete``.

2. The client publishes a message to the topic ``/<repo_name>/delete``.
The message payload is ``RepoCommandParam`` containing one or more ``ObjectParam`` with the following fields:
The message payload is ``RepoCommandParam`` containing one or more ``ObjParam`` with the following fields:

* ``name``: either a Data packet name, or a name prefix of Data packets.
* ``start_block_id`` (Optional): inclusive start segment number.
Expand Down Expand Up @@ -40,12 +40,12 @@ Delete status check

The client can use the :doc:`check` protocol to check the progress of an deletion process.
The deletion check response message payload is ``RepoCommandRes`` containing zero or more
``ObjectResult`` with the following fields:
``ObjStatus`` with the following fields:

* ``status_code``: status code, as defined on :doc:`check`.
Both the command itself and objects has a status code.
* ``name``: the name of object to delete.
* ``delete_num``: number of Data packets deleted by the repo so far.
* The number of ``ObjectResult`` in the result should be either:
* The number of ``ObjStatus`` in the result should be either:
* =0, which means the command is malformed or not allowed.
* equals to the number of ``ObjectParam`` in the deletion command.
* equals to the number of ``ObjParam`` in the deletion command.
84 changes: 56 additions & 28 deletions docs/src/specification/encoding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,42 @@ Most repo commands and status reports are Data packets whose Content contains
``RepoCommandParam`` or ``RepoCommandRes`` structure.
These Data are issued via Pub-Sub protocol.
Each ``RepoCommandParam`` and ``RepoCommandRes`` contains
multiple ``ObjectParam`` and ``ObjectResult``, resp.
multiple ``ObjParam`` and ``ObjStatus``, resp.
These structures are defined as follows:

.. code-block:: abnf
ObjectParam =
ObjParam =
Name
[ForwardingHint]
[StartBlockId]
[EndBlockId]
[RegisterPrefix]
ObjectResult =
SyncParam =
SyncPrefix
[RegisterPrefix]
[DataNameDedupe]
[Reset]
ObjStatus =
Name
StatusCode
[InsertNum]
[DeleteNum]
SyncStatus =
Name
StatusCode
RepoCommandParam =
1 * (OBJECT-PARAM-TYPE TLV-LENGTH ObjectParam)
0* (OBJECT-PARAM-TYPE TLV-LENGTH ObjParam)
0* (SYNC-PARAM-TYPE TLV-LENGTH SyncParam)
RepoCommandRes =
StatusCode
0 * (OBJECT-RESULT-TYPE TLV-LENGTH ObjectResult)
0* (OBJECT-RESULT-TYPE TLV-LENGTH ObjStatus)
0* (SYNC-RESULT-TYPE TLV-LENGTH SyncStatus)
RepoStatQuery =
RequestNo
Expand All @@ -41,6 +53,12 @@ These structures are defined as follows:
RegisterPrefix = REGISTER-PREFIX-TYPE TLV-LENGTH Name
SyncPrefix = SYNC-PREFIX-TYPE TLV-LENGTH Name
DataNameDedupe = SYNC-DATA-NAME-DEDUPE-TYPE TLV-LENGTH ; TLV-LENGTH = 0
Reset = SYNC-RESET-TYPE TLV-LENGTH ; TLV-LENGTH = 0
StatusCode = STATUS-CODE-TYPE TLV-LENGTH NonNegativeInteger
InsertNum = INSERT-NUM-TYPE TLV-LENGTH NonNegativeInteger
Expand All @@ -51,29 +69,39 @@ These structures are defined as follows:
The type number assignments are as follows:

+----------------------+----------------------------+--------------------------------+
| type | Assigned number (decimal) | Assigned number (hexadecimal) |
+======================+============================+================================+
| START-BLOCK-ID-TYPE | 204 | 0xCC |
+----------------------+----------------------------+--------------------------------+
| END-BLOCK-ID-TYPE | 205 | 0xCD |
+----------------------+----------------------------+--------------------------------+
| REQUEST-NO-TYPE | 206 | 0xCE |
+----------------------+----------------------------+--------------------------------+
| STATUS-CODE-TYPE | 208 | 0xD0 |
+----------------------+----------------------------+--------------------------------+
| INSERT-NUM-TYPE | 209 | 0xD1 |
+----------------------+----------------------------+--------------------------------+
| DELETE-NUM-TYPE | 210 | 0xD2 |
+----------------------+----------------------------+--------------------------------+
| FORWARDING-HINT-TYPE | 211 | 0xD3 |
+----------------------+----------------------------+--------------------------------+
| REGISTER-PREFIX-TYPE | 212 | 0xD4 |
+----------------------+----------------------------+--------------------------------+
| OBJECT-PARAM-TYPE | 301 | 0x12D |
+----------------------+----------------------------+--------------------------------+
| OBJECT-RESULT-TYPE | 302 | 0x12E |
+----------------------+----------------------------+--------------------------------+
+----------------------------+----------------------------+--------------------------------+
| type | Assigned number (decimal) | Assigned number (hexadecimal) |
+============================+============================+================================+
| START-BLOCK-ID-TYPE | 204 | 0xCC |
+----------------------------+----------------------------+--------------------------------+
| END-BLOCK-ID-TYPE | 205 | 0xCD |
+----------------------------+----------------------------+--------------------------------+
| REQUEST-NO-TYPE | 206 | 0xCE |
+----------------------------+----------------------------+--------------------------------+
| STATUS-CODE-TYPE | 208 | 0xD0 |
+----------------------------+----------------------------+--------------------------------+
| INSERT-NUM-TYPE | 209 | 0xD1 |
+----------------------------+----------------------------+--------------------------------+
| DELETE-NUM-TYPE | 210 | 0xD2 |
+----------------------------+----------------------------+--------------------------------+
| FORWARDING-HINT-TYPE | 211 | 0xD3 |
+----------------------------+----------------------------+--------------------------------+
| REGISTER-PREFIX-TYPE | 212 | 0xD4 |
+----------------------------+----------------------------+--------------------------------+
| OBJECT-PARAM-TYPE | 301 | 0x12D |
+----------------------------+----------------------------+--------------------------------+
| OBJECT-RESULT-TYPE | 302 | 0x12E |
+----------------------------+----------------------------+--------------------------------+
| SYNC-PARAM-TYPE | 401 | 0x191 |
+----------------------------+----------------------------+--------------------------------+
| SYNC-RESULT-TYPE | 402 | 0x192 |
+----------------------------+----------------------------+--------------------------------+
| SYNC-DATA-NAME-DEDUPE-TYPE | 403 | 0x193 |
+----------------------------+----------------------------+--------------------------------+
| SYNC-RESET-TYPE | 404 | 0x194 |
+----------------------------+----------------------------+--------------------------------+
| SYNC-PREFIX-TYPE | 405 | 0x195 |
+----------------------------+----------------------------+--------------------------------+


Status Code Definition
Expand Down
8 changes: 4 additions & 4 deletions docs/src/specification/insert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Repo insertion process makes use of the :doc:`../misc_pkgs/pub_sub`.
1. The repo subscribes to the topic ``/<repo_name>/insert``.

2. The client publishes a message to the topic ``/<repo_name>/insert``.
The message payload is ``RepoCommandParam`` containing one or more ``ObjectParam`` with the following fields:
The message payload is ``RepoCommandParam`` containing one or more ``ObjParam`` with the following fields:

* ``name``: either a Data packet name, or a name prefix of segmented Data packets.
* ``start_block_id`` (Optional): inclusive start segment number.
Expand Down Expand Up @@ -37,12 +37,12 @@ Insert status check

The client can use the :doc:`check` protocol to check the progress of an insertion process.
The insertion check response message payload is ``RepoCommandRes`` containing zero or more
``ObjectResult`` with the following fields:
``ObjStatus`` with the following fields:

* ``status_code``: status code, as defined on :doc:`check`.
Both the command itself and objects has a status code.
* ``name``: the name of object to insert.
* ``insert_num``: number of Data packets received by the repo so far.
* The number of ``ObjectResult`` in the result should be either:
* The number of ``ObjStatus`` in the result should be either:
* =0, which means the command is malformed or not allowed.
* equals to the number of ``ObjectParam`` in the insertion command.
* equals to the number of ``ObjParam`` in the insertion command.
8 changes: 4 additions & 4 deletions ndn_python_repo/clients/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
sys.path.insert(1, os.path.join(sys.path[0], '..'))

from ..command import RepoCommandParam, SyncParam
from ..command import RepoCommandParam, SyncParam, EmbName
from ..utils import PubSub
import logging
from ndn.app import NDNApp
Expand Down Expand Up @@ -46,8 +46,8 @@ async def join_sync(self, sync_prefix: NonStrictName, register_prefix: NonStrict
# construct insert cmd msg
cmd_param = RepoCommandParam()
cmd_sync = SyncParam()
cmd_sync.sync_prefix = sync_prefix
cmd_sync.register_prefix = register_prefix
cmd_sync.sync_prefix = EmbName.from_name(sync_prefix)
cmd_sync.register_prefix = EmbName.from_name(register_prefix)
cmd_sync.data_name_dedupe = data_name_dedupe
cmd_sync.reset = reset

Expand All @@ -67,7 +67,7 @@ async def leave_sync(self, sync_prefix: NonStrictName) -> bytes:
# construct insert cmd msg
cmd_param = RepoCommandParam()
cmd_sync = SyncParam()
cmd_sync.sync_prefix = sync_prefix
cmd_sync.sync_prefix = EmbName.from_name(sync_prefix)
cmd_param.sync_groups = [cmd_sync]
cmd_param_bytes = bytes(cmd_param.encode())

Expand Down
44 changes: 37 additions & 7 deletions ndn_python_repo/command/repo_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@
@Author [email protected]
@Date 2019-11-01
"""

from typing import TypeVar
import ndn.encoding as enc

__all__ = ['RepoTypeNumber', 'EmbName', 'ObjParam', 'SyncParam', 'SyncStatus', 'RepoCommandParam', 'ObjStatus', 'RepoCommandRes',
'RepeatedNames', 'RepoStatCode', 'RepoStatQuery']
__all__ = [
"RepoTypeNumber",
"EmbName",
"ObjParam",
"SyncParam",
"SyncStatus",
"RepoCommandParam",
"ObjStatus",
"RepoCommandRes",
"RepeatedNames",
"RepoStatCode",
"RepoStatQuery",
]


class RepoTypeNumber:
Expand All @@ -26,6 +39,8 @@ class RepoTypeNumber:
SYNC_RESULT = 402
SYNC_DATA_NAME_DEDUPE = 403
SYNC_RESET = 404
SYNC_PREFIX = 405


class RepoStatCode:
# 100 has not been used by previous code, but defined and documented.
Expand All @@ -43,9 +58,18 @@ class RepoStatCode:
NOT_FOUND = 404


TEmbName = TypeVar('TEmbName', bound='EmbName')


class EmbName(enc.TlvModel):
name = enc.NameField()

@staticmethod
def from_name(name: enc.NonStrictName) -> TEmbName:
ret = EmbName()
ret.name = name
return ret


class ObjParam(enc.TlvModel):
name = enc.NameField()
Expand All @@ -54,17 +78,21 @@ class ObjParam(enc.TlvModel):
end_block_id = enc.UintField(RepoTypeNumber.END_BLOCK_ID)
register_prefix = enc.ModelField(RepoTypeNumber.REGISTER_PREFIX, EmbName)


class SyncParam(enc.TlvModel):
sync_prefix = enc.NameField()
register_prefix = enc.NameField()
sync_prefix = enc.ModelField(RepoTypeNumber.SYNC_PREFIX, EmbName)
register_prefix = enc.ModelField(RepoTypeNumber.REGISTER_PREFIX, EmbName)
data_name_dedupe = enc.BoolField(RepoTypeNumber.SYNC_DATA_NAME_DEDUPE)
reset = enc.BoolField(RepoTypeNumber.SYNC_RESET)
# forwarding_hint = enc.ModelField(RepoTypeNumber.FORWARDING_HINT, enc.Links)
# sync_prefix = enc.ModelField(RepoTypeNumber.REGISTER_PREFIX, EmbName)


class RepoCommandParam(enc.TlvModel):
objs = enc.RepeatedField(enc.ModelField(RepoTypeNumber.OBJECT_PARAM, ObjParam))
sync_groups = enc.RepeatedField(enc.ModelField(RepoTypeNumber.SYNC_PARAM, SyncParam))
sync_groups = enc.RepeatedField(
enc.ModelField(RepoTypeNumber.SYNC_PARAM, SyncParam)
)


class RepoStatQuery(enc.TlvModel):
Expand All @@ -81,12 +109,14 @@ class ObjStatus(enc.TlvModel):
class SyncStatus(enc.TlvModel):
name = enc.NameField()
status_code = enc.UintField(RepoTypeNumber.STATUS_CODE)
insert_num = enc.UintField(RepoTypeNumber.INSERT_NUM)


class RepoCommandRes(enc.TlvModel):
status_code = enc.UintField(RepoTypeNumber.STATUS_CODE)
objs = enc.RepeatedField(enc.ModelField(RepoTypeNumber.OBJECT_RESULT, ObjStatus))
sync_groups = enc.RepeatedField(enc.ModelField(RepoTypeNumber.SYNC_RESULT, ObjStatus))
sync_groups = enc.RepeatedField(
enc.ModelField(RepoTypeNumber.SYNC_RESULT, SyncStatus)
)


class RepeatedNames(enc.TlvModel):
Expand Down
Loading

0 comments on commit e3d031e

Please sign in to comment.