|
| 1 | +""" |
| 2 | +Example demonstrating custom codec usage with COMPAS EVE. |
| 3 | +
|
| 4 | +This example shows how to: |
| 5 | +1. Use the default JsonMessageCodec |
| 6 | +2. Create a custom codec |
| 7 | +3. Use ProtobufMessageCodec (if compas_pb is installed) |
| 8 | +""" |
| 9 | + |
| 10 | +import json |
| 11 | + |
| 12 | +import compas_eve as eve |
| 13 | +from compas_eve import MessageCodec |
| 14 | +from compas_eve.codecs import JsonMessageCodec |
| 15 | + |
| 16 | + |
| 17 | +# Example 1: Using the default JSON codec (implicit) |
| 18 | +print("=" * 60) |
| 19 | +print("Example 1: Default JSON Codec (implicit)") |
| 20 | +print("=" * 60) |
| 21 | + |
| 22 | +pub = eve.Publisher("/example/default") |
| 23 | +sub = eve.EchoSubscriber("/example/default") |
| 24 | +sub.subscribe() |
| 25 | + |
| 26 | +pub.publish(eve.Message(text="Hello with default JSON codec", count=1)) |
| 27 | +print() |
| 28 | + |
| 29 | + |
| 30 | +# Example 2: Explicitly using JSON codec |
| 31 | +print("=" * 60) |
| 32 | +print("Example 2: Explicit JSON Codec") |
| 33 | +print("=" * 60) |
| 34 | + |
| 35 | +json_codec = JsonMessageCodec() |
| 36 | +transport = eve.InMemoryTransport(codec=json_codec) |
| 37 | + |
| 38 | +pub = eve.Publisher("/example/json", transport=transport) |
| 39 | +sub = eve.EchoSubscriber("/example/json", transport=transport) |
| 40 | +sub.subscribe() |
| 41 | + |
| 42 | +pub.publish(eve.Message(text="Hello with explicit JSON codec", count=2)) |
| 43 | +print() |
| 44 | + |
| 45 | + |
| 46 | +# Example 3: Custom codec |
| 47 | +print("=" * 60) |
| 48 | +print("Example 3: Custom Codec") |
| 49 | +print("=" * 60) |
| 50 | + |
| 51 | + |
| 52 | +class UpperCaseCodec(MessageCodec): |
| 53 | + """A simple custom codec that converts all string values to uppercase.""" |
| 54 | + |
| 55 | + def encode(self, message): |
| 56 | + """Encode message by converting all string values to uppercase.""" |
| 57 | + # Assume message is a Message instance |
| 58 | + data = message.data |
| 59 | + |
| 60 | + # Convert string values to uppercase |
| 61 | + encoded_data = {} |
| 62 | + for key, value in data.items(): |
| 63 | + if isinstance(value, str): |
| 64 | + encoded_data[key] = value.upper() |
| 65 | + else: |
| 66 | + encoded_data[key] = value |
| 67 | + return json.dumps(encoded_data) |
| 68 | + |
| 69 | + def decode(self, encoded_data, message_type): |
| 70 | + """Decode data (strings remain uppercase).""" |
| 71 | + data = json.loads(encoded_data) |
| 72 | + return message_type.parse(data) |
| 73 | + |
| 74 | + |
| 75 | +custom_codec = UpperCaseCodec() |
| 76 | +custom_transport = eve.InMemoryTransport(codec=custom_codec) |
| 77 | + |
| 78 | +pub = eve.Publisher("/example/custom", transport=custom_transport) |
| 79 | + |
| 80 | +# Create a custom subscriber that prints the message |
| 81 | +class PrintSubscriber(eve.Subscriber): |
| 82 | + def message_received(self, message): |
| 83 | + print(f"Received: {message}") |
| 84 | + |
| 85 | +sub = PrintSubscriber("/example/custom", transport=custom_transport) |
| 86 | +sub.subscribe() |
| 87 | + |
| 88 | +pub.publish(eve.Message(text="hello world", count=3)) |
| 89 | +print() |
| 90 | + |
| 91 | + |
| 92 | +# Example 4: Protocol Buffers codec (if available) |
| 93 | +print("=" * 60) |
| 94 | +print("Example 4: Protocol Buffers Codec (if available)") |
| 95 | +print("=" * 60) |
| 96 | + |
| 97 | +try: |
| 98 | + from compas_eve.codecs import ProtobufMessageCodec |
| 99 | + |
| 100 | + pb_codec = ProtobufMessageCodec() |
| 101 | + pb_transport = eve.InMemoryTransport(codec=pb_codec) |
| 102 | + |
| 103 | + pub = eve.Publisher("/example/protobuf", transport=pb_transport) |
| 104 | + sub = eve.EchoSubscriber("/example/protobuf", transport=pb_transport) |
| 105 | + sub.subscribe() |
| 106 | + |
| 107 | + pub.publish(eve.Message(text="Hello with Protocol Buffers", count=4)) |
| 108 | + print("✓ Protocol Buffers codec is working!") |
| 109 | + |
| 110 | +except ImportError as e: |
| 111 | + print(f"Protocol Buffers codec not available: {e}") |
| 112 | + print("Install with: pip install compas_pb") |
| 113 | + |
| 114 | +print() |
| 115 | +print("=" * 60) |
| 116 | +print("All examples completed!") |
| 117 | +print("=" * 60) |
0 commit comments