-
Notifications
You must be signed in to change notification settings - Fork 4
/
bob.py
executable file
·136 lines (108 loc) · 3.75 KB
/
bob.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# vim: set fileencoding=UTF-8 filetype=python :
r"""
Bob
Responder peer for QKD stack.
AUTHORS:
- Thomas Loruenser (2013): initial version
"""
###############################################################################
# Copyright 2013, Thomas Loruenser <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
from __future__ import print_function
import sys
import socket
import struct
import argparse
from time import sleep
from multiprocessing import Process, Queue
from common import *
from ll import *
import numpy as np
### globals
set_name('Bob')
n = get_name()
### load staq modules
from ec_ldpc import *
from co_toeplitz import *
from pa_toeplitz import *
from ks_stats import *
### main
def parseargs():
""" Parse the commandline arguments
"""
parser = argparse.ArgumentParser(description=n)
parser.add_argument('-b', '--blocks',
type=int, default=0,
help='Number of blocks to read from file.')
parser.add_argument('-ec', '--err-corr',
type=int, default=1,
help='Select error correction.')
parser.add_argument('-co', '--confirm',
type=int, default=1,
help='Select confirmation.')
parser.add_argument('-pa', '--priv-amp',
type=int, default=1,
help='Select privacy amplification.')
parser.add_argument('-v', '--verbose', nargs='?',
type=int, default=0, const=1,
help='Verbosity of debug messages.')
return parser.parse_args()
if __name__ == '__main__':
# init
HOST = ''
PORT = SLAVE_PORT
args = parseargs()
set_debug_lvl(args.verbose)
debug("Arguments", args)
block_limit = args.blocks
# setup processing chain
modules = []
if args.err_corr == 0: modules.append(SqModule)
else: modules.append(SqEcLdpc)
if args.confirm == 0: modules.append(SqModule)
else: modules.append(SqCoToeplitz)
if args.priv_amp == 0: modules.append(SqModule)
else: modules.append(SqPaToeplitz)
modules.append(SqKsStats)
# accept connections
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # FIXME: why?
srv.bind((HOST, PORT))
srv.listen(5)
s = [srv.accept()[0]] # cntrl connection
for mod in modules: s.append(srv.accept()[0])
srv.close()
# queues and modules
q = [Queue(QUEUE_SIZE)]
m = []
for i in xrange(len(modules)):
q.append(Queue(QUEUE_SIZE))
m.append(modules[i](s[i+1], q[i], q[i+1], master=False))
m[i].set_debug_level(args.verbose)
m[0].set_first_module()
# start processes
for mod in m: mod.start()
debug("Everybody is connected and running.")
# open file
f = open(rk_fname(RK_ERROR_BOB), "rb")
# start main loop, TODO: change to exception exit
main_loop(q, s, m, f, block_limit)
# fade out
fadeout(q, s, m)
# exit
f.close()
sys.exit(0)