CARMA C++
EmailManager.h
1 /*
2  * Fault System Error Reporting via Email
3  *
4  * Copyright (c) 2010-2011 Ira W. Snyder <iws@ovro.caltech.edu>
5  */
6 
7 #ifndef EMAIL_MANAGER_H
8 #define EMAIL_MANAGER_H
9 
10 #include <queue>
11 #include <list>
12 
13 #include <boost/shared_ptr.hpp>
14 #include <boost/regex.hpp>
15 
16 #include <carma/fault/DagMLNode.h>
17 
18 #include <carma/util/ConcurrentQueue.h>
19 #include <carma/util/types.h>
20 
21 typedef std::list<std::string> StringList;
22 typedef boost::shared_ptr<class EmailBin> EmailBinPtr;
23 
24 /* Holds the settings for the EmailBin class */
25 struct EmailBinSettings
26 {
27  std::string regex;
28  std::string filename;
29  std::string address;
30  bool stop_processing;
31  int hysteresis; /* in seconds */
32 };
33 
34 class EmailBin
35 {
36  public:
37  EmailBin(const struct EmailBinSettings &settings);
38 
39  /* clear internal state at the beginning of each frame */
40  void clear();
41 
42  /* add a fault in the given frame */
43  bool addFault(const int frame, const std::string &name);
44 
45  /* should we send email */
46  bool shouldSendEmail() const;
47 
48  /* message contents */
49  std::string formatMessageSubject(const carma::util::frameType frame) const;
50  std::string formatMessageBody(const carma::util::frameType frame) const;
51 
52  /* get some settings */
53  std::string getFileName() const;
54  std::string getEmailAddress() const;
55 
56  protected:
57 
58  bool matchesRegex(const std::string &name) const;
59 
60  /* the settings we were given */
61  const struct EmailBinSettings settings_;
62 
63  /* the regular expression and hysteresis frames */
64  boost::regex regex_;
65  const int hysteresisFrames_;
66 
67  /* email template body */
68  const std::string templateBody_;
69 
70  /* active data that changes per-frame */
71  StringList faults_;
72  int lastActiveFrame_;
73  bool shouldSendEmail_;
74 };
75 
76 class EmailManager
77 {
78  public:
79  EmailManager(const std::string &tab, const int holdoff);
80 
81  /* add the faults for a given frame */
82  void addFaults(const int frame, const DagMLNodeList faults);
83 
84  /* thread entry point */
85  static void emailThreadEP(EmailManager &This);
86 
87  protected:
88 
89  /* private structure for internal queueing */
90  struct FaultQueueElem {
91  int frame;
92  StringList faults;
93  };
94 
95  typedef carma::util::ConcurrentQueue<struct FaultQueueElem> FaultQueue;
96  typedef std::list<EmailBinPtr> EmailBinList;
97 
98  /* support for startup holdoff */
99  unsigned int frames_processed_;
100  unsigned int frames_holdoff_;
101 
102  EmailBinList emailList_;
103  FaultQueue queue_;
104 
105  /* process a queue element */
106  void processElement(struct FaultQueueElem &elem);
107 };
108 
109 #endif /* EMAIL_MANAGER_H */
110 
111 /* vim: set ts=4 sts=4 sw=4 noet tw=92: */
unsigned int frameType
Half second frames since Jan 1, 2000.
Definition: types.h:29
Various type definitions for util classes.