// DialWs.cxx #include #include #include "dataset_util/FileStatus.h" #include "dial_ws/DialWs.h" using std::string; using std::ostream; using std::ofstream; using std::endl; using std::map; using dial::DialWs; typedef DialWs::Path Path; typedef DialWs::WebPageUpdate WebPageUpdate; typedef map PageMap; typedef map UpdateMap; typedef DialWs::TextList TextList; namespace { PageMap m_pgs; UpdateMap m_ups; TextList m_hpis; } //********************************************************************** // Member functions. //********************************************************************** // Constructor. DialWs::DialWs() { assert(false); } //********************************************************************** // Service termination. bool& DialWs::terminate() { static bool term = false; if ( FileStatus("terminate_service").exists() ) { term = true; } return term; } //********************************************************************** // Output log. // If argument is given, a new file is opened. ostream& DialWs::lout(string newfile) { static ostream* pout = new ofstream("/dev/null"); if ( newfile.size() ) { delete pout; pout = new ofstream(newfile.c_str()); } return *pout; } //********************************************************************** // Fetch the condition. PThreadCondition*& DialWs::condition() { static PThreadCondition* pmtx = 0; return pmtx; } //********************************************************************** // Store a web page. int DialWs::insert_web_page(Path page, Path entry, Text txt) { string path = page; if ( entry.size() ) path += "?" + entry; m_pgs[path] = txt; return 0; } //********************************************************************** // Store a web page update. int DialWs::insert_web_page_update(Path path, WebPageUpdate pfun) { m_ups[path] = pfun; return 0; } //********************************************************************** // Return if a page exists. bool DialWs::has_web_page(string path) { return m_pgs.find(path) != m_pgs.end(); } //********************************************************************** // Retrieve a web page. Text DialWs::web_page(string path) { // Split path into page and entry. string::size_type ipos = path.find('?'); string page = path; string entry = ""; if ( ipos != string::npos ) { page = path.substr(0,ipos); if ( ipos+1 < path.size() ) { entry = path.substr(ipos+1); } } // Update page. UpdateMap::const_iterator iup = m_ups.find(page); if ( iup != m_ups.end() ) { WebPageUpdate pfun = iup->second; if ( pfun != 0 ) { lout() << "Updating page " << page; if ( entry.size() ) { lout() << " for entry " << entry; } lout() << endl; iup->second(page, entry); } } // Find the page. PageMap::const_iterator ipg = m_pgs.find(path); if ( ipg == m_pgs.end() ) { Text txt; txt.append("Page not found: " + path + ""); return txt; } return ipg->second; } //********************************************************************** // Add text to home page. int DialWs::insert_home_page(const Text& txt) { m_hpis.push_back(txt); return 0; } //********************************************************************** // Return the home page insertions. const TextList& DialWs::home_page_insertions() { return m_hpis; } //**********************************************************************