NCBI C++ Toolkit Cross Reference

C++/src/gui/app/gbench/named_pipe.cpp


  1 /*  $Id: named_pipe.cpp 14515 2007-05-04 17:18:18Z kazimird $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Authors:  Josh Cherry
 27  *
 28  * File Description:
 29  *    Named pipe functionality for Genome Workbench
 30  */
 31 
 32 #include <ncbi_pch.hpp>
 33 
 34 #include    <corelib/ncbiapp.hpp>
 35 
 36 #include "named_pipe.hpp"
 37 #include "load_file.hpp"
 38 
 39 #include <gui/utils/system_path.hpp>
 40 
 41 #include <FL/Fl.H>
 42 
 43 
 44 BEGIN_NCBI_SCOPE
 45 
 46 
 47 CNamedPipeServer CGBenchPipe::sm_NamedPipeServer;
 48 bool CGBenchPipe::sm_IsOpen;
 49 CGBenchPipe::FRaiseCallback CGBenchPipe::sm_RaiseCallback = NULL;
 50 
 51 void CGBenchPipe::InstallRaiseCallback(FRaiseCallback callback)
 52 {
 53     sm_RaiseCallback = callback;
 54 }
 55 
 56 
 57 void CGBenchPipe::Start(void)
 58 {
 59     sm_IsOpen = false;
 60     if (sm_NamedPipeServer.Create(x_GetPipeName()) != eIO_Success) {
 61         throw runtime_error("CNamedPipeServer::Create failed");
 62     }
 63 
 64     // zero timeouts for open (Listen) and read
 65     STimeout to = {0, 0};
 66     if (sm_NamedPipeServer.SetTimeout(eIO_Open, &to) != eIO_Success) {
 67         throw runtime_error("CNamedPipeServer::SetTimeout failed for Open");
 68     }
 69     if (sm_NamedPipeServer.SetTimeout(eIO_Read, &to) != eIO_Success) {
 70         throw runtime_error("CNamedPipeServer::SetTimeout failed for Read");
 71     }
 72     sm_IsOpen = true;
 73 }
 74 
 75 
 76 // Determine appropriate name of pipe
 77 string CGBenchPipe::x_GetPipeName(void)
 78 {
 79 #ifdef NCBI_OS_MSWIN
 80     string user_name = CNcbiApplication::Instance()->GetEnvironment().Get("USERNAME");
 81     return "\\\\.\\pipe\\gbench_pipe-" + user_name;
 82 #else  // unix of some sort
 83     return CSystemPath::ResolvePath("<home>/gbench_pipe");
 84 #endif
 85 }
 86 
 87 
 88 // fltk timeout callback to read pipe.
 89 // ptr argument is a pointer to the app.
 90 void CGBenchPipe::Process()
 91 {
 92     CNamedPipeServer& srv = sm_NamedPipeServer;
 93     if (!sm_IsOpen) {
 94         // pipe is not open; don't try to listen
 95         return;
 96     }
 97     if (srv.Listen() == eIO_Success) {
 98         string data;
 99         const size_t buf_size = 1024;
100         char buf[buf_size];
101         size_t n_read;
102         while (srv.Status(eIO_Read) == eIO_Success ||
103                srv.Status(eIO_Read) == eIO_Timeout) {
104             srv.Read(buf, buf_size, &n_read);
105             data.append(buf, n_read);
106         }
107 
108         // data is either just a file name, or a file name
109         // and a file type separated by a null
110         string fname;
111         string ftype;
112 
113         vector<string> tokens;
114         string delim;
115         delim = '\0';  // not an empty string
116         NStr::Tokenize (data, delim, tokens);
117         fname = tokens[0];
118         if (tokens.size() > 1) {
119             ftype = tokens[1];
120         } else {
121             ftype = "auto";
122         }
123 
124         // a "file" of type "seqid" is really an accession
125         if (ftype == "seqid") {
126             GBenchLoadAccession(fname);
127         } else {
128             GBenchLoadFile(fname, ftype);
129         }
130 
131         if(sm_RaiseCallback)    {
132             (*sm_RaiseCallback)();
133         }
134     }
135 }
136 
137 
138 void CGBenchPipe::OpenRemote(const string& fname, const string& ftype)
139 {
140     size_t n_written;
141     size_t total_written = 0;
142 
143     string msg = fname + '\0' + ftype;
144 
145     CNamedPipeClient cli;
146 
147     STimeout to = {1, 0};
148     if (cli.SetTimeout(eIO_Open, &to) != eIO_Success) {
149         throw runtime_error("CNamedPipeServer::SetTimeout failed for Open");
150     }
151 
152     if (cli.Open(x_GetPipeName()) != eIO_Success) {
153         throw runtime_error("Failed to open named pipe for writing");
154     }
155 
156     while (1) {
157         EIO_Status status = cli.Write(msg.data() + total_written,
158                                       msg.size() - total_written, &n_written);
159         total_written += n_written;
160         if (total_written == msg.size()) {
161             break;
162         }
163         if (status != eIO_Success && status != eIO_Timeout) {
164             break;
165         }
166     }
167     if (total_written != msg.size()) {
168         throw runtime_error("Failed to write to named pipe");
169     }
170 }
171 
172 
173 END_NCBI_SCOPE
174 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.