shamblett / coap

A Coap package for dart
Other
16 stars 13 forks source link

CoAP Client: First attempt always get timed out and response received on second retransmit #10

Closed chetan87-kamble closed 3 years ago

chetan87-kamble commented 3 years ago

Hi @shamblett I am using your flutter example to working on my CoAP Client. When I send request every time, first attempt get timed out and response received on second retransmit. Is it right? Please guide me if I am doing wrong. Here is the code snippet and log out put:

import 'package:coap/coap.dart';
import 'package:flutter/material.dart';
import 'config/coap_config.dart';

void main() => runApp(MyApp());

/// make linter happy
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

/// make linter happy
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _data;
  bool _isLoading = false;

  final _config = CoapConfig();

  final host = 'coap.me';

  // Client
  CoapClient _client;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    _client = CoapClient(
        Uri(scheme: 'coap', host: host, port: _config.defaultPort), _config);
  }

  Future<void> _loadData() async {
    try {
      // Create the request for the get request
      final CoapRequest request = CoapRequest.newGet();

      request.addUriPath('hello');
      _client.request = request;

      setState(() => _isLoading = true);

      final CoapResponse response = await _client.get();
      setState(() {
        _isLoading = false;
        _data = response?.payloadString ?? 'No Data';
      });

    } catch (e) {
      print("Caught Error Doing COAP Stuff $e");
    }
  }

  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: const Text('Flutter CoAP'),
    ),
    body: Center(
      child: Builder(
        builder: (_) {
          if (_isLoading) {
            return const CircularProgressIndicator();
          }

          if (_data != null) {
            return Text('Response:\n $_data',
                textAlign: TextAlign.center,
                style: const TextStyle(fontSize: 18)
            );
          }
          return const Text('Press the button to load data');
        },
      ),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: _loadData,
      tooltip: 'Load',
      child: Icon(Icons.cloud_download),
    ),
  );
}

Log Output:

2020-12-10 11:04:17.810495+0530 Runner[432:56145] flutter: 2020-12-10 11:04:17.810: INFO: >> Reliability - Scheduling transmission for 
<<< Request Message >>>
Type: 0, Code: GET, Id: 6812, Token: 00000ca2, 
Options =
[
If-Match : 
Uri Host : Uri-Host: coap.me
E-tags : 
None
Uri Port : 5683
Location Paths: 
Uri Paths : hello
Content-Type : None
Max Age : None
Uri Queries : 
Accept : None
Location Queries : 
Proxy Uri : None
Proxy Scheme : None
Block 1 : None
Block 2 : None
Observe : -1
Size 1 : 0
Size 2 : 0
], 
Payload :
null
2020-12-10 11:04:17.810922+0530 Runner[432:56145] flutter: 2020-12-10 11:04:17.810: INFO: >> Reliability - sending request, failed transmission count: 0
2020-12-10 11:04:17.811139+0530 Runner[432:56145] flutter: 2020-12-10 11:04:17.811: INFO: >> Reliability - Retransmission timeout is 1393 ms
2020-12-10 11:04:17.811626+0530 Runner[432:56145] flutter: 2020-12-10 11:04:17.811: INFO: >> Matcher - Stored open request by KeyID[6812]) + KeyToken[00000ca2]
2020-12-10 11:04:17.812571+0530 Runner[432:56145] flutter: 2020-12-10 11:04:17.812: INFO: >> CoapNetworkUDP - binding to 0.0.0.0
2020-12-10 11:04:19.206718+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.206: WARNING: >> Reliability - Retransmission timeout elapsed
2020-12-10 11:04:19.207221+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.207: WARNING: >> Reliability - Timeout: retransmit message, failed count: 1 message: 6812
2020-12-10 11:04:19.207848+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.207: INFO: >> Reliability - Scheduling transmission for 
<<< Request Message >>>
Type: 0, Code: GET, Id: 6812, Token: 00000ca2, 
Options =
[
If-Match : 
Uri Host : Uri-Host: coap.me
E-tags : 
None
Uri Port : 5683
Location Paths: 
Uri Paths : hello
Content-Type : None
Max Age : None
Uri Queries : 
Accept : None
Location Queries : 
Proxy Uri : None
Proxy Scheme : None
Block 1 : None
Block 2 : None
Observe : -1
Size 1 : 0
Size 2 : 0
], 
Payload :
null
2020-12-10 11:04:19.208282+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.208: INFO: >> Reliability - sending request, failed transmission count: 0
2020-12-10 11:04:19.208480+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.208: INFO: >> Reliability - Retransmission timeout is 1291 ms
2020-12-10 11:04:19.208956+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.208: INFO: >> Matcher - Stored open request by KeyID[6812]) + KeyToken[00000ca2]
2020-12-10 11:04:19.455576+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.455: INFO: >> Matcher - received response 
<<< Response Message >>> 
Type: 2, Code: 2.05 Content, Id: 6812, Token: 00000ca2, 
Options =
[
If-Match : 
Uri Host : None
E-tags : 
None
Uri Port : None
Location Paths: 
Uri Paths : 
Content-Type : 0
Max Age : None
Uri Queries : 
Accept : -1
Location Queries : 
Proxy Uri : None
Proxy Scheme : None
Block 1 : None
Block 2 : None
Observe : -1
Size 1 : 0
Size 2 : 0
], 
Payload :
world
2020-12-10 11:04:19.455972+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.455: INFO: >> Matcher - cleaning up KeyID[6812])
2020-12-10 11:04:19.456568+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.456: INFO: >> Reliability - Cancel retransmission for token: 00000ca2 id: 6812
2020-12-10 11:04:19.457640+0530 Runner[432:56145] flutter: 2020-12-10 11:04:19.457: INFO: >> Matcher - Exchange completed: Cleaning up KeyToken[00000ca2]
shamblett commented 3 years ago

Your observation is correct, the first request times out as the Retransmission timeout is 1393 ms, this is probably due to the fact it has to bind on the first request, the second request Retransmission timeout is 1291 ms has been adjusted in light of this and this succeeds. maybe the first request should either raise the transmission timeout or not bother with it until bind has worked.

You are doing nothing wrong as such.

chetan87-kamble commented 3 years ago

Your observation is correct, the first request times out as the Retransmission timeout is 1393 ms, this is probably due to the fact it has to bind on the first request, the second request Retransmission timeout is 1291 ms has been adjusted in light of this and this succeeds. maybe the first request should either raise the transmission timeout or not bother with it until bind has worked.

You are doing nothing wrong as such.

@shamblett OK! Thank you for your prompt reply. It's cleared my doubt. I will adjust my transmission timeout as per my requirement. Thank you once again!