|
| 1 | +# Environment Configuration Module |
| 2 | + |
| 3 | +## Overview |
| 4 | +The `multicast.env` module provides environment-based configuration for the multicast package, allowing runtime customization of network settings through environment variables. |
| 5 | + |
| 6 | +## Environment Variables |
| 7 | + |
| 8 | +| Variable | Default | Description | |
| 9 | +|----------|---------|-------------| |
| 10 | +| `MULTICAST_PORT` | 59259 | Port number for multicast communication (49152-65535) | |
| 11 | +| `MULTICAST_GROUP` | "224.0.0.1" | Primary multicast group address (224.0.0.0/4) | |
| 12 | +| `MULTICAST_GROUPS` | - | Space-separated list of additional multicast addresses | |
| 13 | +| `MULTICAST_TTL` | 1 | Time-to-live value (1-126) | |
| 14 | +| `MULTICAST_BIND_ADDR` | "0.0.0.0" | Address to bind to | |
| 15 | +| `MULTICAST_BUFFER_SIZE` | 1024 | Receive buffer size in bytes | |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### Basic Configuration |
| 20 | +```python |
| 21 | +import os # used to set environment |
| 22 | + |
| 23 | +# Set custom configuration |
| 24 | +os.environ['MULTICAST_PORT'] = '50000' |
| 25 | +os.environ['MULTICAST_GROUP'] = '224.0.0.2' |
| 26 | + |
| 27 | +# see os.environ['MULTICAST_GROUPS'] for multi-group config |
| 28 | + |
| 29 | +# Uncommonly there is need to increase TTL and BUFFER size, |
| 30 | +# however users probably want raw sockets in those cases. |
| 31 | +# os.environ['MULTICAST_TTL'] = '2' # May harm your own bandwidth when increasing |
| 32 | +# os.environ['MULTICAST_BUFFER_SIZE'] = '2048' # Expect increased data loss when increasing |
| 33 | + |
| 34 | +# Load configuration Loads on Import |
| 35 | +import multicast |
| 36 | +``` |
| 37 | + |
| 38 | +### Multiple Group Configuration |
| 39 | +```python |
| 40 | +# Join multiple multicast groups |
| 41 | +os.environ['MULTICAST_GROUPS'] = '224.0.0.1 224.0.0.2 224.0.0.3' |
| 42 | +# Load configuration Loads on Import |
| 43 | +import multicast |
| 44 | + |
| 45 | +# load config |
| 46 | +from multicast.env import load_config |
| 47 | +config = load_config() |
| 48 | + |
| 49 | +# you can now access configuration |
| 50 | +# e.g. |
| 51 | +print(config['groups']) # ['224.0.0.1', '224.0.0.2', '224.0.0.3'] |
| 52 | +``` |
| 53 | + |
| 54 | +_(changing the resulting config values only causes the setting to loose its special value, |
| 55 | +and does not actually update Multicast after import)_ |
| 56 | + |
| 57 | +## Configuration Details |
| 58 | + |
| 59 | +### Port Configuration |
| 60 | +- Valid range: 49152-65535 (dynamic/private ports) |
| 61 | +- Invalid values trigger warning and fall back to default |
| 62 | +- Example: |
| 63 | + ```python |
| 64 | + os.environ['MULTICAST_PORT'] = '50000' |
| 65 | + ``` |
| 66 | + |
| 67 | +### Group Configuration |
| 68 | +- Must be valid multicast addresses (224.0.0.0/4) |
| 69 | +- Invalid addresses trigger warning and fall back to default |
| 70 | +- Example: |
| 71 | + ```python |
| 72 | + os.environ['MULTICAST_GROUP'] = '224.0.0.2' |
| 73 | + ``` |
| 74 | + |
| 75 | +### TTL Configuration |
| 76 | +- Valid range: 1-126 (as per RFC-1112) |
| 77 | +- Also sets socket default timeout |
| 78 | +- Example: |
| 79 | + ```python |
| 80 | + os.environ['MULTICAST_TTL'] = '2' |
| 81 | + ``` |
| 82 | + |
| 83 | +### Buffer Size Configuration |
| 84 | +- Must be positive integer |
| 85 | +- Invalid values trigger warning and fall back to default |
| 86 | +- Example: |
| 87 | + ```python |
| 88 | + os.environ['MULTICAST_BUFFER_SIZE'] = '2048' |
| 89 | + ``` |
| 90 | + |
| 91 | +## Caveat |
| 92 | +- Environment variables must be set before importing the module |
| 93 | +- Invalid values trigger warnings but won't cause failures |
| 94 | +- Changes to configuration affect all subsequent operations |
| 95 | + |
| 96 | +## References |
| 97 | +- [RFC-1112: Host Extensions for IP Multicasting](https://datatracker.ietf.org/doc/html/rfc1112) |
| 98 | +- [RFC-6335: IANA Port Number Registry](https://datatracker.ietf.org/doc/html/rfc6335) |
0 commit comments