blob: 2f4acab0c1164694831129e0934bf7b7b8d71087 [file] [log] [blame]
Juha Hyttinen0388dd92020-01-09 14:14:16 +02001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17==================================================================================
18*/
19
20package control
21
22import (
Juha Hyttinen0388dd92020-01-09 14:14:16 +020023 "strconv"
24 "sync"
25)
26
27//-----------------------------------------------------------------------------
28//
29//-----------------------------------------------------------------------------
30type TransactionXappKey struct {
31 RmrEndpoint
32 Xid string // xapp xid in req
33}
34
35func (key *TransactionXappKey) String() string {
36 return key.RmrEndpoint.String() + "/" + key.Xid
37}
38
39//-----------------------------------------------------------------------------
40//
41//-----------------------------------------------------------------------------
42type Transaction struct {
43 mutex sync.Mutex
44 tracker *Tracker // tracker instance
45 Subs *Subscription
46 RmrEndpoint RmrEndpoint
Juha Hyttinene406a342020-01-13 13:02:26 +020047 Mtype int
48 Xid string // xapp xid in req
49 OrigParams *RMRParams // request orginal params
Juha Hyttinen0388dd92020-01-09 14:14:16 +020050 RespReceived bool
51 ForwardRespToXapp bool
52}
53
54func (t *Transaction) String() string {
55 t.mutex.Lock()
56 defer t.mutex.Unlock()
57 var subId string = "?"
58 if t.Subs != nil {
59 subId = strconv.FormatUint(uint64(t.Subs.Seq), 10)
60 }
61 return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid
62}
63
Juha Hyttinene406a342020-01-13 13:02:26 +020064func (t *Transaction) GetXid() string {
65 t.mutex.Lock()
66 defer t.mutex.Unlock()
67 return t.Xid
68}
69
70func (t *Transaction) GetMtype() int {
71 t.mutex.Lock()
72 defer t.mutex.Unlock()
73 return t.Mtype
74}
75
76func (t *Transaction) GetSrc() string {
77 t.mutex.Lock()
78 defer t.mutex.Unlock()
79 return t.RmrEndpoint.String()
80}
81
Juha Hyttinen0388dd92020-01-09 14:14:16 +020082func (t *Transaction) CheckResponseReceived() bool {
83 t.mutex.Lock()
84 defer t.mutex.Unlock()
85 if t.RespReceived == false {
86 t.RespReceived = true
87 return false
88 }
89 return true
90}
91
92func (t *Transaction) RetryTransaction() {
93 t.mutex.Lock()
94 defer t.mutex.Unlock()
95 t.RespReceived = false
96}
97
98func (t *Transaction) Release() {
99 t.mutex.Lock()
100 defer t.mutex.Unlock()
101 if t.Subs != nil {
102 t.Subs.UnSetTransaction(t)
103 }
104 if t.tracker != nil {
105 xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid}
106 t.tracker.UnTrackTransaction(xappkey)
107 }
108 t.Subs = nil
109 t.tracker = nil
110}