aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/synchandler.cpp
blob: ae69523ed9da12fcd3c6ac4a899fb2a529621cb9 (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "remote_client_session.h"
#include "remote_connection.h"
#include "ybus.h"

using namespace NBus;
using namespace NBus::NPrivate;

/////////////////////////////////////////////////////////////////
/// Object that encapsulates all messgae data required for sending
/// a message synchronously and receiving a reply. It includes:
/// 1. ConditionVariable to wait on message reply
/// 2. Lock used by condition variable
/// 3. Message reply
/// 4. Reply status
struct TBusSyncMessageData {
    TCondVar ReplyEvent;
    TMutex ReplyLock;
    TBusMessage* Reply;
    EMessageStatus ReplyStatus;

    TBusSyncMessageData()
        : Reply(nullptr)
        , ReplyStatus(MESSAGE_DONT_ASK)
    {
    }
};

class TSyncHandler: public IBusClientHandler {
public:
    TSyncHandler(bool expectReply = true)
        : ExpectReply(expectReply)
        , Session(nullptr)
    {
    }
    ~TSyncHandler() override {
    }

    void OnReply(TAutoPtr<TBusMessage> pMessage0, TAutoPtr<TBusMessage> pReply0) override {
        TBusMessage* pMessage = pMessage0.Release();
        TBusMessage* pReply = pReply0.Release();

        if (!ExpectReply) { // Maybe need VERIFY, but it will be better to support backward compatibility here.
            return;
        }

        TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage->Data);
        SignalResult(data, pReply, MESSAGE_OK);
    }

    void OnError(TAutoPtr<TBusMessage> pMessage0, EMessageStatus status) override {
        TBusMessage* pMessage = pMessage0.Release();
        TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage->Data);
        if (!data) {
            return;
        }

        SignalResult(data, /*pReply=*/nullptr, status);
    }

    void OnMessageSent(TBusMessage* pMessage) override {
        Y_UNUSED(pMessage);
        Y_ASSERT(ExpectReply);
    }

    void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) override {
        Y_ASSERT(!ExpectReply);
        TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage.Release()->Data);
        SignalResult(data, /*pReply=*/nullptr, MESSAGE_OK);
    }

    void SetSession(TRemoteClientSession* session) {
        if (!ExpectReply) {
            Session = session;
        }
    }

private:
    void SignalResult(TBusSyncMessageData* data, TBusMessage* pReply, EMessageStatus status) const {
        Y_VERIFY(data, "Message data is set to NULL.");
        TGuard<TMutex> G(data->ReplyLock);
        data->Reply = pReply;
        data->ReplyStatus = status;
        data->ReplyEvent.Signal();
    }

private:
    // This is weird, because in regular client one-way-ness is selected per call, not per session.
    bool ExpectReply;
    TRemoteClientSession* Session;
};

namespace NBus {
    namespace NPrivate {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4250) //  'NBus::NPrivate::TRemoteClientSession' : inherits 'NBus::NPrivate::TBusSessionImpl::NBus::NPrivate::TBusSessionImpl::GetConfig' via dominance
#endif

        ///////////////////////////////////////////////////////////////////////////
        class TBusSyncSourceSessionImpl
           : private TSyncHandler
            // TODO: do not extend TRemoteClientSession
            ,
              public TRemoteClientSession {
        private:
            bool NeedReply;

        public:
            TBusSyncSourceSessionImpl(TBusMessageQueue* queue, TBusProtocol* proto, const TBusClientSessionConfig& config, bool needReply, const TString& name)
                : TSyncHandler(needReply)
                , TRemoteClientSession(queue, proto, this, config, name)
                , NeedReply(needReply)
            {
                SetSession(this);
            }

            TBusMessage* SendSyncMessage(TBusMessage* pMessage, EMessageStatus& status, const TNetAddr* addr = nullptr) {
                Y_VERIFY(!Queue->GetExecutor()->IsInExecutorThread(),
                         "SendSyncMessage must not be called from executor thread");

                TBusMessage* reply = nullptr;
                THolder<TBusSyncMessageData> data(new TBusSyncMessageData());

                pMessage->Data = data.Get();

                {
                    TGuard<TMutex> G(data->ReplyLock);
                    if (NeedReply) {
                        status = SendMessage(pMessage, addr, false); // probably should be true
                    } else {
                        status = SendMessageOneWay(pMessage, addr);
                    }

                    if (status == MESSAGE_OK) {
                        data->ReplyEvent.Wait(data->ReplyLock);
                        TBusSyncMessageData* rdata = static_cast<TBusSyncMessageData*>(pMessage->Data);
                        Y_VERIFY(rdata == data.Get(), "Message data pointer should not be modified.");
                        reply = rdata->Reply;
                        status = rdata->ReplyStatus;
                    }
                }

                // deletion of message and reply is a job of application.
                pMessage->Data = nullptr;

                return reply;
            }
        };

#ifdef _MSC_VER
#pragma warning(pop)
#endif
    }
}

TBusSyncSourceSession::TBusSyncSourceSession(TIntrusivePtr< ::NBus::NPrivate::TBusSyncSourceSessionImpl> session)
    : Session(session)
{
}

TBusSyncSourceSession::~TBusSyncSourceSession() {
    Shutdown();
}

void TBusSyncSourceSession::Shutdown() {
    Session->Shutdown();
}

TBusMessage* TBusSyncSourceSession::SendSyncMessage(TBusMessage* pMessage, EMessageStatus& status, const TNetAddr* addr) {
    return Session->SendSyncMessage(pMessage, status, addr);
}

int TBusSyncSourceSession::RegisterService(const char* hostname, TBusKey start, TBusKey end, EIpVersion ipVersion) {
    return Session->RegisterService(hostname, start, end, ipVersion);
}

int TBusSyncSourceSession::GetInFlight() {
    return Session->GetInFlight();
}

const TBusProtocol* TBusSyncSourceSession::GetProto() const {
    return Session->GetProto();
}

const TBusClientSession* TBusSyncSourceSession::GetBusClientSessionWorkaroundDoNotUse() const {
    return Session.Get();
}

TBusSyncClientSessionPtr TBusMessageQueue::CreateSyncSource(TBusProtocol* proto, const TBusClientSessionConfig& config, bool needReply, const TString& name) { 
    TIntrusivePtr<TBusSyncSourceSessionImpl> session = new TBusSyncSourceSessionImpl(this, proto, config, needReply, name);
    Add(session.Get());
    return new TBusSyncSourceSession(session);
}

void TBusMessageQueue::Destroy(TBusSyncClientSessionPtr session) {
    Destroy(session->Session.Get());
    Y_UNUSED(session->Session.Release());
}