Skip to content

Commit

Permalink
SERVER-24080 Implement transport layer primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
samantharitter committed May 31, 2016
1 parent a3fcc35 commit 6e4141c
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 3 deletions.
87 changes: 87 additions & 0 deletions src/mongo/transport/session.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright (C) 2016 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#include "mongo/platform/basic.h"

#include "mongo/transport/session.h"

#include "mongo/platform/atomic_word.h"
#include "mongo/transport/transport_layer.h"

namespace mongo {
namespace transport {

namespace {

AtomicUInt64 sessionIdCounter(0);

} // namespace

Session::Session(HostAndPort remote, HostAndPort local, TransportLayer* tl)
: _id(sessionIdCounter.addAndFetch(1)), _remote(remote), _local(local), _tl(tl) {}

Session::~Session() {
if (_tl != nullptr) {
invariant(_tl);
_tl->end(*this);
}
}

Session::Session(Session&& other)
: _id(other._id),
_remote(std::move(other._remote)),
_local(std::move(other._local)),
_tl(other._tl) {
// We do not want to call tl->end() on moved-from Sessions.
other._tl = nullptr;
}

Session& Session::operator=(Session&& other) {
_id = other._id;
_remote = std::move(other._remote);
_local = std::move(other._local);
_tl = other._tl;
_tl = nullptr;

return *this;
}

Session::SessionId Session::id() const {
return _id;
}

const HostAndPort& Session::remote() const {
return _remote;
}

const HostAndPort& Session::local() const {
return _local;
}

} // namespace transport
} // namespace mongo
9 changes: 9 additions & 0 deletions src/mongo/transport/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#pragma once

#include "mongo/base/disallow_copying.h"
#include "mongo/util/net/hostandport.h"

namespace mongo {
Expand All @@ -40,6 +41,8 @@ class TransportLayer;
* (on the transport side) and Messages with Client objects (on the database side).
*/
class Session {
MONGO_DISALLOW_COPYING(Session);

public:
/**
* Type to indicate the internal id for this session.
Expand All @@ -56,6 +59,12 @@ class Session {
*/
~Session();

/**
* Move constructor and assignment operator.
*/
Session(Session&& other);
Session& operator=(Session&& other);

/**
* Return the id for this session.
*/
Expand Down
60 changes: 60 additions & 0 deletions src/mongo/transport/ticket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (C) 2016 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#include "mongo/platform/basic.h"

#include "mongo/transport/ticket.h"
#include "mongo/transport/ticket_impl.h"

namespace mongo {
namespace transport {

const Date_t Ticket::kNoExpirationDate{Date_t::max()};

Ticket::Ticket(std::unique_ptr<TicketImpl> ticket) : _ticket(std::move(ticket)) {}

Ticket::~Ticket() = default;

Ticket::Ticket(Ticket&&) = default;
Ticket& Ticket::operator=(Ticket&&) = default;

Session::SessionId Ticket::sessionId() const {
return _ticket->sessionId();
}

Date_t Ticket::expiration() const {
return _ticket->expiration();
}

// TODO should this actually be const?
TicketImpl* Ticket::impl() const {
return _ticket.get();
}

} // namespace transport
} // namespace mongo
16 changes: 16 additions & 0 deletions src/mongo/transport/ticket.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#pragma once

#include "mongo/base/disallow_copying.h"
#include "mongo/transport/session.h"
#include "mongo/util/time_support.h"

Expand All @@ -45,6 +46,8 @@ class Ticket {
MONGO_DISALLOW_COPYING(Ticket);

public:
friend class TransportLayer;

using SessionId = Session::SessionId;

/**
Expand All @@ -53,6 +56,13 @@ class Ticket {
static const Date_t kNoExpirationDate;

Ticket(std::unique_ptr<TicketImpl> ticket);
~Ticket();

/**
* Move constructor and assignment operator.
*/
Ticket(Ticket&&);
Ticket& operator=(Ticket&&);

/**
* Return this ticket's session id.
Expand All @@ -64,6 +74,12 @@ class Ticket {
*/
Date_t expiration() const;

protected:
/**
* Return a non-owning pointer to the underlying TicketImpl type
*/
TicketImpl* impl() const;

private:
std::unique_ptr<TicketImpl> _ticket;
};
Expand Down
5 changes: 4 additions & 1 deletion src/mongo/transport/ticket_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class TicketImpl {
public:
using SessionId = Session::SessionId;

virtual ~Ticket() = default;
virtual ~TicketImpl() = default;

TicketImpl(TicketImpl&&) = default;
TicketImpl& operator=(TicketImpl&&) = default;

/**
* Return this ticket's session id.
Expand Down
44 changes: 44 additions & 0 deletions src/mongo/transport/transport_layer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (C) 2016 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#include "mongo/platform/basic.h"

#include "mongo/transport/transport_layer.h"

namespace mongo {
namespace transport {

TransportLayer::TransportLayer() = default;
TransportLayer::~TransportLayer() = default;

TicketImpl* TransportLayer::getTicketImpl(const Ticket& ticket) {
return ticket.impl();
}

} // namespace transport
} // namespace mongo
11 changes: 9 additions & 2 deletions src/mongo/transport/transport_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
namespace mongo {
namespace transport {

class TicketImpl;

/**
* The TransportLayer moves Messages between transport::Endpoints and the database.
* This class owns an Acceptor that generates new endpoints from which it can
Expand All @@ -55,7 +57,7 @@ class TransportLayer {
MONGO_DISALLOW_COPYING(TransportLayer);

public:
virtual ~TransportLayer() = default;
virtual ~TransportLayer();

/**
* Source (receive) a new Message for this Session.
Expand Down Expand Up @@ -145,7 +147,12 @@ class TransportLayer {
virtual void shutdown() = 0;

protected:
TransportLayer() = default;
TransportLayer();

/**
* Return the implementation of this Ticket.
*/
TicketImpl* getTicketImpl(const Ticket& ticket);
};

} // namespace transport
Expand Down

0 comments on commit 6e4141c

Please sign in to comment.