File size: 7,066 Bytes
8aedc84
 
51f51c3
8aedc84
 
 
 
 
 
 
 
 
 
 
ec2d8f0
8aedc84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51f51c3
8aedc84
51f51c3
8aedc84
51f51c3
8aedc84
 
 
 
 
 
 
 
 
 
3367d1b
 
8aedc84
51f51c3
 
 
8aedc84
 
 
 
 
 
 
 
 
 
51f51c3
8aedc84
 
51f51c3
8aedc84
 
 
51f51c3
 
 
8aedc84
 
 
 
ec2d8f0
8aedc84
 
 
 
 
 
 
 
 
 
 
 
 
 
51f51c3
 
8aedc84
 
 
 
 
 
 
51f51c3
 
 
8aedc84
 
51f51c3
 
8aedc84
 
 
 
 
ec2d8f0
 
8aedc84
 
 
 
 
3367d1b
 
8aedc84
 
51f51c3
 
 
 
 
 
3367d1b
 
51f51c3
8aedc84
 
 
 
 
 
 
 
51f51c3
 
8aedc84
51f51c3
8aedc84
51f51c3
8aedc84
 
 
51f51c3
8aedc84
 
 
 
 
 
 
 
 
51f51c3
8aedc84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51f51c3
 
 
 
 
 
3367d1b
 
51f51c3
8aedc84
 
 
 
 
51f51c3
8aedc84
 
 
 
 
 
 
 
 
3367d1b
 
8aedc84
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""
Context Manager Example - RobotHub TransportServer

This example demonstrates:
- Using clients as context managers for automatic cleanup
- Exception handling with proper resource cleanup
- Factory functions for quick client creation
- Graceful error recovery
"""

import asyncio
import logging

from transport_server_client import (
    RoboticsConsumer,
    RoboticsProducer,
    create_consumer_client,
    create_producer_client,
)

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


async def basic_context_manager_example():
    """Basic example using context managers."""
    logger.info("=== Basic Context Manager Example ===")

    # Using producer as context manager
    async with RoboticsProducer("http://localhost:8000") as producer:
        workspace_id, room_id = await producer.create_room()
        logger.info(f"Created room: {room_id}")
        logger.info(f"Workspace ID: {workspace_id}")

        await producer.connect(workspace_id, room_id)
        logger.info("Producer connected")

        # Send some data
        await producer.send_state_sync({"joint1": 45.0, "joint2": -30.0})
        logger.info("State sent")

        # Exception handling within context
        try:
            # This might fail if room doesn't exist
            await producer.send_joint_update([{"name": "invalid", "value": 999.0}])
        except Exception:
            logger.exception("Handled exception")

        # Clean up room before context exit
        await producer.delete_room(workspace_id, room_id)

    # Producer is automatically disconnected here
    logger.info("Producer automatically disconnected")


async def factory_function_example():
    """Example using factory functions for quick setup."""
    logger.info("\n=== Factory Function Example ===")

    # Create and auto-connect producer with factory function
    producer = await create_producer_client("http://localhost:8000")
    workspace_id = producer.workspace_id
    room_id = producer.room_id
    logger.info(f"Producer auto-connected to room: {room_id}")
    logger.info(f"Workspace ID: {workspace_id}")

    try:
        # Create and auto-connect consumer
        consumer = await create_consumer_client(
            workspace_id, room_id, "http://localhost:8000"
        )
        logger.info("Consumer auto-connected")

        # Set up callback
        received_updates = []
        consumer.on_joint_update(received_updates.append)

        # Send some updates
        await producer.send_joint_update([
            {"name": "shoulder", "value": 90.0},
            {"name": "elbow", "value": -45.0},
        ])

        await asyncio.sleep(0.5)  # Wait for message propagation

        logger.info(f"Consumer received {len(received_updates)} updates")

    finally:
        # Manual cleanup for factory-created clients
        await consumer.disconnect()
        await producer.disconnect()
        await producer.delete_room(workspace_id, room_id)
        logger.info("Manual cleanup completed")


async def exception_handling_example():
    """Example showing exception handling with context managers."""
    logger.info("\n=== Exception Handling Example ===")

    workspace_id = None
    room_id = None

    try:
        async with RoboticsProducer("http://localhost:8000") as producer:
            workspace_id, room_id = await producer.create_room()
            await producer.connect(workspace_id, room_id)

            # Simulate some work that might fail
            for i in range(5):
                if i == 3:
                    # Simulate an error
                    msg = "Simulated error during operation"
                    raise ValueError(msg)

                await producer.send_state_sync({f"joint_{i}": float(i * 10)})
                logger.info(f"Sent update {i}")
                await asyncio.sleep(0.1)

    except ValueError:
        logger.exception("Caught expected error")
        logger.info("Context manager still ensures cleanup")

    # Clean up room after exception
    if workspace_id and room_id:
        try:
            temp_producer = RoboticsProducer("http://localhost:8000")
            await temp_producer.delete_room(workspace_id, room_id)
            logger.info("Room cleaned up after exception")
        except Exception:
            logger.exception("Failed to clean up room")

    logger.info("Exception handling example completed")


async def multiple_clients_example():
    """Example with multiple clients using context managers."""
    logger.info("\n=== Multiple Clients Example ===")

    # Create room first
    workspace_id = None
    room_id = None
    async with RoboticsProducer("http://localhost:8000") as setup_producer:
        workspace_id, room_id = await setup_producer.create_room()
        logger.info(f"Setup room: {room_id}")
        logger.info(f"Workspace ID: {workspace_id}")

    # Now use multiple clients in the same room
    async with RoboticsProducer("http://localhost:8000") as producer:
        await producer.connect(workspace_id, room_id)

        # Use multiple consumers
        consumers = []
        try:
            # Create multiple consumers with context managers
            for i in range(3):
                consumer = RoboticsConsumer("http://localhost:8000")
                consumers.append(consumer)
                # Note: We're not using context manager here to show manual management
                await consumer.connect(workspace_id, room_id, f"consumer-{i}")

            logger.info(f"Connected {len(consumers)} consumers")

            # Send data to all consumers
            await producer.send_state_sync({
                "base": 0.0,
                "shoulder": 45.0,
                "elbow": -30.0,
                "wrist": 15.0,
            })

            await asyncio.sleep(0.5)  # Wait for propagation
            logger.info("Data sent to all consumers")

        finally:
            # Manual cleanup for consumers
            for i, consumer in enumerate(consumers):
                await consumer.disconnect()
                logger.info(f"Disconnected consumer-{i}")

    # Clean up room
    if workspace_id and room_id:
        try:
            temp_producer = RoboticsProducer("http://localhost:8000")
            await temp_producer.delete_room(workspace_id, room_id)
            logger.info("Room cleaned up")
        except Exception:
            logger.exception("Failed to clean up room")

    logger.info("Multiple clients example completed")


async def main():
    """Run all context manager examples."""
    logger.info("🤖 RobotHub TransportServer Context Manager Examples 🤖")

    try:
        await basic_context_manager_example()
        await factory_function_example()
        await exception_handling_example()
        await multiple_clients_example()

        logger.info("\n✅ All context manager examples completed successfully!")

    except Exception:
        logger.exception("❌ Example failed")


if __name__ == "__main__":
    asyncio.run(main())