forked from named-data/python-ndn
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app: Write a new design for NDNApp (v2).
Add support for PIT Token. Add SignedInterest support for NFD mgmt command Interests. Ref: named-data#48
- Loading branch information
Showing
10 changed files
with
1,206 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (C) 2019-2022 The python-ndn authors | ||
# | ||
# This file is part of python-ndn. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ----------------------------------------------------------------------------- | ||
import logging | ||
from ndn import utils, appv2, types | ||
from ndn import encoding as enc | ||
|
||
|
||
logging.basicConfig(format='[{asctime}]{levelname}:{message}', | ||
datefmt='%Y-%m-%d %H:%M:%S', | ||
level=logging.INFO, | ||
style='{') | ||
|
||
|
||
app = appv2.NDNApp() | ||
|
||
|
||
async def main(): | ||
try: | ||
timestamp = utils.timestamp() | ||
name = enc.Name.from_str('/example/testApp/randomData') + [enc.Component.from_timestamp(timestamp)] | ||
print(f'Sending Interest {enc.Name.to_str(name)}, {enc.InterestParam(must_be_fresh=True, lifetime=6000)}') | ||
# TODO: Write a better validator | ||
data_name, content, pkt_context = await app.express( | ||
name, validator=appv2.pass_all, | ||
must_be_fresh=True, can_be_prefix=False, lifetime=6000) | ||
|
||
print(f'Received Data Name: {enc.Name.to_str(data_name)}') | ||
print(pkt_context['meta_info']) | ||
print(bytes(content) if content else None) | ||
except types.InterestNack as e: | ||
print(f'Nacked with reason={e.reason}') | ||
except types.InterestTimeout: | ||
print(f'Timeout') | ||
except types.InterestCanceled: | ||
print(f'Canceled') | ||
except types.ValidationFailure: | ||
print(f'Data failed to validate') | ||
finally: | ||
app.shutdown() | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run_forever(after_start=main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (C) 2019-2022 The python-ndn authors | ||
# | ||
# This file is part of python-ndn. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ----------------------------------------------------------------------------- | ||
import typing | ||
import logging | ||
from ndn import appv2 | ||
from ndn import encoding as enc | ||
|
||
|
||
logging.basicConfig(format='[{asctime}]{levelname}:{message}', | ||
datefmt='%Y-%m-%d %H:%M:%S', | ||
level=logging.INFO, | ||
style='{') | ||
|
||
|
||
app = appv2.NDNApp() | ||
keychain = app.default_keychain() | ||
|
||
|
||
@app.route('/example/testApp') | ||
def on_interest(name: enc.FormalName, _app_param: typing.Optional[enc.BinaryStr], | ||
reply: appv2.ReplyFunc, context: appv2.PktContext): | ||
print(f'>> I: {enc.Name.to_str(name)}, {context["int_param"]}') | ||
content = "Hello, world!".encode() | ||
reply(app.make_data(name, content=content, signer=keychain.get_signer({}), | ||
freshness_period=10000)) | ||
print(f'<< D: {enc.Name.to_str(name)}') | ||
print(enc.MetaInfo(freshness_period=10000)) | ||
print(f'Content: (size: {len(content)})') | ||
print('') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.