Skip to content

Commit ef324b3

Browse files
committed
bug-fix: introduced an infinite loop of message sending in 2-node-long circuits
1 parent fd224c7 commit ef324b3

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

src/LoRaCommunication/TorCell.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum CellCommand {
2323

2424
enum RelayCellCommand {
2525
DATA,
26+
DATA_ACK,
2627
BEGIN,
2728
END,
2829
TEARDOWN,

src/LoRaCommunication/circuit.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,15 @@ static void forwardCreated(Circuit* circuit, ControlCell cell) {
276276

277277
//Sends data towards the other end of the circuit. If the data is larger than
278278
//the maximum payload size, it will be split into multiple cells and sent sequentially.
279-
static void sendData(Circuit* circuit, uint16_t nextNodeID, uint16_t circID, uint8_t* data, size_t length, bool direction) {
279+
static void sendData(Circuit* circuit, uint16_t nextNodeID, uint16_t circID, uint8_t* data,
280+
size_t length, bool direction, RelayCellCommand command = DATA) {
280281
uint16_t maxPayload = CELL_SIZE - RELAY_HEADER_SIZE - HASH_SIZE;
281282
uint16_t offset = 0; //keep track of how much of the data has been sent
282-
printf("after entry check\n");
283283
while (offset < length) {
284-
printf("loop, offset: %d, total: %zu, max space: %d\n", offset, length, maxPayload);
285284
uint16_t size = (length - offset) < maxPayload ? (length - offset) : maxPayload; //payload size
286285
//fill the information cell
287286
RelayCell dataCell;
288-
fillRelayCell(dataCell, circID, RelayCellCommand::DATA, nodeID, nextNodeID, size);
287+
fillRelayCell(dataCell, circID, command, nodeID, nextNodeID, size);
289288
memcpy(dataCell.payload, data + offset, size);
290289
// write zeroes to remaining payload, prevent leakage of previous data if payload is smaller than max
291290
memset(dataCell.payload + size, 0, maxPayload - size);
@@ -326,9 +325,10 @@ void sendDataForwards(Circuit* circuit, uint8_t* data, size_t length) {
326325
}
327326

328327
//function to send a RELAY DATA cell towards the origin.
329-
void sendDataBackwards(Circuit* circuit, uint16_t prevNodeID, uint16_t prevCircID, uint8_t* data, size_t length) {
328+
void sendDataBackwards(Circuit* circuit, uint16_t prevNodeID, uint16_t prevCircID,
329+
uint8_t* data, size_t length, RelayCellCommand command = DATA) {
330330
printf("sendDataBackwards\n");
331-
sendData(circuit, prevNodeID, prevCircID, data, length, false);
331+
sendData(circuit, prevNodeID, prevCircID, data, length, false, command);
332332
}
333333

334334
//checks the validity of the digest, and returns a buffer with the content of the message.
@@ -359,9 +359,9 @@ static void handleDataAtExit(Circuit* circuit, RelayCell cell) {
359359
uint8_t buffer[CELL_SIZE - RELAY_HEADER_SIZE - HASH_SIZE];
360360
uint8_t* result = handleData(circuit, cell, buffer);
361361
if (result != nullptr) {
362-
printf("Received data: %s\n", result);
362+
printf("Circuit %d: Received data: %s\n", circuit->circID ,result);
363363
const char* msg = "Data received successfully!";
364-
sendDataBackwards(circuit, cell.srcNodeID, cell.circID, (uint8_t*)msg, strlen(msg) + 1);
364+
sendDataBackwards(circuit, cell.srcNodeID, cell.circID, (uint8_t*)msg, strlen(msg) + 1, DATA_ACK);
365365
}
366366
}
367367

@@ -645,6 +645,9 @@ static void dispatchRelay(Circuit* circuit, uint8_t* data, uint16_t circID) {
645645
case DATA:
646646
handleDataAtExit(circuit, cell);
647647
break;
648+
case DATA_ACK:
649+
handleDataAtOrigin(circuit, copy);
650+
break;
648651
default:
649652
break;
650653
}

src/LoRaCommunication/main.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,24 @@ int main(int argc, char* argv[]) {
7272

7373
const char* hello = "Hello World!";
7474

75-
/*
75+
const char* incoming = "Third message incoming... It arrived!";
76+
77+
7678
printf("------------------------ build Circuit A - B - C - D -----------------------\n");
7779
Circuit* circuit = buildCircuit(444, 3, LocalPeers, nodeRole);
7880

7981
while (circuit->state != CircuitState::READY) {
8082
std::this_thread::sleep_for(std::chrono::milliseconds(50));
8183
}
82-
*/
8384

84-
/*
85+
8586
printf("------------------------ build Circuit A - B - C -----------------------\n");
8687
Circuit* circuit2 = buildCircuit(333, 2, LocalPeers, nodeRole);
8788

8889
while (circuit2->state != CircuitState::READY) {
8990
std::this_thread::sleep_for(std::chrono::milliseconds(50));
9091
}
91-
92-
const char* msg = longmsg;
93-
//sendDataForwards(circuit, (uint8_t*)hello, strlen(msg) + 1);
94-
sendDataForwards(circuit2, (uint8_t*)msg, strlen(hello) + 1);
95-
*/
92+
9693

9794
printf("------------------------ build Circuit A - B -----------------------\n");
9895
Circuit* circuit3 = buildCircuit(222, 1, LocalPeers, nodeRole);
@@ -102,8 +99,9 @@ int main(int argc, char* argv[]) {
10299
}
103100

104101
const char* msg = longmsg;
105-
//sendDataForwards(circuit, (uint8_t*)hello, strlen(msg) + 1);
106-
sendDataForwards(circuit3, (uint8_t*)msg, strlen(hello) + 1);
102+
sendDataForwards(circuit, (uint8_t*)hello, strlen(hello) + 1);
103+
sendDataForwards(circuit, (uint8_t*)longmsg, strlen(longmsg) + 1);
104+
sendDataForwards(circuit, (uint8_t*)incoming, strlen(incoming) + 1);
107105

108106
/*
109107
printf("------------------------ Destroy Circuit A - B - C - D -----------------------\n");

0 commit comments

Comments
 (0)