|
@@ -6,8 +6,12 @@
|
|
#include <string>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <type_traits>
|
|
|
|
|
|
-#include "../../../include/Config.h"
|
|
|
|
-#include "../../../include/Queue.h"
|
|
|
|
|
|
+#include <boost/filesystem.hpp>
|
|
|
|
+
|
|
|
|
+#include "../../Config.h"
|
|
|
|
+#include "../../Notifications.h"
|
|
|
|
+#include "../../Queue.h"
|
|
|
|
+#include "../ChannelControls.h"
|
|
|
|
|
|
/**
|
|
/**
|
|
* @class CovertProtocol
|
|
* @class CovertProtocol
|
|
@@ -19,7 +23,7 @@
|
|
* @param N number of bytes which can be used to transmit data
|
|
* @param N number of bytes which can be used to transmit data
|
|
* @param PASSIVE passive mode
|
|
* @param PASSIVE passive mode
|
|
*/
|
|
*/
|
|
-template <int N, bool PASSIVE> class CovertProtocol {
|
|
|
|
|
|
+template <int N, bool PASSIVE> class CovertProtocol : public ChannelControls {
|
|
static_assert(N >= 1);
|
|
static_assert(N >= 1);
|
|
|
|
|
|
public:
|
|
public:
|
|
@@ -35,33 +39,6 @@ public:
|
|
*/
|
|
*/
|
|
~CovertProtocol() { file.close(); }
|
|
~CovertProtocol() { file.close(); }
|
|
|
|
|
|
- /**
|
|
|
|
- * Starts sending a file.
|
|
|
|
- *
|
|
|
|
- * Starts sending a file if no transmission is running and the file exists.
|
|
|
|
- *
|
|
|
|
- * @param fileName name of the file in the file directory
|
|
|
|
- * @return true - file will be sent | false - file was not accepted
|
|
|
|
- */
|
|
|
|
- bool sendFile(const std::string &fileName) {
|
|
|
|
- static_assert(!PASSIVE);
|
|
|
|
-
|
|
|
|
- if (state != ProtocolState::idle || file.is_open()) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- file.open(fileDirectory + fileName, std::ios::in);
|
|
|
|
- if (file.is_open()) {
|
|
|
|
- file.close();
|
|
|
|
- this->fileName = fileName;
|
|
|
|
- state = ProtocolState::fileNameSize;
|
|
|
|
- std::cout << "starting sending file \"" << fileName << "\"" << std::endl;
|
|
|
|
- return true;
|
|
|
|
- } else {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* send encodes the data into the data array
|
|
* send encodes the data into the data array
|
|
*
|
|
*
|
|
@@ -103,8 +80,10 @@ public:
|
|
file.open(fileDirectory + fileName, std::ios::in | std::ios::binary | std::ios::ate);
|
|
file.open(fileDirectory + fileName, std::ios::in | std::ios::binary | std::ios::ate);
|
|
if (!file.is_open()) {
|
|
if (!file.is_open()) {
|
|
file.close();
|
|
file.close();
|
|
- std::cerr << "File \"" << fileName << "\" does exists. Error state!!!" << std::endl;
|
|
|
|
- state = ProtocolState::error;
|
|
|
|
|
|
+ std::cerr << "File \"" << fileName << "\" does exists. Resetting!!!" << std::endl;
|
|
|
|
+ Notifications::newNotification("File \"" + fileName + "\" does not exists. Skipping.");
|
|
|
|
+ // state = ProtocolState::error;
|
|
|
|
+ Queue::channel->reset();
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -156,6 +135,7 @@ public:
|
|
file.close();
|
|
file.close();
|
|
state = ProtocolState::idle;
|
|
state = ProtocolState::idle;
|
|
std::cout << "finished sending file \"" << fileName << "\"" << std::endl;
|
|
std::cout << "finished sending file \"" << fileName << "\"" << std::endl;
|
|
|
|
+ Notifications::newNotification("Finished sending file \"" + fileName + "\".");
|
|
|
|
|
|
// schedule next file transfer
|
|
// schedule next file transfer
|
|
Queue::schedule();
|
|
Queue::schedule();
|
|
@@ -164,7 +144,7 @@ public:
|
|
break;
|
|
break;
|
|
|
|
|
|
case ProtocolState::error:
|
|
case ProtocolState::error:
|
|
-
|
|
|
|
|
|
+ Queue::channel->reset();
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -192,11 +172,13 @@ public:
|
|
|
|
|
|
// no break because the first data received is the filename size
|
|
// no break because the first data received is the filename size
|
|
case ProtocolState::fileNameSize:
|
|
case ProtocolState::fileNameSize:
|
|
|
|
+ Notifications::newNotification("Incoming transmission.");
|
|
std::cout << "incoming file transmission" << std::endl;
|
|
std::cout << "incoming file transmission" << std::endl;
|
|
|
|
|
|
fileNameSize = data[0];
|
|
fileNameSize = data[0];
|
|
fileNamePosition = 0;
|
|
fileNamePosition = 0;
|
|
fileName = "";
|
|
fileName = "";
|
|
|
|
+ transferStart = std::time(nullptr);
|
|
state = ProtocolState::fileName;
|
|
state = ProtocolState::fileName;
|
|
break;
|
|
break;
|
|
|
|
|
|
@@ -217,8 +199,10 @@ public:
|
|
file.open(fileDirectory + fileName, std::ios::in);
|
|
file.open(fileDirectory + fileName, std::ios::in);
|
|
if (file.is_open()) {
|
|
if (file.is_open()) {
|
|
file.close();
|
|
file.close();
|
|
- std::cerr << "File \"" << fileName << "\" already exists. Error state!!!" << std::endl;
|
|
|
|
- state = ProtocolState::error;
|
|
|
|
|
|
+ std::cerr << "File \"" << fileName << "\" already exists. Resetting!!!" << std::endl;
|
|
|
|
+ Notifications::newNotification("File \"" + fileName + "\" already exists. Skipping.");
|
|
|
|
+ // state = ProtocolState::error;
|
|
|
|
+ Queue::channel->reset();
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -226,8 +210,10 @@ public:
|
|
|
|
|
|
file.open(fileDirectory + fileName, std::ios::out | std::ios::binary | std::ios::app);
|
|
file.open(fileDirectory + fileName, std::ios::out | std::ios::binary | std::ios::app);
|
|
if (!file.is_open()) {
|
|
if (!file.is_open()) {
|
|
- std::cerr << "File \"" << fileName << "\" could not be opened! Error state!!!" << std::endl;
|
|
|
|
- state = ProtocolState::error;
|
|
|
|
|
|
+ std::cerr << "File \"" << fileName << "\" could not be opened! Resetting!!!" << std::endl;
|
|
|
|
+ Notifications::newNotification("File \"" + fileName + "\" could not be written. Skipping.");
|
|
|
|
+ // state = ProtocolState::error;
|
|
|
|
+ Queue::channel->reset();
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -236,6 +222,7 @@ public:
|
|
state = ProtocolState::dataSize;
|
|
state = ProtocolState::dataSize;
|
|
|
|
|
|
std::cout << "starting receiving file \"" << fileName << "\"" << std::endl;
|
|
std::cout << "starting receiving file \"" << fileName << "\"" << std::endl;
|
|
|
|
+ Notifications::newNotification("Receiving file \"" + fileName + "\".");
|
|
}
|
|
}
|
|
|
|
|
|
break;
|
|
break;
|
|
@@ -280,6 +267,7 @@ public:
|
|
state = ProtocolState::idle;
|
|
state = ProtocolState::idle;
|
|
|
|
|
|
std::cout << "finished receiving file \"" << fileName << "\"" << std::endl;
|
|
std::cout << "finished receiving file \"" << fileName << "\"" << std::endl;
|
|
|
|
+ Notifications::newNotification("Finished receiving file \"" + fileName + "\".");
|
|
}
|
|
}
|
|
|
|
|
|
break;
|
|
break;
|
|
@@ -289,6 +277,39 @@ public:
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /* ChannelControls */
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Starts sending a file.
|
|
|
|
+ *
|
|
|
|
+ * Starts sending a file if no transmission is running and the file exists.
|
|
|
|
+ *
|
|
|
|
+ * @param fileName name of the file in the file directory
|
|
|
|
+ * @return true - file will be sent | false - file was not accepted
|
|
|
|
+ */
|
|
|
|
+ virtual bool sendFile(const std::string &fileName) {
|
|
|
|
+ if constexpr (PASSIVE) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (state != ProtocolState::idle || file.is_open()) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ file.open(fileDirectory + fileName, std::ios::in);
|
|
|
|
+ if (file.is_open()) {
|
|
|
|
+ file.close();
|
|
|
|
+ this->fileName = fileName;
|
|
|
|
+ transferStart = std::time(nullptr);
|
|
|
|
+ state = ProtocolState::fileNameSize;
|
|
|
|
+ Notifications::newNotification("Start sending file \"" + fileName + "\".");
|
|
|
|
+ std::cout << "starting sending file \"" << fileName << "\"" << std::endl;
|
|
|
|
+ return true;
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Get the progress
|
|
* Get the progress
|
|
*
|
|
*
|
|
@@ -309,6 +330,44 @@ public:
|
|
*/
|
|
*/
|
|
virtual bool isTransferRunning() { return state != ProtocolState::idle; }
|
|
virtual bool isTransferRunning() { return state != ProtocolState::idle; }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Resets protocol state
|
|
|
|
+ */
|
|
|
|
+ virtual void reset() {
|
|
|
|
+ file.close();
|
|
|
|
+
|
|
|
|
+ if (PASSIVE && state == ProtocolState::data) {
|
|
|
|
+ // delete file if in passive mode and data has been received
|
|
|
|
+ deleteFile(fileName);
|
|
|
|
+ Notifications::newNotification("Transfer of file \"" + fileName + "\" was aborted.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ state = ProtocolState::idle;
|
|
|
|
+ dataSize = 0;
|
|
|
|
+ dataPosition = 0;
|
|
|
|
+ fileNameSize = 0;
|
|
|
|
+ fileNamePosition = 0;
|
|
|
|
+ fileName = "";
|
|
|
|
+
|
|
|
|
+ Queue::schedule();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Test if a transfer is running
|
|
|
|
+ *
|
|
|
|
+ * @return true - a transfer runs | false - no transfer runs
|
|
|
|
+ */
|
|
|
|
+ virtual std::time_t getTransferStart() { return transferStart; }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Get file name of the file which is currently be sent or received.
|
|
|
|
+ *
|
|
|
|
+ * @return file name
|
|
|
|
+ */
|
|
|
|
+ virtual std::string getFileName() { return fileName; };
|
|
|
|
+
|
|
|
|
+ /* =============== */
|
|
|
|
+
|
|
private:
|
|
private:
|
|
/**
|
|
/**
|
|
* folder of the files
|
|
* folder of the files
|
|
@@ -353,9 +412,16 @@ private:
|
|
uint8_t fileNamePosition;
|
|
uint8_t fileNamePosition;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * name of the file to be sent/received
|
|
|
|
|
|
+ * Deletes a file
|
|
*/
|
|
*/
|
|
- std::string fileName;
|
|
|
|
|
|
+ void deleteFile(const std::string &fileName) {
|
|
|
|
+ std::string fname = this->fileDirectory;
|
|
|
|
+ fname.append(fileName);
|
|
|
|
+
|
|
|
|
+ if (boost::filesystem::exists(fname)) {
|
|
|
|
+ boost::filesystem::remove(fname);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
#endif
|
|
#endif
|