Skip to content

Commit c2bda41

Browse files
Upgrade to zenoh-python 1.0.0-alpha.6 (#16)
* Upgrade to zenoh-python 1.0.0-alpha.6 * Incorporate review comment
1 parent 516b8af commit c2bda41

File tree

5 files changed

+60
-73
lines changed

5 files changed

+60
-73
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
up-python==0.2.0-dev
2-
eclipse-zenoh==0.11.0
2+
eclipse-zenoh==1.0.0-alpha.6
33

up_transport_zenoh/examples/publish.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
from up_transport_zenoh.examples.common_uuri import get_zenoh_default_config
2424
from up_transport_zenoh.uptransportzenoh import UPTransportZenoh
2525

26-
source = UUri(authority_name="vehicle1", ue_id=18)
26+
source = UUri(authority_name="publisher", ue_id=1, ue_version_major=1)
2727
publisher = UPTransportZenoh.new(get_zenoh_default_config(), source)
2828

2929

3030
async def publish_to_zenoh():
3131
# create uuri
32-
uuri = UUri(ue_id=4, ue_version_major=1, resource_id=0x8000)
33-
builder = UMessageBuilder.publish(uuri)
32+
source.resource_id = 0x8001
33+
builder = UMessageBuilder.publish(source)
3434
payload = UPayload.pack(UUri())
3535
umessage = builder.build_from_upayload(payload)
3636
status = await publisher.send(umessage)

up_transport_zenoh/examples/subscribe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ async def on_receive(self, msg: UMessage) -> None:
3333
return UStatus(message="Received event")
3434

3535

36-
source = UUri(authority_name="vehicle1", ue_id=18)
36+
source = UUri(authority_name="subscriber", ue_id=9)
3737
transport = UPTransportZenoh.new(get_zenoh_default_config(), source)
3838
# create topic uuri
39-
uuri = UUri(ue_id=4, ue_version_major=1, resource_id=0x8000)
39+
uuri = UUri(authority_name="publisher", ue_id=1, ue_version_major=1, resource_id=0x8001)
4040

4141

4242
async def subscribe_to_zenoh_if_subscription_service_is_not_running():

up_transport_zenoh/uptransportzenoh.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
from uprotocol.v1.umessage_pb2 import UMessage
3030
from uprotocol.v1.uri_pb2 import UUri
3131
from uprotocol.v1.ustatus_pb2 import UStatus
32-
from zenoh import Config, Query, Queryable, Sample, Session, Subscriber, Value
33-
from zenoh.keyexpr import KeyExpr
32+
from zenoh import Config, Query, Queryable, Sample, Session, Subscriber
33+
from zenoh.zenoh import KeyExpr
3434

3535
from up_transport_zenoh.zenohutils import MessageFlag, ZenohUtils
3636

@@ -93,7 +93,7 @@ def send_publish_notification(self, zenoh_key: str, payload: bytes, attributes:
9393
logging.debug(f"Priority: {priority}")
9494
logging.debug(f"Attachment: {attachment}")
9595

96-
self.session.put(keyexpr=zenoh_key, value=payload, attachment=attachment, priority=priority)
96+
self.session.put(key_expr=zenoh_key, payload=payload, attachment=attachment, priority=priority)
9797
msg = "Successfully sent data to Zenoh"
9898
logging.debug(f"SUCCESS:{msg}")
9999
return UStatus(code=UCode.OK, message=msg)
@@ -111,8 +111,8 @@ def send_request(self, zenoh_key: str, payload: bytes, attributes: UAttributes)
111111
return UStatus(code=UCode.INVALID_ARGUMENT, message=msg)
112112
resp_callback = None
113113
for saved_zenoh_key, listener in self.rpc_callback_map.items():
114-
keyexpr_zenohkey = KeyExpr.new(zenoh_key)
115-
keyexpr_savedkey = KeyExpr.new(saved_zenoh_key)
114+
keyexpr_zenohkey = KeyExpr(zenoh_key)
115+
keyexpr_savedkey = KeyExpr(saved_zenoh_key)
116116

117117
if keyexpr_zenohkey.intersects(keyexpr_savedkey):
118118
resp_callback = self.rpc_callback_map.get(saved_zenoh_key)
@@ -138,7 +138,7 @@ def handle_response(reply: Query.reply) -> None:
138138
logging.debug(msg)
139139
return UStatus(code=UCode.INTERNAL, message=msg)
140140
# Create UMessage
141-
msg = UMessage(attributes=u_attribute, payload=sample.payload)
141+
msg = UMessage(attributes=u_attribute, payload=bytes(sample.payload))
142142
asyncio.run(resp_callback.on_receive(msg))
143143
except Exception:
144144
msg = f"Error while parsing Zenoh reply: {reply.error}"
@@ -148,21 +148,19 @@ def handle_response(reply: Query.reply) -> None:
148148
# Send query
149149
ttl = attributes.ttl / 1000 if attributes.ttl is not None else 1000
150150

151-
value = Value(payload)
152151
# Send the query
153-
get_builder = self.session.get(
154-
zenoh_key,
155-
zenoh.Queue(),
156-
target=zenoh.QueryTarget.BEST_MATCHING(),
152+
replies = self.session.get(
153+
selector=zenoh_key,
154+
target=zenoh.QueryTarget.BEST_MATCHING,
157155
attachment=attachment,
158-
value=value,
156+
payload=payload,
159157
timeout=ttl,
160158
)
161159

162160
def get_response():
163161
try:
164-
for reply in get_builder.receiver:
165-
if reply.is_ok:
162+
for reply in replies:
163+
if reply.ok:
166164
handle_response(reply)
167165
break
168166
except Exception:
@@ -192,11 +190,9 @@ def send_response(self, payload: bytes, attributes: UAttributes) -> UStatus:
192190
msg = "Query doesn't exist"
193191
logging.debug(msg)
194192
return UStatus(code=UCode.INTERNAL, message=msg) # Send back the query
195-
value = Value(payload)
196-
reply = Sample(query.key_expr, value, attachment=attachment)
197193

198194
try:
199-
query.reply(reply)
195+
query.reply(query.key_expr, payload, attachment=attachment)
200196
msg = "Successfully sent rpc response to Zenoh"
201197
logging.debug(f"SUCCESS:{msg}")
202198
return UStatus(code=UCode.OK, message=msg)
@@ -223,7 +219,7 @@ def callback(sample: Sample) -> None:
223219
msg = "Unable to decode attributes"
224220
logging.debug(msg)
225221
return UStatus(code=UCode.INTERNAL, message=msg)
226-
message = UMessage(attributes=u_attribute, payload=sample.payload)
222+
message = UMessage(attributes=u_attribute, payload=bytes(sample.payload))
227223
asyncio.run(listener.on_receive(message))
228224

229225
# Create Zenoh subscriber
@@ -260,7 +256,7 @@ def callback(query: Query) -> None:
260256
logging.debug(msg)
261257
return UStatus(code=UCode.INTERNAL, message=msg)
262258

263-
message = UMessage(attributes=u_attribute, payload=query.value.payload if query.value else None)
259+
message = UMessage(attributes=u_attribute, payload=bytes(query.payload) if query.payload else None)
264260
self.query_map[u_attribute.id.SerializeToString()] = query
265261
asyncio.run(listener.on_receive(message))
266262

@@ -286,7 +282,6 @@ async def send(self, message: UMessage) -> UStatus:
286282
source = attributes.source
287283
sink = attributes.sink
288284
zenoh_key = ZenohUtils.to_zenoh_key_string(self.authority_name, source, sink)
289-
290285
if not source:
291286
return UStatus(code=UCode.INVALID_ARGUMENT, message="attributes.source shouldn't be empty")
292287
payload = message.payload or b''

up_transport_zenoh/zenohutils.py

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
from uprotocol.uri.factory.uri_factory import UriFactory
2121
from uprotocol.v1.uattributes_pb2 import (
2222
UAttributes,
23-
UPayloadFormat,
2423
UPriority,
2524
)
2625
from uprotocol.v1.ucode_pb2 import UCode
2726
from uprotocol.v1.uri_pb2 import UUri
2827
from uprotocol.v1.ustatus_pb2 import UStatus
29-
from zenoh import Encoding, Priority
30-
from zenoh.value import Attachment
28+
from zenoh import Priority, ZBytes
3129

3230
UATTRIBUTE_VERSION: int = 1
3331

@@ -72,77 +70,71 @@ def get_uauth_from_uuri(uri: UUri) -> Union[str, UStatus]:
7270
@staticmethod
7371
def to_zenoh_key_string(authority_name: str, src_uri: UUri, dst_uri: UUri = None) -> str:
7472
src = ZenohUtils.uri_to_zenoh_key(authority_name, src_uri)
75-
dst = ZenohUtils.uri_to_zenoh_key(authority_name, dst_uri) if dst_uri else "{}/{}/{}/{}"
73+
dst = ZenohUtils.uri_to_zenoh_key(authority_name, dst_uri) if dst_uri and dst_uri != UUri() else "{}/{}/{}/{}"
7674
return f"up/{src}/{dst}"
7775

7876
@staticmethod
7977
def map_zenoh_priority(upriority: UPriority) -> Priority:
8078
mapping = {
81-
UPriority.UPRIORITY_CS0: Priority.BACKGROUND(),
82-
UPriority.UPRIORITY_CS1: Priority.DATA_LOW(),
83-
UPriority.UPRIORITY_CS2: Priority.DATA(),
84-
UPriority.UPRIORITY_CS3: Priority.DATA_HIGH(),
85-
UPriority.UPRIORITY_CS4: Priority.INTERACTIVE_LOW(),
86-
UPriority.UPRIORITY_CS5: Priority.INTERACTIVE_HIGH(),
87-
UPriority.UPRIORITY_CS6: Priority.REAL_TIME(),
88-
UPriority.UPRIORITY_UNSPECIFIED: Priority.DATA_LOW(),
79+
UPriority.UPRIORITY_CS0: Priority.BACKGROUND,
80+
UPriority.UPRIORITY_CS1: Priority.DATA_LOW,
81+
UPriority.UPRIORITY_CS2: Priority.DATA,
82+
UPriority.UPRIORITY_CS3: Priority.DATA_HIGH,
83+
UPriority.UPRIORITY_CS4: Priority.INTERACTIVE_LOW,
84+
UPriority.UPRIORITY_CS5: Priority.INTERACTIVE_HIGH,
85+
UPriority.UPRIORITY_CS6: Priority.REAL_TIME,
86+
UPriority.UPRIORITY_UNSPECIFIED: Priority.DATA_LOW,
8987
}
9088
return mapping[upriority]
9189

92-
@staticmethod
93-
def to_upayload_format(encoding: Encoding) -> UPayloadFormat:
94-
try:
95-
value = int(encoding.suffix)
96-
return value if UPayloadFormat.Name(value) else None
97-
except (ValueError, AttributeError):
98-
return None
99-
10090
@staticmethod
10191
def uattributes_to_attachment(uattributes: UAttributes):
102-
attachment = [("", UATTRIBUTE_VERSION.to_bytes(1, byteorder='little')), ("", uattributes.SerializeToString())]
103-
return attachment
92+
# Convert the version number to bytes (assuming 1 as in the Rust example)
93+
version_bytes = UATTRIBUTE_VERSION.to_bytes(1, byteorder='little')
94+
95+
# Serialize the UAttributes to bytes
96+
uattributes_bytes = uattributes.SerializeToString()
97+
98+
# Combine version bytes and uattributes bytes into one list of bytes
99+
attachment_bytes = [version_bytes, uattributes_bytes]
100+
101+
# Convert the combined bytes to ZBytes
102+
return attachment_bytes
104103

105104
@staticmethod
106-
def attachment_to_uattributes(attachment: Attachment) -> UAttributes:
105+
def attachment_to_uattributes(attachment: ZBytes) -> UAttributes:
107106
try:
108-
version = None
109-
version_found = False
110-
uattributes = None
111-
112-
items = attachment.items()
113-
for pair in items:
114-
if not version_found:
115-
version = pair[1]
116-
version_found = True
117-
else:
118-
# Process UAttributes data
119-
uattributes = UAttributes()
120-
uattributes.ParseFromString(pair[1])
121-
break
122-
123-
if version is None:
124-
msg = f"UAttributes version is empty (should be {UATTRIBUTE_VERSION})"
125-
logging.debug(msg)
126-
raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg)
107+
# Convert ZBytes to a list of bytes
108+
attachment_bytes = attachment.deserialize(list)
127109

128-
if not version_found:
129-
msg = "UAttributes version is missing in the attachment"
110+
# Ensure there is at least one byte for the version
111+
if len(attachment_bytes) < 1:
112+
msg = "Unable to get the UAttributes version"
130113
logging.debug(msg)
131114
raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg)
132115

133-
if version != UATTRIBUTE_VERSION.to_bytes(1, byteorder='little'):
116+
# Check the version
117+
version = int.from_bytes(bytes(attachment_bytes[0]), byteorder='big')
118+
if version != UATTRIBUTE_VERSION:
134119
msg = f"UAttributes version is {version} (should be {UATTRIBUTE_VERSION})"
135120
logging.debug(msg)
136121
raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg)
137122

138-
if uattributes is None:
123+
# Get the attributes from the remaining bytes
124+
uattributes_data = bytes(attachment_bytes[1])
125+
if not uattributes_data:
139126
msg = "Unable to get the UAttributes"
140127
logging.debug(msg)
141128
raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg)
142129

130+
# Parse the UAttributes from the bytes
131+
uattributes = UAttributes()
132+
uattributes.ParseFromString(uattributes_data)
133+
143134
return uattributes
135+
144136
except Exception as e:
145-
msg = f"Failed to convert Attachment to UAttributes: {e}"
137+
msg = f"Failed to convert Attachment to UAttributes: {str(e)}"
146138
logging.debug(msg)
147139
raise UStatusError.from_code_message(code=UCode.INVALID_ARGUMENT, message=msg)
148140

0 commit comments

Comments
 (0)