Skip to content

Make Joystream protocole_session compatible with streaming#7

Merged
bedeho merged 5 commits intoJoystreamClassic:developmentfrom
rllola:custom-piece-picker
Aug 14, 2017
Merged

Make Joystream protocole_session compatible with streaming#7
bedeho merged 5 commits intoJoystreamClassic:developmentfrom
rllola:custom-piece-picker

Conversation

@rllola
Copy link
Copy Markdown
Contributor

@rllola rllola commented Aug 4, 2017

Will pick next piece to buy by priority.

Copy link
Copy Markdown

@bedeho bedeho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you see, bigger changes may be needed, but only do the minor ones now, I created a new issue which can be tackled later, or if we start running into buggy performance.

Comment thread include/protocol_session/Session.hpp Outdated
void startDownloading(const Coin::Transaction & contractTx,
const PeerToStartDownloadInformationMap<ConnectionIdType> & peerToStartDownloadInformationMap);
const PeerToStartDownloadInformationMap<ConnectionIdType> & peerToStartDownloadInformationMap,
const std::function<int(const std::vector<detail::Piece<ConnectionIdType>>*)> & pickNextPieceMethod);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will help readability, and ease of future upgrades, to typedef the type for the piecePicker method, rather than having to write out the full type each time. Have a look in callback.hpp, it gives similar examples for other callbacks we have.

Comment thread include/protocol_session/Session.cpp Outdated

assert(_observing == nullptr && _buying != nullptr && _selling == nullptr);

if (pickNextPieceMethod != nullptr) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don´t need this test pickNextPieceMethod != nullptr, since the variable is not a pointer, but a reference. I am surprised the compiler did not, at the very least, warn or complain about this.


// We did not find anything
throw std::runtime_error("Unable to find any unassigned pieces.");
assert(this->_pickNextPieceMethod != nullptr);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is also not needed, see point above about referencs.


uint32_t i = this->_pickNextPieceMethod(&_pieces);

std::cout << "Hello next piece : " << i << std::endl;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned, lets drop all logging to std::cout. If anything, log to std::clog, but lets avoid even that for now, as we dont have a well planned logging infrastructure on the c++ side.

throw std::runtime_error("Unable to find any unassigned pieces.");
assert(this->_pickNextPieceMethod != nullptr);

uint32_t i = this->_pickNextPieceMethod(&_pieces);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you just return uint32_t, then there is not way for the method to tell you that no piece was available, which can certainly happen at times. You are better off introducing a new exception in this library, e.g. called NoPieceAvailableException, and allow the callback to throw it. Combine this with entirely dropping this routine (getNextUnassignedPiece), and just have the caller of this method directly call _pickNextPieceMethod, and catch if NoPieceAvailableException is thrown. Currently, it is catching std::runtime_error for the same purpose.

Comment thread include/protocol_session/Session.cpp Outdated
void Session<ConnectionIdType>::startDownloading(const Coin::Transaction & contractTx, const PeerToStartDownloadInformationMap<ConnectionIdType> & peerToStartDownloadInformationMap) {
void Session<ConnectionIdType>::startDownloading(const Coin::Transaction & contractTx,
const PeerToStartDownloadInformationMap<ConnectionIdType> & peerToStartDownloadInformationMap,
const std::function<int(const std::vector<detail::Piece<ConnectionIdType>>*)> & pickNextPieceMethod) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two problems with this callback type

  1. We are giving the user a raw pointer to your piece vector, which allows the user to perhaps change or corrupt the information.

  2. Even if the user could not change the information, e.g. if we passed a const *, then we are still telling the user about too much information which is not of relevance, just look at how much information is in the detail::Piece class, and also about pieces which are no longer relevant. The only thing the callback really needs to know is which piece indexes it is allowed to pick from each time.

  3. We are exposing the detail::Piecetype, which is supposed to be private, at least since its in the detail namespace.

I think a proper fix to these issues will require a larger rewrite of protocol_session, so for now, perhaps just fixing 1) is enough. I will add an issue about this on this repo. If you finish the other work, we can circle back to this later.

See #8

@bedeho bedeho merged commit 05a6149 into JoystreamClassic:development Aug 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants