summaryrefslogtreecommitdiff
path: root/widgets/JttyN1mmOutput.hpp
blob: dc64f7a8158ba49bae17f4aaf38957d6b30297c3 (plain)
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
// -*- Mode: C++ -*-
#ifndef JTTY_N1MM_OUTPUT_HPP
#define JTTY_N1MM_OUTPUT_HPP

#include <QSet>
#include <QtGlobal>

namespace Jtty
{
  class N1mmOutput
  {
  public:
    void submit (qint64 requestId)
    {
      requests_.insert (requestId);
      completionPending_ = true;
    }

    bool accept (qint64 requestId)
    {
      if (!requests_.contains (requestId)) return false;
      acceptedAudio_ = true;
      return true;
    }
    bool resolve (qint64 requestId) { return requests_.remove (requestId) != 0; }
    bool pending () const { return completionPending_; }
    bool finishRequested () const { return finishRequested_; }
    bool startRequested () const { return startRequested_; }
    void start () { finishRequested_ = false; startRequested_ = true; }
    void started () { startRequested_ = false; }
    void finish ()
    {
      if (completionPending_) finishRequested_ = true;
      if (requests_.isEmpty ()) startRequested_ = false;
    }
    void abort () { *this = N1mmOutput {}; }

    // An externally triggered abort (operator stop, watchdog) must resolve
    // outstanding requests and close the transaction so OUTPUTCOMPLETE can
    // still be reported. Explicit N1MM ABORT uses abort(), which discards it.
    void externalAbort ()
    {
      requests_.clear ();
      startRequested_ = false;
      if (completionPending_) finishRequested_ = true;
    }

    bool takeCompletion (bool audioActive, bool drained = false)
    {
      // Rejections can precede more TXTEXT in the same transaction. OFF
      // closes an empty transaction; accepted audio completes only at drain.
      if (!completionPending_ || !requests_.isEmpty () || audioActive
          || (!finishRequested_ && !(drained && acceptedAudio_))) return false;
      abort ();
      return true;
    }

  private:
    QSet<qint64> requests_;
    bool completionPending_ {false};
    bool finishRequested_ {false};
    bool startRequested_ {false};
    bool acceptedAudio_ {false};
  };
}

#endif