named_pipe.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:6k
- /*
- * ===========================================================================
- * PRODUCTION $Log: named_pipe.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 20:48:30 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: named_pipe.cpp,v 1000.1 2004/06/01 20:48:30 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Josh Cherry
- *
- * File Description:
- * Named pipe functionality for Genome Workbench
- */
- #include <ncbi_pch.hpp>
- #include "named_pipe.hpp"
- #include "load_file.hpp"
- #include "gbench.hpp"
- #include <gui/utils/system_path.hpp>
- #include <FL/Fl.H>
- BEGIN_NCBI_SCOPE
- CNamedPipeServer CGBenchPipe::sm_NamedPipeServer;
- void CGBenchPipe::Start(void)
- {
- if (sm_NamedPipeServer.Create(x_GetPipeName()) != eIO_Success) {
- throw runtime_error("CNamedPipeServer::Create failed");
- }
- // zero timeouts for open (Listen) and read
- STimeout to = {0, 0};
- if (sm_NamedPipeServer.SetTimeout(eIO_Open, &to) != eIO_Success) {
- throw runtime_error("CNamedPipeServer::SetTimeout failed for Open");
- }
- if (sm_NamedPipeServer.SetTimeout(eIO_Read, &to) != eIO_Success) {
- throw runtime_error("CNamedPipeServer::SetTimeout failed for Read");
- }
- // call the callback to read, which has itself called repeatedly
- Fl::add_timeout(0.1, x_ReadPipe, NULL);
- }
- // Determine appropriate name of pipe
- string CGBenchPipe::x_GetPipeName(void)
- {
- #ifdef NCBI_OS_MSWIN
- string user_name = CGBenchApp::Instance()->GetEnvironment().Get("USERNAME");
- return "\\.\pipe\gbench_pipe-" + user_name;
- #else // unix of some sort
- return CSystemPath::ResolvePath("<home>/gbench_pipe");
- #endif
- }
- // fltk timeout callback to read pipe.
- // ptr argument is a pointer to the app.
- void CGBenchPipe::x_ReadPipe(void *ptr)
- {
- CNamedPipeServer& srv = sm_NamedPipeServer;
- if (srv.Listen() == eIO_Success) {
- string data;
- const size_t buf_size = 1024;
- char buf[buf_size];
- size_t n_read;
- while (srv.Status(eIO_Read) == eIO_Success ||
- srv.Status(eIO_Read) == eIO_Timeout) {
- srv.Read(buf, buf_size, &n_read);
- data.append(buf, n_read);
- }
- // data is either just a file name, or a file name
- // and a file type separated by a null
- string fname;
- string ftype;
- vector<string> tokens;
- string delim;
- delim = ' '; // not an empty string
- NStr::Tokenize (data, delim, tokens);
- fname = tokens[0];
- if (tokens.size() > 1) {
- ftype = tokens[1];
- } else {
- ftype = "auto";
- }
- GBenchLoadFile(fname, ftype, true);
- }
- Fl::repeat_timeout(0.1, x_ReadPipe, ptr);
- }
- void CGBenchPipe::OpenRemote(const string& fname, const string& ftype)
- {
- size_t n_written;
- size_t total_written = 0;
- string msg = fname + ' ' + ftype;
- CNamedPipeClient cli;
- STimeout to = {0, 100000};
- if (cli.SetTimeout(eIO_Open, &to) != eIO_Success) {
- throw runtime_error("CNamedPipeServer::SetTimeout failed for Open");
- }
- if (cli.Open(x_GetPipeName()) != eIO_Success) {
- throw runtime_error("Failed to open named pipe for writing");
- }
- while (1) {
- EIO_Status status = cli.Write(msg.c_str() + total_written,
- msg.size(), &n_written);
- total_written += n_written;
- if (total_written == msg.size()) {
- break;
- }
- if (status != eIO_Success && status != eIO_Timeout) {
- break;
- }
- }
- if (total_written != msg.size()) {
- throw runtime_error("Failed to write to named pipe");
- }
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: named_pipe.cpp,v $
- * Revision 1000.1 2004/06/01 20:48:30 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10
- *
- * Revision 1.10 2004/05/21 22:27:42 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.9 2004/03/23 13:38:08 dicuccio
- * MOre efficient use of FLTK timeouts
- *
- * Revision 1.8 2004/03/22 15:40:20 jcherry
- * Added file type command-line argument
- *
- * Revision 1.7 2004/03/17 15:46:18 jcherry
- * Windows hack no longer necessary
- *
- * Revision 1.6 2004/03/16 18:34:44 jcherry
- * Append username to pipe name on Windows to make it unique
- *
- * Revision 1.5 2004/03/16 17:40:06 jcherry
- * A hack for Windows
- *
- * Revision 1.4 2004/03/16 17:29:02 jcherry
- * Added timeout for opening for write
- *
- * Revision 1.3 2004/03/15 16:06:18 jcherry
- * Launch default viewer when opening file from command line
- *
- * Revision 1.2 2004/03/11 19:25:35 jcherry
- * Moved load_file to gui/gbench
- *
- * Revision 1.1 2004/03/11 17:10:35 jcherry
- * Initial version
- *
- * ===========================================================================
- */