+void
+Route::_set_processor_states(const XMLNodeList &nlist)
+{
+ XMLNodeConstIterator niter;
+ char buf[64];
+
+ ProcessorList::iterator i, o;
+
+ // Iterate through existing processors, remove those which are not in the state list
+ for (i = _processors.begin(); i != _processors.end(); ) {
+ ProcessorList::iterator tmp = i;
+ ++tmp;
+
+ bool processorInStateList = false;
+
+ (*i)->id().print (buf, sizeof (buf));
+
+
+ for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
+
+ // legacy sessions (IOProcessor as a child of Processor, both is-a IO)
+ if (strncmp(buf,(*niter)->child(X_("IOProcessor"))->child(X_("IO"))->property(X_("id"))->value().c_str(), sizeof(buf)) == 0) {
+ processorInStateList = true;
+ break;
+ } else if (strncmp(buf,(*niter)->property(X_("id"))->value().c_str(), sizeof(buf)) == 0) {
+ processorInStateList = true;
+ break;
+ }
+ }
+
+ if (!processorInStateList) {
+ remove_processor (*i);
+ }
+
+
+ i = tmp;
+ }
+
+
+ // Iterate through state list and make sure all processors are on the track and in the correct order,
+ // set the state of existing processors according to the new state on the same go
+ i = _processors.begin();
+ for (niter = nlist.begin(); niter != nlist.end(); ++niter, ++i) {
+
+ // Check whether the next processor in the list
+ o = i;
+
+ while (o != _processors.end()) {
+ (*o)->id().print (buf, sizeof (buf));
+ if ( strncmp(buf, (*niter)->child(X_("IOProcessor"))->child(X_("IO"))->property(X_("id"))->value().c_str(), sizeof(buf)) == 0)
+ break;
+ else if (strncmp(buf,(*niter)->property(X_("id"))->value().c_str(), sizeof(buf)) == 0)
+ break;
+
+ ++o;
+ }
+
+ if (o == _processors.end()) {
+ // If the processor (*niter) is not on the route, we need to create it
+ // and move it to the correct location
+
+ ProcessorList::iterator prev_last = _processors.end();
+ --prev_last; // We need this to check whether adding succeeded
+
+ add_processor_from_xml (**niter);
+
+ ProcessorList::iterator last = _processors.end();
+ --last;
+
+ if (prev_last == last) {
+ cerr << "Could not fully restore state as some processors were not possible to create" << endl;
+ continue;
+
+ }
+
+ boost::shared_ptr<Processor> tmp = (*last);
+ // remove the processor from the wrong location
+ _processors.erase(last);
+ // processor the new processor at the current location
+ _processors.insert(i, tmp);
+
+ --i; // move pointer to the newly processored processor
+ continue;
+ }
+
+ // We found the processor (*niter) on the route, first we must make sure the processor
+ // is at the location provided in the XML state
+ if (i != o) {
+ boost::shared_ptr<Processor> tmp = (*o);
+ // remove the old copy
+ _processors.erase(o);
+ // processor the processor at the correct location
+ _processors.insert(i, tmp);
+
+ --i; // move pointer so it points to the right processor
+ }
+
+ (*i)->set_state( (**niter) );
+ }
+
+ processors_changed ();
+}
+