|
|
@@ -13,6 +13,7 @@ |
|
|
|
#include "../CodeDweller/faults.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
@@ -109,8 +110,9 @@ void Job::doINTF() { |
|
|
|
|
|
|
|
void Job::doFAIL() {
|
|
|
|
ostringstream O;
|
|
|
|
O << "SNF4CGP Does not understand: " << formatAsCGPString(CurrentCommand.Data);
|
|
|
|
emitComment(O.str());
|
|
|
|
O << "* SNF4CGP[" << CurrentCommand.Number << "] Does not understand: "
|
|
|
|
<< formatAsCGPString(CurrentCommand.Data) << endl;
|
|
|
|
OutputBuffer.append(O.str());
|
|
|
|
emitFAILURE();
|
|
|
|
}
|
|
|
|
|
|
|
@@ -118,6 +120,7 @@ void Job::doFILE() { |
|
|
|
doRead();
|
|
|
|
doScan();
|
|
|
|
doAction();
|
|
|
|
closeReader();
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeCheck CheckMessageReaderIsValid("Job::Reader() Check(0 != Reader_)");
|
|
|
@@ -180,13 +183,20 @@ string Job::ScanName() { |
|
|
|
return ScanNameFormatter.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::setConfigurationFromScanResult(ScopeScanner& myScanner) {
|
|
|
|
ScanResultConfiguration = Scanners.ConfigurationForResultCode(ScanResultCode);
|
|
|
|
if(ScanResultConfiguration.InjectHeaders) HeadersToInject = myScanner.Engine().getXHDRs();
|
|
|
|
if(ScanResultConfiguration.EmitXMLLog) XMLLogData = myScanner.Engine().getXMLLog();
|
|
|
|
if(ScanResultConfiguration.EmitClassicLog) ClassicLogData = myScanner.Engine().getClassicLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::doScan() {
|
|
|
|
ScopeScanner myScanner(Scanners);
|
|
|
|
ScanResultCode =
|
|
|
|
myScanner.Engine().scanMessage(
|
|
|
|
&ReadBuffer[0], Reader().gcount(), ScanName(), JobTimer.getElapsedTime()
|
|
|
|
);
|
|
|
|
HeadersToInject = myScanner.Engine().getXHDRs();
|
|
|
|
setConfigurationFromScanResult(myScanner);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::doAction() {
|
|
|
@@ -206,21 +216,115 @@ void Job::doReject() { |
|
|
|
void Job::doDelete() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::moveMessageToHoldPath(ifstream& Reader) {
|
|
|
|
void Job::emitMessageMovedTo(string& Destination) {
|
|
|
|
ostringstream O;
|
|
|
|
O << "* SNF4CGP[" << CurrentCommand.Number << "] Moved message to " << Destination << endl;
|
|
|
|
OutputBuffer.append(O.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::identifyLocalLineEnd() { // Identify the line endings in use:
|
|
|
|
if(0 < LocalLineEnd.length()) return; // Do this only once for any system.
|
|
|
|
for(int i = 0; i < Reader().gcount(); i++) { // Look for the first line end.
|
|
|
|
if('\r' == ReadBuffer[i] && '\n' == ReadBuffer[i+1]) { // It may be \r\n.
|
|
|
|
LocalLineEnd = "\r\n"; return;
|
|
|
|
} else
|
|
|
|
if('\n' == ReadBuffer[i] && '\n' == ReadBuffer[i+1]) { // It may be \n. (find block end \n\n)
|
|
|
|
LocalLineEnd = "\n"; return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeCheck CheckLineEndsIdentified("Job::findHeaderInsertPoint() Check(0 < LocalLineEnd.length())");
|
|
|
|
|
|
|
|
size_t Job::findHeaderInsertPoint() { // How to find the insert point:
|
|
|
|
identifyLocalLineEnd();
|
|
|
|
CheckLineEndsIdentified(0 < LocalLineEnd.length());
|
|
|
|
string BlockTerminus = LocalLineEnd + LocalLineEnd;
|
|
|
|
size_t InsertCursor = 0;
|
|
|
|
|
|
|
|
//// Search for the end of the CGP control block.
|
|
|
|
|
|
|
|
for(;InsertCursor < (Reader().gcount() - BlockTerminus.length()); InsertCursor++) {
|
|
|
|
if(0 == BlockTerminus.compare(reinterpret_cast<char*>(&ReadBuffer[InsertCursor]))) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//// Search for the end of the message headers.
|
|
|
|
|
|
|
|
InsertCursor += BlockTerminus.length();
|
|
|
|
for(;InsertCursor < (Reader().gcount() - BlockTerminus.length()); InsertCursor++) {
|
|
|
|
if(0 == BlockTerminus.compare(reinterpret_cast<char*>(&ReadBuffer[InsertCursor]))) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//// The correct insert point is between the two LocalLineEnd in the BlockTerminus
|
|
|
|
|
|
|
|
InsertCursor += LocalLineEnd.length();
|
|
|
|
|
|
|
|
return InsertCursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::writeUpToInjectionPoint(size_t InjectionPoint) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::writeHeaders() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job:: writeTheRestOfTheFirstBuffer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::injectHeadersInMessageMove() {
|
|
|
|
//// writeUpToTheInjectionPoint()
|
|
|
|
//// writeTheHeaders()
|
|
|
|
//// writeTheRestOfTheFirstBuffer()
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::copyRestOfLongMessage() {
|
|
|
|
//// while(MoreToDo) {
|
|
|
|
//// readBufferFull()
|
|
|
|
//// writeBufferFull()
|
|
|
|
//// }
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::moveMessageToHoldPath(string& Destination) {
|
|
|
|
openWriter(Destination);
|
|
|
|
if(ScanResultConfiguration.InjectHeaders) injectHeadersInMessageMove();
|
|
|
|
copyRestOfLongMessage();
|
|
|
|
closeWriter();
|
|
|
|
}
|
|
|
|
|
|
|
|
const string PathSeparators = "\\/";
|
|
|
|
|
|
|
|
string FileNamePart(string Path) { // How to get the file name part of a path:
|
|
|
|
string::size_type Found = Path.find_last_of(PathSeparators);
|
|
|
|
if(string::npos != Found) Path = Path.substr(Found+1); // Return all after the last separator
|
|
|
|
return Path; // or Path if no separators are found.
|
|
|
|
}
|
|
|
|
|
|
|
|
string Job::calculateDestinationFileName() { // How to get the message move destination:
|
|
|
|
string Source = CurrentCommand.Data;
|
|
|
|
string Destination = ScanResultConfiguration.HoldPath;
|
|
|
|
Destination.append(FileNamePart(Source));
|
|
|
|
return Destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::doHold() {
|
|
|
|
string Destination = calculateDestinationFileName();
|
|
|
|
moveMessageToHoldPath(Destination);
|
|
|
|
emitMessageMovedTo(Destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int NoScanYet = -1;
|
|
|
|
|
|
|
|
Job::Job(ScannerPool& S, OutputProcessor& O) : // Construct with important links.
|
|
|
|
Scanners(S),
|
|
|
|
Output(O),
|
|
|
|
ScanResultCode(0),
|
|
|
|
ScanResultCode(NoScanYet),
|
|
|
|
Reader_(0),
|
|
|
|
Writer_(0) { // Minimize heap thrashing.
|
|
|
|
OutputBuffer.reserve(StringReserveSize);
|
|
|
|
HeadersToInject.reserve(StringReserveSize);
|
|
|
|
ReadBuffer.reserve(ReadBufferSize);
|
|
|
|
OutputBuffer.reserve(StringReserveSize);
|
|
|
|
HeadersToInject.reserve(StringReserveSize);
|
|
|
|
XMLLogData.reserve(StringReserveSize);
|
|
|
|
ClassicLogData.reserve(StringReserveSize);
|
|
|
|
ReadBuffer.reserve(ReadBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
Job::~Job() { // Cleanup when destructing.
|
|
|
@@ -230,10 +334,11 @@ Job::~Job() { |
|
|
|
|
|
|
|
void Job::clear() { // Cleanup for the next command.
|
|
|
|
CurrentCommand.clear();
|
|
|
|
ScanResultCode = 0;
|
|
|
|
ScanResultCode = NoScanYet;
|
|
|
|
OutputBuffer.clear();
|
|
|
|
HeadersToInject.clear();
|
|
|
|
MessageMoveFilePath.clear();
|
|
|
|
XMLLogData.clear();
|
|
|
|
ClassicLogData.clear();
|
|
|
|
ReadBuffer.clear();
|
|
|
|
closeReader();
|
|
|
|
closeWriter();
|
|
|
@@ -258,15 +363,24 @@ void Job::executeCommand() { |
|
|
|
}
|
|
|
|
|
|
|
|
void Job::emitException(const string& What) {
|
|
|
|
ostringstream O;
|
|
|
|
O << "* SNF4CGP[" << CurrentCommand.Number << "] Exception: " << What << endl;
|
|
|
|
OutputBuffer.append(O.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::emitUnknownException() {
|
|
|
|
ostringstream O;
|
|
|
|
O << "* SNF4CGP[" << CurrentCommand.Number << "] Unknown Exception!" << endl;
|
|
|
|
OutputBuffer.append(O.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::doIt() { // Get the job done.
|
|
|
|
try { executeCommand(); }
|
|
|
|
catch(exception& e) { emitException(e.what()); }
|
|
|
|
catch(...) { emitUnknownException(); }
|
|
|
|
try { // emitFAILURE() on all exceptions.
|
|
|
|
try { executeCommand(); } // executeCommand() and report exceptions.
|
|
|
|
catch(exception& e) { emitException(e.what()); throw; }
|
|
|
|
catch(...) { emitUnknownException(); throw; }
|
|
|
|
}
|
|
|
|
catch(...) { emitFAILURE(); }
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
|