Skip to content

Commit bd247e6

Browse files
authored
Merge pull request #29 from JoyStream/development
dev to master - v0.2.0
2 parents 9900179 + 8674713 commit bd247e6

23 files changed

+735
-526
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# ProtocolSession
33

4-
conan package name: `ProtocolSession/0.1.4@joystream/stable`
4+
conan package name: `ProtocolSession/0.2.0@joystream/stable`
55

66
### Dependencies
77

conan_package/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
class ProtocolSessionBase(ConanFile):
55
name = "ProtocolSession"
6-
version = "0.1.4"
6+
version = "0.2.0"
77
license = "(c) JoyStream Inc. 2016-2017"
88
url = "https://github.com/JoyStream/protocol_session-cpp.git"
99
repo_ssh_url = "git@github.com:JoyStream/protocol_session-cpp.git"
1010
repo_https_url = "https://github.com/JoyStream/protocol_session-cpp.git"
1111
settings = "os", "compiler", "build_type", "arch"
1212
generators = "cmake"
13-
requires = "ProtocolStateMachine/0.1.2@joystream/stable"
13+
requires = "ProtocolStateMachine/0.2.0@joystream/stable"
1414
build_policy = "missing"
1515

1616
def source(self):

sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(
1616
library_sources
1717
src/PieceInformation.cpp
1818
src/common.cpp
19+
src/PieceDeliveryPipeline.cpp
1920
)
2021

2122
# === build library ===

sources/include/protocol_session/Callbacks.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ enum class DisconnectCause {
5050

5151
seller_sent_invalid_piece,
5252

53+
seller_message_overflow,
54+
5355
//// selling
5456

5557
//buyer_invited_with_bad_terms,
@@ -58,7 +60,9 @@ enum class DisconnectCause {
5860

5961
buyer_interrupted_payment,
6062

61-
buyer_sent_invalid_payment
63+
buyer_sent_invalid_payment,
64+
65+
buyer_message_overflow
6266
};
6367

6468
// Removal of a connection from the session: c++11 alias declaration
@@ -85,8 +89,9 @@ typedef protocol_statemachine::Send SendMessageOnConnectionCallbacks;
8589
//// Buying
8690

8791
// Process arrival of a full piece, with given index over peer connection with given id
92+
// Return true if full piece was valid
8893
template <class ConnectionIdType>
89-
using FullPieceArrived = std::function<void(const ConnectionIdType &, const protocol_wire::PieceData &, int)>;
94+
using FullPieceArrived = std::function<bool(const ConnectionIdType &, const protocol_wire::PieceData &, int)>;
9095

9196
// Buyer with givne connection id send a valid payment
9297
template <class ConnectionIdType>
@@ -125,8 +130,9 @@ using ReceivedValidPayment = std::function<void(const ConnectionIdType &,
125130
uint64_t totalNumberOfPayments,
126131
uint64_t totalAmountPaid)>;
127132

133+
typedef std::function<void(void)> AllSellersGone;
134+
128135
}
129136
}
130137

131138
#endif // JOYSTREAM_PROTOCOLSESSION_CALLBACKS_HPP
132-

sources/include/protocol_session/SellerState.hpp

Lines changed: 0 additions & 33 deletions
This file was deleted.

sources/include/protocol_session/Session.cpp

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ namespace protocol_session {
165165
const FullPieceArrived<ConnectionIdType> & fullPieceArrived,
166166
const SentPayment<ConnectionIdType> & sentPayment,
167167
const protocol_wire::BuyerTerms & terms,
168-
const TorrentPieceInformation & information) {
168+
const TorrentPieceInformation & information,
169+
const AllSellersGone & allSellersGone) {
169170

170171
// Prepare for exiting current state
171172
switch(_mode) {
@@ -208,7 +209,8 @@ namespace protocol_session {
208209
fullPieceArrived,
209210
sentPayment,
210211
terms,
211-
information);
212+
information,
213+
allSellersGone);
212214
}
213215

214216
template <class ConnectionIdType>
@@ -431,6 +433,37 @@ namespace protocol_session {
431433
return true;
432434
}
433435

436+
template<class ConnectionIdType>
437+
void Session<ConnectionIdType>::disconnectSlowSellers(const std::chrono::duration<double> & limit) {
438+
switch(_mode) {
439+
440+
case SessionMode::not_set:
441+
442+
assert(_observing == nullptr && _buying == nullptr && _selling == nullptr);
443+
throw exception::SessionModeNotSetException();
444+
break;
445+
446+
case SessionMode::observing:
447+
448+
assert(_observing != nullptr && _buying == nullptr && _selling == nullptr);
449+
break;
450+
451+
case SessionMode::buying:
452+
453+
assert(_observing == nullptr && _buying != nullptr && _selling == nullptr);
454+
_buying->disconnectSlowSellers(limit);
455+
break;
456+
457+
case SessionMode::selling:
458+
459+
assert(_observing == nullptr && _buying == nullptr && _selling != nullptr);
460+
break;
461+
462+
default:
463+
assert(false);
464+
}
465+
}
466+
434467
template<class ConnectionIdType>
435468
status::Connection<ConnectionIdType> Session<ConnectionIdType>::connectionStatus(const ConnectionIdType & id) const noexcept {
436469

@@ -517,73 +550,6 @@ namespace protocol_session {
517550

518551
}
519552

520-
template<class ConnectionIdType>
521-
void Session<ConnectionIdType>::validPieceReceivedOnConnection(const ConnectionIdType & id, int index) {
522-
523-
switch(_mode) {
524-
525-
case SessionMode::not_set:
526-
527-
assert(_observing == nullptr && _buying == nullptr && _selling == nullptr);
528-
throw exception::SessionModeNotSetException();
529-
530-
case SessionMode::observing:
531-
532-
assert(_observing != nullptr && _buying == nullptr && _selling == nullptr);
533-
throw exception::ModeIncompatibleOperation();
534-
break;
535-
536-
case SessionMode::buying:
537-
538-
assert(_observing == nullptr && _buying != nullptr && _selling == nullptr);
539-
_buying->validPieceReceivedOnConnection(id, index);
540-
break;
541-
542-
case SessionMode::selling:
543-
544-
assert(_observing == nullptr && _buying == nullptr && _selling != nullptr);
545-
throw exception::ModeIncompatibleOperation();
546-
break;
547-
548-
default:
549-
assert(false);
550-
}
551-
552-
}
553-
554-
template<class ConnectionIdType>
555-
void Session<ConnectionIdType>::invalidPieceReceivedOnConnection(const ConnectionIdType & id, int index) {
556-
557-
switch(_mode) {
558-
559-
case SessionMode::not_set:
560-
561-
throw exception::SessionModeNotSetException();
562-
563-
case SessionMode::observing:
564-
565-
assert(_observing != nullptr && _buying == nullptr && _selling == nullptr);
566-
throw exception::ModeIncompatibleOperation();
567-
break;
568-
569-
case SessionMode::buying:
570-
571-
assert(_observing == nullptr && _buying != nullptr && _selling == nullptr);
572-
_buying->invalidPieceReceivedOnConnection(id, index);
573-
break;
574-
575-
case SessionMode::selling:
576-
577-
assert(_observing == nullptr && _buying == nullptr && _selling != nullptr);
578-
throw exception::ModeIncompatibleOperation();
579-
break;
580-
581-
default:
582-
assert(false);
583-
}
584-
585-
}
586-
587553
template<class ConnectionIdType>
588554
void Session<ConnectionIdType>::pieceDownloaded(int index) {
589555

@@ -689,7 +655,7 @@ namespace protocol_session {
689655
}
690656

691657
template<class ConnectionIdType>
692-
void Session<ConnectionIdType>::pieceLoaded(const ConnectionIdType & id, const protocol_wire::PieceData & data, int index) {
658+
void Session<ConnectionIdType>::pieceLoaded(const protocol_wire::PieceData & data, int index) {
693659

694660
switch(_mode) {
695661

@@ -712,7 +678,7 @@ namespace protocol_session {
712678
case SessionMode::selling:
713679

714680
assert(_observing == nullptr && _buying == nullptr && _selling != nullptr);
715-
_selling->pieceLoaded(id, data, index);
681+
_selling->pieceLoaded(data, index);
716682
break;
717683

718684
default:
@@ -915,6 +881,28 @@ namespace protocol_session {
915881
_buying->receivedFullPiece(id, data);
916882
}
917883

884+
template<class ConnectionIdType>
885+
void Session<ConnectionIdType>::remoteMessageOverflow(const ConnectionIdType & id) {
886+
887+
assert(hasConnection(id));
888+
889+
if (_buying != nullptr) {
890+
_buying->remoteMessageOverflow(id);
891+
} else if (_selling != nullptr) {
892+
_selling->remoteMessageOverflow(id);
893+
}
894+
}
895+
896+
template<class ConnectionIdType>
897+
void Session<ConnectionIdType>::localMessageOverflow(const ConnectionIdType & id) {
898+
// This callback will come from the connection state machine if we try to send too many payments
899+
// or as a seller, too many pieces.
900+
// This should not happen if our implementation is correct
901+
assert(hasConnection(id));
902+
std::clog << "Error: localMessageOverflow on connection " << id << std::endl;
903+
assert(false);
904+
}
905+
918906
template<class ConnectionIdType>
919907
detail::Connection<ConnectionIdType> * Session<ConnectionIdType>::createConnection(const ConnectionIdType & id, const SendMessageOnConnectionCallbacks & sendMessageCallbacks) {
920908

@@ -932,7 +920,9 @@ namespace protocol_session {
932920
[this, id](const Coin::Signature & s) { this->receivedInvalidPayment(id, s); },
933921
[this, id]() { this->sellerHasJoined(id); },
934922
[this, id]() { this->sellerHasInterruptedContract(id); },
935-
[this, id](const protocol_wire::PieceData & p) { this->receivedFullPiece(id, p); });
923+
[this, id](const protocol_wire::PieceData & p) { this->receivedFullPiece(id, p); },
924+
[this, id]() { this->remoteMessageOverflow(id); },
925+
[this, id]() { this->localMessageOverflow(id); });
936926
}
937927

938928
template <class ConnectionIdType>

sources/include/protocol_session/Session.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ namespace detail {
8686
const FullPieceArrived<ConnectionIdType> &,
8787
const SentPayment<ConnectionIdType> &,
8888
const protocol_wire::BuyerTerms &,
89-
const TorrentPieceInformation &);
89+
const TorrentPieceInformation &,
90+
const AllSellersGone &);
9091

9192
/**
9293
* Warning: Do not call any of these operations
@@ -119,6 +120,8 @@ namespace detail {
119120
// Remove connection if one exists with given id, otherwise returns false.
120121
bool removeConnection(const ConnectionIdType &);
121122

123+
void disconnectSlowSellers(const std::chrono::duration<double> & limit);
124+
122125
// *** TEMPORARY FIX ***
123126

124127
/**
@@ -154,12 +157,6 @@ namespace detail {
154157
const PeerToStartDownloadInformationMap<ConnectionIdType> & peerToStartDownloadInformationMap,
155158
const PickNextPieceMethod<ConnectionIdType> & pickNextPieceMethod);
156159

157-
// A valid piece was sent too us on given connection
158-
void validPieceReceivedOnConnection(const ConnectionIdType &, int index);
159-
160-
// An invalid piece was sent too us on given connection
161-
void invalidPieceReceivedOnConnection(const ConnectionIdType &, int index);
162-
163160
// Piece with given index has been downloaded, but not through
164161
// a regitered connection. Could be non-joystream peers, or something out of bounds.
165162
void pieceDownloaded(int);
@@ -190,7 +187,7 @@ namespace detail {
190187
const Coin::PubKeyHash & finalPkHash);
191188

192189
// Data for given piece has been loaded
193-
void pieceLoaded(const ConnectionIdType &, const protocol_wire::PieceData &, int);
190+
void pieceLoaded(const protocol_wire::PieceData &, int);
194191

195192
// Update terms when selling
196193
void updateTerms(const protocol_wire::SellerTerms &);
@@ -249,6 +246,8 @@ namespace detail {
249246
void sellerHasJoined(const ConnectionIdType &);
250247
void sellerHasInterruptedContract(const ConnectionIdType &);
251248
void receivedFullPiece(const ConnectionIdType &, const protocol_wire::PieceData &);
249+
void remoteMessageOverflow(const ConnectionIdType &);
250+
void localMessageOverflow(const ConnectionIdType &);
252251

253252
//// Utility routines
254253

0 commit comments

Comments
 (0)