Skip to content

Commit bc27ae5

Browse files
author
Axel Dahlberg
committed
Added pack and unpack to EGPRequest
1 parent e7869da commit bc27ae5

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

qlinklayer/egp.py

+73-8
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,32 @@
1313
from qlinklayer.mhp import SimulatedNodeCentricMHPService
1414
from easysquid.toolbox import logger
1515
import random
16+
import bitstring
1617

1718

1819
class EGPRequest:
19-
def __init__(self, otherID, num_pairs, min_fidelity, max_time, purpose_id=0, priority=None, store=True,
20-
measure_directly=False):
20+
21+
package_format = 'uint:32=other_ip, ' \
22+
'uint:16=other_port, ' \
23+
'uint:16=purpose_id, ' \
24+
'float:32=create_time, ' \
25+
'float:32=min_fidelity, ' \
26+
'float:32=max_time, ' \
27+
'uint:16=create_id, ' \
28+
'uint:8=num_pairs, ' \
29+
'uint:4=priority', \
30+
'uint:1=store, ' \
31+
'uint:1=measure_directly'
32+
HDR_LENGTH = 24
33+
34+
def __init__(self, other_ip=0, other_port=0, num_pairs=0, min_fidelity=0, max_time=0,
35+
purpose_id=0, priority=0, store=True, measure_directly=False):
2136
"""
2237
Stores required parameters of Entanglement Generation Protocol Request
23-
:param otherID: int
24-
ID of the other node we are attempting to generate entanglement with
38+
:param other_ip: int
39+
IP of the other node we are attempting to generate entanglement with
40+
:param other_port: int
41+
Port number of other node.
2542
:param num_pairs: int
2643
The number of entangled pairs we are trying to generate
2744
:param min_fidelity: float
@@ -38,7 +55,9 @@ def __init__(self, otherID, num_pairs, min_fidelity, max_time, purpose_id=0, pri
3855
:param measure_directly: bool
3956
Specifies whether to measure the communication qubit directly after the photon is emitted
4057
"""
41-
self.otherID = otherID
58+
self.other_ip = other_ip
59+
self.other_port = other_port
60+
self.otherID = (self.other_ip, self.other_port)
4261
self.num_pairs = num_pairs
4362
self.min_fidelity = min_fidelity
4463
self.max_time = max_time
@@ -57,9 +76,9 @@ def __copy__(self):
5776
:return: obj `~qlinklayer.egp.EGPRequest`
5877
A copy of the EGPRequest object
5978
"""
60-
c = type(self)(otherID=self.otherID, num_pairs=self.num_pairs, min_fidelity=self.min_fidelity,
61-
max_time=self.max_time, purpose_id=self.purpose_id, priority=self.priority, store=self.store,
62-
measure_directly=self.measure_directly)
79+
c = type(self)(other_ip=self.other_ip, other_port=self.other_port, num_pairs=self.num_pairs,
80+
min_fidelity=self.min_fidelity, max_time=self.max_time, purpose_id=self.purpose_id,
81+
priority=self.priority, store=self.store, measure_directly=self.measure_directly)
6382
c.assign_create_id(self.create_id, self.create_time)
6483
return c
6584

@@ -75,6 +94,52 @@ def assign_create_id(self, create_id, create_time):
7594
def get_create_info(self):
7695
return self.create_id, self.create_time
7796

97+
def pack(self):
98+
"""
99+
Pack the data in packet form.
100+
:return: str
101+
"""
102+
if self.create_time is None:
103+
raise ValueError("Cannot pack if create_time is None")
104+
if self.create_id is None:
105+
raise ValueError("Cannot pack if create_id is None")
106+
to_pack = {"other_ip": self.other_ip,
107+
"other_port": self.other_port,
108+
"purpose_id": self.purpose_id,
109+
"create_time": self.create_time,
110+
"min_fidelity": self.min_fidelity,
111+
"max_time": self.max_time,
112+
"create_id": self.create_id,
113+
"num_pairs": self.num_pairs,
114+
"priority": self.priority,
115+
"store": self.store,
116+
"measure_directly": self.measure_directly}
117+
request_Bitstring = bitstring.pack(self.package_format, **to_pack)
118+
requestH = request_Bitstring.tobytes()
119+
120+
return requestH
121+
122+
def unpack(self, headerBytes):
123+
"""
124+
Unpack data.
125+
:param headerBytes: str
126+
:return:
127+
"""
128+
request_Bitstring = bitstring.BitString(headerBytes)
129+
request_fields = request_Bitstring.unpack(self.package_format)
130+
self.other_ip = request_fields[0]
131+
self.other_port = request_fields[1]
132+
self.otherID = (self.other_ip, self.other_port)
133+
self.purpose_id = request_fields[2]
134+
self.create_time = request_fields[3]
135+
self.min_fidelity = request_fields[4]
136+
self.max_time = request_fields[5]
137+
self.create_id = request_fields[6]
138+
self.num_pairs = request_fields[7]
139+
self.priority = request_fields[8]
140+
self.store = bool(request_fields[9])
141+
self.measure_directly = bool(request_fields[10])
142+
78143

79144
class EGP(EasyProtocol):
80145
def __init__(self, node, conn=None, err_callback=None, ok_callback=None):

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
flake8==3.5.0
1+
flake8>=3.5.0
2+
bitstring>=3.1.5

0 commit comments

Comments
 (0)