summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/messaging/amqp/SenderContext.cpp
blob: 5289fbdf9b07b2ab1375f2b93d58d79e45fc3e8f (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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */
#include "SenderContext.h"
#include "Transaction.h"
#include "EncodedMessage.h"
#include "PnData.h"
#include "util.h"
#include "qpid/messaging/AddressImpl.h"
#include "qpid/messaging/exceptions.h"
#include "qpid/Exception.h"
#include "qpid/amqp/descriptors.h"
#include "qpid/amqp/MapHandler.h"
#include "qpid/amqp/MessageEncoder.h"
#include "qpid/messaging/exceptions.h"
#include "qpid/messaging/Message.h"
#include "qpid/messaging/MessageImpl.h"
#include "qpid/log/Statement.h"
#include "config.h"
extern "C" {
#include <proton/engine.h>
}
#include <boost/shared_ptr.hpp>
#include <string.h>

namespace qpid {
namespace messaging {
namespace amqp {

//TODO: proper conversion to wide string for address
SenderContext::SenderContext(pn_session_t* session, const std::string& n,
                             const qpid::messaging::Address& a,
                             bool setToOnSend_,
                             const CoordinatorPtr& coord)
  : sender(pn_sender(session, n.c_str())),
    name(n),
    address(a),
    helper(address),
    nextId(0), capacity(50), unreliable(helper.isUnreliable()),
    setToOnSend(setToOnSend_),
    transaction(coord)
{}

SenderContext::~SenderContext()
{
    if (sender) pn_link_free(sender);
}

void SenderContext::close()
{
    if (sender) pn_link_close(sender);
}

void SenderContext::setCapacity(uint32_t c)
{
    if (c < deliveries.size()) throw qpid::messaging::SenderError("Desired capacity is less than unsettled message count!");
    capacity = c;
}

uint32_t SenderContext::getCapacity()
{
    return capacity;
}

uint32_t SenderContext::getUnsettled()
{
    return processUnsettled(true/*always allow retrieval of unsettled count, even if link has failed*/);
}

const std::string& SenderContext::getName() const
{
    return name;
}

const std::string& SenderContext::getTarget() const
{
    return address.getName();
}

bool SenderContext::send(const qpid::messaging::Message& message, SenderContext::Delivery** out)
{
    resend();//if there are any messages needing to be resent at the front of the queue, send them first
    if (processUnsettled(false) < capacity && pn_link_credit(sender)) {
        types::Variant state;
        if (transaction)
            state = transaction->getSendState();
        if (unreliable) {
            Delivery delivery(nextId++);
            delivery.encode(MessageImplAccess::get(message), address, setToOnSend);
            delivery.send(sender, unreliable, state);
            *out = 0;
            return true;
        } else {
            deliveries.push_back(Delivery(nextId++));
            try {
                Delivery& delivery = deliveries.back();
                delivery.encode(MessageImplAccess::get(message), address, setToOnSend);
                delivery.send(sender, unreliable, state);
                *out = &delivery;
                return true;
            } catch (const std::exception& e) {
                deliveries.pop_back();
                --nextId;
                throw SendError(e.what());
            }
        }
    } else {
        return false;
    }
}

void SenderContext::check()
{
    if (pn_link_state(sender) & PN_REMOTE_CLOSED && !(pn_link_state(sender) & PN_LOCAL_CLOSED)) {
        std::string text = get_error_string(pn_link_remote_condition(sender), "Link detached by peer");
        pn_link_close(sender);
        throw qpid::messaging::LinkError(text);
    }
}

uint32_t SenderContext::processUnsettled(bool silent)
{
    if (!silent) {
        check();
    }
    //remove messages from front of deque once peer has confirmed receipt
    while (!deliveries.empty() && deliveries.front().delivered() && !(pn_link_state(sender) & PN_REMOTE_CLOSED)) {
        deliveries.front().settle();
        deliveries.pop_front();
    }
    return deliveries.size();
}
namespace {
const std::string X_AMQP("x-amqp-");
const std::string X_AMQP_FIRST_ACQUIRER("x-amqp-first-acquirer");
const std::string X_AMQP_DELIVERY_COUNT("x-amqp-delivery-count");
const std::string X_AMQP_0_10_APP_ID("x-amqp-0-10.app-id");

class HeaderAdapter : public qpid::amqp::MessageEncoder::Header
{
  public:
    HeaderAdapter(const qpid::messaging::MessageImpl& impl) : msg(impl), headers(msg.getHeaders()) {}
    virtual bool isDurable() const
    {
        return msg.isDurable();
    }
    virtual uint8_t getPriority() const
    {
        return msg.getPriority();
    }
    virtual bool hasTtl() const
    {
        return msg.getTtl();
    }
    virtual uint32_t getTtl() const
    {
        return msg.getTtl();
    }
    virtual bool isFirstAcquirer() const
    {
        qpid::types::Variant::Map::const_iterator i = headers.find(X_AMQP_FIRST_ACQUIRER);
        if (i != headers.end()) {
            return i->second;
        } else {
            return false;
        }
    }
    virtual uint32_t getDeliveryCount() const
    {
        qpid::types::Variant::Map::const_iterator i = headers.find(X_AMQP_DELIVERY_COUNT);
        if (i != headers.end()) {
            return i->second;
        } else {
            return msg.isRedelivered() ? 1 : 0;
        }
    }
  private:
    const qpid::messaging::MessageImpl& msg;
    const qpid::types::Variant::Map& headers;
};
const std::string EMPTY;
const std::string FORWARD_SLASH("/");
const std::string X_AMQP_TO("x-amqp-to");
const std::string X_AMQP_CONTENT_ENCODING("x-amqp-content-encoding");
const std::string X_AMQP_CREATION_TIME("x-amqp-creation-time");
const std::string X_AMQP_ABSOLUTE_EXPIRY_TIME("x-amqp-absolute-expiry-time");
const std::string X_AMQP_GROUP_ID("x-amqp-group-id");
const std::string X_AMQP_GROUP_SEQUENCE("x-amqp-group-sequence");
const std::string X_AMQP_REPLY_TO_GROUP_ID("x-amqp-reply-to-group-id");
const std::string X_AMQP_MESSAGE_ANNOTATIONS("x-amqp-message-annotations");
const std::string X_AMQP_DELIVERY_ANNOTATIONS("x-amqp-delivery-annotations");

class PropertiesAdapter : public qpid::amqp::MessageEncoder::Properties
{
  public:
    PropertiesAdapter(const qpid::messaging::MessageImpl& impl, const std::string& s, const std::string& t) : msg(impl), headers(msg.getHeaders()), subject(s), to(t) {}
    bool hasMessageId() const
    {
        return getMessageId().size();
    }
    std::string getMessageId() const
    {
        return msg.getMessageId();
    }

    bool hasUserId() const
    {
        return getUserId().size();
    }

    std::string getUserId() const
    {
        return msg.getUserId();
    }

    bool hasTo() const
    {
        return hasHeader(X_AMQP_TO) || !to.empty();
    }

    std::string getTo() const
    {
        qpid::types::Variant::Map::const_iterator i = headers.find(X_AMQP_TO);
        if (i == headers.end()) return to;
        else return i->second;
    }

    bool hasSubject() const
    {
        return subject.size() || getSubject().size();
    }

    std::string getSubject() const
    {
        return subject.size() ? subject : msg.getSubject();
    }

    bool hasReplyTo() const
    {
        return msg.getReplyTo();
    }

    std::string getReplyTo() const
    {
        Address a = msg.getReplyTo();
        if (a.getSubject().size()) {
            return a.getName() + FORWARD_SLASH + a.getSubject();
        } else {
            return a.getName();
        }
    }

    bool hasCorrelationId() const
    {
        return getCorrelationId().size();
    }

    std::string getCorrelationId() const
    {
        return msg.getCorrelationId();
    }

    bool hasContentType() const
    {
        return getContentType().size();
    }

    std::string getContentType() const
    {
        return msg.getContentType();
    }

    bool hasContentEncoding() const
    {
        return hasHeader(X_AMQP_CONTENT_ENCODING);
    }

    std::string getContentEncoding() const
    {
        return headers.find(X_AMQP_CONTENT_ENCODING)->second;
    }

    bool hasAbsoluteExpiryTime() const
    {
        return hasHeader(X_AMQP_ABSOLUTE_EXPIRY_TIME);
    }

    int64_t getAbsoluteExpiryTime() const
    {
        return headers.find(X_AMQP_ABSOLUTE_EXPIRY_TIME)->second;
    }

    bool hasCreationTime() const
    {
        return hasHeader(X_AMQP_CREATION_TIME);
    }

    int64_t getCreationTime() const
    {
        return headers.find(X_AMQP_CREATION_TIME)->second;
    }

    bool hasGroupId() const
    {
        return hasHeader(X_AMQP_GROUP_ID);
    }

    std::string getGroupId() const
    {
        return headers.find(X_AMQP_GROUP_ID)->second;
    }

    bool hasGroupSequence() const
    {
        return hasHeader(X_AMQP_GROUP_SEQUENCE);
    }

    uint32_t getGroupSequence() const
    {
        return headers.find(X_AMQP_GROUP_SEQUENCE)->second;
    }

    bool hasReplyToGroupId() const
    {
        return hasHeader(X_AMQP_REPLY_TO_GROUP_ID);
    }

    std::string getReplyToGroupId() const
    {
        return headers.find(X_AMQP_REPLY_TO_GROUP_ID)->second;
    }
  private:
    const qpid::messaging::MessageImpl& msg;
    const qpid::types::Variant::Map& headers;
    const std::string subject;
    const std::string to;

    bool hasHeader(const std::string& key) const
    {
        return headers.find(key) != headers.end();
    }
};

bool startsWith(const std::string& input, const std::string& pattern)
{
    if (input.size() < pattern.size()) return false;
    for (std::string::const_iterator b = pattern.begin(), a = input.begin(); b != pattern.end(); ++b, ++a) {
        if (*a != *b) return false;
    }
    return true;
}
class ApplicationPropertiesAdapter : public qpid::amqp::MessageEncoder::ApplicationProperties
{
  public:
    ApplicationPropertiesAdapter(const qpid::types::Variant::Map& h) : headers(h) {}
    void handle(qpid::amqp::MapHandler& h) const
    {
        for (qpid::types::Variant::Map::const_iterator i = headers.begin(); i != headers.end(); ++i) {
            //strip out values with special keys as they are sent in standard fields
            if (!startsWith(i->first, X_AMQP) || i->first == X_AMQP_0_10_APP_ID) {
                qpid::amqp::CharSequence key(convert(i->first));
                switch (i->second.getType()) {
                  case qpid::types::VAR_VOID:
                    h.handleVoid(key);
                    break;
                  case qpid::types::VAR_BOOL:
                    h.handleBool(key, i->second);
                    break;
                  case qpid::types::VAR_UINT8:
                    h.handleUint8(key, i->second);
                    break;
                  case qpid::types::VAR_UINT16:
                    h.handleUint16(key, i->second);
                    break;
                  case qpid::types::VAR_UINT32:
                    h.handleUint32(key, i->second);
                    break;
                  case qpid::types::VAR_UINT64:
                    h.handleUint64(key, i->second);
                    break;
                  case qpid::types::VAR_INT8:
                    h.handleInt8(key, i->second);
                    break;
                  case qpid::types::VAR_INT16:
                    h.handleInt16(key, i->second);
                    break;
                  case qpid::types::VAR_INT32:
                    h.handleInt32(key, i->second);
                    break;
                  case qpid::types::VAR_INT64:
                    h.handleInt64(key, i->second);
                    break;
                  case qpid::types::VAR_FLOAT:
                    h.handleFloat(key, i->second);
                    break;
                  case qpid::types::VAR_DOUBLE:
                    h.handleDouble(key, i->second);
                    break;
                  case qpid::types::VAR_STRING:
                    h.handleString(key, convert(i->second), convert(i->second.getEncoding()));
                    break;
                  case qpid::types::VAR_UUID:
                    QPID_LOG(warning, "Skipping UUID  in application properties; not yet handled correctly.");
                    break;
                  case qpid::types::VAR_MAP:
                  case qpid::types::VAR_LIST:
                    QPID_LOG(warning, "Skipping nested list and map; not allowed in application properties.");
                    break;
                }
            }
        }
    }
  private:
    const qpid::types::Variant::Map& headers;

    static qpid::amqp::CharSequence convert(const std::string& in)
    {
        qpid::amqp::CharSequence out;
        out.data = in.data();
        out.size = in.size();
        return out;
    }
};

bool changedSubject(const qpid::messaging::MessageImpl& msg, const qpid::messaging::Address& address)
{
    return address.getSubject().size() && address.getSubject() != msg.getSubject();
}

}

SenderContext::Delivery::Delivery(int32_t i) : id(i), token(0), presettled(false) {}

void SenderContext::Delivery::reset()
{
    token = 0;
}

void SenderContext::Delivery::encode(const qpid::messaging::MessageImpl& msg, const qpid::messaging::Address& address, bool setToField)
{
    try {
        boost::shared_ptr<const EncodedMessage> original = msg.getEncoded();

        if (original && !changedSubject(msg, address)) { //still have the content as received, send at least the bare message unaltered
            //do we need to alter the header? are durable, priority, ttl, first-acquirer, delivery-count different from what was received?
            if (original->hasHeaderChanged(msg)) {
                //since as yet have no annotations, just write the revised header then the rest of the message as received
                encoded.resize(16/*max header size*/ + original->getBareMessage().size);
                qpid::amqp::MessageEncoder encoder(encoded.getData(), encoded.getSize());
                HeaderAdapter header(msg);
                encoder.writeHeader(header);
                ::memcpy(encoded.getData() + encoder.getPosition(), original->getBareMessage().data, original->getBareMessage().size);
            } else {
                //since as yet have no annotations, if the header hasn't
                //changed and we still have the original bare message, can
                //send the entire content as is
                encoded.resize(original->getSize());
                ::memcpy(encoded.getData(), original->getData(), original->getSize());
            }
        } else {
            HeaderAdapter header(msg);
            PropertiesAdapter properties(msg, address.getSubject(), setToField ? address.getName() : EMPTY);
            ApplicationPropertiesAdapter applicationProperties(msg.getHeaders());
            //compute size:
            size_t contentSize = qpid::amqp::MessageEncoder::getEncodedSize(header)
                + qpid::amqp::MessageEncoder::getEncodedSize(properties)
                + qpid::amqp::MessageEncoder::getEncodedSize(applicationProperties);
            if (msg.getContent().isVoid()) {
                contentSize += qpid::amqp::MessageEncoder::getEncodedSizeForContent(msg.getBytes());
            } else {
                contentSize += qpid::amqp::MessageEncoder::getEncodedSizeForValue(msg.getContent()) + 3/*descriptor*/;
            }
            encoded.resize(contentSize);
            QPID_LOG(debug, "Sending message, buffer is " << encoded.getSize() << " bytes")
                qpid::amqp::MessageEncoder encoder(encoded.getData(), encoded.getSize());
            //write header:
            encoder.writeHeader(header);
            //write delivery-annotations, write message-annotations (none yet supported)
            //write properties
            encoder.writeProperties(properties);
            //write application-properties
            encoder.writeApplicationProperties(applicationProperties);
            //write body
            if (!msg.getContent().isVoid()) {
                //write as AmqpValue
                encoder.writeValue(msg.getContent(), &qpid::amqp::message::AMQP_VALUE);
            } else if (msg.getBytes().size()) {
                encoder.writeBinary(msg.getBytes(), &qpid::amqp::message::DATA);//structured content not yet directly supported
            }
            if (encoder.getPosition() < encoded.getSize()) {
                QPID_LOG(debug, "Trimming buffer from " << encoded.getSize() << " to " << encoder.getPosition());
                encoded.trim(encoder.getPosition());
            }
            //write footer (no annotations yet supported)
        }
    } catch (const qpid::Exception& e) {
        throw SendError(e.what());
    }
}

void SenderContext::Delivery::send(pn_link_t* sender, bool unreliable, const types::Variant& state)
{
    pn_delivery_tag_t tag;
    tag.size = sizeof(id);
#ifdef NO_PROTON_DELIVERY_TAG_T
    tag.start = reinterpret_cast<const char*>(&id);
#else
    tag.bytes = reinterpret_cast<const char*>(&id);
#endif
    token = pn_delivery(sender, tag);
    if (!state.isVoid()) {      // Add transaction state
        PnData data(pn_disposition_data(pn_delivery_local(token)));
        data.put(state);
        pn_delivery_update(token, qpid::amqp::transaction::TRANSACTIONAL_STATE_CODE);
    }
    pn_link_send(sender, encoded.getData(), encoded.getSize());
    if (unreliable) {
        pn_delivery_settle(token);
        presettled = true;
    }
    pn_link_advance(sender);
}

bool SenderContext::Delivery::sent() const
{
    return presettled || token;
}
bool SenderContext::Delivery::delivered()
{
    if (presettled || (token && (pn_delivery_remote_state(token) || pn_delivery_settled(token)))) {
        //TODO: need a better means for signalling outcomes other than accepted
        if (rejected()) {
            QPID_LOG(warning, "delivery " << id << " was rejected by peer");
        } else if (!accepted()) {
            QPID_LOG(info, "delivery " << id << " was not accepted by peer");
        }
        return true;
    } else {
        return false;
    }
}
bool SenderContext::Delivery::accepted()
{
    return pn_delivery_remote_state(token) == PN_ACCEPTED;
}
bool SenderContext::Delivery::rejected()
{
    return pn_delivery_remote_state(token) == PN_REJECTED;
}

std::string SenderContext::Delivery::error()
{
    pn_condition_t *condition = pn_disposition_condition(pn_delivery_remote(token));
    return (condition && pn_condition_is_set(condition)) ?
        Msg() << get_error_string(condition, std::string(), std::string()) :
        std::string();
}

void SenderContext::Delivery::settle()
{
    pn_delivery_settle(token);
}
void SenderContext::verify()
{
    pn_terminus_t* target = pn_link_remote_target(sender);
    if (!pn_terminus_get_address(target)) {
        std::string msg("No such target : ");
        msg += getTarget();
        QPID_LOG(debug, msg);
        throw qpid::messaging::NotFound(msg);
    } else if (AddressImpl::isTemporary(address)) {
        address.setName(pn_terminus_get_address(target));
        QPID_LOG(debug, "Dynamic target name set to " << address.getName());
    }

    helper.checkAssertion(target, AddressHelper::FOR_SENDER);
}

void SenderContext::configure()
{
    if (sender) configure(pn_link_target(sender));
}

void SenderContext::configure(pn_terminus_t* target)
{
    helper.configure(sender, target, AddressHelper::FOR_SENDER);
    std::string option;
    if (helper.getLinkSource(option)) {
        pn_terminus_set_address(pn_link_source(sender), option.c_str());
    } else {
        pn_terminus_set_address(pn_link_source(sender), pn_terminus_get_address(pn_link_target(sender)));
    }
}

bool SenderContext::settled()
{
    return processUnsettled(false) == 0;
}

bool SenderContext::closed()
{
    return pn_link_state(sender) & PN_LOCAL_CLOSED;
}

Address SenderContext::getAddress() const
{
    return address;
}


void SenderContext::reset(pn_session_t* session)
{
    sender = session ? pn_sender(session, name.c_str()) : 0;
    if (sender) configure();
    for (Deliveries::iterator i = deliveries.begin(); i != deliveries.end(); ++i)
        i->reset();
}

void SenderContext::resend()
{
    for (Deliveries::iterator i = deliveries.begin(); i != deliveries.end() && pn_link_credit(sender) && !i->sent(); ++i) {
        i->send(sender, false/*only resend reliable transfers*/);
    }
}

}}} // namespace qpid::messaging::amqp