source: src/controller.cpp@ 014bc4

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 014bc4 was 014bc4, checked in by Frederik Heber <heber@…>, 13 years ago

Introduced interface controller_AddOn.hpp that encapsulates controller extending functions in struct controller_AddOn.

  • allocating function for derived structure controller_AddOn is contained in extra module to allow indepent linking, i.e. combine controller.cpp, controller_AddOn_SystemCommandJob.cpp and controller_SystemCommandJon.cpp or with controller_AddOn_MPQCCommandJob.cpp and controller_MPQCCommandJon.cpp.
  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[7ca772e]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2011 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * \file controller.cpp
10 *
11 * This file strongly follows the Serialization example from the boost::asio
12 * library (see client.cpp)
13 *
14 * Created on: Nov 27, 2011
15 * Author: heber
16 */
17
18
19// include config.h
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24// boost asio needs specific operator new
25#include <boost/asio.hpp>
26
27#include "CodePatterns/MemDebug.hpp"
28
[685e28]29#include <boost/assign.hpp>
[af9b9ff]30#include <boost/program_options.hpp>
[685e28]31#include <deque>
[ff60cfa]32#include <fstream>
[7ca772e]33#include <iostream>
34#include <map>
[ff60cfa]35#include <sstream>
36#include <streambuf>
37#include <vector>
[7ca772e]38
[014bc4]39#include "CodePatterns/Assert.hpp"
[7ca772e]40#include "CodePatterns/Info.hpp"
41#include "CodePatterns/Log.hpp"
[cbcbbd]42
[014bc4]43#include "atexit.hpp"
[d4885d]44#include "FragmentController.hpp"
[ee61ce]45#include "ControllerCommandRegistry.hpp"
[014bc4]46#include "ControllerOptions.hpp"
47#include "controller_AddOn.hpp"
[cbcbbd]48
[685e28]49/** Print the status of scheduled and done jobs.
50 *
51 * @param status pair of number of schedule and done jobs
52 */
53void printJobStatus(const std::pair<size_t, size_t> &JobStatus)
54{
55 LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #"
56 << JobStatus.second << " jobs are calculated so far.");
57}
[ff60cfa]58
[cbcbbd]59inline std::vector<std::string> getListOfCommands(const ControllerCommandRegistry &ControllerCommands)
[5a8512]60{
[cbcbbd]61 std::vector<std::string> Commands;
62 for (ControllerCommandRegistry::const_iterator iter = ControllerCommands.getBeginIter();
63 iter != ControllerCommands.getEndIter(); ++iter)
64 Commands.push_back(iter->first);
[5a8512]65
[cbcbbd]66 return Commands;
[685e28]67}
68
[7ca772e]69int main(int argc, char* argv[])
70{
71 // from this moment on, we need to be sure to deeinitialize in the correct order
72 // this is handled by the cleanup function
73 atexit(cleanUp);
74
75 size_t Exitflag = 0;
[685e28]76
[014bc4]77 controller_AddOn *AddOn = getAddOn();
78 ASSERT(AddOn != NULL,
79 "main() - returned AddOn is NULL.");
80
81 ControllerOptions *ControllerInfo = AddOn->allocateControllerInfo();
[685e28]82 boost::asio::io_service io_service;
83 FragmentController controller(io_service);
84 boost::program_options::variables_map vm;
85
[ee61ce]86 // prepare ControllerCommand
87 // note: we need "< ControllerCommand::commands_t >" because parseExecutable(),... return int
[685e28]88 // in contrast to other functions that return void
[ee61ce]89 ControllerCommandRegistry ControllerCommands;
[cbcbbd]90 boost::function<void (ControllerCommand *)> registrator =
91 (boost::bind(&Registry<ControllerCommand>::registerInstance, boost::ref(ControllerCommands), _1));
92 registrator(new ControllerCommand("checkresults",
[ee61ce]93 boost::assign::list_of< ControllerCommand::commands_t >
94 (boost::bind(&FragmentController::checkResults,
[cbcbbd]95 boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
[ee61ce]96 (boost::bind(&printJobStatus,
97 boost::bind(&FragmentController::getJobStatus, boost::ref(controller))))
98 ));
[cbcbbd]99 registrator(new ControllerCommand("removeall",
[ee61ce]100 boost::assign::list_of< ControllerCommand::commands_t >
101 (boost::bind(&FragmentController::removeall,
[cbcbbd]102 boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
[ee61ce]103 ));
[cbcbbd]104 registrator(new ControllerCommand("shutdown",
[ee61ce]105 boost::assign::list_of< ControllerCommand::commands_t >
106 (boost::bind(&FragmentController::shutdown,
[cbcbbd]107 boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
[ee61ce]108 ));
[014bc4]109 AddOn->addSpecificCommands(registrator, controller, *ControllerInfo);
[af9b9ff]110
111 // Declare the supported options.
112 boost::program_options::options_description desc("Allowed options");
113 desc.add_options()
114 ("help,h", "produce help message")
115 ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
116 ("server", boost::program_options::value< std::string >(), "connect to server at this address (host:port)")
[cbcbbd]117 ("command", boost::program_options::value< std::string >(), (std::string("command to send to server: ")+toString(getListOfCommands(ControllerCommands))).c_str())
[af9b9ff]118 ;
[014bc4]119 AddOn->addSpecificOptions(desc.add_options());
[af9b9ff]120
[685e28]121 // parse command line
[af9b9ff]122 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
123 boost::program_options::notify(vm);
124
[685e28]125 // set controller information
[5fa1f0d]126 int status = 0;
[cbcbbd]127 status = ControllerInfo->parseHelp(vm, desc);
[5fa1f0d]128 if (status) return status;
[cbcbbd]129 status = ControllerInfo->parseVerbosity(vm);
[5fa1f0d]130 if (status) return status;
[cbcbbd]131 status = ControllerInfo->parseServerPort(vm);
[5fa1f0d]132 if (status) return status;
[cbcbbd]133 status = ControllerInfo->parseCommand(vm, getListOfCommands(ControllerCommands));
[5fa1f0d]134 if (status) return status;
[9dcce3]135
[685e28]136 // all later parse functions depend on parsed command
[014bc4]137 status = AddOn->addOtherParsings(*ControllerInfo, vm);
[5fa1f0d]138 if (status) return status;
[685e28]139
140 // parse given ControllerCommand
[cbcbbd]141 if(!ControllerCommands.isPresentByName(ControllerInfo->command)) {
142 ELOG(1, "Unrecognized command '"+toString(ControllerInfo->command)+"'.");
[685e28]143 return 255;
144 }
[cbcbbd]145 const ControllerCommand *commands = ControllerCommands.getByName(ControllerInfo->command);
[7ca772e]146 try
147 {
[685e28]148 // execute each command in the queue synchronously
149 size_t phase = 1;
[ee61ce]150 for (ControllerCommand::const_iterator iter = commands->begin();
151 iter != commands->end(); ++iter, ++phase) {
152 (*iter)();
[7ca772e]153 {
[685e28]154 io_service.reset();
155 Info info((std::string("io_service: ")+toString(phase)).c_str());
156 io_service.run();
[7ca772e]157 }
158 }
159 Exitflag = controller.getExitflag();
160 }
161 catch (std::exception& e)
162 {
163 std::cerr << e.what() << std::endl;
164 }
[014bc4]165 delete AddOn;
[7ca772e]166
167 return Exitflag;
168}
[4dd16e]169
Note: See TracBrowser for help on using the repository browser.