// dialproc.cxx // // David Adams // August 2002 // // Main program for looping over events in an event dataset, applying // a task to each and returning a result which is a VirtualEventDataset. // // Communication is via files: // Input: // task.xml - task (contains input empty result) // dataset.xml - input dataset // (library defines symbol task) // Output: // pid - process ID // result.xml - result (output datset) // #include #include #include #include #include #include "dataset_util/Environment.h" #include "dataset_xml/XmlParser.h" #include "dataset_id/EventId.h" #include "dataset_id/UniqueIdGenerator.h" #include "dataset_credential/GssCredentialManager.h" #include "dataset_base/Dataset.h" #include "dataset_base/DatasetCreator.h" #include "dataset_base/VirtualEventDataset.h" #include "dataset_base/DatasetRepository.h" #include "dial_task/Task.h" #include "dial_com/dial_init.h" #include "dial_com/registrations.h" using std::string; using std::ofstream; using std::cerr; using std::endl; using dset::GssCredentialManager; using dset::ContentBlock; using dset::Content; using dset::Dataset; using dset::DatasetCreator; using dset::DatasetRepository; using dset::VirtualEventDataset; using dial::Task; typedef int (*TaskFun) (const EventId& eid, dset::Dataset& res); int main(int, char*) { if ( dial::dial_init() ) return 101; // Set to true to wait for debugger. bool waiting = false; while ( waiting ) { sleep(2); } // Set the credential for this thread. // This is needed to find owner for task. int cstat = GssCredentialManager::set_default(); if ( cstat != 0 ) { cerr << "Unable to find credential" << endl; return 18; } if ( GssCredentialManager::owner().size() == 0 ) { cerr << "Unable to find credential owner" << endl; return 19; } ofstream out("out.txt"); out << "Begin processing task" << endl; // Check that we have a global dataset ID generator. // Otherwise we will not be able to write out the result dataset. if ( ! Dataset::id_generator().is_global() ) { out << endl; out << "Dataset ID generator is not global" << endl; out << " DIAL_UIDS = " << Environment::current().value("DIAL_UIDS") << endl; return 2; } // Open the task library. TaskFun task; { Text tskloc("taskdir"); string dir = tskloc.line(0); string libname = dir + "/" + "task.so"; void* handle = dlopen(libname.c_str(), RTLD_NOW); if ( ! handle ) { out << "Unable to open task library " << libname << endl; out << dlerror() << endl; return 3; } task = (TaskFun) dlsym(handle, "task"); if ( task == 0 ) { out << "Unable to load symbol" << endl; out << dlerror() << endl; return 4; } } // Write the DTD. DtdRegistry::instance("dataset").write(); DtdRegistry::instance("dial").write(); XmlParser parser; DatasetRepository& drep = DatasetRepository::default_instance(); const XmlElement* pxdst = parser.parse("dataset.xml"); if ( pxdst == 0 ) { out << "Unable to parse dataset.xml" << endl; return 5; } const Dataset* pdst = DatasetCreator::create(*pxdst, &drep); if ( pdst == 0 ) { out << "Unable to create dataset from dataset.xml" << endl; out << endl; DatasetCreator::display(out); out << endl; out << endl; out << *pxdst << endl; return 6; } const Dataset& dst = *pdst; out << endl; out << "Input dataset: " << endl; out << dst << endl; // Loop over events and fill result. if ( dst.content().size() != 1 ) return 7; const ContentBlock& blk = dst.content().front(); EventIdList eids; Content con(blk.dataset_type(), blk.name(), blk.content_ids(), eids); Dataset* pres2 = new VirtualEventDataset(con); Dataset& res2 = *pres2; out << endl << "Input result:" << endl; out << res2 << endl; out << endl << "Begin event loop" << endl; out << res2 << endl << endl; int rstat = 0; if ( dst.is_event_dataset() ) { for ( EventIdList::const_iterator ieid=dst.event_ids().begin(); ieid!=dst.event_ids().end(); ++ieid ) { const EventId& eid = *ieid; out << eid << endl; rstat = task(eid, res2); out << endl << "Merged" << endl << endl; out << res2 << endl << endl; out << eid << " returns " << rstat << endl; if ( rstat != 0 ) break; } res2.lock(); } else { out << "Warning: This is not an event dataset--no event loop." << endl; } out << endl; out << "Output result:" << endl; out << res2 << endl; // Write the result. const XmlElement* pxres2 = res2.xml(); if ( pxres2 == 0 ) return 9; int stat = parser.write("result.xml", *pxres2); if ( stat != 0 ) return 10; out << "Output result:" << endl; out << res2 << endl; return 0; }