Commit 14676f9e authored by Matej Feder's avatar Matej Feder

Merge branch 'small-tasks' into 'asyncio-and-code-improvement'

Small tasks

See merge request !22
parents c874eebb 16a2cc00
[flake8]
max-line-length = 120
ignore = E203,W503
......@@ -14,3 +14,5 @@ Submodules
"""
from .pydtn import start
__all__ = ["start"]
......@@ -21,4 +21,6 @@ Note:
from .bp import BPAgent
__all__ = ["bp_agent"]
bp_agent = BPAgent()
......@@ -19,3 +19,5 @@ Note:
"""
from .tcp_cl import TCPCL
__all__ = ["TCPCL"]
......@@ -7,11 +7,11 @@ Conforms to::
(which is compatible with uPCN 0.6.0)
Dependencies:
This module depends on the ``cbor`` package which can be installed via pip:
This module depends on the ``cbor2`` package which can be installed via pip:
.. code:: bash
pip install cbor
pip install cbor2
Usage:
If you are in the root directory of the project you can simply run:
......@@ -44,8 +44,7 @@ import struct
from enum import IntEnum, IntFlag
from datetime import datetime, tzinfo, timedelta
from random import random
import cbor
from cbor.cbor import CBOR_ARRAY
import cbor2
from .crc import crc16_x25, crc32
......@@ -308,7 +307,7 @@ class PrimaryBlock(object):
# encode each field
for field in primary_block:
binary.append(cbor.dumps(field))
binary.append(cbor2.dumps(field))
if self.crc_type == CRCType.CRC32:
# Empty CRC-32 bit field: CBOR "byte string"
......@@ -324,7 +323,7 @@ class PrimaryBlock(object):
crc = crc16_x25(binary)
primary_block.append(struct.pack("!H", crc))
return cbor.dumps(primary_block)
return cbor2.dumps(primary_block)
class CanonicalBlock(object):
......@@ -350,7 +349,7 @@ class CanonicalBlock(object):
# Block data length
# If the value can be expressed in one byte, we have to ensure
# that we do not return 0 as block length. Therefore max(1, ...)
block.append(max(1, len(cbor.dumps(serialized_data))))
block.append(max(1, len(cbor2.dumps(serialized_data))))
# Block-specific data
block.append(serialized_data)
......@@ -364,7 +363,7 @@ class CanonicalBlock(object):
else:
empty_crc = b"\x42\x00\x00"
binary = struct.pack("B", 0x80 | 7) + b"".join([cbor.dumps(item) for item in block]) + empty_crc
binary = struct.pack("B", 0x80 | 7) + b"".join([cbor2.dumps(item) for item in block]) + empty_crc
if self.crc_type == CRCType.CRC32:
crc = crc32(binary)
......@@ -375,7 +374,7 @@ class CanonicalBlock(object):
# unsigned short in network byte order
block.append(struct.pack("!H", crc))
return cbor.dumps(block)
return cbor2.dumps(block)
class PayloadBlock(CanonicalBlock):
......@@ -504,6 +503,7 @@ class Bundle(object):
def serialize(self):
# Header for indefinite array
CBOR_ARRAY = 0x80
head = struct.pack("B", CBOR_ARRAY | 31)
# Stop-code for indefinite array
stop = b"\xff"
......
......@@ -25,8 +25,16 @@ class TestCommon:
def test_decode(self):
assert sdnv_decode(bytearray(self.DATA_READ), 2) == (16, 3)
SERIAL_RESPONSE = b"\x9f\x89\x07\x00\x01\x82\x01etest2\x82\x01dtest\x82\x01\x00\x82\x1a$(\xde\xf4\x00\x19\x01,B%r\x87\t\x01\x00\x02\x04\x82\x18\x1e\x00D]}\xd8u\x87\x08\x02\x00\x02\x01\x00D\x98\x9d\x838\x87\x01\x00\x00\x02\x02aHDF\xa27\x80\xff"
SERIAL_TCPCL_RESPONSE = b"\x13M\x9f\x89\x07\x00\x01\x82\x01etest2\x82\x01dtest\x82\x01\x00\x82\x1a$(\xde\xf4\x00\x19\x01,B%r\x87\t\x01\x00\x02\x04\x82\x18\x1e\x00D]}\xd8u\x87\x08\x02\x00\x02\x01\x00D\x98\x9d\x838\x87\x01\x00\x00\x02\x02aHDF\xa27\x80\xff"
SERIAL_RESPONSE = (
b"\x9f\x89\x07\x00\x01\x82\x01etest2\x82\x01dtest\x82\x01\x00\x82\x1a$(\xde\xf4\x00\x19\x01,B"
+ b"%r\x87\t\x01\x00\x02\x04\x82\x18\x1e\x00D]}\xd8u\x87\x08\x02\x00\x02\x01\x00D\x98\x9d\x838"
+ b"\x87\x01\x00\x00\x02\x02aHDF\xa27\x80\xff"
)
SERIAL_TCPCL_RESPONSE = (
b"\x13M\x9f\x89\x07\x00\x01\x82\x01etest2\x82\x01dtest\x82\x01\x00\x82\x1a$(\xde\xf4\x00"
+ b"\x19\x01,B%r\x87\t\x01\x00\x02\x04\x82\x18\x1e\x00D]}\xd8u\x87\x08\x02\x00\x02\x01\x00"
+ b"D\x98\x9d\x838\x87\x01\x00\x00\x02\x02aHDF\xa27\x80\xff"
)
def test_serialize(self):
bundle = serialize_bundle("dtn:test", "dtn:test2", "H", NULL_EID, self.UNIX_TIME)
......
......@@ -16,15 +16,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Hexdump implementation in Python 2.7 and 3.
Hexdump implementation in Python 3.
"""
from __future__ import unicode_literals
import sys
__python3__ = sys.version_info > (3,)
def ordp(c):
"""
......@@ -32,19 +26,11 @@ def ordp(c):
"""
output = []
if __python3__:
for i in c:
if (i < 32) or (i >= 127):
output.append(".")
else:
output.append(chr(i))
else:
for i in c:
j = ord(i)
if (j < 32) or (j >= 127):
output.append(".")
else:
output.append(i)
for i in c:
if (i < 32) or (i >= 127):
output.append(".")
else:
output.append(chr(i))
return "".join(output)
......@@ -66,10 +52,7 @@ def hexdump(p):
output.append("{:04d} ".format(i))
for j in range(16):
if (i + j) < length:
if __python3__:
byte = p[i + j]
else:
byte = ord(p[i + j])
byte = p[i + j]
output.append("{:02X} ".format(byte))
else:
output.append(" ")
......
pyyaml~=3.13
cbor~=1.0.0
cbor2~=4.1.2
asyncio~=3.4.3
from setuptools import setup, find_packages
install_requires = ["pyyaml>=3.13", "cbor>=1.0.0", "cbor2>=4.1.2", "asyncio>=3.4.3"]
install_requires = ["pyyaml>=3.13", "cbor2>=4.1.2", "asyncio>=3.4.3"]
setup(
name="pydtn",
......
......@@ -10,3 +10,5 @@ Submodules
"""
from .pydtn_rest import start
__all__ = ["start"]
......@@ -35,17 +35,15 @@ async def handle_config(request):
json = await request.json()
destination = request.rel_url.query["destination"]
if "messages" not in json:
raise web.HTTPBadRequest
try:
for msg in json["messages"]:
await bp_agent.create_and_send_config_msg(
"dtn://ops-sat.dtn/config",
msg["eid"],
msg["cla_address"],
int(msg["start_offset"]),
int(msg["end_offset"]),
destination, msg["eid"], msg["cla_address"], int(msg["start_offset"]), int(msg["end_offset"])
)
except NoContactError as e:
return web.json_response({"success": False, "details": f'There is no scheduled contact for node "{e.eid}"'})
......
......@@ -24,7 +24,7 @@
"raw": "{\n \"messages\": [\n {\n \"eid\": \"dtn://pyDTN-2.dtn\",\n \"cla_address\": \"172.25.2.11:2002\",\n \"start_offset\": 5,\n \"end_offset\": 20\n }\n ]\n}"
},
"url": {
"raw": "http://localhost:8082/config",
"raw": "http://localhost:8082/config?destination=dtn://ops-sat.dtn/config",
"protocol": "http",
"host": [
"localhost"
......@@ -32,6 +32,12 @@
"port": "8082",
"path": [
"config"
],
"query": [
{
"key": "destination",
"value": "dtn://ops-sat.dtn/config"
}
]
}
},
......
......@@ -2,7 +2,7 @@
# Tell uPCN-1 that pyDTN-2 is going to be available in 5s for next 15s, use REST API
echo "Scheduling contact"
curl -H "Content-Type: application/json" --data @schedule_contact.json "http://localhost:8082/config" || exit 1
curl -H "Content-Type: application/json" --data @schedule_contact.json "http://localhost:8082/config?destination=dtn://ops-sat.dtn/config" || exit 1
# Wait, but not longer than contact is going to be visible, otherwise we are hitting uPCN bug
echo "Waiting"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment