1
1
/* *
2
- * This file implements the types used in the Hycast protocol.
2
+ * This file implements the types used in the Bicast protocol.
3
3
*
4
- * @file: HycastProto .cpp
4
+ * @file: BicastProto .cpp
5
5
* @author: Steven R. Emmerson <[email protected] >
6
6
*
7
- * Copyright 2021 University Corporation for Atmospheric Research
7
+ * Copyright 2023 University Corporation for Atmospheric Research
8
8
*
9
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
10
* you may not use this file except in compliance with the License.
20
20
*/
21
21
#include " config.h"
22
22
23
+ #include " BicastProto.h"
24
+
23
25
#include " error.h"
24
- #include " FileUtil.h"
25
- #include " HycastProto.h"
26
26
#include " Xprt.h"
27
27
28
28
#include < chrono>
29
+ #include < cinttypes>
29
30
#include < climits>
30
31
#include < cstdio>
31
32
#include < cstring>
32
- #include < time.h >
33
- #include < inttypes.h >
33
+ #include < ctime >
34
+ #include < unordered_set >
34
35
#include < openssl/sha.h>
35
36
36
- namespace hycast {
37
+ namespace bicast {
38
+
39
+ PduId::PduId (Xprt& xprt) {
40
+ if (!xprt.read (value))
41
+ throw EOF_ERROR (" Couldn't read value" );
42
+ if (value > MAX_PDU_ID)
43
+ throw INVALID_ARGUMENT (" value=" + to_string ());
44
+ }
37
45
38
- bool FeedInfo::write (Xprt xprt) const {
39
- return mcastGroup.write (xprt) &&
40
- mcastSource.write (xprt) &&
41
- xprt.write (segSize);
46
+ bool PduId::write (Xprt& xprt) const {
47
+ return xprt.write (value);
42
48
}
43
49
44
- bool FeedInfo::read (Xprt xprt) {
45
- return mcastGroup.read (xprt) &&
46
- mcastSource.read (xprt) &&
47
- xprt.read (segSize);
50
+ bool PduId::read (Xprt& xprt) {
51
+ if (!xprt.read (value))
52
+ return false ;
53
+ if (value > MAX_PDU_ID)
54
+ throw INVALID_ARGUMENT (" value=" + to_string ());
55
+ return true ;
48
56
}
49
57
50
58
/* *
@@ -78,47 +86,33 @@ String ProdId::to_string() const noexcept {
78
86
return String (buf);
79
87
}
80
88
81
- std::string DataSegId::to_string (const bool withName) const
82
- {
89
+ bool ProdId::write (Xprt& xprt) const {
90
+ return xprt.write (id);
91
+ }
92
+
93
+ bool ProdId::read (Xprt& xprt) {
94
+ return xprt.read (id);
95
+ }
96
+
97
+ std::string DataSegId::to_string (const bool withName) const {
83
98
String string;
84
99
if (withName)
85
100
string += " DataSegId" ;
86
101
return string + " {prodId=" + prodId.to_string () + " , offset=" + std::to_string (offset) + " }" ;
87
102
}
88
103
89
- String Notice::to_string () const {
90
- switch (id) {
91
- case Id::DATA_SEG_ID:
92
- return dataSegId.to_string ();
93
- case Id::PROD_INDEX:
94
- return prodId.to_string ();
95
- case Id::GOOD_PEER_SRVR:
96
- return srvrAddr.to_string ();
97
- case Id::BAD_PEER_SRVR:
98
- return srvrAddr.to_string ();
99
- case Id::GOOD_PEER_SRVRS:
100
- return tracker.to_string ();
101
- case Id::BAD_PEER_SRVRS:
102
- return tracker.to_string ();
103
- default :
104
- return " <unset>" ;
105
- }
106
- }
107
-
108
- size_t Notice::hash () const noexcept {
109
- return (id == Id::PROD_INDEX)
110
- ? prodId.hash ()
111
- : (id == Id::DATA_SEG_ID)
112
- ? dataSegId.hash ()
113
- : 0 ;
104
+ bool DataSegId::write (Xprt& xprt) const {
105
+ auto success = prodId.write (xprt);
106
+ if (success)
107
+ success = xprt.write (offset);
108
+ return success;
114
109
}
115
110
116
- bool Notice::operator ==(const Notice& rhs) const noexcept {
117
- if (id != rhs.id ) return false ;
118
- if (id == Id::UNSET) return true ;
119
- return (id == Id::PROD_INDEX)
120
- ? prodId == rhs.prodId
121
- : dataSegId == rhs.dataSegId ;
111
+ bool DataSegId::read (Xprt& xprt) {
112
+ auto success = prodId.read (xprt);
113
+ if (success)
114
+ success = xprt.read (offset);
115
+ return success;
122
116
}
123
117
124
118
/* *****************************************************************************/
@@ -127,17 +121,16 @@ bool Notice::operator==(const Notice& rhs) const noexcept {
127
121
128
122
class ProdIdSet ::Impl
129
123
{
130
- using Set = std::unordered_set<ProdId>;
131
-
132
- Set prodIds;
124
+ private:
125
+ std::unordered_set<ProdId> prodIds;
133
126
134
127
public:
135
128
/* *
136
129
* Constructs.
137
130
* @param[in] n Initial capacity
138
131
*/
139
132
Impl (const size_t n)
140
- : prodIds{n}
133
+ : prodIds(n)
141
134
{}
142
135
143
136
/* *
@@ -163,7 +156,7 @@ class ProdIdSet::Impl
163
156
* @retval true Success
164
157
* @retval false Connection lost
165
158
*/
166
- bool write (Xprt xprt) const {
159
+ bool write (Xprt& xprt) const {
167
160
if (!xprt.write (static_cast <uint32_t >(prodIds.size ())))
168
161
return false ;
169
162
for (auto iter = prodIds.begin (), end = prodIds.end (); iter != end; ++iter)
@@ -178,7 +171,7 @@ class ProdIdSet::Impl
178
171
* @retval true Success
179
172
* @retval false Connection lost
180
173
*/
181
- bool read (Xprt xprt) {
174
+ bool read (Xprt& xprt) {
182
175
uint32_t size;
183
176
if (!xprt.read (size))
184
177
return false ;
@@ -223,15 +216,15 @@ class ProdIdSet::Impl
223
216
* Returns an iterator over the product identifiers.
224
217
* @return An iterator over the product identifiers
225
218
*/
226
- Set ::iterator begin () {
219
+ decltype (prodIds) ::iterator begin () {
227
220
return prodIds.begin ();
228
221
}
229
222
230
223
/* *
231
224
* Returns an iterator just outside the product identifiers.
232
225
* @return An iterator just outside the product identifiers
233
226
*/
234
- Set ::iterator end () {
227
+ decltype (prodIds) ::iterator end () {
235
228
return prodIds.end ();
236
229
}
237
230
@@ -255,11 +248,11 @@ void ProdIdSet::subtract(const ProdIdSet rhs) {
255
248
pImpl->subtract (*rhs.pImpl );
256
249
}
257
250
258
- bool ProdIdSet::write (Xprt xprt) const {
251
+ bool ProdIdSet::write (Xprt& xprt) const {
259
252
return pImpl->write (xprt);
260
253
}
261
254
262
- bool ProdIdSet::read (Xprt xprt) {
255
+ bool ProdIdSet::read (Xprt& xprt) {
263
256
return pImpl->read (xprt);
264
257
}
265
258
@@ -302,7 +295,12 @@ class ProdInfo::Impl
302
295
SysTimePoint creationTime; // /< When product was initially created
303
296
304
297
public:
305
- Impl () =default ;
298
+ Impl ()
299
+ : prodId()
300
+ , name()
301
+ , size(0 )
302
+ , creationTime()
303
+ {}
306
304
307
305
/* *
308
306
* Constructs.
@@ -370,7 +368,7 @@ class ProdInfo::Impl
370
368
* @retval true Success
371
369
* @retval false Lost connection
372
370
*/
373
- bool write (Xprt xprt) const {
371
+ bool write (Xprt& xprt) const {
374
372
// LOG_DEBUG("Writing product information to %s", xprt.to_string().data());
375
373
auto success = prodId.write (xprt);
376
374
if (success) {
@@ -396,7 +394,7 @@ class ProdInfo::Impl
396
394
* @retval true Success
397
395
* @retval false Lost connection
398
396
*/
399
- bool read (Xprt xprt) {
397
+ bool read (Xprt& xprt) {
400
398
// LOG_DEBUG("Reading product information from %s", xprt.to_string().data());
401
399
auto success = prodId.read (xprt);
402
400
if (success) {
@@ -429,13 +427,13 @@ class ProdInfo::Impl
429
427
ProdInfo::ProdInfo (const ProdId index,
430
428
const std::string& name,
431
429
const ProdSize size,
432
- const SysTimePoint& createTime)
430
+ const SysTimePoint createTime)
433
431
: pImpl{new Impl (index , name, size, createTime)}
434
432
{}
435
433
436
434
ProdInfo::ProdInfo (const std::string& name,
437
435
const ProdSize size,
438
- const SysTimePoint& createTime)
436
+ const SysTimePoint createTime)
439
437
: ProdInfo(ProdId{name}, name, size, createTime)
440
438
{}
441
439
@@ -472,47 +470,27 @@ String ProdInfo::to_string(const bool withName) const {
472
470
return pImpl ? pImpl->to_string (withName) : " <unset>" ;
473
471
}
474
472
475
- bool ProdInfo::write (Xprt xprt) const {
473
+ bool ProdInfo::write (Xprt& xprt) const {
476
474
return pImpl->write (xprt);
477
475
}
478
476
479
- bool ProdInfo::read (Xprt xprt) {
477
+ bool ProdInfo::read (Xprt& xprt) {
480
478
if (!pImpl)
481
479
pImpl = std::make_shared<Impl>();
482
480
return pImpl->read (xprt);
483
481
}
484
482
485
- /* *************************************************************************************************/
486
- // P2P server information
487
-
488
- String P2pSrvrInfo::to_string () const {
489
- return " {addr=" + srvrAddr.to_string () + " , tier=" + std::to_string (tier) +
490
- " , numAvail=" + std::to_string (numAvail) + " , valid=" + std::to_string (valid) + " }" ;
491
- }
492
-
493
- bool P2pSrvrInfo::write (Xprt xprt) const {
494
- return srvrAddr.write (xprt) &&
495
- xprt.write (tier) &&
496
- xprt.write (numAvail) &&
497
- xprt.write (valid);
498
- }
499
-
500
- bool P2pSrvrInfo::read (Xprt xprt) {
501
- return srvrAddr.read (xprt) &&
502
- xprt.read (tier) &&
503
- xprt.read (numAvail) &&
504
- xprt.read (valid);
505
- }
506
-
507
483
} // namespace
508
484
509
485
namespace std {
486
+ using namespace bicast ;
487
+
510
488
// / Returns the string representation of a data product's identifier
511
- string to_string (const hycast:: ProdId prodId) {
489
+ string to_string (const ProdId prodId) {
512
490
return prodId.to_string ();
513
491
}
514
492
// / Returns the string representation of information on a data product
515
- string to_string (const hycast:: ProdInfo prodInfo) {
493
+ string to_string (const ProdInfo prodInfo) {
516
494
return prodInfo.to_string ();
517
495
}
518
496
}
0 commit comments