views:

465

answers:

3

I have a distributed C++ application, which is composed of 4 processes spread on 2 machines. One of the applications serves as "control center" for the rest of the applications.

I want to be able to save the current state to a file and load it again later. What exactly is a "state" is defined separately by each module in the system. When saving, the modules states should be combined to one file. When loading, each module should read the state data back from the file.

The state has to be saved to a human-readable text file, as it is going to be edited by some users. So a binary file format is not an option. In addition, a standard file format such as XML or YAML is preferred.

How would you recommend implement a basic framework for state saving/loading as I just described? I prefer to do the minimum data serialization work needed for this task. Also, the framework should allow to easily add more data to be saved in the future.

+1  A: 

Use XML. Have each class have a Save and a Load function. Then in the XML you put its highest level tag as the name of the class. That way when you load back you can look up which class to use and then create one and calls its Load function.

Goz
+5  A: 

Have a look at the boost.Serialize lib. It's a very nice lib for (un)streaming objects to an (xml) file.

Instead of writing a Load and Save function your class only need to write a serialize function and this function will work both ways.

class X
{
   friend class boost::serialization::access;
   template<class Archive>
   void serialize(Archive & ar, const unsigned int version)
   {
       ar & make_nvp("degrees(=xml_tagname)", degrees);
       ar & make_nvp("minutes(=xml_tagname)", minutes);;
       ar & BOOST_SERIALIZATION_NVP(seconds);  // =auto xml tag
   }
}
TimW
+1  A: 

I'm definitely on XML's side. Enclosing data saved by each process in a process identifier tag will be useful. We use a similar system in our application for saving/loading data contained by plugins and it is very convenient end expandable.

My first approach would be this:

  1. Control Center(CC) initiates save.
  2. CC sends each process the save signal.
  3. Each process forms its own part of XML including its process identifier and sends it to CC
  4. CC gathers these XML snippets and combines them into a one, large file & saves it to disk.

When loading:

  1. CC reads the XML file.
  2. When it encounters a process identifier, sends the appropriate part to the corresponding process with a load signal.
  3. Each process loads its own part and sends a load complete signal to the CC.
  4. When all processes are finished loading, CC sends them Start signal and they start normal workflow.
erelender