summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/broker/amqp/Incoming.cpp
blob: 3986818846300974f83975b3a01bd48b055d480f (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
/*
 *
 * 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 "Incoming.h"
#include "Exception.h"
#include "ManagedConnection.h"
#include "Message.h"
#include "Session.h"
#include "qpid/amqp/descriptors.h"
#include "qpid/broker/AsyncCompletion.h"
#include "qpid/broker/Message.h"
#include "qpid/broker/Broker.h"
#include "qpid/log/Statement.h"

namespace qpid {
namespace broker {
namespace amqp {
Incoming::Incoming(pn_link_t* l, Broker& broker, Session& parent, const std::string& source, const std::string& target, const std::string& name)
    : ManagedIncomingLink(broker, parent, source, target, name), credit(500), window(0), link(l), session(parent) {}


Incoming::~Incoming() {}
bool Incoming::doWork()
{
    uint32_t c = getCredit();
    bool issue = window < c;
    if (issue) {
        pn_link_flow(link, c - window);
        window = c;
    }
    return issue;
}
bool Incoming::haveWork()
{
    return window <= (getCredit()/2);
}

uint32_t Incoming::getCredit()
{
    return credit;//TODO: proper flow control
}

void Incoming::detached(bool /*closed*/)
{
}

void Incoming::wakeup()
{
    session.wakeup();
}

void Incoming::verify(const std::string& u, const std::string& r)
{
    userid.init(u, r);
}

Incoming::UserId::UserId() : inDefaultRealm(false) {}
void Incoming::UserId::init(const std::string& u, const std::string& defaultRealm)
{
    userid = u;
    size_t at = userid.find('@');
    if (at != std::string::npos) {
        unqualified = userid.substr(0, at);
        inDefaultRealm = defaultRealm == userid.substr(at+1);
    }
}
void Incoming::UserId::verify(const std::string& claimed)
{
    if(!userid.empty() && !claimed.empty() && userid != claimed && !(inDefaultRealm && claimed == unqualified)) {
        throw Exception(qpid::amqp::error_conditions::NOT_ALLOWED, QPID_MSG("Authenticated user id is " << userid << " but user id in message declared as " << claimed));
    }
}


namespace {
    class Transfer : public qpid::broker::AsyncCompletion::Callback
    {
      public:
        Transfer(pn_delivery_t* d, boost::shared_ptr<Session> s) : delivery(d), session(s) {}
        void completed(bool sync) { session->accepted(delivery, sync); }
        boost::intrusive_ptr<qpid::broker::AsyncCompletion::Callback> clone()
        {
            boost::intrusive_ptr<qpid::broker::AsyncCompletion::Callback> copy(new Transfer(delivery, session));
            return copy;
        }

      private:
        pn_delivery_t* delivery;
        boost::shared_ptr<Session> session;
    };
}

DecodingIncoming::DecodingIncoming(pn_link_t* link, Broker& broker, Session& parent, const std::string& source, const std::string& target, const std::string& name)
    : Incoming(link, broker, parent, source, target, name), sessionPtr(parent.shared_from_this()) {}
DecodingIncoming::~DecodingIncoming() {}

void DecodingIncoming::readable(pn_delivery_t* delivery)
{
    size_t pending = pn_delivery_pending(delivery);
    size_t offset = partial ? partial->getSize() : 0;
    boost::intrusive_ptr<Message> received(new Message(offset + pending));
    if (partial) {
        ::memcpy(received->getData(), partial->getData(), offset);
        partial = boost::intrusive_ptr<Message>();
    }
    assert(received->getSize() == pending + offset);
    pn_link_recv(link, received->getData() + offset, pending);

    if (pn_delivery_partial(delivery)) {
        QPID_LOG(debug, "Message incomplete: received " << pending << " bytes, now have " << received->getSize());
        partial = received;
    } else {
	incomingMessageReceived();
        if (offset) {
            QPID_LOG(debug, "Message complete: received " << pending << " bytes, " << received->getSize() << " in total");
        } else {
            QPID_LOG(debug, "Message received: " << received->getSize() << " bytes");
        }

        received->scan();
        pn_link_advance(link);
        received->setPublisher(&session.getParent());
        received->computeExpiration();
        --window;
        deliver(received, delivery);
    }
}

void DecodingIncoming::deliver(boost::intrusive_ptr<qpid::broker::amqp::Message> received, pn_delivery_t* delivery)
{
    qpid::broker::Message message(received, received);
    userid.verify(message.getUserId());
    received->begin();
    handle(message, session.getTransaction(delivery));
    Transfer t(delivery, sessionPtr);
    received->end(t);
}
}}} // namespace qpid::broker::amqp