Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qnetworker.cc
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------
2 * Q2NS - Quantum Network Simulator
3 * Copyright (c) 2026 quantuminternet.it
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *---------------------------------------------------------------------------*/
9/**
10 * @file q2ns-qnetworker.cc
11 * @brief Defines q2ns::QNetworker.
12 */
13
14#include "q2ns-qnetworker.h"
15
16#include "ns3/q2ns-qnet-device.h"
17#include "ns3/q2ns-qnode.h"
18#include "ns3/q2ns-qubit.h"
19
20#include "ns3/channel.h"
21#include "ns3/log.h"
22
23#include <cstdint>
24
25namespace q2ns {
26
27NS_LOG_COMPONENT_DEFINE("QNetworker");
28
29
30
31QNetworker::QNetworker(QNode& owner) : owner_(owner) {}
32
33
34
35uint32_t QNetworker::AddInterface(ns3::Ptr<ns3::NetDevice> dev) {
36
37 ns3::Ptr<QNetDevice> qdev = DynamicCast<QNetDevice>(dev);
38 NS_ABORT_MSG_IF(qdev == nullptr, "QNetworker requires QNetDevice interfaces");
39 m_ifaces.push_back(qdev);
40
41 return static_cast<uint32_t>(m_ifaces.size() - 1);
42}
43
44
45
46void QNetworker::AddRoute(uint32_t dstNodeId, uint32_t oif) {
47 m_hostRoutes[dstNodeId] = oif;
48}
49
50
51
52bool QNetworker::Send(std::shared_ptr<Qubit> q, uint32_t dstNodeId) {
53 if (!q) {
54 NS_LOG_WARN("Send rejected: null qubit.");
55 return false;
56 }
57
58 const auto loc = q->GetLocation();
59 if (loc.type == LocationType::Lost) {
60 NS_LOG_WARN("Send rejected: qubit " << q->GetQubitId() << " is lost.");
61 return false;
62 }
63
64 if (!(loc.type == LocationType::Node && loc.ownerId == owner_.GetId())) {
65 NS_LOG_WARN("Send rejected: qubit " << q->GetQubitId() << " is not local to this node.");
66 return false;
67 }
68
69 const auto itRoutes = m_hostRoutes.find(dstNodeId);
70 if (itRoutes == m_hostRoutes.end()) {
71 NS_LOG_WARN("Send rejected: no route to destination node " << dstNodeId << ".");
72 return false;
73 }
74
75 const uint32_t oif = itRoutes->second;
76 if (oif >= m_ifaces.size()) {
77 NS_LOG_WARN("Send rejected: route references invalid interface index.");
78 return false;
79 }
80
81 auto dev = m_ifaces[oif];
82 if (!dev) {
83 NS_LOG_WARN("Send rejected: outgoing interface has no device.");
84 return false;
85 }
86
87 if (!dev->GetChannel()) {
88 NS_LOG_WARN("Send rejected: outgoing device is not attached to a channel.");
89 return false;
90 }
91
92 q->SetLocationChannel(dev->GetChannel()->GetId());
93 return dev->Send(std::move(q));
94}
95
96
97
98void QNetworker::ReceiveFromDevice(std::shared_ptr<Qubit> q, const QMapInstance& map) {
100
101 // Apply the sampled channel map only after the qubit has become local to the
102 // destination node. This gives the map access to the destination-side node
103 // context and ensures any resulting loss is visible before callback delivery.
104 if (map) {
105 map(owner_, q);
106 }
107
108 if (!q) {
109 NS_LOG_WARN("ReceiveFromDevice terminated early with no adopted qubit: QMapInstance appears to "
110 "have caused "
111 "the qubit pointer to become null.");
112 return;
113 }
114
115 const auto loc = q->GetLocation();
116 if (loc.type != LocationType::Lost && !recvCallback_.IsNull()) {
117 recvCallback_(q);
118 }
119}
120
121
122
124 recvCallback_ = std::move(cb);
125}
126
127} // namespace q2ns
void AddRoute(uint32_t dstNodeId, uint32_t oif)
Add or replace a host route for a destination node.
void SetRecvCallback(RecvCallback cb)
Set the application-level receive callback.
std::vector< ns3::Ptr< QNetDevice > > m_ifaces
Outgoing interfaces by index.
bool Send(std::shared_ptr< Qubit > q, uint32_t dstNodeId)
Send a local qubit toward a destination node.
QNode & owner_
Owning node.
RecvCallback recvCallback_
Application-level receive callback.
uint32_t AddInterface(ns3::Ptr< ns3::NetDevice > dev)
Register a quantum device as an outgoing interface. Analogue of Ipv4L3Protocol::AddInterface(dev).
void ReceiveFromDevice(std::shared_ptr< Qubit > q, const QMapInstance &map)
Handle a qubit delivered from a channel.
std::unordered_map< uint32_t, uint32_t > m_hostRoutes
Host routes: dst node id to oif.
QNetworker(QNode &owner_)
Construct a networker bound to an owning node.
Main user-facing per-node API for quantum operations and transmission.
Definition q2ns-qnode.h:63
void AdoptQubit(const std::shared_ptr< Qubit > &q)
Mark a qubit as local to this node.
ns3::Callback< void, std::shared_ptr< Qubit > > RecvCallback
Callback invoked when a qubit is successfully received at a node.
Definition q2ns-types.h:82
std::function< void(QNode &, std::shared_ptr< Qubit > &)> QMapInstance
Per-transmission quantum map callable applied to a received qubit.
Definition q2ns-types.h:71
@ Node
Qubit is local to a node identified by node id.
@ Lost
Qubit was lost and is no longer accessible.
Declares q2ns::QNetworker, an internal per-node networking component for quantum transmission and rec...