Ignore:
Timestamp:
Jun 29, 2012, 8:01:46 AM (13 years ago)
Author:
Frederik Heber <heber@…>
Branches:
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, 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
Children:
701bbc
Parents:
f98c8e
git-author:
Frederik Heber <heber@…> (03/04/12 16:05:02)
git-committer:
Frederik Heber <heber@…> (06/29/12 08:01:46)
Message:

Added template functions for sync_read() and sync_write() to Connection.

  • also factored our prepare_write from async_write() which is needed in case of sync_write() as well.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Fragmentation/Automation/Connection.hpp

    rf98c8e r7dd8bc  
    2525#include <sstream>
    2626#include <vector>
     27
     28#include "CodePatterns/Log.hpp"
    2729
    2830/** The Connection class provides serialization primitives on top of a socket.
     
    4850  }
    4951
    50   /// Asynchronously write a data structure to the socket.
    51   template <typename T, typename Handler>
    52   void async_write(const T& t, Handler handler)
     52  typedef std::vector<boost::asio::const_buffer> buffers_t;
     53
     54  /** Prepare data for write operation.
     55   *
     56   * The dara is prepared in two blocks: header and data.
     57   *
     58   * @param t data to place into buffers
     59   * @param vector of buffers
     60   * @return true - data placed in buffer, false - some mismatch, data not placed
     61   */
     62  template <typename T>
     63  bool prepare_write(const T& t, buffers_t &buffers)
    5364  {
    5465    // Serialize the data first so we know how large it is.
     
    6374      << std::hex << outbound_data_.size();
    6475    if (!header_stream || header_stream.str().size() != header_length)
    65     {
     76      return false;
     77    outbound_header_ = header_stream.str();
     78
     79    // Write the serialized data to the socket. We use "gather-write" to send
     80    // both the header and the data in a single write operation.
     81    buffers.push_back(boost::asio::buffer(outbound_header_));
     82    buffers.push_back(boost::asio::buffer(outbound_data_));
     83    return true;
     84  }
     85
     86  /// Asynchronously write a data structure to the socket.
     87  template <typename T, typename Handler>
     88  void async_write(const T& t, Handler handler)
     89  {
     90    buffers_t buffers;
     91    if (!prepare_write(t, buffers)) {
    6692      // Something went wrong, inform the caller.
    6793      boost::system::error_code error(boost::asio::error::invalid_argument);
     
    6995      return;
    7096    }
    71     outbound_header_ = header_stream.str();
    72 
    73     // Write the serialized data to the socket. We use "gather-write" to send
    74     // both the header and the data in a single write operation.
    75     std::vector<boost::asio::const_buffer> buffers;
    76     buffers.push_back(boost::asio::buffer(outbound_header_));
    77     buffers.push_back(boost::asio::buffer(outbound_data_));
    7897    boost::asio::async_write(socket_, buffers, handler);
     98  }
     99
     100  /// Synchronously write a data structure to the socket.
     101  template <typename T>
     102  void sync_write(const T& t)
     103  {
     104    buffers_t buffers;
     105    if (prepare_write(t, buffers)) {
     106      boost::asio::write(socket_, buffers);
     107    } else {
     108      ELOG(2, "sync_write failed, data could not be placed in buffers.");
     109    }
    79110  }
    80111
     
    92123          this, boost::asio::placeholders::error, boost::ref(t),
    93124          boost::make_tuple(handler)));
     125  }
     126
     127  /// Synchronously read a data structure from the socket.
     128  template <typename T, typename Handler>
     129  void sync_read(T& t, Handler handler)
     130  {
     131    // Issue a read operation to read exactly the number of bytes in a header.
     132    void (Connection::*f)(
     133        const boost::system::error_code&,
     134        T&, boost::tuple<Handler>)
     135      = &Connection::handle_read_header<T, Handler>;
     136    boost::asio::read(socket_, boost::asio::buffer(inbound_header_));
    94137  }
    95138
Note: See TracChangeset for help on using the changeset viewer.