Definition at line 278 of file ClientMain.cpp. 00278 { 00279 cout << DateFormater::getDate() << "Beginning the calculation in the worker thread..." << endl; 00280 // get the strings from the message object 00281 string inputString = pServiceOffer->getInputBytes(); 00282 string presetString = pServiceOffer->getPrefixBytes(); 00283 string methodString = pServiceOffer->getMethod(); 00284 string outputString = pServiceOffer->getReferenceOutput(); 00285 // reserve the needed space for the real bytes 00286 int inputLength = inputString.length()/2; 00287 char inputBytes[inputLength]; 00288 char presetBytes[presetString.length()/2]; 00289 char detectedBytes[LENGTH_DETECTED_BYTES]; 00290 const char* method = methodString.c_str(); 00291 char outputBytes[outputString.length()/2]; 00292 // format the hex strings to bytes 00293 StringByteConverter::hexStringToBytes(inputString, inputBytes, 0); 00294 StringByteConverter::hexStringToBytes(presetString, presetBytes, 0); 00295 StringByteConverter::hexStringToBytes(outputString, outputBytes, 0); 00296 // call the iteration method to do the magic stuff... 00297 int bytesFound = detectBytes(method, inputBytes, inputLength, presetBytes, 00298 outputBytes, detectedBytes); 00299 enterCriticalSection(); 00300 // unset the working status 00301 working = false; 00302 serviceNeeded = false; 00303 // stop the reminder thread 00304 if (pthread_cancel(reminder) ) { 00305 cout << DateFormater::getDate() << "Cancelation of the reminder failed!" << endl; 00306 } 00307 if (bytesFound == DETECT_BYTES_DETECTED || 00308 bytesFound == DETECT_BYTES_NOT_DETECTED) { 00309 // send the server the calculation result 00310 cout << DateFormater::getDate() << "Sending the server a service done message..." << endl; 00311 try { 00312 bool answerReceived; 00313 string detectedString(""); 00314 if (bytesFound == DETECT_BYTES_DETECTED) { 00315 detectedString = StringByteConverter::bytesToHexString(detectedBytes,LENGTH_DETECTED_BYTES); 00316 } 00317 bool booleanDetectionStatus = false; 00318 if (bytesFound == DETECT_BYTES_DETECTED) { 00319 booleanDetectionStatus = true; 00320 } 00321 ServiceDone serviceDone(pServiceOffer->getSessionId(), booleanDetectionStatus, detectedString); 00322 Message response; 00323 answerReceived = sendAndReceive("main",serviceDone,response); 00324 if (answerReceived) { 00325 bool found = false; 00326 if ( !found ) try { 00327 Acknowledgement ack = response; 00328 cout << DateFormater::getDate() << "The server acknowledged the service done." << endl; 00329 found = true; 00330 } 00331 catch ( MessageCastException& e ) {} 00332 if ( !found ) try { 00333 Error err = response; 00334 cout << DateFormater::getDate() << "The server detected an error about the service done:" << endl; 00335 cout << DateFormater::getDate() << err.getReason() << endl; 00336 found = true; 00337 } 00338 catch ( MessageCastException& e ) {} 00339 if ( !found ) { 00340 cout << DateFormater::getDate() << "Unexpected answer from the server." << endl; 00341 } 00342 } 00343 else { 00344 cout << DateFormater::getDate() << "Can't communicate with server." << endl; 00345 } 00346 } 00347 catch ( NetInterfaceException& e ) { 00348 cout << DateFormater::getDate() << "Connection failed..." << endl; 00349 cout << DateFormater::getDate() << "NetInterface: "; 00350 e.print(); 00351 } 00352 } else { 00353 // send the server a service cancel 00354 cout << DateFormater::getDate() << "Sending the server a service cancel because the method \""<< pServiceOffer->getMethod() << "\" is not supported..." << endl; 00355 try { 00356 bool answerReceived; 00357 string reason("The method \""); 00358 reason += pServiceOffer->getMethod(); 00359 reason += "\" is not supported by this client."; 00360 ServiceCancel serviceCancel(pServiceOffer->getSessionId(), reason); 00361 Message response; 00362 answerReceived = sendAndReceive("main",serviceCancel,response); 00363 if (answerReceived) { 00364 bool found = false; 00365 if ( !found ) try { 00366 Acknowledgement ack = response; 00367 cout << DateFormater::getDate() << "The server accepted our cancelation." << endl; 00368 found = true; 00369 } 00370 catch ( MessageCastException& e ) {} 00371 if ( !found ) try { 00372 Error err = response; 00373 cout << DateFormater::getDate() << "The server detected an error about the service cancel:" << endl; 00374 cout << DateFormater::getDate() << err.getReason() << endl; 00375 found = true; 00376 } 00377 catch ( MessageCastException& e ) {} 00378 if ( !found ) { 00379 cout << DateFormater::getDate() << "Unexpected answer from the server." << endl; 00380 } 00381 } 00382 else { 00383 cout << DateFormater::getDate() << "Can't communicate with server." << endl; 00384 } 00385 } 00386 catch ( NetInterfaceException& e ) { 00387 cout << DateFormater::getDate() << "Connection failed..." << endl; 00388 cout << DateFormater::getDate() << "NetInterface: "; 00389 e.print(); 00390 } 00391 } 00392 // leave the critical section here to prevent the signal handler 00393 // to quit when the calculation is already done and the only 00394 // thing left to do is to inform the server about the result... 00395 leaveCriticalSection(); 00396 }
|
1.5.1