/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2012 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * WorkOnJobOperation.cpp * * Created on: 22.02.2012 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif // boost asio needs specific operator new #include #include "CodePatterns/MemDebug.hpp" #include "WorkOnJobOperation.hpp" #include #include "CodePatterns/Info.hpp" #include "CodePatterns/Log.hpp" #include "Jobs/FragmentJob.hpp" #include "Jobs/MPQCCommandJob.hpp" #include "Jobs/SystemCommandJob.hpp" #include "Results/FragmentResult.hpp" FragmentResult::ptr WorkOnJobOperation::EmptyResult( new FragmentResult(JobId::NoJob, std::string("EmptyResult")) ); /// Handle completion of a connect operation. void WorkOnJobOperation::handle_connect(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { Info info(__FUNCTION__); if (!e) { // Successfully established connection. Start operation to read the list // of jobs. The connection::async_read() function will automatically // decode the data that is read from the underlying socket. LOG(1, "INFO: Receiving a job ..."); connection_.async_read(job, boost::bind(&WorkOnJobOperation::handle_ReceiveJob, this, boost::asio::placeholders::error)); } else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) { // Try the next endpoint. connection_.socket().close(); boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator; connection_.socket().async_connect(endpoint, boost::bind(&WorkOnJobOperation::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator)); } else { // An error occurred. Log it and return. Since we are not starting a new // operation the io_service will run out of work to do and the client will // exit. exitflag = ErrorFlag; ELOG(1, e.message()); } } /// Callback function when job has been received. void WorkOnJobOperation::handle_ReceiveJob(const boost::system::error_code& e) { Info info(__FUNCTION__); if (!e) { LOG(1, "INFO: Received job #" << job->getId() << "."); if (job->getId() != JobId::NoJob) { // Print out the data that was received. std::cout << "Job id: " << job->getId() << "\n"; // do something .. right now: wait LOG(1, "INFO: Calculating job #" << job->getId() << " ..."); FragmentResult::ptr result(job->Work()); // write the result to the server LOG(1, "INFO: Sending result for job #" << job->getId() << " ..."); connection_.async_write(result, boost::bind(&WorkOnJobOperation::handle_SendResult, this, boost::asio::placeholders::error)); } else { std::cout << "The server has no job for me." << std::endl; // send out empty result LOG(1, "INFO: Sending empty result ..."); connection_.async_write(EmptyResult, boost::bind(&WorkOnJobOperation::handle_SendResult, this, boost::asio::placeholders::error)); exitflag = ErrorFlag; } } else { // An error occurred. exitflag = ErrorFlag; ELOG(1, e.message()); } // Since we are not starting a new operation the io_service will run out of // work to do and the client will exit. } /// Callback function when result has been sent. void WorkOnJobOperation::handle_SendResult(const boost::system::error_code& e) { Info info(__FUNCTION__); // Nothing to do. LOG(1, "INFO: Job #" << job->getId() << " calculated and sent."); // erase job job.reset(); }