NCBI C++ ToolKit
file_messaging.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: file_messaging.hpp 33815 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: Paul Thiessen
27 *
28 * File Description:
29 * file-based messaging system
30 *
31 * ===========================================================================
32 */
33 
34 #ifndef FILE_MESSAGING__HPP
35 #define FILE_MESSAGING__HPP
36 
37 #include <corelib/ncbistd.hpp>
38 #include <corelib/ncbistl.hpp>
39 #include <corelib/ncbifile.hpp>
40 
42 
43 #include <string>
44 #include <map>
45 #include <list>
46 
47 
49 
50 //
51 // FileMessageResponder is a mix-in class - an interface-only class that defines callback methods that
52 // will be used to pass messages to an application object of this type. For both commands and replies,
53 // if the 'data' string is not empty (size()>0), then it will be a series of '\n'-terminated lines.
54 //
56 {
57 public:
58  virtual ~MessageResponder(void) { }
59 
60  // called when a (new) command is received
61  virtual void ReceivedCommand(const string& fromApp, unsigned long id,
62  const string& command, const string& data) = 0;
63 
64  // possible reply status values
65  enum ReplyStatus {
68  };
69 
70  // called when a reply is received
71  virtual void ReceivedReply(const string& fromApp, unsigned long id,
72  ReplyStatus status, const string& data) = 0;
73 };
74 
75 //
76 // FileMessenger represents an individual message file connection
77 //
79 
81 {
82 private:
83  // (de)construction handled only by FileMessagingManager parent
84  friend class FileMessagingManager;
85  FileMessenger(FileMessagingManager *parentManager,
86  const string& messageFilename, MessageResponder *responderObject, bool isReadOnly);
87 
88  // any pending commands are sent before the object is deconstructed
89  ~FileMessenger(void);
90 
94  const bool readOnly;
95 
96  // keep track of id's used and sent to/from which recipient, and whether reply was received
100 
104 
105  // keep a queue of commands to send next time the file is opened for writing
106  typedef struct {
107  string to;
108  unsigned long id;
109  string command, data;
110  } CommandInfo;
111  typedef list < CommandInfo > CommandList;
113 
114  // keep track of file size last time the file was read
116 
117  // read the file and process any new commands
118  void ReceiveCommands(void);
119 
120  // send any commands waiting in the queue
121  void SendPendingCommands(void);
122 
123 public:
124  // send a new command (must have a unique id for given target app)
125  void SendCommand(const string& targetApp, unsigned long id,
126  const string& command, const string& data);
127 
128  // send reply to previously received command
129  void SendReply(const string& targetApp, unsigned long id,
130  MessageResponder::ReplyStatus status, const string& data);
131 
132  // receives any new commands/replies and sends any pending ones
133  void PollMessageFile(void);
134 };
135 
136 //
137 // FileMessagingManager is a container for FileMessengers, mostly as a convenience
138 // to poll all message files from one central function
139 //
141 {
142 private:
143  friend class FileMessenger;
144  const string applicationName;
145 
146  typedef list < FileMessenger * > FileMessengerList;
148 
149 public:
150  FileMessagingManager(const string& appName);
151  // stops all monitoring and destroys all messenger objects, but does not delete any message files
152  ~FileMessagingManager(void);
153 
154  // to create a new message file connection; if readOnly==false then replies are sent through
155  // the same file, otherwise replies are logged internally but the message file isn't changed
156  FileMessenger * CreateNewFileMessenger(const string& messageFilename,
157  MessageResponder *responderObject, bool readOnly);
158 
159  // stop monitoring a connection, and destroy the messenger (but doesn't delete the file)
161 
162  // for all FileMessengers, receives any new commands/replies and sends any pending ones
163  void PollMessageFiles(void);
164 };
165 
166 // convenience functions for reading/writing ids for Cn3D's Highlight command
167 bool SeqIdToIdentifier(const CRef < ncbi::objects::CSeq_id >& seqID, string& identifier);
168 bool IdentifierToSeqId(const string& identifier, CRef < ncbi::objects::CSeq_id >& seqID);
169 
171 
172 #endif // FILE_MESSAGING__HPP
CDirEntry –.
Definition: ncbifile.hpp:262
CRef –.
Definition: ncbiobj.hpp:618
const string applicationName
void DeleteFileMessenger(FileMessenger *messenger)
FileMessenger * CreateNewFileMessenger(const string &messageFilename, MessageResponder *responderObject, bool readOnly)
FileMessagingManager(const string &appName)
list< FileMessenger * > FileMessengerList
FileMessengerList messengers
FileMessenger(FileMessagingManager *parentManager, const string &messageFilename, MessageResponder *responderObject, bool isReadOnly)
void SendReply(const string &targetApp, unsigned long id, MessageResponder::ReplyStatus status, const string &data)
map< unsigned long, TargetApp2Status > CommandReplies
const bool readOnly
const CDirEntry lockFile
CommandReplies repliesReceived
map< string, string > TargetApp2Command
void ReceiveCommands(void)
map< string, MessageResponder::ReplyStatus > TargetApp2Status
void PollMessageFile(void)
map< unsigned long, TargetApp2Command > CommandOriginators
void SendPendingCommands(void)
CommandReplies repliesSent
CommandOriginators commandsReceived
const FileMessagingManager *const manager
MessageResponder *const responder
list< CommandInfo > CommandList
CommandOriginators commandsSent
const CDirEntry messageFile
void SendCommand(const string &targetApp, unsigned long id, const string &command, const string &data)
CommandList pendingCommands
virtual void ReceivedCommand(const string &fromApp, unsigned long id, const string &command, const string &data)=0
virtual ~MessageResponder(void)
virtual void ReceivedReply(const string &fromApp, unsigned long id, ReplyStatus status, const string &data)=0
Include a standard set of the NCBI C++ Toolkit most basic headers.
bool IdentifierToSeqId(const string &identifier, CRef< ncbi::objects::CSeq_id > &seqID)
bool SeqIdToIdentifier(const CRef< ncbi::objects::CSeq_id > &seqID, string &identifier)
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static Messenger messenger
Definition: messenger.cpp:71
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
The NCBI C++/STL use hints.
const char * command
Modified on Wed Feb 21 09:57:02 2024 by modify_doxy.py rev. 669887