Fabcoin Core  0.16.2
P2P Digital Currency
HostCapability.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #pragma once
25 
26 #include <memory>
27 #include "Peer.h"
28 #include "Common.h"
29 #include "Session.h"
30 
31 namespace dev
32 {
33 
34 namespace p2p
35 {
36 
38 {
39  friend class Host;
40  template <class T> friend class HostCapability;
41  friend class Capability;
42  friend class Session;
43 
44 public:
46  virtual ~HostCapabilityFace() {}
47 
48  Host* host() const { return m_host; }
49 
50  std::vector<std::pair<std::shared_ptr<SessionFace>, std::shared_ptr<Peer>>> peerSessions() const;
51  std::vector<std::pair<std::shared_ptr<SessionFace>, std::shared_ptr<Peer>>> peerSessions(u256 const& _version) const;
52 
53 protected:
54  virtual std::string name() const = 0;
55  virtual u256 version() const = 0;
56  CapDesc capDesc() const { return std::make_pair(name(), version()); }
57  virtual unsigned messageCount() const = 0;
58  virtual std::shared_ptr<Capability> newPeerCapability(std::shared_ptr<SessionFace> const& _s, unsigned _idOffset, CapDesc const& _cap, uint16_t _capID) = 0;
59 
60  virtual void onStarting() {}
61  virtual void onStopping() {}
62 
63 private:
64  Host* m_host = nullptr;
65 };
66 
67 template<class PeerCap>
69 {
70 public:
72  virtual ~HostCapability() {}
73 
74  static std::string staticName() { return PeerCap::name(); }
75  static u256 staticVersion() { return PeerCap::version(); }
76  static unsigned staticMessageCount() { return PeerCap::messageCount(); }
77 
78 protected:
79  virtual std::string name() const { return PeerCap::name(); }
80  virtual u256 version() const { return PeerCap::version(); }
81  virtual unsigned messageCount() const { return PeerCap::messageCount(); }
82 
83  virtual std::shared_ptr<Capability> newPeerCapability(std::shared_ptr<SessionFace> const& _s, unsigned _idOffset, CapDesc const& _cap, uint16_t _capID)
84  {
85  _s->registerFraming(_capID);
86  auto p = std::make_shared<PeerCap>(_s, this, _idOffset, _cap, _capID);
87  _s->registerCapability(_cap, p);
88  return p;
89  }
90 };
91 
92 }
93 
94 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::vector< std::pair< std::shared_ptr< SessionFace >, std::shared_ptr< Peer > > > peerSessions() const
virtual std::shared_ptr< Capability > newPeerCapability(std::shared_ptr< SessionFace > const &_s, unsigned _idOffset, CapDesc const &_cap, uint16_t _capID)
std::pair< std::string, u256 > CapDesc
Definition: Common.h:142
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition: Host.h:129
static u256 staticVersion()
virtual u256 version() const
virtual unsigned messageCount() const
void version()
Definition: main.cpp:53
const char * name
Definition: rest.cpp:36
virtual std::string name() const
static unsigned staticMessageCount()
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
virtual std::shared_ptr< Capability > newPeerCapability(std::shared_ptr< SessionFace > const &_s, unsigned _idOffset, CapDesc const &_cap, uint16_t _capID)=0
virtual unsigned messageCount() const =0
virtual std::string name() const =0
static std::string staticName()
virtual u256 version() const =0
The Session class.
Definition: Session.h:90