summaryrefslogtreecommitdiff
path: root/widgets/JttyMmttyHandoff.hpp
blob: 9969674cc7d6a2f17dab5dcda3f9b8c8ae9412b6 (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
68
69
70
71
72
73
74
75
76
77
78
// -*- Mode: C++ -*-
#ifndef JTTY_MMTTY_HANDOFF_HPP
#define JTTY_MMTTY_HANDOFF_HPP

#include <QVector>
#include <QtGlobal>

namespace Jtty
{
  class MmttyHandoff
  {
  public:
    enum class SubmitAction
    {
      Reject,
      Submit,
      StopThenSubmit
    };

    explicit MmttyHandoff (int timeoutMs = 10000)
      : timeoutMs_ {timeoutMs}
    {
    }

    int timeoutMs () const noexcept {return timeoutMs_;}
    bool active () const noexcept {return waitingForStop_;}
    bool empty () const noexcept {return requestIds_.isEmpty ();}
    QVector<qint64> const& requestIds () const noexcept {return requestIds_;}

    static SubmitAction planSubmission (bool valid, bool jttySessionActive,
                                        bool backendBusy) noexcept
    {
      if (!valid) return SubmitAction::Reject;
      if (!jttySessionActive && backendBusy) {
        return SubmitAction::StopThenSubmit;
      }
      return SubmitAction::Submit;
    }

    bool queue (qint64 requestId)
    {
      if (requestIds_.size () >= 64) return false;
      requestIds_.append (requestId);
      return true;
    }
    void waitForStop () noexcept {waitingForStop_ = true;}

    bool stopCompleted () noexcept
    {
      if (!waitingForStop_) return false;
      waitingForStop_ = false;
      return true;
    }

    QVector<qint64> expire ()
    {
      auto const ids = requestIds_;
      reset ();
      return ids;
    }

    void submitted (qint64 requestId) {requestIds_.removeOne (requestId);}
    void abort () {reset ();}

  private:
    void reset ()
    {
      requestIds_.clear ();
      waitingForStop_ = false;
    }

    int timeoutMs_;
    QVector<qint64> requestIds_;
    bool waitingForStop_ {false};
  };
}

#endif