|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2025, The OTNS Authors. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are met: |
| 7 | +# 1. Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# 3. Neither the name of the copyright holder nor the |
| 13 | +# names of its contributors may be used to endorse or promote products |
| 14 | +# derived from this software without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | +# POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +# Case study on use of CoAP observe. |
| 29 | + |
| 30 | +from otns.cli import OTNS |
| 31 | +from otns.cli.errors import OTNSExitedError |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + ns = OTNS(otns_args=[]) |
| 36 | + ns.speed = 1000 |
| 37 | + |
| 38 | + # CoAP server |
| 39 | + srv = ns.add("router") |
| 40 | + ns.node_cmd(srv, "coap start") |
| 41 | + ns.node_cmd(srv, "coap resource test-resource") |
| 42 | + ns.node_cmd(srv, "coap set TestPayload_1") |
| 43 | + srv_addr = ns.get_ipaddrs(srv, 'mleid')[0] |
| 44 | + |
| 45 | + # CoAP client |
| 46 | + cl = ns.add("router") |
| 47 | + ns.node_cmd(cl, "coap start") |
| 48 | + |
| 49 | + # form the network |
| 50 | + ns.kpi_start() |
| 51 | + ns.go(10) |
| 52 | + |
| 53 | + # client sends observe request |
| 54 | + ns.node_cmd(cl, f"coap observe {srv_addr} test-resource") |
| 55 | + ns.go(90) |
| 56 | + |
| 57 | + # time passes, and resource changes |
| 58 | + ns.node_cmd(srv, "coap set TestPayload_2") |
| 59 | + ns.go(100) |
| 60 | + |
| 61 | + ns.node_cmd(srv, "coap set TestPayload_3") |
| 62 | + ns.go(100) |
| 63 | + |
| 64 | + ns.node_cmd(srv, "coap set TestPayload_4") |
| 65 | + ns.go(100) |
| 66 | + |
| 67 | + # now repeat the observe requests using CON type. This triggers CON notifications to be sent. |
| 68 | + ns.node_cmd(cl, "coap cancel") |
| 69 | + ns.go(100) |
| 70 | + ns.node_cmd(cl, f"coap observe {srv_addr} test-resource con") |
| 71 | + ns.go(100) |
| 72 | + ns.node_cmd(srv, "coap set TestPayload_5") |
| 73 | + ns.go(100) |
| 74 | + ns.node_cmd(srv, "coap set TestPayload_6") |
| 75 | + ns.go(100) |
| 76 | + ns.node_cmd(srv, "coap set TestPayload_7") |
| 77 | + ns.go(100) |
| 78 | + |
| 79 | + # send a cancel request |
| 80 | + ns.node_cmd(cl, f"coap cancel") |
| 81 | + ns.go(100) |
| 82 | + |
| 83 | + # server sends an interspersed CON notification after sending 5 NON notifications |
| 84 | + ns.node_cmd(cl, f"coap observe {srv_addr} test-resource") |
| 85 | + ns.go(100) |
| 86 | + ns.node_cmd(srv, "coap set TestPayload_8") |
| 87 | + ns.go(100) |
| 88 | + ns.node_cmd(srv, "coap set TestPayload_9") |
| 89 | + ns.go(100) |
| 90 | + ns.node_cmd(srv, "coap set TestPayload_a") |
| 91 | + ns.go(100) |
| 92 | + ns.node_cmd(srv, "coap set TestPayload_b") |
| 93 | + ns.go(100) |
| 94 | + ns.node_cmd(srv, "coap set TestPayload_c") |
| 95 | + ns.go(100) |
| 96 | + ns.node_cmd(srv, "coap set TestPayload_d") # this one will be sent CON |
| 97 | + ns.go(0.001) |
| 98 | + # this fast one will be NON. Note that the previous one isn't Ack'ed yet. |
| 99 | + ns.node_cmd(srv, "coap set TestPayload_d2") |
| 100 | + ns.go(100) |
| 101 | + ns.node_cmd(srv, "coap set TestPayload_e") |
| 102 | + ns.go(100) |
| 103 | + ns.node_cmd(srv, "coap set TestPayload_e") |
| 104 | + ns.go(100) |
| 105 | + |
| 106 | + # observe another resource (a non-existing one) |
| 107 | + ns.node_cmd(cl, f"coap observe {srv_addr} resnotfound") |
| 108 | + ns.go(10) |
| 109 | + |
| 110 | + ns.kpi_stop() |
| 111 | + ns.web_display() |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + try: |
| 115 | + main() |
| 116 | + except OTNSExitedError as ex: |
| 117 | + if ex.exit_code != 0: |
| 118 | + raise |
0 commit comments