xref: /freebsd/lib/libdevdctl/consumer.h (revision 186034134768ffc5968a266be37fc99feb652fad)
1 /*-
2  * Copyright (c) 2011, 2012, 2013, 2014 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31  */
32 
33 /**
34  * \file devdctl_consumer.h
35  */
36 #ifndef	_DEVDCTL_CONSUMER_H_
37 #define	_DEVDCTL_CONSUMER_H_
38 
39 /*============================ Namespace Control =============================*/
40 namespace DevdCtl
41 {
42 
43 /*=========================== Forward Declarations ===========================*/
44 class Event;
45 
46 /*============================ Class Declarations ============================*/
47 /*----------------------------- DevdCtl::Consumer ----------------------------*/
48 
49 /**
50  */
51 class Consumer
52 {
53 public:
54 	Consumer(Event::BuildMethod *defBuilder = NULL,
55 		 EventFactory::Record *regEntries = NULL,
56 		 size_t numEntries = 0);
57 	virtual ~Consumer();
58 
59 	bool Connected() const;
60 
61 	/**
62 	 * Return file descriptor useful for client's wishing to poll(2)
63 	 * for new events.
64 	 */
65 	int GetPollFd();
66 
67 	/**
68          * Queue an event for deferred processing or replay.
69          */
70 	bool SaveEvent(const Event &event);
71 
72 	/**
73 	 * Reprocess any events saved via the SaveEvent() facility.
74 	 *
75 	 * \param discardUnconsumed  If true, events that are not consumed
76 	 *                           during replay are discarded.
77 	 */
78 	void ReplayUnconsumedEvents(bool discardUnconsumed);
79 
80 	/** Return an event, if one is available.  */
81 	Event *NextEvent();
82 
83 	/**
84 	 * Extract events and invoke each event's Process method.
85 	 */
86 	void ProcessEvents();
87 
88 	/** Discard all data pending in m_devdSockFD. */
89 	void FlushEvents();
90 
91 	/**
92 	 * Test for data pending in m_devdSockFD
93 	 *
94 	 * \return  True if data is pending.  Otherwise false.
95 	 */
96 	bool EventsPending();
97 
98 	/**
99 	 * Open a connection to devd's unix domain socket.
100 	 *
101 	 * \return  True if the connection attempt is successful.  Otherwise
102 	 *          false.
103 	 */
104 	bool ConnectToDevd();
105 
106 	/**
107 	 * Close a connection (if any) to devd's unix domain socket.
108 	 */
109 	void DisconnectFromDevd();
110 
111 	EventFactory GetFactory();
112 
113 protected:
114 	/**
115 	 * \brief Reads the most recent record
116 	 *
117 	 * On error, "" is returned, and errno will be set by the OS
118 	 *
119 	 * \returns      A string containing the record
120 	 */
121 	std::string ReadEvent();
122 
123 	enum {
124 		/*
125 		 * The maximum event size supported by libdevdctl.
126 		 */
127 		MAX_EVENT_SIZE = 8192,
128 	};
129 
130 	static const char  s_devdSockPath[];
131 
132 	/**
133 	 * File descriptor representing the unix domain socket
134 	 * connection with devd.
135 	 */
136 	int                m_devdSockFD;
137 
138 	EventFactory	   m_eventFactory;
139 
140 	/** Queued events for replay. */
141 	EventList	   m_unconsumedEvents;
142 
143 	/**
144 	 * Flag controlling whether events can be queued.  This boolean
145 	 * is set during event replay to ensure that previosuly deferred
146 	 * events are not requeued and thus retained forever.
147 	 */
148 	bool		   m_replayingEvents;
149 };
150 
151 //- Consumer Const Public Inline Methods ---------------------------------------
152 inline bool
Connected()153 Consumer::Connected() const
154 {
155 	return (m_devdSockFD != -1);
156 }
157 
158 //- Consumer Public Inline Methods ---------------------------------------------
159 inline int
GetPollFd()160 Consumer::GetPollFd()
161 {
162 	return (m_devdSockFD);
163 }
164 
165 inline EventFactory
GetFactory()166 Consumer::GetFactory()
167 {
168 	return (m_eventFactory);
169 }
170 
171 } // namespace DevdCtl
172 #endif	/* _DEVDCTL_CONSUMER_H_ */
173