forked from jan-hoelscher-mss/safmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForwardThread.cpp
executable file
·225 lines (179 loc) · 6.19 KB
/
ForwardThread.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
Copyright 2004-2008 Matthew J. Battey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
This software implements a platform independent Store and Forward Message Queue.
*/
#pragma warning(disable:4996) // Disable warnings about deprecated functions
#include "main.h"
#include <iostream>
#include "ForwardThread.h"
#include "QManager.h"
#include "QAccessControl.h"
#include "QStorage.h"
#include "url/urldecode.h"
#include "lib/MessageQueue.h"
#include "lib/MQFactory.h"
#include "SystemConnection.h"
#include "SystemDelivery.h"
#include "Log.h"
using namespace std;
namespace safmq {
const char* EC_Decode(ErrorCode ec);
}
extern QManager* theQueueManager;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ForwardThread::ForwardThread()
{
}
ForwardThread::~ForwardThread()
{
}
void ForwardThread::stop()
{
Thread::stop();
quitsignal.Set();
}
void ForwardThread::SendError(safmq::QueueMessage& src, safmq::ErrorCode err)
{
std::string rq;
src.getResponseQueueName(rq);
if (rq.length()) {
SystemMessage msg;
SystemConnection::BuildErrorMsg(msg,err,src.getMessageID());
msg.setResponseQueueName(rq);
SystemDelivery::Enqueue(msg);
}
}
void* ForwardThread::run()
{
std::string allow_forwarding = pcfg->getParam(ENABLE_FOWARDING_PARAM,"no");
std::string cert = pcfg->getParam(SSL_CLIENT_CERT_PARAM,"");
std::string key = pcfg->getParam(SSL_CLIENT_KEY_PARAM,"");
tcpsocket::SSLContext ctx;
#if defined(SAFMQ_SSL)
tcpsocket::PrivateKey pkey;
tcpsocket::X509Certificate x509;
if (pkey.loadPEMFile(key) == tcpsocket::PrivateKey::noError) {
ctx.setPrivateKey(&pkey);
}
if (x509.loadPEMFile(cert) == tcpsocket::X509Certificate::noError) {
ctx.setX509Certificate(&x509);
}
#endif
if (stricmp(allow_forwarding.c_str(),"yes")==0 || allow_forwarding=="1") {
QStorage* forward;
forward = theQueueManager->GetQueue(FORWARD_QUEUE_NAME);
if (forward) {
while (!m_bStop) {{
// NOTE: double block here to reallocate the MSG every time.
QueueMessage msg;
QStorage::CursorHandle retrieveID;
ErrorCode err;
// TODO: set a maximum time to re-attempt delivery
// TODO: set a retry-timeout capability.
if ((err=forward->BeginRetrieve(true, msg, retrieveID)) == EC_NOERROR) {
forward->FinalizeRetrieve(retrieveID);
std::string urlLabel = msg.getLabel();
bool success = false;
try {
URL::urldecode url(urlLabel);
Log::getLog()->Info("Forwarding to %s", urlLabel.c_str());
MQConnection* con = MQFactory::BuildConnection(urlLabel,"","",&ctx);
MQConnection::QueueHandle que;
ErrorCode ec;
std::string qname = url.getResource();
if (qname.length() && qname[0] == '/')
qname.erase(0,1);
ec = con->OpenQueue(qname, que);
if (ec == EC_NOERROR) {
URL::urldecode::ParameterMultimap::const_iterator plabel = url.getParameters().find("label");
if (plabel != url.getParameters().end()) {
msg.setLabel(plabel->second);
} else {
msg.setLabel("");
}
ec = ((SystemConnection*)con)->EnqueueForward(que,msg);
}
switch (ec) {
case EC_NOERROR:
success = true;
break;
case EC_NETWORKERROR:
case EC_FILESYSTEMERROR:
Log::getLog()->Forward(Log::error, EC_Decode(ec), urlLabel.c_str());
success = false;
break;
case EC_DOESNOTEXIST: // Will get this error if the dest queue does not exits
case EC_NOTAUTHORIZED:
case EC_WRONGMESSAGETYPE:
case EC_NOTOPEN: // NOTE: Shouldn't ever receive this error.
case EC_FORWARDNOTALLOWED:
case EC_DUPLICATEMSGID:
// send an error and throw out the message
Log::getLog()->Forward(Log::error, EC_Decode(ec), urlLabel.c_str());
SendError(msg,ec);
success = true;
break;
//NOTE: Errors not produced by OpenQueue or EnqueueForward
//case EC_TIMEDOUT,
//case EC_ALREADYCLOSED:
//case EC_TTLEXPIRED:
//case EC_CURSORINVALIDATED:
//case EC_SERVERUNAVAIL:
//case EC_NOMOREMESSAGES:
//case EC_CANNOTCHANGEGROUP:
//case EC_CANNOTCHANGEUSER:
//case EC_ALREADYDEFINED:
//case EC_NOTIMPLEMENTED:
//case EC_QUEUEOPEN:
//case EC_NOTLOGGEDIN:
//case EC_INVALIDNAME:
}
delete con;
} catch (ErrorCode ec) {
Log::getLog()->Forward(Log::error, EC_Decode(ec), urlLabel.c_str());
switch (ec) {
// TODO: Other Error Codes thrown?
case EC_NOTAUTHORIZED:
case EC_UNSUPPORTED_PROTOCOL:
case EC_NOTIMPLEMENTED:
// Connected but not allowed, do not reattemp
SendError(msg,ec);
success = true;
break;
default:
success = false;
break;
}
} catch (std::exception& e) {
Log::getLog()->Forward(Log::error, e.what(), urlLabel.c_str());
success = false;
}
if (!success) {
// requeue the message
Log::getLog()->Info("Requeueing Message: %s", urlLabel.c_str());
msg.setLabel(urlLabel);
QStorage::CursorHandle enq;
if (forward->Enqueue(msg,enq) == EC_NOERROR)
forward->FinalizeEnqueue(enq);
}
} else {
forward->WaitFront(1);
}
}}
theQueueManager->ReleaseQueue(forward);
} else {
Log::getLog()->Forward(Log::error,"Forwarding Disabled, queue does not exist", FORWARD_QUEUE_NAME);
}
}
return NULL;
}