-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectioncontroller.cpp
More file actions
128 lines (113 loc) · 4.24 KB
/
connectioncontroller.cpp
File metadata and controls
128 lines (113 loc) · 4.24 KB
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
#include "connectioncontroller.h"
#include <iostream>
#include <QWaitCondition>
using namespace std;
ConnectionController::ConnectionController(UiController* uicontr) : uicontr(uicontr)
{
QuantumChannel* qChannel = new QuantumChannel();
OpenChannel* oChannel = new OpenChannel();
cout << "Successfully created 2 channels!" << endl;
alice = new Transmitter(qChannel, oChannel);
bob = new Receiver(qChannel, oChannel);
eve = new Intercepter(qChannel, oChannel);
eveActive = true;
cout << "Successfully created bob, alice and eve!" << endl;
}
bool ConnectionController::isEveActive()
{
return eveActive;
}
void ConnectionController::setEveActive(bool active)
{
eveActive = active;
}
bool ConnectionController::generateKey(int keyLength)
{
uicontr->clearLog();
uicontr->writeToLog("Beginnig to generate the key");
static const QUrl POLARIZATIONS[4] = {
QUrl("qrc:/qml/qc_main_from/Pictures/Polarizations/S-polariz.gif"),
QUrl("qrc:/qml/qc_main_from/Pictures/Polarizations/D-polariz.gif"),
QUrl("qrc:/qml/qc_main_from/Pictures/Polarizations/S+polariz.gif"),
QUrl("qrc:/qml/qc_main_from/Pictures/Polarizations/D+polariz.gif")
};
alice->clearKey(); bob->clearKey(); eve->clearKey();
for( int i=0; i<keyLength; ++i)
{
alice->sendQBit(alice->generateRandomQBit());
uicontr->refreshForm();
if (eveActive)
{
eve->interceptQBit();
uicontr->refreshForm();
}
bob->processQBit(bob->getQBit());
if (i % (keyLength/10) == 1)
{
uicontr->refreshForm();
uicontr->setProperty("alice_polarization", "source",
POLARIZATIONS[alice->getLastQBitInfo()].toString().toStdString().c_str());
if (eveActive)
uicontr->setProperty("eve_polarization", "source",
POLARIZATIONS[eve->getLastQBitInfo()].toString().toStdString().c_str());
uicontr->setProperty("bob_polarization", "source",
POLARIZATIONS[bob->getLastQBitInfo()].toString().toStdString().c_str());
Sleep(1000);
}
}
uicontr->writeToLog("Sending information about bob filtering");
bob->sendTypeInfo(keyLength);
if (eveActive)
eve->interceptTypeInfo(keyLength);
alice->getTypeInfo(keyLength);
uicontr->writeToLog("Sending information about correct bob quesses");
int count = alice->sendCorrectIndexes();
char* msg = (char*) malloc(sizeof(char)*500);
sprintf(msg, "bob quessed rigth %d out of %d QBits", count, keyLength);
uicontr->writeToLog(msg);
if (eveActive)
eve->interceptCorrectIndexes();
bob->getCorrectIndexes();
uicontr->writeToLog("Perform partial key checking");
int checkLength = keyLength/5;
int beginPos = random() % (keyLength - checkLength - 1);
string aliceChKey = alice->sendCheck(beginPos, checkLength);
sprintf(msg, "Alice's part of the key: %s", aliceChKey.c_str());
uicontr->writeToLog(msg);
if (eveActive)
eve->interceptCheck();
bool successfulGeneration;
string bobChKey = bob->getCheck(&successfulGeneration);
sprintf(msg, "Bob's part of the key: %s", bobChKey.c_str());
uicontr->writeToLog(msg);
alice->calcActiveKey();
bob->calcActiveKey();
eve->calcActiveKey();
if (successfulGeneration)
uicontr->writeToLog("Everything is fine!");
else
uicontr->writeToLog("Something went wrong");
string keyMsg = "Key: " + alice->getActiveKey();
uicontr->setProperty("alice_key", "text", keyMsg.c_str());
keyMsg = "Key: " + bob->getActiveKey();
uicontr->setProperty("bob_key", "text", keyMsg.c_str());
keyMsg = "Key: " + eve->getActiveKey();
uicontr->setProperty("eve_key", "text", keyMsg.c_str());
return successfulGeneration;
}
void ConnectionController::sendText(QString msg)
{
string eveText, bobText;
alice->sendText(msg.toStdString());
if (eveActive)
eveText = eve->interceptText();
bobText = bob->getText();
uicontr->setProperty("eve_text", "text", eveText.c_str());
uicontr->setProperty("bob_text", "text", bobText.c_str());
}
void Sleep(int ms)
{
QWaitCondition sleep;
QMutex mutex;
sleep.wait(&mutex, ms);
}