-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ability to negotiate a protocol version before commencing opera…
…tion
- Loading branch information
1 parent
71bf46e
commit 664e96c
Showing
13 changed files
with
272 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <algorithm> | ||
#include <boost/asio/error.hpp> | ||
#include <Osmosis/Chain/Remote/ProtocolVersionNegotiator.h> | ||
#include <Osmosis/TCPConnection.h> | ||
#include <Osmosis/Tongue.h> | ||
#include <Osmosis/Stream/AckOps.h> | ||
|
||
namespace Osmosis { | ||
namespace Chain { | ||
namespace Remote | ||
{ | ||
|
||
ProtocolVersionNegotiator::ProtocolVersionNegotiator( TCPConnection & connection, | ||
uint32_t currentProtocol ) : | ||
_connection( connection ), | ||
_currentProtocol( currentProtocol ) | ||
{} | ||
|
||
Tongue::SupportedProtocolVersions ProtocolVersionNegotiator::getSupportedVersionsInServer() { | ||
const struct Tongue::Header header = { static_cast< unsigned char >( | ||
Tongue::Opcode::GET_SUPPORTED_PROTOCOL_VERSIONS ) }; | ||
_connection.socket().sendAll( header ); | ||
struct Tongue::SupportedProtocolVersions response = { Tongue::FIRST_PROTOCOL_VERSION, | ||
Tongue::FIRST_PROTOCOL_VERSION }; | ||
try { | ||
response = _connection.socket().receiveAll< struct Tongue::SupportedProtocolVersions >(); | ||
} catch( boost::system::system_error &ex ) { | ||
if ( ex.code().value() == boost::asio::error::misc_errors::eof ) { | ||
// Nothing to do. The server does not support this op | ||
_connection.connect(); | ||
} else { | ||
TRACE_ERROR("An unknown error (code: " << ex.code() << ") has occurred while trying to get the " | ||
<< "list of supported protocols from the server."); | ||
throw ex; | ||
} | ||
} | ||
return response; | ||
} | ||
|
||
uint32_t ProtocolVersionNegotiator::getMaxCommonSupportedProtocolVersion() { | ||
Tongue::SupportedProtocolVersions serverVersions; | ||
serverVersions = getSupportedVersionsInServer(); | ||
const uint32_t maxCommonSupportedVersion = std::min( serverVersions.max, | ||
static_cast<uint32_t>(Tongue::MAX_SUPPORTED_PROTOCOL_VERSION) ); | ||
if ( maxCommonSupportedVersion < Tongue::MIN_SUPPORTED_PROTOCOL_VERSION or | ||
maxCommonSupportedVersion < serverVersions.min ) { | ||
return Tongue::FIRST_PROTOCOL_VERSION; | ||
} | ||
return maxCommonSupportedVersion; | ||
} | ||
|
||
uint32_t ProtocolVersionNegotiator::negotiate() { | ||
const uint32_t maxCommonSupportedVersion = getMaxCommonSupportedProtocolVersion(); | ||
if ( maxCommonSupportedVersion > _currentProtocol ) { | ||
ASSERT( maxCommonSupportedVersion > Tongue::FIRST_PROTOCOL_VERSION ); | ||
const struct Tongue::Header header = | ||
{ static_cast< unsigned char >( Tongue::Opcode::UPGRADE_PROTOCOL_VERSION ) }; | ||
_connection.socket().sendAllConcated( header, maxCommonSupportedVersion ); | ||
Stream::AckOps( _connection.socket() ).wait( "Protocol upgrade confirmation" ); | ||
return maxCommonSupportedVersion; | ||
} | ||
return _currentProtocol; | ||
} | ||
|
||
} // namespace Remote | ||
} // namespace Chain | ||
} // namespace Osmosis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#ifndef __OSMOSIS_PROTOCOL_NEGOTIATOR_H__ | ||
#define __OSMOSIS_PROTOCOL_NEGOTIATOR_H__ | ||
|
||
#include <cstdint> | ||
#include <boost/filesystem/path.hpp> | ||
#include "Osmosis/TCPConnection.h" | ||
#include "Osmosis/Hash.h" | ||
|
||
namespace Osmosis { | ||
namespace Chain { | ||
namespace Remote | ||
{ | ||
|
||
class ProtocolVersionNegotiator | ||
{ | ||
public: | ||
ProtocolVersionNegotiator( TCPConnection & connection, uint32_t currentProtocol ); | ||
|
||
uint32_t negotiate(); | ||
|
||
private: | ||
TCPConnection & _connection; | ||
uint32_t _currentProtocol; | ||
|
||
Tongue::SupportedProtocolVersions getSupportedVersionsInServer(); | ||
|
||
uint32_t getMaxCommonSupportedProtocolVersion(); | ||
|
||
ProtocolVersionNegotiator( const ProtocolVersionNegotiator & rhs ) = delete; | ||
ProtocolVersionNegotiator & operator= ( const ProtocolVersionNegotiator & rhs ) = delete; | ||
}; | ||
|
||
} // namespace Remote | ||
} // namespace Chain | ||
} // namespace Osmosis | ||
|
||
#endif // __OSMOSIS_PROTOCOL_NEGOTIATOR_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <Osmosis/Server/GetSupportedProtocolVersionsOp.h> | ||
#include <Osmosis/TCPSocket.h> | ||
#include <Osmosis/Tongue.h> | ||
#include "Common/Error.h" | ||
|
||
namespace Osmosis { | ||
namespace Server | ||
{ | ||
|
||
GetSupportedProtocolVersionsOp::GetSupportedProtocolVersionsOp( TCPSocket & socket ): | ||
_socket( socket ) | ||
{} | ||
|
||
void GetSupportedProtocolVersionsOp::go() | ||
{ | ||
BACKTRACE_BEGIN | ||
Tongue::SupportedProtocolVersions message = { Tongue::MIN_SUPPORTED_PROTOCOL_VERSION, | ||
Tongue::MAX_SUPPORTED_PROTOCOL_VERSION }; | ||
_socket.sendAll( message ); | ||
BACKTRACE_END | ||
} | ||
|
||
} // namespace Server | ||
} // namespace Osmosis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef OSMOSIS_SERVER_GET_SUPPORTED_PROTOCOL_VERSIONS_OP_H_ | ||
#define OSMOSIS_SERVER_GET_SUPPORTED_PROTOCOL_VERSIONS_OP_H_ | ||
|
||
#include <Osmosis/TCPSocket.h> | ||
#include <Osmosis/Tongue.h> | ||
|
||
namespace Osmosis { | ||
namespace Server | ||
{ | ||
|
||
class GetSupportedProtocolVersionsOp | ||
{ | ||
public: | ||
GetSupportedProtocolVersionsOp( TCPSocket & socket ); | ||
|
||
void go(); | ||
|
||
private: | ||
TCPSocket & _socket; | ||
|
||
GetSupportedProtocolVersionsOp( const GetSupportedProtocolVersionsOp & rhs ) = delete; | ||
GetSupportedProtocolVersionsOp & operator= ( const GetSupportedProtocolVersionsOp & rhs ) = delete; | ||
}; | ||
|
||
} // namespace Server | ||
} // namespace Osmosis | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <Osmosis/Server/UpgradeProtocolOp.h> | ||
#include <Osmosis/TCPSocket.h> | ||
#include <Osmosis/Tongue.h> | ||
#include <Osmosis/Debug.h> | ||
#include <Osmosis/Stream/AckOps.h> | ||
#include "Common/Error.h" | ||
|
||
namespace Osmosis { | ||
namespace Server | ||
{ | ||
|
||
UpgradeProtocolOp::UpgradeProtocolOp( TCPSocket & socket, uint32_t &protocolVersion ): | ||
_socket( socket ), | ||
_protocolVersion( protocolVersion ) | ||
{} | ||
|
||
void UpgradeProtocolOp::go() | ||
{ | ||
auto requestedProtocol = _socket.receiveAll< uint32_t >(); | ||
const auto minSupportedVersion = static_cast< unsigned >( Tongue::MIN_SUPPORTED_PROTOCOL_VERSION ); | ||
const auto maxSupportedVersion = static_cast< unsigned >( Tongue::MAX_SUPPORTED_PROTOCOL_VERSION ); | ||
if ( requestedProtocol < minSupportedVersion or requestedProtocol > maxSupportedVersion ) | ||
THROW( Error, "Cannot upgrade protocol to version " << requestedProtocol << "." | ||
" The server only supports versions from " << | ||
Tongue::MIN_SUPPORTED_PROTOCOL_VERSION << "to " << Tongue::MAX_SUPPORTED_PROTOCOL_VERSION ); | ||
_protocolVersion = requestedProtocol; | ||
Stream::AckOps( _socket ).sendAck(); | ||
} | ||
|
||
} // namespace Server | ||
} // namespace Osmosis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef OSMOSIS_SERVER_UPGRADE_PROTOCOL_OP_H_ | ||
#define OSMOSIS_SERVER_UPGRADE_PROTOCOL_OP_H_ | ||
|
||
#include <Osmosis/TCPSocket.h> | ||
#include <Osmosis/Tongue.h> | ||
|
||
namespace Osmosis { | ||
namespace Server | ||
{ | ||
|
||
class UpgradeProtocolOp | ||
{ | ||
public: | ||
UpgradeProtocolOp( TCPSocket & socket, uint32_t &protocolVersion ); | ||
|
||
void go(); | ||
|
||
private: | ||
TCPSocket & _socket; | ||
uint32_t & _protocolVersion; | ||
|
||
UpgradeProtocolOp( const UpgradeProtocolOp & rhs ) = delete; | ||
UpgradeProtocolOp & operator= ( const UpgradeProtocolOp & rhs ) = delete; | ||
}; | ||
|
||
} // namespace Server | ||
} // namespace Osmosis | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.