ArnLib  4.0.x
Active Registry Network
ArnPipe.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnInc/ArnPipe.hpp"
33 #include "private/ArnPipe_p.hpp"
34 #include "ArnInc/Arn.hpp"
35 #include <QDebug>
36 
37 
38 ArnPipePrivate::ArnPipePrivate()
39 {
40  _useSendSeq = true;
41  _useCheckSeq = true;
42  _sendSeqNum = 0;
43  _checkSeqNum = -1; // Indicate first time (no history)
44 }
45 
46 
47 ArnPipePrivate::~ArnPipePrivate()
48 {
49 }
50 
51 
52 void ArnPipe::init()
53 {
54  setPipeMode();
55 }
56 
57 
58 ArnPipe::ArnPipe( QObject* parent)
59  : ArnItemB( *new ArnPipePrivate, parent)
60 {
61  init();
62 }
63 
64 
65 ArnPipe::ArnPipe( const QString& path, QObject* parent)
66  : ArnItemB( *new ArnPipePrivate, parent)
67 {
68  init();
69  open( path);
70 }
71 
72 
73 ArnPipe::ArnPipe( ArnPipePrivate& dd, QObject* parent)
74  : ArnItemB( dd, parent)
75 {
76 }
77 
78 
80 {
81 }
82 
83 
84 void ArnPipe::setValue( const QByteArray& value)
85 {
86  if (isOpen()) {
87  ArnLinkHandle handleData;
88  setupSeq( handleData);
89  ArnItemB::setValue( value, Arn::SameValue::Accept, handleData);
90  }
91  else {
92  errorLog( QString(tr("Assigning bytearray Pipe:")) + QString::fromUtf8( value.constData(), value.size()),
94  }
95 }
96 
97 
98 ArnPipe& ArnPipe::operator=( const QByteArray& value)
99 {
100  setValue( value);
101  return *this;
102 }
103 
104 
105 void ArnPipe::setValueOverwrite( const QByteArray& value, const ARN_RegExp& rx)
106 {
107  if (isOpen()) {
108  ArnLinkHandle handleData;
109  handleData.add( ArnLinkHandle::QueueFindRegexp, QVariant( rx));
110  ArnItemB::setValue( value, Arn::SameValue::Accept, handleData);
111  }
112  else {
113  errorLog( QString(tr("Assigning bytearray PipeOW:")) + QString::fromUtf8( value.constData(), value.size()),
115  }
116 }
117 
118 
119 void ArnPipe::setupSeq( ArnLinkHandle& handleData)
120 {
121  Q_D(ArnPipe);
122 
123  if (!d->_useSendSeq) return; // Sequence not used
124 
125  handleData.add( ArnLinkHandle::SeqNo, QVariant( d->_sendSeqNum));
126  d->_sendSeqNum = (d->_sendSeqNum + 1) % 1000;
127 }
128 
129 
130 bool ArnPipe::isSendSeq() const
131 {
132  Q_D(const ArnPipe);
133 
134  return d->_useSendSeq;
135 }
136 
137 
138 void ArnPipe::setSendSeq( bool useSeq)
139 {
140  Q_D(ArnPipe);
141 
142  d->_useSendSeq = useSeq;
143 }
144 
145 
147 {
148  Q_D(const ArnPipe);
149 
150  return d->_useCheckSeq;
151 }
152 
153 
154 void ArnPipe::setCheckSeq( bool useCheckSeq)
155 {
156  Q_D(ArnPipe);
157 
158  d->_useCheckSeq = useCheckSeq;
159 }
160 
161 
162 void ArnPipe::itemUpdated( const ArnLinkHandle& handleData, const QByteArray* value)
163 {
164  Q_D(ArnPipe);
165 
166  ArnItemB::itemUpdated( handleData, value);
167 
168  if (d->_useCheckSeq && handleData.has( ArnLinkHandle::SeqNo)) {
169  int seqNum = handleData.valueRef( ArnLinkHandle::SeqNo).toInt();
170  if (seqNum != d->_checkSeqNum) { // Sequence not matching
171  if (d->_checkSeqNum != -1) // If not initial, this is out of sequence
172  emit outOfSequence();
173  d->_checkSeqNum = seqNum; // Resync to this received SeqNum
174  }
175  d->_checkSeqNum = (d->_checkSeqNum + 1) % 1000;
176  }
177  if (value)
178  emit changed( *value);
179  else
180  emit changed( toByteArray());
181 }
void setValue(const QByteArray &value)
Assign a QByteArray to a Pipe
Definition: ArnPipe.cpp:84
void outOfSequence()
Signal emitted when the received sequence numbers are "out of sequence".
void setValue(const ArnBasicItem &other, int ignoreSame=Arn::SameValue::DefaultAction)
ArnBasicItem & setPipeMode()
Set general mode as Pipe for this Arn Data Object
void setSendSeq(bool useSendSeq)
Change usage of sending sequence numbers.
Definition: ArnPipe.cpp:138
Assigning same value generates an update of the Arn Data Object
Definition: Arn.hpp:64
#define ARN_RegExp
Definition: ArnCompat.hpp:70
QByteArray toByteArray(bool *isOk=arnNullptr) const
bool isCheckSeq() const
Returns true if checking received sequence numbers.
Definition: ArnPipe.cpp:146
void setValueOverwrite(const QByteArray &value, const ARN_RegExp &rx)
Assign a QByteArray to a Pipe by using Anti congest logic.
Definition: ArnPipe.cpp:105
bool open(const QString &path)
Open a handle to an Arn Data Object
Definition: ArnItemB.cpp:91
bool isOpen() const
State of the handle.
virtual ~ArnPipe()
Definition: ArnPipe.cpp:79
QString path(Arn::NameF nameF=Arn::NameF::EmptyOk) const
Path of the Arn Data Object
Base class handle for an Arn Data Object.
Definition: ArnItemB.hpp:59
ArnItem specialized as a pipe.
Definition: ArnPipe.hpp:64
void changed(const QByteArray &value)
Signal emitted when Pipe has received data.
ArnPipe & operator=(const QByteArray &value)
Definition: ArnPipe.cpp:98
ArnPipe(QObject *parent=arnNullptr)
Standard constructor of a closed handle.
Definition: ArnPipe.cpp:58
void setCheckSeq(bool useCheckSeq)
Change usage of checking received sequence numbers.
Definition: ArnPipe.cpp:154
bool isSendSeq() const
Returns true if sending sequence numbers.
Definition: ArnPipe.cpp:130